Skip to content

Commit f0a8a6b

Browse files
committed
fix tests
1 parent de881e5 commit f0a8a6b

File tree

6 files changed

+190
-120
lines changed

6 files changed

+190
-120
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dependencies = [
2626
"gunicorn==22.0.0",
2727
"python-dateutil==2.8.2",
2828
"tzlocal>=5.2",
29+
"freezegun>=1.5.5",
2930
]
3031

3132
[project.optional-dependencies]

uv.lock

Lines changed: 112 additions & 99 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webapp/__init__.py

Whitespace-only changes.

webapp/accounts/tests/factories.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ class VokoUserFactory(DjangoModelFactory):
77
class Meta:
88
model = "accounts.VokoUser"
99

10-
first_name = Sequence(lambda n: 'John%s' % n)
11-
last_name = Sequence(lambda n: 'Doe%s' % n)
12-
email = LazyAttribute(lambda o: '%[email protected]' % o.last_name)
10+
first_name = Sequence(lambda n: "John%s" % n)
11+
last_name = Sequence(lambda n: "Doe%s" % n)
12+
email = LazyAttribute(lambda o: "%[email protected]" % o.last_name)
1313
is_active = True
1414

1515

webapp/ordering/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def calculate_next_orderround_dates(open_date):
217217
return open_datetime, close_datetime, collect_datetime
218218

219219

220-
def get_first_and_last_day_of_next_quarter():
220+
def get_quarter_end_dates():
221221
"""
222222
Get the last day of the current quarter and the last day of the next quarter as date objects.
223223
@@ -269,7 +269,7 @@ def create_orderround_batch():
269269
list[models.OrderRound]: The order round batches created
270270
"""
271271
# Get the last day of the current and next quarter
272-
last_day_current_quarter, last_day_next_quarter = get_first_and_last_day_of_next_quarter()
272+
last_day_current_quarter, last_day_next_quarter = get_quarter_end_dates()
273273

274274
# Check if we should create a new order round batch
275275
last_round = get_last_order_round()

webapp/ordering/tests/test_core.py

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
from datetime import datetime, timedelta
22
from pytz import UTC
3+
from freezegun import freeze_time
4+
from constance import config
35
from ordering.core import (
46
get_current_order_round,
57
get_latest_order_round,
68
update_totals_for_products_with_max_order_amounts,
79
create_orderround_batch,
8-
get_last_day_of_next_quarter,
10+
get_quarter_end_dates,
11+
get_last_order_round,
912
)
1013
from ordering.models import OrderProduct
1114
from ordering.tests.factories import (
@@ -265,20 +268,73 @@ def test_latest_order_round_is_returned(self):
265268

266269

267270
class TestAutomaticOrderRoundCreation(VokoTestCase):
268-
def test_new_order_round_batch_needed(self):
269-
"""Test that a new order round batch is created when the last round is less than 30 days away."""
270-
self.round = OrderRoundFactory(
271-
open_for_orders=datetime.now(tz=UTC) + timedelta(days=30),
271+
@freeze_time("2025-09-18")
272+
def test_create_orderround_batch_no_order_rounds_yet(self):
273+
"""Test creating order rounds when no order rounds exist yet"""
274+
# When there are no order rounds, should create from next week to end of current quarter
275+
result = create_orderround_batch()
276+
277+
self.assertEqual(len(result), 1) # Only one order round should fit in this period
278+
279+
def test_create_orderround_batch_one_order_round_no_more_in_quarter(self):
280+
"""Test when there's one order round but no more planned for rest of current quarter"""
281+
# Create an existing order round early in the quarter
282+
existing_round = OrderRoundFactory(
283+
open_for_orders=datetime(2024, 1, 8, 8, 0, tzinfo=UTC), # Early January
284+
closed_for_orders=datetime(2024, 1, 12, 8, 0, tzinfo=UTC),
285+
collect_datetime=datetime(2024, 1, 17, 18, 0, tzinfo=UTC),
272286
)
273-
order_rounds = create_orderround_batch()
274-
self.assertTrue(len(order_rounds) > 0)
275-
last_day = get_last_day_of_next_quarter()
276-
self.assertTrue(last_day < order_rounds[-1].open_for_orders.date() + timedelta(days=14))
277-
278-
def test_no_new_order_round_batch_needed(self):
279-
"""Test that no new order round batch is created when the last round is more than 30 days away."""
280-
self.round = OrderRoundFactory(
281-
open_for_orders=datetime.now(tz=UTC) + timedelta(days=32),
287+
288+
result = create_orderround_batch()
289+
290+
# Should create order rounds from January 22 (2 weeks after Jan 8) to end of Q1
291+
# This should create multiple order rounds to fill the gap
292+
self.assertGreater(len(result), 0)
293+
294+
# Get last ourder round
295+
last_round = get_last_order_round()
296+
297+
# Last order round should be before next quarter
298+
last_day_current_quarter, _ = get_quarter_end_dates()
299+
self.assertGreater(last_day_current_quarter, last_round.closed_for_orders.date())
300+
301+
@freeze_time("2024-02-01")
302+
def test_create_orderround_batch_next_quarter_far_away(self):
303+
"""Test when order rounds are planned for this quarter but next quarter is far away"""
304+
# Set test date to early February so next quarter (Q2) is more than 31 days away
305+
306+
# Create order rounds that go close to end of Q1
307+
OrderRoundFactory(
308+
open_for_orders=datetime(2024, 3, 25, 8, 0, tzinfo=UTC), # Near end of Q1
309+
closed_for_orders=datetime(2024, 3, 29, 8, 0, tzinfo=UTC),
310+
collect_datetime=datetime(2024, 4, 3, 18, 0, tzinfo=UTC),
311+
)
312+
313+
result = create_orderround_batch()
314+
315+
# Should not create any new order rounds since next quarter is far away
316+
# and current quarter is already covered
317+
self.assertEqual(len(result), 0)
318+
319+
@freeze_time("2025-09-02")
320+
def test_create_orderround_batch_next_quarter_close(self):
321+
"""Test when order rounds are planned for this quarter and next quarter is close"""
322+
# Set test date to late March so next quarter (Q2) is less than 31 days away
323+
324+
# Create order rounds that go to end of Q1
325+
existing_round = OrderRoundFactory(
326+
open_for_orders=datetime(2025, 9, 28, 12, 0, tzinfo=UTC) # Recent round in Q1
282327
)
283-
order_rounds = create_orderround_batch()
284-
self.assertEqual(len(order_rounds), 0)
328+
329+
result = create_orderround_batch()
330+
331+
# Should create order rounds for next quarter (Q2) since it's less than 31 days away
332+
self.assertGreater(len(result), 0)
333+
334+
# New order rounds should start in Q2 (April 1, 2024 onwards)
335+
first_round = result[0]
336+
expected_start = existing_round.open_for_orders.date() + timedelta(weeks=config.ORDERROUND_INTERVAL_WEEKS)
337+
338+
self.assertEqual(first_round.open_for_orders.date(), expected_start)
339+
# Should be in Q2 (April or later)
340+
self.assertGreaterEqual(first_round.open_for_orders.month, 10)

0 commit comments

Comments
 (0)