|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from datetime import date, datetime |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from sqlmesh.utils.conversions import ensure_bool, make_serializable, try_str_to_bool |
| 8 | + |
| 9 | + |
| 10 | +class TestTryStrToBool: |
| 11 | + """Tests for the try_str_to_bool function.""" |
| 12 | + |
| 13 | + @pytest.mark.parametrize( |
| 14 | + "input_val,expected", |
| 15 | + [ |
| 16 | + ("true", True), |
| 17 | + ("True", True), |
| 18 | + ("TRUE", True), |
| 19 | + ("TrUe", True), |
| 20 | + ("false", False), |
| 21 | + ("False", False), |
| 22 | + ("FALSE", False), |
| 23 | + ("FaLsE", False), |
| 24 | + ], |
| 25 | + ) |
| 26 | + def test_boolean_strings(self, input_val: str, expected: bool) -> None: |
| 27 | + """Strings 'true' and 'false' (case-insensitive) convert to bool.""" |
| 28 | + assert try_str_to_bool(input_val) is expected |
| 29 | + |
| 30 | + @pytest.mark.parametrize( |
| 31 | + "input_val", |
| 32 | + [ |
| 33 | + "yes", |
| 34 | + "no", |
| 35 | + "1", |
| 36 | + "0", |
| 37 | + "", |
| 38 | + "truthy", |
| 39 | + "falsey", |
| 40 | + "t", |
| 41 | + "f", |
| 42 | + "on", |
| 43 | + "off", |
| 44 | + ], |
| 45 | + ) |
| 46 | + def test_non_boolean_strings_pass_through(self, input_val: str) -> None: |
| 47 | + """Non-boolean strings are returned unchanged.""" |
| 48 | + assert try_str_to_bool(input_val) == input_val |
| 49 | + |
| 50 | + def test_return_type_for_true(self) -> None: |
| 51 | + """Returns actual bool True, not truthy value.""" |
| 52 | + result = try_str_to_bool("true") |
| 53 | + assert result is True |
| 54 | + assert isinstance(result, bool) |
| 55 | + |
| 56 | + def test_return_type_for_false(self) -> None: |
| 57 | + """Returns actual bool False, not falsey value.""" |
| 58 | + result = try_str_to_bool("false") |
| 59 | + assert result is False |
| 60 | + assert isinstance(result, bool) |
| 61 | + |
| 62 | + |
| 63 | +class TestEnsureBool: |
| 64 | + """Tests for the ensure_bool function.""" |
| 65 | + |
| 66 | + def test_bool_true_passthrough(self) -> None: |
| 67 | + """Boolean True passes through unchanged.""" |
| 68 | + assert ensure_bool(True) is True |
| 69 | + |
| 70 | + def test_bool_false_passthrough(self) -> None: |
| 71 | + """Boolean False passes through unchanged.""" |
| 72 | + assert ensure_bool(False) is False |
| 73 | + |
| 74 | + @pytest.mark.parametrize( |
| 75 | + "input_val,expected", |
| 76 | + [ |
| 77 | + ("true", True), |
| 78 | + ("True", True), |
| 79 | + ("false", False), |
| 80 | + ("False", False), |
| 81 | + ], |
| 82 | + ) |
| 83 | + def test_boolean_strings(self, input_val: str, expected: bool) -> None: |
| 84 | + """String 'true'/'false' converts to corresponding bool.""" |
| 85 | + assert ensure_bool(input_val) is expected |
| 86 | + |
| 87 | + @pytest.mark.parametrize( |
| 88 | + "input_val,expected", |
| 89 | + [ |
| 90 | + ("yes", True), # Non-empty string is truthy |
| 91 | + ("no", True), # Non-empty string is truthy |
| 92 | + ("", False), # Empty string is falsey |
| 93 | + ("0", True), # String "0" is truthy (non-empty) |
| 94 | + ], |
| 95 | + ) |
| 96 | + def test_other_strings_use_bool_conversion(self, input_val: str, expected: bool) -> None: |
| 97 | + """Non-boolean strings fall back to bool() conversion.""" |
| 98 | + assert ensure_bool(input_val) is expected |
| 99 | + |
| 100 | + @pytest.mark.parametrize( |
| 101 | + "input_val,expected", |
| 102 | + [ |
| 103 | + (1, True), |
| 104 | + (0, False), |
| 105 | + (-1, True), |
| 106 | + (100, True), |
| 107 | + ], |
| 108 | + ) |
| 109 | + def test_integers(self, input_val: int, expected: bool) -> None: |
| 110 | + """Integers convert via bool() - 0 is False, others True.""" |
| 111 | + assert ensure_bool(input_val) is expected |
| 112 | + |
| 113 | + @pytest.mark.parametrize( |
| 114 | + "input_val,expected", |
| 115 | + [ |
| 116 | + (1.0, True), |
| 117 | + (0.0, False), |
| 118 | + (-0.5, True), |
| 119 | + ], |
| 120 | + ) |
| 121 | + def test_floats(self, input_val: float, expected: bool) -> None: |
| 122 | + """Floats convert via bool() - 0.0 is False, others True.""" |
| 123 | + assert ensure_bool(input_val) is expected |
| 124 | + |
| 125 | + @pytest.mark.parametrize( |
| 126 | + "input_val,expected", |
| 127 | + [ |
| 128 | + ([], False), |
| 129 | + ([1], True), |
| 130 | + ({}, False), |
| 131 | + ({"a": 1}, True), |
| 132 | + (None, False), |
| 133 | + ], |
| 134 | + ) |
| 135 | + def test_other_types(self, input_val: object, expected: bool) -> None: |
| 136 | + """Other types convert via bool().""" |
| 137 | + assert ensure_bool(input_val) is expected |
| 138 | + |
| 139 | + |
| 140 | +class TestMakeSerializable: |
| 141 | + """Tests for the make_serializable function.""" |
| 142 | + |
| 143 | + def test_date_to_isoformat(self) -> None: |
| 144 | + """date objects convert to ISO format string.""" |
| 145 | + d = date(2024, 1, 15) |
| 146 | + assert make_serializable(d) == "2024-01-15" |
| 147 | + |
| 148 | + def test_datetime_to_isoformat(self) -> None: |
| 149 | + """datetime objects convert to ISO format string.""" |
| 150 | + dt = datetime(2024, 1, 15, 10, 30, 45) |
| 151 | + assert make_serializable(dt) == "2024-01-15T10:30:45" |
| 152 | + |
| 153 | + def test_datetime_with_microseconds(self) -> None: |
| 154 | + """datetime with microseconds preserves precision.""" |
| 155 | + dt = datetime(2024, 1, 15, 10, 30, 45, 123456) |
| 156 | + assert make_serializable(dt) == "2024-01-15T10:30:45.123456" |
| 157 | + |
| 158 | + def test_dict_recursive(self) -> None: |
| 159 | + """Dictionaries are processed recursively.""" |
| 160 | + obj = {"date": date(2024, 1, 15), "name": "test"} |
| 161 | + result = make_serializable(obj) |
| 162 | + assert result == {"date": "2024-01-15", "name": "test"} |
| 163 | + |
| 164 | + def test_list_recursive(self) -> None: |
| 165 | + """Lists are processed recursively.""" |
| 166 | + obj = [date(2024, 1, 15), "test", 123] |
| 167 | + result = make_serializable(obj) |
| 168 | + assert result == ["2024-01-15", "test", 123] |
| 169 | + |
| 170 | + def test_nested_structure(self) -> None: |
| 171 | + """Deeply nested structures are fully processed.""" |
| 172 | + obj = { |
| 173 | + "dates": [date(2024, 1, 1), date(2024, 12, 31)], |
| 174 | + "nested": {"inner": {"dt": datetime(2024, 6, 15, 12, 0, 0)}}, |
| 175 | + } |
| 176 | + result = make_serializable(obj) |
| 177 | + assert result == { |
| 178 | + "dates": ["2024-01-01", "2024-12-31"], |
| 179 | + "nested": {"inner": {"dt": "2024-06-15T12:00:00"}}, |
| 180 | + } |
| 181 | + |
| 182 | + @pytest.mark.parametrize( |
| 183 | + "input_val", |
| 184 | + [ |
| 185 | + "string", |
| 186 | + 123, |
| 187 | + 45.67, |
| 188 | + True, |
| 189 | + False, |
| 190 | + None, |
| 191 | + ], |
| 192 | + ) |
| 193 | + def test_primitives_unchanged(self, input_val: object) -> None: |
| 194 | + """Primitive types pass through unchanged.""" |
| 195 | + assert make_serializable(input_val) == input_val |
| 196 | + |
| 197 | + def test_empty_dict(self) -> None: |
| 198 | + """Empty dict returns empty dict.""" |
| 199 | + assert make_serializable({}) == {} |
| 200 | + |
| 201 | + def test_empty_list(self) -> None: |
| 202 | + """Empty list returns empty list.""" |
| 203 | + assert make_serializable([]) == [] |
| 204 | + |
| 205 | + def test_dict_keys_unchanged(self) -> None: |
| 206 | + """Dictionary keys are not modified.""" |
| 207 | + obj = {"key_with_date": date(2024, 1, 1)} |
| 208 | + result = make_serializable(obj) |
| 209 | + assert "key_with_date" in result |
0 commit comments