Skip to content

Commit 5cf25e1

Browse files
committed
tests: refactor across multiple files
1 parent 03464a1 commit 5cf25e1

File tree

3 files changed

+172
-169
lines changed

3 files changed

+172
-169
lines changed

chores/tests/__init__.py

Whitespace-only changes.
Lines changed: 1 addition & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from members.models import User
1212

13-
from .models import Chore, ChoreNotification, ChoreVolunteer
13+
from ..models import Chore, ChoreNotification, ChoreVolunteer
1414

1515

1616
class CustomCommandTest(TestCase):
@@ -247,171 +247,6 @@ def test_email_reminding_volunteers(self):
247247
self.assertIn("Volunteering reminder", email.subject)
248248
self.assertIn("friendly reminder that you signed up for", email.body)
249249

250-
251-
class ChoreModelTest(TestCase):
252-
def setUp(self):
253-
self.user = User.objects.create_user(
254-
255-
password="testpass123",
256-
first_name="Test",
257-
last_name="User",
258-
)
259-
260-
def test_chore_creation(self):
261-
"""Test basic chore creation"""
262-
chore = Chore.objects.create(
263-
name="Test Chore",
264-
description="A test chore",
265-
class_type="BasicChore",
266-
configuration={"min_required_people": 1},
267-
creator=self.user,
268-
)
269-
self.assertEqual(chore.name, "Test Chore")
270-
self.assertEqual(chore.description, "A test chore")
271-
self.assertEqual(chore.class_type, "BasicChore")
272-
self.assertEqual(chore.creator, self.user)
273-
274-
def test_chore_str_representation(self):
275-
"""Test string representation of chore"""
276-
chore = Chore.objects.create(
277-
name="Test Chore",
278-
description="A test chore",
279-
class_type="BasicChore",
280-
configuration={"min_required_people": 1},
281-
creator=self.user,
282-
)
283-
self.assertEqual(str(chore), "Test Chore")
284-
285-
def test_chore_unique_name_constraint(self):
286-
"""Test that chore names must be unique"""
287-
Chore.objects.create(
288-
name="Test Chore",
289-
description="A test chore",
290-
class_type="BasicChore",
291-
configuration={"min_required_people": 1},
292-
creator=self.user,
293-
)
294-
295-
with self.assertRaises(Exception): # Should raise IntegrityError
296-
Chore.objects.create(
297-
name="Test Chore", # Same name
298-
description="Another test chore",
299-
class_type="BasicChore",
300-
configuration={"min_required_people": 1},
301-
creator=self.user,
302-
)
303-
304-
305-
class ChoreVolunteerModelTest(TestCase):
306-
def setUp(self):
307-
self.user = User.objects.create_user(
308-
309-
password="testpass123",
310-
first_name="Test",
311-
last_name="User",
312-
)
313-
self.chore = Chore.objects.create(
314-
name="Test Chore",
315-
description="A test chore",
316-
class_type="BasicChore",
317-
configuration={"min_required_people": 1},
318-
creator=self.user,
319-
)
320-
321-
def test_chore_volunteer_creation(self):
322-
"""Test basic chore volunteer creation"""
323-
volunteer = ChoreVolunteer.objects.create(
324-
user=self.user,
325-
chore=self.chore,
326-
timestamp=1753041600,
327-
)
328-
self.assertEqual(volunteer.user, self.user)
329-
self.assertEqual(volunteer.chore, self.chore)
330-
self.assertEqual(volunteer.timestamp, 1753041600)
331-
332-
def test_chore_volunteer_properties(self):
333-
"""Test chore volunteer properties"""
334-
volunteer = ChoreVolunteer.objects.create(
335-
user=self.user,
336-
chore=self.chore,
337-
timestamp=1753041600,
338-
)
339-
self.assertEqual(volunteer.first_name, "Test")
340-
self.assertEqual(volunteer.full_name, "Test User")
341-
342-
343-
class ChoreNotificationModelTest(TestCase):
344-
def setUp(self):
345-
self.user = User.objects.create_user(
346-
347-
password="testpass123",
348-
first_name="Test",
349-
last_name="User",
350-
)
351-
self.chore = Chore.objects.create(
352-
name="Test Chore",
353-
description="A test chore",
354-
class_type="BasicChore",
355-
configuration={"min_required_people": 1},
356-
creator=self.user,
357-
)
358-
359-
def test_notification_with_user_recipient(self):
360-
"""Test notification creation with user recipient"""
361-
notification = ChoreNotification.objects.create(
362-
event_key="test-key-123",
363-
chore=self.chore,
364-
recipient_user=self.user,
365-
)
366-
self.assertEqual(notification.event_key, "test-key-123")
367-
self.assertEqual(notification.chore, self.chore)
368-
self.assertEqual(notification.recipient_user, self.user)
369-
self.assertIsNone(notification.recipient_other)
370-
371-
def test_notification_with_email_recipient(self):
372-
"""Test notification creation with email recipient"""
373-
notification = ChoreNotification.objects.create(
374-
event_key="test-key-456",
375-
chore=self.chore,
376-
recipient_other="[email protected]",
377-
)
378-
self.assertEqual(notification.event_key, "test-key-456")
379-
self.assertEqual(notification.chore, self.chore)
380-
self.assertIsNone(notification.recipient_user)
381-
self.assertEqual(notification.recipient_other, "[email protected]")
382-
383-
def test_notification_validation_both_recipients(self):
384-
"""Test that notification cannot have both user and email recipients"""
385-
notification = ChoreNotification(
386-
event_key="test-key-789",
387-
chore=self.chore,
388-
recipient_user=self.user,
389-
recipient_other="[email protected]",
390-
)
391-
with self.assertRaises(ValidationError):
392-
notification.clean()
393-
394-
def test_notification_validation_no_recipients(self):
395-
"""Test that notification must have at least one recipient"""
396-
notification = ChoreNotification(
397-
event_key="test-key-789",
398-
chore=self.chore,
399-
)
400-
with self.assertRaises(ValidationError):
401-
notification.clean()
402-
403-
def test_notification_str_representation(self):
404-
"""Test string representation of notification"""
405-
notification = ChoreNotification.objects.create(
406-
event_key="test-key-123",
407-
chore=self.chore,
408-
recipient_user=self.user,
409-
)
410-
self.assertIn("Notification to", str(notification))
411-
self.assertIn("Test User", str(notification))
412-
self.assertIn("Test Chore", str(notification))
413-
414-
415250
class SingleOccurrenceChoreTest(TestCase):
416251
def setUp(self):
417252
self.user = User.objects.create_user(
@@ -920,9 +755,6 @@ def test_volunteer_cross_contamination_defect(self):
920755
out = StringIO()
921756
call_command("send_reminders", stdout=out)
922757

923-
# DEFECT: Due to ChoreVolunteer.objects.all() query, chore2 should NOT get a reminder
924-
# because the volunteer from chore1 is incorrectly counted for chore2
925758
# Verify that chore2 should have gotten a reminder if the bug was fixed
926-
# (This assertion documents the expected correct behavior)
927759
self.assertEqual(len(mail.outbox), 1)
928760
self.assertIn("Chore 2 needs volunteers", mail.outbox[0].subject)

chores/tests/test_models.py

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# Model tests for chores app
2+
from django.core.exceptions import ValidationError
3+
from django.test import TestCase
4+
5+
from members.models import User
6+
7+
from ..models import Chore, ChoreNotification, ChoreVolunteer
8+
9+
10+
class ChoreModelTest(TestCase):
11+
def setUp(self):
12+
self.user = User.objects.create_user(
13+
14+
password="testpass123",
15+
first_name="Test",
16+
last_name="User",
17+
)
18+
19+
def test_chore_creation(self):
20+
"""Test basic chore creation"""
21+
chore = Chore.objects.create(
22+
name="Test Chore",
23+
description="A test chore",
24+
class_type="BasicChore",
25+
configuration={"min_required_people": 1},
26+
creator=self.user,
27+
)
28+
self.assertEqual(chore.name, "Test Chore")
29+
self.assertEqual(chore.description, "A test chore")
30+
self.assertEqual(chore.class_type, "BasicChore")
31+
self.assertEqual(chore.creator, self.user)
32+
33+
def test_chore_str_representation(self):
34+
"""Test string representation of chore"""
35+
chore = Chore.objects.create(
36+
name="Test Chore",
37+
description="A test chore",
38+
class_type="BasicChore",
39+
configuration={"min_required_people": 1},
40+
creator=self.user,
41+
)
42+
self.assertEqual(str(chore), "Test Chore")
43+
44+
def test_chore_unique_name_constraint(self):
45+
"""Test that chore names must be unique"""
46+
Chore.objects.create(
47+
name="Test Chore",
48+
description="A test chore",
49+
class_type="BasicChore",
50+
configuration={"min_required_people": 1},
51+
creator=self.user,
52+
)
53+
54+
with self.assertRaises(Exception): # Should raise IntegrityError
55+
Chore.objects.create(
56+
name="Test Chore", # Same name
57+
description="Another test chore",
58+
class_type="BasicChore",
59+
configuration={"min_required_people": 1},
60+
creator=self.user,
61+
)
62+
63+
64+
class ChoreVolunteerModelTest(TestCase):
65+
def setUp(self):
66+
self.user = User.objects.create_user(
67+
68+
password="testpass123",
69+
first_name="Test",
70+
last_name="User",
71+
)
72+
self.chore = Chore.objects.create(
73+
name="Test Chore",
74+
description="A test chore",
75+
class_type="BasicChore",
76+
configuration={"min_required_people": 1},
77+
creator=self.user,
78+
)
79+
80+
def test_chore_volunteer_creation(self):
81+
"""Test basic chore volunteer creation"""
82+
volunteer = ChoreVolunteer.objects.create(
83+
user=self.user,
84+
chore=self.chore,
85+
timestamp=1753041600,
86+
)
87+
self.assertEqual(volunteer.user, self.user)
88+
self.assertEqual(volunteer.chore, self.chore)
89+
self.assertEqual(volunteer.timestamp, 1753041600)
90+
91+
def test_chore_volunteer_properties(self):
92+
"""Test chore volunteer properties"""
93+
volunteer = ChoreVolunteer.objects.create(
94+
user=self.user,
95+
chore=self.chore,
96+
timestamp=1753041600,
97+
)
98+
self.assertEqual(volunteer.first_name, "Test")
99+
self.assertEqual(volunteer.full_name, "Test User")
100+
101+
102+
class ChoreNotificationModelTest(TestCase):
103+
def setUp(self):
104+
self.user = User.objects.create_user(
105+
106+
password="testpass123",
107+
first_name="Test",
108+
last_name="User",
109+
)
110+
self.chore = Chore.objects.create(
111+
name="Test Chore",
112+
description="A test chore",
113+
class_type="BasicChore",
114+
configuration={"min_required_people": 1},
115+
creator=self.user,
116+
)
117+
118+
def test_notification_with_user_recipient(self):
119+
"""Test notification creation with user recipient"""
120+
notification = ChoreNotification.objects.create(
121+
event_key="test-key-123",
122+
chore=self.chore,
123+
recipient_user=self.user,
124+
)
125+
self.assertEqual(notification.event_key, "test-key-123")
126+
self.assertEqual(notification.chore, self.chore)
127+
self.assertEqual(notification.recipient_user, self.user)
128+
self.assertIsNone(notification.recipient_other)
129+
130+
def test_notification_with_email_recipient(self):
131+
"""Test notification creation with email recipient"""
132+
notification = ChoreNotification.objects.create(
133+
event_key="test-key-456",
134+
chore=self.chore,
135+
recipient_other="[email protected]",
136+
)
137+
self.assertEqual(notification.event_key, "test-key-456")
138+
self.assertEqual(notification.chore, self.chore)
139+
self.assertIsNone(notification.recipient_user)
140+
self.assertEqual(notification.recipient_other, "[email protected]")
141+
142+
def test_notification_validation_both_recipients(self):
143+
"""Test that notification cannot have both user and email recipients"""
144+
notification = ChoreNotification(
145+
event_key="test-key-789",
146+
chore=self.chore,
147+
recipient_user=self.user,
148+
recipient_other="[email protected]",
149+
)
150+
with self.assertRaises(ValidationError):
151+
notification.clean()
152+
153+
def test_notification_validation_no_recipients(self):
154+
"""Test that notification must have at least one recipient"""
155+
notification = ChoreNotification(
156+
event_key="test-key-789",
157+
chore=self.chore,
158+
)
159+
with self.assertRaises(ValidationError):
160+
notification.clean()
161+
162+
def test_notification_str_representation(self):
163+
"""Test string representation of notification"""
164+
notification = ChoreNotification.objects.create(
165+
event_key="test-key-123",
166+
chore=self.chore,
167+
recipient_user=self.user,
168+
)
169+
self.assertIn("Notification to", str(notification))
170+
self.assertIn("Test User", str(notification))
171+
self.assertIn("Test Chore", str(notification))

0 commit comments

Comments
 (0)