Skip to content

Commit

Permalink
1071 Fix compare_dicts for numeric values (#1077)
Browse files Browse the repository at this point in the history
* fix `compare_dicts` for numeric values

* version pin esmerald for now
  • Loading branch information
dantownsend committed Sep 14, 2024
1 parent 47e5305 commit 2f8e84c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion piccolo/apps/asgi/commands/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"fastapi": ["fastapi>=0.112.1"],
"blacksheep": ["blacksheep"],
"litestar": ["litestar"],
"esmerald": ["esmerald"],
"esmerald": ["esmerald==3.3.0"],
"lilya": ["lilya"],
}
ROUTERS = list(ROUTER_DEPENDENCIES.keys())
Expand Down
13 changes: 10 additions & 3 deletions piccolo/apps/migrations/auto/diffable_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,17 @@ def compare_dicts(

for key, value in dict_1.items():
dict_2_value = dict_2.get(key, ...)

if (
dict_2_value is not ...
and dict_2_value != value
or dict_2_value is ...
# If the value is `...` then it means no value was found.
(dict_2_value is ...)
# We have to compare the types, because if we just use equality
# then 1.0 == 1 is True.
# See this issue:
# https://github.com/piccolo-orm/piccolo/issues/1071
or (type(value) is not type(dict_2_value))
# Finally compare the actual values.
or (dict_2_value != value)
):
output[key] = value

Expand Down
19 changes: 19 additions & 0 deletions tests/apps/migrations/auto/integration/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,25 @@ def test_column_type_conversion_float_decimal(self):
]
)

def test_column_type_conversion_integer_float(self):
"""
Make sure conversion between ``Integer`` and ``Real`` works - related
to this bug:
https://github.com/piccolo-orm/piccolo/issues/1071
"""
self._test_migrations(
table_snapshots=[
[self.table(column)]
for column in [
Real(default=1.0),
Integer(default=1),
Real(default=1.0),
]
]
)

def test_column_type_conversion_json(self):
self._test_migrations(
table_snapshots=[
Expand Down
13 changes: 13 additions & 0 deletions tests/apps/migrations/auto/test_diffable_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ def test_enum_values(self):
response = compare_dicts(dict_1, dict_2)
self.assertEqual(response, {"a": OnDelete.set_default})

def test_numeric_values(self):
"""
Make sure that if we have two numbers which are equal, but different
types, then they are identified as being different.
https://github.com/piccolo-orm/piccolo/issues/1071
"""
dict_1 = {"a": 1}
dict_2 = {"a": 1.0}
response = compare_dicts(dict_1, dict_2)
self.assertEqual(response, {"a": 1})


class TestDiffableTable(TestCase):
def test_subtract(self):
Expand Down

0 comments on commit 2f8e84c

Please sign in to comment.