|
1 | 1 | from datetime import datetime, timedelta |
2 | 2 | from pytz import UTC |
| 3 | +from freezegun import freeze_time |
| 4 | +from constance import config |
3 | 5 | from ordering.core import ( |
4 | 6 | get_current_order_round, |
5 | 7 | get_latest_order_round, |
6 | 8 | update_totals_for_products_with_max_order_amounts, |
7 | 9 | create_orderround_batch, |
8 | | - get_last_day_of_next_quarter, |
| 10 | + get_quarter_end_dates, |
| 11 | + get_last_order_round, |
9 | 12 | ) |
10 | 13 | from ordering.models import OrderProduct |
11 | 14 | from ordering.tests.factories import ( |
@@ -265,20 +268,73 @@ def test_latest_order_round_is_returned(self): |
265 | 268 |
|
266 | 269 |
|
267 | 270 | 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), |
272 | 286 | ) |
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 |
282 | 327 | ) |
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