Skip to content

Commit 322663d

Browse files
committed
add basic test for mixin logic
1 parent c9a02aa commit 322663d

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

tests/testapp/tests/test_mixin.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,85 @@ def test_comparison_state_should_reset_after_save(self):
366366
self.assertTrue(account.has_changed("first_name"))
367367
account.save()
368368
self.assertFalse(account.has_changed("first_name"))
369+
370+
def test_run_hooked_methods_for_on_commit(self):
371+
instance = UserAccount(first_name="Bob")
372+
373+
instance._potentially_hooked_methods = MagicMock(
374+
return_value = [
375+
MagicMock(
376+
__name__="method_that_fires_on_commit",
377+
_hooked=[
378+
{
379+
"hook": "after_create",
380+
"when": None,
381+
"when_any": None,
382+
"has_changed": None,
383+
"is_now": "*",
384+
"is_not": NotSet,
385+
"was": "*",
386+
"was_not": NotSet,
387+
"changes_to": NotSet,
388+
"on_commit": True
389+
}
390+
],
391+
),
392+
MagicMock(
393+
__name__="method_that_fires_in_transaction",
394+
_hooked=[
395+
{
396+
"hook": "after_create",
397+
"when": None,
398+
"when_any": None,
399+
"has_changed": None,
400+
"is_now": "*",
401+
"is_not": NotSet,
402+
"was": "*",
403+
"was_not": NotSet,
404+
"changes_to": NotSet,
405+
"on_commit": False
406+
}
407+
],
408+
),
409+
MagicMock(
410+
__name__="method_that_fires_in_default",
411+
_hooked=[
412+
{
413+
"hook": "after_create",
414+
"when": None,
415+
"when_any": None,
416+
"has_changed": None,
417+
"is_now": "*",
418+
"is_not": NotSet,
419+
"was": "*",
420+
"was_not": NotSet,
421+
"changes_to": NotSet,
422+
"on_commit": None
423+
}
424+
],
425+
),
426+
MagicMock(
427+
__name__="after_save_method_that_fires_on_commit",
428+
_hooked=[
429+
{
430+
"hook": "after_save",
431+
"when": None,
432+
"when_any": None,
433+
"has_changed": None,
434+
"is_now": "*",
435+
"is_not": NotSet,
436+
"was": "*",
437+
"was_not": NotSet,
438+
"changes_to": NotSet,
439+
"on_commit": True
440+
}
441+
],
442+
),
443+
]
444+
)
445+
446+
fired_methods = instance._run_hooked_methods("after_create")
447+
self.assertEqual(fired_methods, ["method_that_fires_on_commit_on_commit", "method_that_fires_in_transaction", "method_that_fires_in_default"])
448+
449+
fired_methods = instance._run_hooked_methods("after_save")
450+
self.assertEqual(fired_methods, ["after_save_method_that_fires_on_commit_on_commit"])

0 commit comments

Comments
 (0)