Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1071 Fix compare_dicts for numeric values #1077

Merged
merged 2 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading