Skip to content

Commit 551b7f3

Browse files
committed
Add corner-case test for model_update
- When only updating m2m field, don't bump `updated_at`
1 parent d053488 commit 551b7f3

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

styleguide_example/common/services.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def user_update(*, user: User, data) -> User:
3232
- If something in present in `fields` but not present in `data`, we simply skip.
3333
- There's a strict assertion that all values in `fields` are actual fields in `instance`.
3434
- `fields` can support m2m fields, which are handled after the update on `instance`.
35+
- If `auto_updated_at` is True, we'll try bumping `updated_at` with the current timestmap.
3536
"""
3637
has_updated = False
3738
m2m_data = {}
@@ -62,7 +63,7 @@ def user_update(*, user: User, data) -> User:
6263
# Perform an update only if any of the fields were actually changed
6364
if has_updated:
6465
if auto_updated_at:
65-
# We want to take care of the updated_at field,
66+
# We want to take care of the `updated_at` field,
6667
# Only if the models has that field
6768
# And if no value for updated_at has been provided
6869
if "updated_at" in model_fields and "updated_at" not in update_fields:

styleguide_example/common/tests/services/test_model_update.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,19 @@ def test_model_update_updates_many_to_many_fields(self):
8080

8181
self.assertNotIn(simple_obj, instance.simple_objects.all())
8282

83+
original_updated_at = instance.updated_at
84+
8385
updated_instance, has_updated = model_update(instance=instance, fields=update_fields, data=data)
8486

8587
self.assertEqual(updated_instance, instance)
8688
self.assertTrue(has_updated)
8789

8890
self.assertIn(simple_obj, updated_instance.simple_objects.all())
91+
self.assertEqual(
92+
original_updated_at,
93+
updated_instance.updated_at,
94+
"If we are only updating m2m fields, don't auto-bump `updated_at`",
95+
)
8996

9097
def test_model_update_updates_standard_and_many_to_many_fields(self):
9198
instance = RandomModelFactory()

0 commit comments

Comments
 (0)