Skip to content

Commit

Permalink
Fix to handle mixed none and non none values
Browse files Browse the repository at this point in the history
Related to #34"
  • Loading branch information
Somtom committed Jan 27, 2024
1 parent 07a132f commit a75f848
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

* Failing on mixed None values <https://github.com/DeepLcom/sql-mock/issues/34>

## [0.5.4]

**Full Changelog**: <https://github.com/DeepLcom/sql-mock/compare/v0.5.3...v0.5.4>
Expand Down
11 changes: 9 additions & 2 deletions src/sql_mock/table_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,15 @@ def _assert_equal(
keys_to_keep = get_keys_from_list_of_dicts(expected)
data = [{key: value for key, value in dictionary.items() if key in keys_to_keep} for dictionary in data]
if ignore_order:
data = sorted(data, key=lambda d: sorted(d.items()))
expected = sorted(expected, key=lambda d: sorted(d.items()))
def sort_handling_none(d):
"""
Sorts a dictionary by its values, but handles None values as -inf.
We do this to avoid issues with mixed None and non-None values in the same column.
"""
none_safe_items = [(key, value) if value is not None else (key, float('-inf')) for key, value in d.items()]
return sorted(none_safe_items)
data = sorted(data, key=sort_handling_none)
expected = sorted(expected, key=sort_handling_none)
try:
assert expected == data
except Exception as e:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_table_mocks/test_assert_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ class MockTestTable(BaseTableMock):
True, # ignore_order
id="Matching data - Missing keys ignored - Order ignored",
),
pytest.param(
[{"name": "Alice", "age": None, "city": "New York"}, {"name": "Bob", "age": 30, "city": "Munich"}], # data
[{"name": "Alice", "age": None}, {"name": "Bob", "age": 30}], # expected_data
True, # ignore_missing_keys
True, # ignore_order
id="Matching data - Missing keys ignored - Order ignored - including mixed None and int values",
),
pytest.param(
[{"name":None, "age": 25, "city": "New York"}, {"name": "Bob", "age": 30, "city": "Munich"}], # data
[{"name": None, "age": 25}, {"name": "Bob", "age": 30}], # expected_data
True, # ignore_missing_keys
True, # ignore_order
id="Matching data - Missing keys ignored - Order ignored - including mixed None and str values",
),
pytest.param(
[{"name": "Alice", "age": 25, "city": "New York"}, {"name": "Bob", "age": 30, "city": "Munich"}], # data
[{"name": "Alice", "age": 25}, {"name": "Bob", "age": 30}], # expected_data
Expand Down

0 comments on commit a75f848

Please sign in to comment.