Skip to content

Commit f0dd72d

Browse files
committed
Simplify
1 parent 9c73a39 commit f0dd72d

File tree

1 file changed

+30
-165
lines changed

1 file changed

+30
-165
lines changed
Lines changed: 30 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import date, datetime
1+
from datetime import datetime
22

33
import pytest
44
from freezegun import freeze_time
@@ -8,63 +8,15 @@
88
Patch,
99
PatchOnCommitFest,
1010
)
11-
from pgcommitfest.userprofile.models import UserProfile
12-
13-
14-
@pytest.fixture
15-
def alice(db, django_user_model):
16-
user = django_user_model.objects.create_user(
17-
username="alice",
18-
email="alice@example.com",
19-
first_name="Alice",
20-
last_name="Smith",
21-
)
22-
UserProfile.objects.create(user=user, notify_all_author=True)
23-
return user
24-
25-
26-
def create_closed_cf(name, startdate, enddate):
27-
"""Helper to create a closed CF for padding."""
28-
return CommitFest.objects.create(
29-
name=name,
30-
status=CommitFest.STATUS_CLOSED,
31-
startdate=startdate,
32-
enddate=enddate,
33-
)
3411

3512

3613
@pytest.mark.django_db
3714
@freeze_time("2024-12-05")
38-
def test_inprogress_cf_closes_when_enddate_passed(alice):
15+
def test_inprogress_cf_closes_when_enddate_passed(commitfests, alice):
3916
"""When an in_progress CF's enddate has passed, it should be closed."""
40-
# Create some closed CFs for padding (relevant_commitfests needs history)
41-
create_closed_cf("2024-07", date(2024, 7, 1), date(2024, 7, 31))
42-
create_closed_cf("2024-09", date(2024, 9, 1), date(2024, 9, 30))
43-
44-
# Create an in_progress CF that ended
45-
in_progress_cf = CommitFest.objects.create(
46-
name="2024-11",
47-
status=CommitFest.STATUS_INPROGRESS,
48-
startdate=date(2024, 11, 1),
49-
enddate=date(2024, 11, 30),
50-
)
51-
# Create the next open CF (required for auto_move)
52-
open_cf = CommitFest.objects.create(
53-
name="2025-01",
54-
status=CommitFest.STATUS_OPEN,
55-
startdate=date(2025, 1, 1),
56-
enddate=date(2025, 1, 31),
57-
)
58-
# Create draft CF
59-
CommitFest.objects.create(
60-
name="2025-draft",
61-
status=CommitFest.STATUS_OPEN,
62-
startdate=date(2024, 7, 1),
63-
enddate=date(2025, 3, 31),
64-
draft=True,
65-
)
17+
in_progress_cf = commitfests["in_progress"]
18+
open_cf = commitfests["open"]
6619

