Skip to content

Commit 72604ae

Browse files
committed
Add tests
1 parent 90cd008 commit 72604ae

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

tests/testapp/tests/test_mixin.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,24 @@ def test_changes_to_condition_should_pass(self):
316316
user_account.last_name = "Flanders"
317317
user_account.save()
318318

319+
def test_changes_to_condition_included_in_update_fields_should_fire_hook(self):
320+
user_account = UserAccount.objects.create(**self.stub_data)
321+
user_account.first_name = "Flanders"
322+
user_account.last_name = "Flanders"
323+
with self.assertRaises(CannotRename, msg="Oh, not Flanders. Anybody but Flanders."):
324+
user_account.last_name = "Flanders"
325+
user_account.save(update_fields=["last_name"])
326+
327+
def test_changes_to_condition_not_included_in_update_fields_should_not_fire_hook(self):
328+
user_account = UserAccount.objects.create(**self.stub_data)
329+
user_account.first_name = "Flanders"
330+
user_account.last_name = "Flanders"
331+
user_account.save(update_fields=["first_name"]) # `CannotRename` exception is not raised
332+
333+
user_account.refresh_from_db()
334+
self.assertEqual(user_account.first_name, "Flanders")
335+
self.assertNotEqual(user_account.last_name, "Flanders")
336+
319337
def test_changes_to_condition_should_not_pass(self):
320338
data = self.stub_data
321339
data["first_name"] = "Marge"

tests/testapp/tests/test_user_account.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,43 @@ def test_email_user_about_name_change(self):
117117
mail.outbox[0].body, "You changed your first name or your last name"
118118
)
119119

120+
def test_does_not_email_user_about_name_change_when_name_excluded_from_update_fields(self):
121+
account = UserAccount.objects.create(**self.stub_data)
122+
mail.outbox = []
123+
account.first_name = "Homer the Great"
124+
account.password = "New password!"
125+
126+
old_password_updated_at = account.password_updated_at
127+
account.save(update_fields=["password"])
128+
self.assertEqual(len(mail.outbox), 0) # `first_name` change was skipped (as a hook).
129+
self.assertNotEqual(account.password_updated_at, old_password_updated_at) # Ensure the other hook is fired.
130+
131+
def test_emails_user_about_name_change_when_one_field_from_update_fields_intersects_with_condition(self):
132+
account = UserAccount.objects.create(**self.stub_data)
133+
mail.outbox = []
134+
account.first_name = "Homer the Great"
135+
account.password = "New password!"
136+
137+
old_password_updated_at = account.password_updated_at
138+
account.save(update_fields=["first_name", "password"])
139+
self.assertEqual(
140+
mail.outbox[0].body, "You changed your first name or your last name"
141+
)
142+
self.assertNotEqual(account.password_updated_at, old_password_updated_at) # Both hooks fired.
143+
144+
def test_empty_update_fields_does_not_fire_any_hooks(self):
145+
# In Django, an empty list supplied to `update_fields` means not updating any field.
146+
account = UserAccount.objects.create(**self.stub_data)
147+
mail.outbox = []
148+
account.first_name = "Flanders"
149+
account.password = "new pass"
150+
151+
old_password_updated_at = account.password_updated_at
152+
account.save(update_fields=[])
153+
# Did not raise, so last name hook didn't fire.
154+
self.assertEqual(len(mail.outbox), 0)
155+
self.assertEqual(account.password_updated_at, old_password_updated_at) # Password hook didn't fire either.
156+
120157
def test_skip_hooks(self):
121158
"""
122159
Hooked method that auto-lowercases email should be skipped.

0 commit comments

Comments
 (0)