Skip to content

Commit 9435f1f

Browse files
committed
update user_account tests for on_commit
1 parent b447e95 commit 9435f1f

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

tests/testapp/tests/test_user_account.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from django.core import mail
44
from django.test import TestCase
55

6+
from django_capture_on_commit_callbacks import capture_on_commit_callbacks
7+
68
from tests.testapp.models import CannotDeleteActiveTrial, Organization, UserAccount
79

810

@@ -22,7 +24,10 @@ def test_update_joined_at_before_create(self):
2224
self.assertTrue(isinstance(account.joined_at, datetime.datetime))
2325

2426
def test_send_welcome_email_after_create(self):
25-
UserAccount.objects.create(**self.stub_data)
27+
with capture_on_commit_callbacks(execute=True) as callbacks:
28+
UserAccount.objects.create(**self.stub_data)
29+
30+
self.assertEquals(len(callbacks), 1, msg=f"{callbacks}")
2631
self.assertEqual(len(mail.outbox), 1)
2732
self.assertEqual(mail.outbox[0].subject, "Welcome!")
2833

@@ -73,12 +78,16 @@ def test_notify_org_name_change(self):
7378
org = Organization.objects.create(name="Hogwarts")
7479
UserAccount.objects.create(**self.stub_data, organization=org)
7580
mail.outbox = []
81+
7682
account = UserAccount.objects.get()
7783

78-
org.name = "Coursera Wizardry"
79-
org.save()
84+
with capture_on_commit_callbacks(execute=True) as callbacks:
85+
org.name = "Coursera Wizardry"
86+
org.save()
8087

81-
account.save()
88+
account.save()
89+
90+
self.assertEquals(len(callbacks), 1)
8291
self.assertEqual(len(mail.outbox), 1)
8392
self.assertEqual(
8493
mail.outbox[0].subject, "The name of your organization has changed!"
@@ -95,18 +104,23 @@ def test_no_notify_sent_if_org_name_has_not_changed(self):
95104
def test_additional_notify_sent_for_specific_org_name_change(self):
96105
org = Organization.objects.create(name="Hogwarts")
97106
UserAccount.objects.create(**self.stub_data, organization=org)
107+
98108
mail.outbox = []
99-
account = UserAccount.objects.get()
100109

101-
org.name = "Hogwarts Online"
102-
org.save()
110+
with capture_on_commit_callbacks(execute=True) as callbacks:
111+
account = UserAccount.objects.get()
103112

104-
account.save()
113+
org.name = "Hogwarts Online"
114+
org.save()
115+
116+
account.save()
117+
118+
self.assertEquals(len(callbacks), 1, msg="Only one hook should be an on_commit callback")
105119
self.assertEqual(len(mail.outbox), 2)
106120
self.assertEqual(
107-
mail.outbox[0].subject, "The name of your organization has changed!"
121+
mail.outbox[1].subject, "The name of your organization has changed!"
108122
)
109-
self.assertEqual(mail.outbox[1].subject, "You were moved to our online school!")
123+
self.assertEqual(mail.outbox[0].subject, "You were moved to our online school!")
110124

111125
def test_email_user_about_name_change(self):
112126
account = UserAccount.objects.create(**self.stub_data)

0 commit comments

Comments
 (0)