Skip to content

Commit f0d4b0a

Browse files
committed
chore: Make tox tests pass again
1 parent 99ffe17 commit f0d4b0a

File tree

4 files changed

+157
-37
lines changed

4 files changed

+157
-37
lines changed

django_lifecycle/mixins.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def run(self, instance: Any) -> None:
4747

4848

4949
class OnCommitHookedMethod(AbstractHookedMethod):
50-
""" Hooked method that should run on_commit """
50+
"""Hooked method that should run on_commit"""
5151

5252
@property
5353
def name(self) -> str:
@@ -63,8 +63,12 @@ def run(self, instance: Any) -> None:
6363
transaction.on_commit(_on_commit_func)
6464

6565

66-
def instantiate_hooked_method(method: Any, callback_specs: HookConfig) -> AbstractHookedMethod:
67-
hooked_method_class = OnCommitHookedMethod if callback_specs.on_commit else HookedMethod
66+
def instantiate_hooked_method(
67+
method: Any, callback_specs: HookConfig
68+
) -> AbstractHookedMethod:
69+
hooked_method_class = (
70+
OnCommitHookedMethod if callback_specs.on_commit else HookedMethod
71+
)
6872
return hooked_method_class(
6973
method=method,
7074
priority=callback_specs.priority,
@@ -246,7 +250,6 @@ def _watched_fk_model_fields(cls) -> List[str]:
246250
def _watched_fk_models(cls) -> List[str]:
247251
return [_.split(".")[0] for _ in cls._watched_fk_model_fields()]
248252

249-
250253
def _get_hooked_methods(self, hook: str, **kwargs) -> List[AbstractHookedMethod]:
251254
"""
252255
Iterate through decorated methods to find those that should be
@@ -305,7 +308,7 @@ def _get_hooked_methods(self, hook: str, **kwargs) -> List[AbstractHookedMethod]
305308
return sorted(hooked_methods)
306309

307310
def _run_hooked_methods(self, hook: str, **kwargs) -> List[str]:
308-
""" Run hooked methods """
311+
"""Run hooked methods"""
309312
fired = []
310313

311314
for method in self._get_hooked_methods(hook, **kwargs):
@@ -337,7 +340,9 @@ def _check_callback_conditions(
337340

338341
return True
339342

340-
def _check_has_changed(self, field_name: str, specs: HookConfig, is_synced: bool) -> bool:
343+
def _check_has_changed(
344+
self, field_name: str, specs: HookConfig, is_synced: bool
345+
) -> bool:
341346
if not is_synced:
342347
return False
343348

pypi_submit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import os
22

33
os.system("python setup.py sdist --verbose")
4-
os.system("twine upload dist/*")
4+
os.system("twine upload dist/*")

tests/testapp/tests/test_mixin.py

Lines changed: 118 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,35 @@ def test_run_hooked_methods_for_when(self):
110110
MagicMock(
111111
__name__="method_that_does_fires",
112112
_hooked=[
113-
HookConfig(hook="after_create", when="first_name", when_any=None, has_changed=None, is_now="Bob",
114-
is_not=NotSet, was="*", was_not=NotSet, changes_to=NotSet, priority=DEFAULT_PRIORITY)
113+
HookConfig(
114+
hook="after_create",
115+
when="first_name",
116+
when_any=None,
117+
has_changed=None,
118+
is_now="Bob",
119+
is_not=NotSet,
120+
was="*",
121+
was_not=NotSet,
122+
changes_to=NotSet,
123+
priority=DEFAULT_PRIORITY,
124+
)
115125
],
116126
),
117127
MagicMock(
118128
__name__="method_that_does_not_fire",
119129
_hooked=[
120-
HookConfig(hook="after_create", when="first_name", when_any=None, has_changed=None, is_now="Bill",
121-
is_not=NotSet, was="*", was_not=NotSet, changes_to=NotSet, priority=DEFAULT_PRIORITY)
130+
HookConfig(
131+
hook="after_create",
132+
when="first_name",
133+
when_any=None,
134+
has_changed=None,
135+
is_now="Bill",
136+
is_not=NotSet,
137+
was="*",
138+
was_not=NotSet,
139+
changes_to=NotSet,
140+
priority=DEFAULT_PRIORITY,
141+
)
122142
],
123143
),
124144
]
@@ -134,15 +154,35 @@ def test_run_hooked_methods_for_when_any(self):
134154
MagicMock(
135155
__name__="method_that_does_fires",
136156
_hooked=[
137-
HookConfig(hook="after_create", when=None, when_any=["first_name", "last_name", "password"],
138-
has_changed=None, is_now="Bob", is_not=NotSet, was="*", was_not=NotSet, changes_to=NotSet, priority=DEFAULT_PRIORITY)
157+
HookConfig(
158+
hook="after_create",
159+
when=None,
160+
when_any=["first_name", "last_name", "password"],
161+
has_changed=None,
162+
is_now="Bob",
163+
is_not=NotSet,
164+
was="*",
165+
was_not=NotSet,
166+
changes_to=NotSet,
167+
priority=DEFAULT_PRIORITY,
168+
)
139169
],
140170
),
141171
MagicMock(
142172
__name__="method_that_does_not_fire",
143173
_hooked=[
144-
HookConfig(hook="after_create", when="first_name", when_any=None, has_changed=None, is_now="Bill",
145-
is_not=NotSet, was="*", was_not=NotSet, changes_to=NotSet, priority=DEFAULT_PRIORITY)
174+
HookConfig(
175+
hook="after_create",
176+
when="first_name",
177+
when_any=None,
178+
has_changed=None,
179+
is_now="Bill",
180+
is_not=NotSet,
181+
was="*",
182+
was_not=NotSet,
183+
changes_to=NotSet,
184+
priority=DEFAULT_PRIORITY,
185+
)
146186
],
147187
),
148188
]
@@ -350,57 +390,114 @@ def test_comparison_state_should_reset_after_save(self):
350390
self.assertTrue(account.has_changed("first_name"))
351391
with self.captureOnCommitCallbacks(execute=True) as callbacks:
352392
account.save()
353-
self.assertEqual(len(callbacks), 1, msg="Only the _reset_initial_state should be in the on_commit callbacks")
393+
self.assertEqual(
394+
len(callbacks),
395+
1,
396+
msg="Only the _reset_initial_state should be in the on_commit callbacks",
397+
)
354398
self.assertFalse(account.has_changed("first_name"))
355399

356400
def test_run_hooked_methods_for_on_commit(self):
357401
instance = UserAccount(first_name="Bob")
358402

359403
instance._potentially_hooked_methods = MagicMock(
360-
return_value = [
404+
return_value=[
361405
MagicMock(
362406
__name__="method_that_fires_on_commit",
363407
_hooked=[
364-
HookConfig(hook="after_create", when=None, when_any=None, has_changed=None, is_now="*", is_not=NotSet,
365-
was="*", was_not=NotSet, changes_to=NotSet, on_commit=True, priority=DEFAULT_PRIORITY)
408+
HookConfig(
409+
hook="after_create",
410+
when=None,
411+
when_any=None,
412+
has_changed=None,
413+
is_now="*",
414+
is_not=NotSet,
415+
was="*",
416+
was_not=NotSet,
417+
changes_to=NotSet,
418+
on_commit=True,
419+
priority=DEFAULT_PRIORITY,
420+
)
366421
],
367422
),
368423
MagicMock(
369424
__name__="method_that_fires_in_transaction",
370425
_hooked=[
371-
HookConfig(hook="after_create", when=None, when_any=None, has_changed=None, is_now="*", is_not=NotSet,
372-
was="*", was_not=NotSet, changes_to=NotSet, on_commit=False, priority=DEFAULT_PRIORITY)
426+
HookConfig(
427+
hook="after_create",
428+
when=None,
429+
when_any=None,
430+
has_changed=None,
431+
is_now="*",
432+
is_not=NotSet,
433+
was="*",
434+
was_not=NotSet,
435+
changes_to=NotSet,
436+
on_commit=False,
437+
priority=DEFAULT_PRIORITY,
438+
)
373439
],
374440
),
375441
MagicMock(
376442
__name__="method_that_fires_in_default",
377443
_hooked=[
378-
HookConfig(hook="after_create", when=None, when_any=None, has_changed=None, is_now="*", is_not=NotSet,
379-
was="*", was_not=NotSet, changes_to=NotSet, on_commit=None, priority=DEFAULT_PRIORITY)
444+
HookConfig(
445+
hook="after_create",
446+
when=None,
447+
when_any=None,
448+
has_changed=None,
449+
is_now="*",
450+
is_not=NotSet,
451+
was="*",
452+
was_not=NotSet,
453+
changes_to=NotSet,
454+
on_commit=None,
455+
priority=DEFAULT_PRIORITY,
456+
)
380457
],
381458
),
382459
MagicMock(
383460
__name__="after_save_method_that_fires_on_commit",
384461
_hooked=[
385-
HookConfig(hook="after_save", when=None, when_any=None, has_changed=None, is_now="*", is_not=NotSet,
386-
was="*", was_not=NotSet, changes_to=NotSet, on_commit=True, priority=DEFAULT_PRIORITY)
462+
HookConfig(
463+
hook="after_save",
464+
when=None,
465+
when_any=None,
466+
has_changed=None,
467+
is_now="*",
468+
is_not=NotSet,
469+
was="*",
470+
was_not=NotSet,
471+
changes_to=NotSet,
472+
on_commit=True,
473+
priority=DEFAULT_PRIORITY,
474+
)
387475
],
388476
),
389477
MagicMock(
390478
__name__="after_save_method_that_fires_if_changed_on_commit",
391-
_hooked=[HookConfig(hook="after_save", has_changed=True, on_commit=True)],
479+
_hooked=[
480+
HookConfig(hook="after_save", has_changed=True, on_commit=True)
481+
],
392482
),
393483
]
394484
)
395485

396486
fired_methods = instance._run_hooked_methods("after_create")
397-
self.assertEqual(fired_methods, ["method_that_fires_on_commit_on_commit", "method_that_fires_in_transaction", "method_that_fires_in_default"])
487+
self.assertEqual(
488+
fired_methods,
489+
[
490+
"method_that_fires_on_commit_on_commit",
491+
"method_that_fires_in_transaction",
492+
"method_that_fires_in_default",
493+
],
494+
)
398495

399496
fired_methods = instance._run_hooked_methods("after_save")
400497
self.assertEqual(
401498
fired_methods,
402499
[
403500
"after_save_method_that_fires_on_commit_on_commit",
404501
"after_save_method_that_fires_if_changed_on_commit_on_commit",
405-
]
502+
],
406503
)

tests/testapp/tests/test_user_account.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ def test_initial_values_on_commit_hook(self):
5151
self.assertEqual(len(mail.outbox), 1)
5252
self.assertEqual(
5353
mail.outbox[0].body,
54-
account.build_email_changed_body(old_email=initial_email, new_email=new_email)
54+
account.build_email_changed_body(
55+
old_email=initial_email, new_email=new_email
56+
),
5557
)
5658

5759
def test_email_banned_user_after_update(self):
@@ -136,7 +138,11 @@ def test_additional_notify_sent_for_specific_org_name_change(self):
136138
org.save()
137139
account.save()
138140

139-
self.assertEqual(len(callbacks), 3, msg="One hook and the _reset_initial_state (2) should be in the on_commit callbacks")
141+
self.assertEqual(
142+
len(callbacks),
143+
3,
144+
msg="One hook and the _reset_initial_state (2) should be in the on_commit callbacks",
145+
)
140146
self.assertEqual(len(mail.outbox), 2)
141147
self.assertEqual(
142148
mail.outbox[1].subject, "The name of your organization has changed!"
@@ -152,18 +158,26 @@ def test_email_user_about_name_change(self):
152158
mail.outbox[0].body, "You changed your first name or your last name"
153159
)
154160

155-
def test_does_not_email_user_about_name_change_when_name_excluded_from_update_fields(self):
161+
def test_does_not_email_user_about_name_change_when_name_excluded_from_update_fields(
162+
self,
163+
):
156164
account = UserAccount.objects.create(**self.stub_data)
157165
mail.outbox = []
158166
account.first_name = "Homer the Great"
159167
account.password = "New password!"
160168

161169
old_password_updated_at = account.password_updated_at
162170
account.save(update_fields=["password"])
163-
self.assertEqual(len(mail.outbox), 0) # `first_name` change was skipped (as a hook).
164-
self.assertNotEqual(account.password_updated_at, old_password_updated_at) # Ensure the other hook is fired.
165-
166-
def test_emails_user_about_name_change_when_one_field_from_update_fields_intersects_with_condition(self):
171+
self.assertEqual(
172+
len(mail.outbox), 0
173+
) # `first_name` change was skipped (as a hook).
174+
self.assertNotEqual(
175+
account.password_updated_at, old_password_updated_at
176+
) # Ensure the other hook is fired.
177+
178+
def test_emails_user_about_name_change_when_one_field_from_update_fields_intersects_with_condition(
179+
self,
180+
):
167181
account = UserAccount.objects.create(**self.stub_data)
168182
mail.outbox = []
169183
account.first_name = "Homer the Great"
@@ -174,7 +188,9 @@ def test_emails_user_about_name_change_when_one_field_from_update_fields_interse
174188
self.assertEqual(
175189
mail.outbox[0].body, "You changed your first name or your last name"
176190
)
177-
self.assertNotEqual(account.password_updated_at, old_password_updated_at) # Both hooks fired.
191+
self.assertNotEqual(
192+
account.password_updated_at, old_password_updated_at
193+
) # Both hooks fired.
178194

179195
def test_empty_update_fields_does_not_fire_any_hooks(self):
180196
# In Django, an empty list supplied to `update_fields` means not updating any field.
@@ -187,7 +203,9 @@ def test_empty_update_fields_does_not_fire_any_hooks(self):
187203
account.save(update_fields=[])
188204
# Did not raise, so last name hook didn't fire.
189205
self.assertEqual(len(mail.outbox), 0)
190-
self.assertEqual(account.password_updated_at, old_password_updated_at) # Password hook didn't fire either.
206+
self.assertEqual(
207+
account.password_updated_at, old_password_updated_at
208+
) # Password hook didn't fire either.
191209

192210
def test_skip_hooks(self):
193211
"""

0 commit comments

Comments
 (0)