67-
# Create a patch with recent activity that should be auto-moved
6820
patch = Patch.objects.create(
6921
name="Test Patch",
7022
lastmail=datetime(2024, 11, 25),
@@ -82,41 +34,24 @@ def test_inprogress_cf_closes_when_enddate_passed(alice):
8234
in_progress_cf.refresh_from_db()
8335
assert in_progress_cf.status == CommitFest.STATUS_CLOSED
8436

85-
# Patch should have been moved to the open CF
8637
patch.refresh_from_db()
8738
assert patch.current_commitfest().id == open_cf.id
8839

8940

9041
@pytest.mark.django_db
9142
@freeze_time("2025-01-15")
92-
def test_open_cf_becomes_inprogress_when_startdate_reached():
43+
def test_open_cf_becomes_inprogress_when_startdate_reached(commitfests):
9344
"""When an open CF's startdate is reached, it becomes in_progress."""
94-
# Create some closed CFs for padding
95-
create_closed_cf("2024-07", date(2024, 7, 1), date(2024, 7, 31))
96-
create_closed_cf("2024-09", date(2024, 9, 1), date(2024, 9, 30))
97-
create_closed_cf("2024-11", date(2024, 11, 1), date(2024, 11, 30))
98-
99-
open_cf = CommitFest.objects.create(
100-
name="2025-01",
101-
status=CommitFest.STATUS_OPEN,
102-
startdate=date(2025, 1, 1),
103-
enddate=date(2025, 1, 31),
104-
)
105-
# Create draft CF
106-
CommitFest.objects.create(
107-
name="2025-draft",
108-
status=CommitFest.STATUS_OPEN,
109-
startdate=date(2024, 7, 1),
110-
enddate=date(2025, 3, 31),
111-
draft=True,
112-
)
45+
open_cf = commitfests["open"]
46+
# Mark in_progress as closed so open_cf becomes the active one
47+
commitfests["in_progress"].status = CommitFest.STATUS_CLOSED
48+
commitfests["in_progress"].save()
11349

11450
CommitFest._refresh_relevant_commitfests(for_update=False)
11551

11652
open_cf.refresh_from_db()
11753
assert open_cf.status == CommitFest.STATUS_INPROGRESS
11854

119-
# A new open CF should have been created
12055
new_open = CommitFest.objects.filter(
12156
status=CommitFest.STATUS_OPEN, draft=False
12257
).first()
@@ -126,29 +61,13 @@ def test_open_cf_becomes_inprogress_when_startdate_reached():
12661

12762
@pytest.mark.django_db
12863
@freeze_time("2025-02-05")
129-
def test_open_cf_closes_when_enddate_passed(alice):
64+
def test_open_cf_closes_when_enddate_passed(commitfests, alice):
13065
"""When an open CF's enddate has passed (skipping in_progress), it closes."""
131-
# Create some closed CFs for padding
132-
create_closed_cf("2024-07", date(2024, 7, 1), date(2024, 7, 31))
133-
create_closed_cf("2024-09", date(2024, 9, 1), date(2024, 9, 30))
134-
create_closed_cf("2024-11", date(2024, 11, 1), date(2024, 11, 30))
135-
136-
open_cf = CommitFest.objects.create(
137-
name="2025-01",
138-
status=CommitFest.STATUS_OPEN,
139-
startdate=date(2025, 1, 1),
140-
enddate=date(2025, 1, 31),
141-
)
142-
# Create draft CF
143-
CommitFest.objects.create(
144-
name="2025-draft",
145-
status=CommitFest.STATUS_OPEN,
146-
startdate=date(2024, 7, 1),
147-
enddate=date(2025, 3, 31),
148-
draft=True,
149-
)
66+
open_cf = commitfests["open"]
67+
# Mark in_progress as closed
68+
commitfests["in_progress"].status = CommitFest.STATUS_CLOSED
69+
commitfests["in_progress"].save()
15070

151-
# Create a patch with recent activity
15271
patch = Patch.objects.create(
15372
name="Test Patch",
15473
lastmail=datetime(2025, 1, 25),
@@ -166,71 +85,43 @@ def test_open_cf_closes_when_enddate_passed(alice):
16685
open_cf.refresh_from_db()
16786
assert open_cf.status == CommitFest.STATUS_CLOSED
16887

169-
# A new open CF should have been created
17088
new_open = CommitFest.objects.filter(
17189
status=CommitFest.STATUS_OPEN, draft=False
17290
).first()
17391
assert new_open is not None
17492

175-
# Patch should have been moved to the new open CF
17693
patch.refresh_from_db()
17794
assert patch.current_commitfest().id == new_open.id
17895

17996

18097
@pytest.mark.django_db
18198
@freeze_time("2025-01-15")
182-
def test_draft_cf_created_when_missing():
99+
def test_draft_cf_created_when_missing(commitfests):
183100
"""When no draft CF exists, one should be created."""
184-
# Create closed CFs for padding
185-
create_closed_cf("2024-07", date(2024, 7, 1), date(2024, 7, 31))
186-
create_closed_cf("2024-09", date(2024, 9, 1), date(2024, 9, 30))
187-
create_closed_cf("2024-11", date(2024, 11, 1), date(2024, 11, 30))
188-
189-
# Create only regular CFs
190-
CommitFest.objects.create(
191-
name="2025-01",
192-
status=CommitFest.STATUS_OPEN,
193-
startdate=date(2025, 3, 1),
194-
enddate=date(2025, 3, 31),
195-
)
101+
# Mark in_progress as closed
102+
commitfests["in_progress"].status = CommitFest.STATUS_CLOSED
103+
commitfests["in_progress"].save()
104+
# Delete the draft CF
105+
commitfests["draft"].delete()
196106

197107
assert not CommitFest.objects.filter(draft=True).exists()
198108

199109
CommitFest._refresh_relevant_commitfests(for_update=False)
200110

201-
# A draft CF should have been created
202111
draft_cf = CommitFest.objects.filter(draft=True).first()
203112
assert draft_cf is not None
204113
assert draft_cf.status == CommitFest.STATUS_OPEN
205114

206115

207116
@pytest.mark.django_db
208117
@freeze_time("2025-04-05")
209-
def test_draft_cf_closes_when_enddate_passed(alice):
118+
def test_draft_cf_closes_when_enddate_passed(commitfests, alice):
210119
"""When a draft CF's enddate has passed, it should be closed."""
211-
# Create closed CFs for padding
212-
create_closed_cf("2024-07", date(2024, 7, 1), date(2024, 7, 31))
213-
create_closed_cf("2024-09", date(2024, 9, 1), date(2024, 9, 30))
214-
create_closed_cf("2024-11", date(2024, 11, 1), date(2024, 11, 30))
215-
216-
# Create an open regular CF (required)
217-
CommitFest.objects.create(
218-
name="2025-03",
219-
status=CommitFest.STATUS_OPEN,
220-
startdate=date(2025, 5, 1),
221-
enddate=date(2025, 5, 31),
222-
)
223-
224-
# Create a draft CF that ended
225-
draft_cf = CommitFest.objects.create(
226-
name="2025-draft",
227-
status=CommitFest.STATUS_OPEN,
228-
startdate=date(2025, 1, 1),
229-
enddate=date(2025, 3, 31),
230-
draft=True,
231-
)
120+
draft_cf = commitfests["draft"]
121+
# Mark in_progress as closed
122+
commitfests["in_progress"].status = CommitFest.STATUS_CLOSED
123+
commitfests["in_progress"].save()
232124

233-
# Create a patch with recent activity
234125
patch = Patch.objects.create(
235126
name="Draft Patch",
236127
lastmail=datetime(2025, 3, 25),
@@ -248,59 +139,33 @@ def test_draft_cf_closes_when_enddate_passed(alice):
248139
draft_cf.refresh_from_db()
249140
assert draft_cf.status == CommitFest.STATUS_CLOSED
250141

251-
# A new draft CF should have been created
252142
new_draft = CommitFest.objects.filter(
253143
draft=True, status=CommitFest.STATUS_OPEN
254144
).first()
255145
assert new_draft is not None
256146
assert new_draft.startdate > draft_cf.enddate
257147

258-
# Patch should have been moved to the new draft CF
259148
patch.refresh_from_db()
260149
assert patch.current_commitfest().id == new_draft.id
261150

262151

263152
@pytest.mark.django_db
264-
@freeze_time("2025-01-15")
265-
def test_no_changes_when_up_to_date():
153+
@freeze_time("2024-11-15")
154+
def test_no_changes_when_up_to_date(commitfests):
266155
"""When commitfests are up to date, no changes should be made."""
267-
# Create closed CFs for padding
268-
create_closed_cf("2024-07", date(2024, 7, 1), date(2024, 7, 31))
269-
create_closed_cf("2024-09", date(2024, 9, 1), date(2024, 9, 30))
270-
271-
# Create CFs that are all up to date
272-
in_progress_cf = CommitFest.objects.create(
273-
name="2025-01",
274-
status=CommitFest.STATUS_INPROGRESS,
275-
startdate=date(2025, 1, 1),
276-
enddate=date(2025, 1, 31),
277-
)
278-
open_cf = CommitFest.objects.create(
279-
name="2025-03",
280-
status=CommitFest.STATUS_OPEN,
281-
startdate=date(2025, 3, 1),
282-
enddate=date(2025, 3, 31),
283-
)
284-
draft_cf = CommitFest.objects.create(
285-
name="2025-draft",
286-
status=CommitFest.STATUS_OPEN,
287-
startdate=date(2025, 1, 1),
288-
enddate=date(2025, 3, 31),
289-
draft=True,
290-
)
156+
in_progress_cf = commitfests["in_progress"]
157+
open_cf = commitfests["open"]
158+
draft_cf = commitfests["draft"]
291159

292160
initial_cf_count = CommitFest.objects.count()
293161

294162
CommitFest._refresh_relevant_commitfests(for_update=False)
295163

296-
# All statuses should remain unchanged
297164
in_progress_cf.refresh_from_db()
298165
open_cf.refresh_from_db()
299166
draft_cf.refresh_from_db()
300167

301168
assert in_progress_cf.status == CommitFest.STATUS_INPROGRESS
302169
assert open_cf.status == CommitFest.STATUS_OPEN
303170
assert draft_cf.status == CommitFest.STATUS_OPEN
304-
305-
# No new CFs should have been created
306171
assert CommitFest.objects.count() == initial_cf_count

0 commit comments

Comments
 (0)