-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_schedule_config.py
More file actions
824 lines (716 loc) · 28.2 KB
/
Copy pathtest_schedule_config.py
File metadata and controls
824 lines (716 loc) · 28.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
"""Tests for boost_collector_runner.schedule_config: load_config, validation, get_tasks_for_schedule, get_beat_schedule."""
import calendar
import logging
from pathlib import Path
from unittest.mock import patch
import pytest
import yaml
from django.core.management import get_commands
from boost_collector_runner.schedule_config import (
DEFAULT_GROUP_BATCH_SCHEDULE_KIND,
INTERVAL_MINUTES_MAX,
ScheduleConfigurationError,
get_beat_schedule,
get_tasks_for_schedule,
load_config,
_validate_task,
)
# --- load_config validation ---
def test_load_config_requires_path():
"""load_config(path=None) raises ValueError."""
with pytest.raises(ValueError, match="load_config requires a path"):
load_config(None)
def test_load_config_file_not_found(tmp_path):
"""load_config with non-existent path raises FileNotFoundError."""
missing = tmp_path / "missing.yaml"
with pytest.raises(FileNotFoundError, match="Schedule YAML not found"):
load_config(missing)
def test_load_config_data_not_dict(tmp_path):
"""YAML that is not a dict (e.g. list or null) raises ValueError."""
path = tmp_path / "config.yaml"
path.write_text("[]", encoding="utf-8")
with pytest.raises(ValueError, match="Schedule YAML must be a dict with 'groups'"):
load_config(path)
path.write_text("null", encoding="utf-8")
with pytest.raises(ValueError, match="Schedule YAML must be a dict with 'groups'"):
load_config(path)
def test_load_config_groups_missing(tmp_path):
"""YAML without 'groups' key or with null groups raises ValueError."""
path = tmp_path / "config.yaml"
path.write_text(yaml.dump({"other": 1}), encoding="utf-8")
with pytest.raises(ValueError, match="Schedule YAML must have 'groups' \\(dict\\)"):
load_config(path)
path.write_text(yaml.dump({"groups": None}), encoding="utf-8")
with pytest.raises(ValueError, match="Schedule YAML must have 'groups' \\(dict\\)"):
load_config(path)
def test_load_config_groups_not_dict(tmp_path):
"""YAML with groups as non-dict raises ValueError."""
path = tmp_path / "config.yaml"
path.write_text(yaml.dump({"groups": []}), encoding="utf-8")
with pytest.raises(ValueError, match="Schedule YAML must have 'groups' \\(dict\\)"):
load_config(path)
def test_load_config_group_id_empty_string(tmp_path):
"""Group id that is empty string raises ValueError."""
path = tmp_path / "config.yaml"
path.write_text(
yaml.dump(
{
"groups": {
"": {
"default_time": "04:10",
"tasks": [],
},
},
}
),
encoding="utf-8",
)
with pytest.raises(ValueError, match="Group id must be a non-empty string"):
load_config(path)
def test_load_config_group_id_whitespace_only(tmp_path):
"""Group id that is only whitespace raises ValueError."""
path = tmp_path / "config.yaml"
path.write_text(
yaml.dump(
{
"groups": {
" ": {
"default_time": "04:10",
"tasks": [],
},
},
}
),
encoding="utf-8",
)
with pytest.raises(ValueError, match="Group id must be a non-empty string"):
load_config(path)
def test_load_config_group_data_not_dict(tmp_path):
"""Group value that is not a dict raises ValueError."""
path = tmp_path / "config.yaml"
path.write_text(
yaml.dump(
{
"groups": {
"github": "not a dict",
},
}
),
encoding="utf-8",
)
with pytest.raises(ValueError, match="Group 'github' must be a dict"):
load_config(path)
def test_load_config_default_time_missing(tmp_path):
"""Group without default_time or with empty default_time raises ValueError."""
path = tmp_path / "config.yaml"
path.write_text(
yaml.dump(
{
"groups": {
"github": {
"tasks": [],
},
},
}
),
encoding="utf-8",
)
with pytest.raises(ValueError, match="must have 'default_time'"):
load_config(path)
path.write_text(
yaml.dump(
{
"groups": {
"github": {
"default_time": " ",
"tasks": [],
},
},
}
),
encoding="utf-8",
)
with pytest.raises(ValueError, match="must have 'default_time'"):
load_config(path)
def test_load_config_default_time_invalid(tmp_path):
"""Group with invalid default_time format raises ValueError."""
path = tmp_path / "config.yaml"
path.write_text(
yaml.dump(
{
"groups": {
"github": {
"default_time": "25:00",
"tasks": [],
},
},
}
),
encoding="utf-8",
)
with pytest.raises(ValueError, match="Invalid time"):
load_config(path)
path.write_text(
yaml.dump(
{
"groups": {
"github": {
"default_time": "not-time",
"tasks": [],
},
},
}
),
encoding="utf-8",
)
with pytest.raises(ValueError, match="Invalid time"):
load_config(path)
def test_load_config_tasks_not_list(tmp_path):
"""Group with tasks not a list raises ValueError."""
path = tmp_path / "config.yaml"
path.write_text(
yaml.dump(
{
"groups": {
"github": {
"default_time": "04:10",
"tasks": "not a list",
},
},
}
),
encoding="utf-8",
)
with pytest.raises(ValueError, match="must have 'tasks' \\(list\\)"):
load_config(path)
def test_load_config_valid_minimal(tmp_path):
"""Valid minimal YAML loads and returns data dict."""
path = tmp_path / "config.yaml"
path.write_text(
yaml.dump(
{
"groups": {
"github": {
"default_time": "04:10",
"tasks": [
{"command": "run_foo", "schedule": "daily"},
],
},
},
}
),
encoding="utf-8",
)
data = load_config(path)
assert data is not None
assert "groups" in data
assert "github" in data["groups"]
assert data["groups"]["github"]["default_time"] == "04:10"
def test_load_config_invalid_task_fails(tmp_path):
"""Invalid task in YAML causes load_config to raise ValueError from _validate_task."""
path = tmp_path / "config.yaml"
path.write_text(
yaml.dump(
{
"groups": {
"github": {
"default_time": "04:10",
"tasks": [
{"command": "run_foo", "schedule": "daily"},
{"command": "run_bar"}, # missing schedule
],
},
},
}
),
encoding="utf-8",
)
with pytest.raises(ValueError, match="'schedule' must be one of"):
load_config(path)
# --- _validate_task validation ---
def test_validate_task_not_dict():
"""Task that is not a dict raises ValueError."""
with pytest.raises(ValueError, match="Task in group .* must be a dict"):
_validate_task([], "g1")
with pytest.raises(ValueError, match="Task in group .* must be a dict"):
_validate_task("task", "g1")
def test_validate_task_command_missing():
"""Task without command or with empty command raises ValueError."""
with pytest.raises(ValueError, match=r"must have 'command' \(non-empty string\)"):
_validate_task({"schedule": "daily"}, "g1")
with pytest.raises(ValueError, match=r"must have 'command' \(non-empty string\)"):
_validate_task({"command": "", "schedule": "daily"}, "g1")
with pytest.raises(ValueError, match=r"must have 'command' \(non-empty string\)"):
_validate_task({"command": 123, "schedule": "daily"}, "g1")
def test_validate_task_schedule_invalid():
"""Task with missing or invalid schedule raises ValueError."""
with pytest.raises(ValueError, match="'schedule' must be one of"):
_validate_task({"command": "c1", "schedule": "invalid"}, "g1")
with pytest.raises(ValueError, match="'schedule' must be one of"):
_validate_task({"command": "c1"}, "g1")
def test_validate_task_weekly_requires_on():
"""Task with schedule weekly but no valid on/day_of_week raises ValueError."""
with pytest.raises(ValueError, match="'schedule: weekly' requires 'on'"):
_validate_task({"command": "c1", "schedule": "weekly"}, "g1")
with pytest.raises(ValueError, match="'schedule: weekly' requires 'on'"):
_validate_task({"command": "c1", "schedule": "weekly", "on": "notaday"}, "g1")
def test_validate_task_weekly_valid():
"""Task with schedule weekly and valid on passes."""
_validate_task({"command": "c1", "schedule": "weekly", "on": "monday"}, "g1")
_validate_task({"command": "c1", "schedule": "weekly", "on": "mon"}, "g1")
def test_validate_task_monthly_requires_on():
"""Task with schedule monthly but no on raises ValueError."""
with pytest.raises(ValueError, match="'schedule: monthly' requires 'on'"):
_validate_task({"command": "c1", "schedule": "monthly"}, "g1")
def test_validate_task_monthly_on_non_numeric():
"""Task with schedule monthly and on not convertible to int raises ValueError."""
with pytest.raises(ValueError, match="'schedule: monthly' requires 'on' \\(1-31"):
_validate_task({"command": "c1", "schedule": "monthly", "on": "abc"}, "g1")
def test_validate_task_monthly_on_out_of_range():
"""Task with schedule monthly and on outside 1-31 raises ValueError."""
with pytest.raises(ValueError, match="'schedule: monthly' requires 'on' \\(1-31"):
_validate_task({"command": "c1", "schedule": "monthly", "on": 0}, "g1")
with pytest.raises(ValueError, match="'schedule: monthly' requires 'on' \\(1-31"):
_validate_task({"command": "c1", "schedule": "monthly", "on": 32}, "g1")
def test_validate_task_monthly_valid():
"""Task with schedule monthly and valid on (1-31) passes."""
_validate_task({"command": "c1", "schedule": "monthly", "on": 1}, "g1")
_validate_task({"command": "c1", "schedule": "monthly", "on": "15"}, "g1")
_validate_task({"command": "c1", "schedule": "monthly", "on": 31}, "g1")
def test_validate_task_interval_requires_minutes():
"""Task with schedule interval but no minutes raises ValueError."""
with pytest.raises(ValueError, match="'schedule: interval' requires 'minutes'"):
_validate_task({"command": "c1", "schedule": "interval"}, "g1")
def test_validate_task_interval_minutes_not_int():
"""Task with schedule interval and minutes not int raises ValueError."""
with pytest.raises(ValueError, match="'minutes' must be an integer"):
_validate_task({"command": "c1", "schedule": "interval", "minutes": "x"}, "g1")
def test_validate_task_interval_minutes_out_of_range():
"""Task with schedule interval and minutes outside 1-180 raises ValueError."""
with pytest.raises(ValueError, match="'minutes' must be 1-180"):
_validate_task({"command": "c1", "schedule": "interval", "minutes": 0}, "g1")
with pytest.raises(ValueError, match="'minutes' must be 1-180"):
_validate_task(
{
"command": "c1",
"schedule": "interval",
"minutes": INTERVAL_MINUTES_MAX + 1,
},
"g1",
)
def test_validate_task_interval_valid():
"""Task with schedule interval and valid minutes passes."""
_validate_task({"command": "c1", "schedule": "interval", "minutes": 1}, "g1")
_validate_task({"command": "c1", "schedule": "interval", "minutes": 60}, "g1")
_validate_task(
{
"command": "c1",
"schedule": "interval",
"minutes": INTERVAL_MINUTES_MAX,
},
"g1",
)
def test_validate_task_enabled_not_bool():
"""Task with enabled not boolean raises ValueError."""
with pytest.raises(ValueError, match="'enabled' must be boolean"):
_validate_task(
{"command": "c1", "schedule": "daily", "enabled": "yes"},
"g1",
)
def test_validate_task_args_not_list():
"""Task with args not a list raises ValueError."""
with pytest.raises(ValueError, match="'args' must be a list of strings"):
_validate_task(
{"command": "c1", "schedule": "daily", "args": "not-a-list"},
"g1",
)
def test_validate_task_args_element_not_string():
"""Task with args containing non-string element raises ValueError."""
with pytest.raises(ValueError, match=r"'args\[0\]' must be a string"):
_validate_task(
{"command": "c1", "schedule": "daily", "args": [123]},
"g1",
)
with pytest.raises(ValueError, match=r"'args\[1\]' must be a string"):
_validate_task(
{"command": "c1", "schedule": "daily", "args": ["--ok", None]},
"g1",
)
def test_validate_task_args_valid():
"""Task with args as list of strings passes."""
_validate_task(
{"command": "c1", "schedule": "daily", "args": ["--a", "b"]},
"g1",
)
def test_validate_task_daily_valid():
"""Minimal valid daily task passes."""
_validate_task({"command": "c1", "schedule": "daily"}, "g1")
def test_validate_task_on_release_valid():
"""Minimal valid on_release task passes."""
_validate_task({"command": "c1", "schedule": "on_release"}, "g1")
# --- get_tasks_for_schedule runtime ---
def test_get_tasks_for_schedule_monthly_exact_match_with_month_year(tmp_path):
"""get_tasks_for_schedule with month and year returns only tasks matching that day (exact or last-day fallback)."""
path = tmp_path / "schedule.yaml"
path.write_text(
yaml.dump(
{
"groups": {
"g1": {
"default_time": "04:10",
"tasks": [
{
"command": "cmd_mid",
"schedule": "monthly",
"on": 15,
},
{
"command": "cmd_last",
"schedule": "monthly",
"on": 31,
},
],
},
},
}
),
encoding="utf-8",
)
data = load_config(path)
# March 2024 has 31 days; day 15 is exact match
tasks_15 = get_tasks_for_schedule(
"monthly",
day_of_month=15,
month=3,
year=2024,
data=data,
)
assert len(tasks_15) == 1
assert tasks_15[0][1]["command"] == "cmd_mid"
# day 31 in March: both task 15 and 31 match (15 not, 31 yes)
tasks_31 = get_tasks_for_schedule(
"monthly",
day_of_month=31,
month=3,
year=2024,
data=data,
)
assert len(tasks_31) == 1
assert tasks_31[0][1]["command"] == "cmd_last"
# day 10: no monthly task on 10
tasks_10 = get_tasks_for_schedule(
"monthly",
day_of_month=10,
month=3,
year=2024,
data=data,
)
assert len(tasks_10) == 0
def test_get_tasks_for_schedule_monthly_last_day_fallback(tmp_path):
"""get_tasks_for_schedule with month/year returns task with day_of_month 31 on last day of February."""
path = tmp_path / "schedule.yaml"
path.write_text(
yaml.dump(
{
"groups": {
"g1": {
"default_time": "04:10",
"tasks": [
{
"command": "month_end",
"schedule": "monthly",
"on": 31,
},
],
},
},
}
),
encoding="utf-8",
)
data = load_config(path)
# Feb 2023 has 28 days; request day 28 with month/year -> effective_day = min(31, 28) = 28, task runs
_, last_day = calendar.monthrange(2023, 2)
assert last_day == 28
tasks = get_tasks_for_schedule(
"monthly",
day_of_month=28,
month=2,
year=2023,
data=data,
)
assert len(tasks) == 1
assert tasks[0][1]["command"] == "month_end"
assert tasks[0][1]["day_of_month"] == 31
# Feb 2024 has 29 days; request day 29 -> effective_day = min(31, 29) = 29
_, last_day_2024 = calendar.monthrange(2024, 2)
assert last_day_2024 == 29
tasks_leap = get_tasks_for_schedule(
"monthly",
day_of_month=29,
month=2,
year=2024,
data=data,
)
assert len(tasks_leap) == 1
assert tasks_leap[0][1]["command"] == "month_end"
@pytest.mark.django_db
def test_get_beat_schedule_missing_yaml_non_strict_returns_empty(
tmp_path, caplog, settings
):
"""With DEBUG True and no strict env, missing YAML yields empty beat schedule and a warning."""
settings.DEBUG = True
settings.BOOST_COLLECTOR_SCHEDULE_STRICT = False
missing = tmp_path / "missing.yaml"
settings.BOOST_COLLECTOR_SCHEDULE_YAML = str(missing)
caplog.set_level(logging.WARNING)
with patch(
"boost_collector_runner.schedule_config._get_yaml_path", return_value=missing
):
assert get_beat_schedule() == {}
assert any("not found" in r.getMessage().lower() for r in caplog.records)
@pytest.mark.django_db
def test_get_beat_schedule_missing_yaml_strict_raises(tmp_path, caplog, settings):
"""With DEBUG False, missing YAML raises ScheduleConfigurationError and logs ERROR."""
settings.DEBUG = False
settings.BOOST_COLLECTOR_SCHEDULE_STRICT = False
missing = tmp_path / "missing.yaml"
settings.BOOST_COLLECTOR_SCHEDULE_YAML = str(missing)
caplog.set_level(logging.ERROR)
with patch(
"boost_collector_runner.schedule_config._get_yaml_path", return_value=missing
):
with pytest.raises(ScheduleConfigurationError, match="not found"):
get_beat_schedule()
assert any(r.levelno >= logging.ERROR for r in caplog.records)
@pytest.mark.django_db
def test_get_beat_schedule_missing_yaml_strict_with_debug_true_via_env(
tmp_path, caplog, settings
):
"""BOOST_COLLECTOR_SCHEDULE_STRICT forces strict behavior when DEBUG is True."""
settings.DEBUG = True
settings.BOOST_COLLECTOR_SCHEDULE_STRICT = True
missing = tmp_path / "missing.yaml"
settings.BOOST_COLLECTOR_SCHEDULE_YAML = str(missing)
caplog.set_level(logging.ERROR)
with patch(
"boost_collector_runner.schedule_config._get_yaml_path", return_value=missing
):
with pytest.raises(ScheduleConfigurationError):
get_beat_schedule()
@pytest.mark.django_db
def test_get_beat_schedule_invalid_yaml_strict_raises(tmp_path, caplog, settings):
settings.DEBUG = False
bad = tmp_path / "bad.yaml"
bad.write_text(yaml.dump({"groups": []}), encoding="utf-8")
settings.BOOST_COLLECTOR_SCHEDULE_YAML = str(bad)
caplog.set_level(logging.ERROR)
with patch(
"boost_collector_runner.schedule_config._get_yaml_path", return_value=bad
):
with pytest.raises(ScheduleConfigurationError, match="Invalid schedule YAML"):
get_beat_schedule()
def test_get_beat_schedule_generates_expected_entries(tmp_path, settings):
"""get_beat_schedule returns Beat entries with expected task name, keys, and interval."""
yaml_path = tmp_path / "boost_collector_schedule.yaml"
yaml_path.write_text(
yaml.dump(
{
"groups": {
"github": {
"default_time": "04:10",
"tasks": [
{"command": "run_foo", "schedule": "daily"},
{
"command": "run_bar",
"schedule": "interval",
"minutes": 15,
},
],
},
},
}
),
encoding="utf-8",
)
settings.BOOST_COLLECTOR_SCHEDULE_YAML = str(yaml_path)
schedule = get_beat_schedule()
assert isinstance(schedule, dict)
group_key = "boost-collector-group-github-04-10"
assert group_key in schedule
assert (
schedule[group_key]["task"]
== "boost_collector_runner.tasks.run_scheduled_collectors_task"
)
assert "kwargs" in schedule[group_key]
assert (
schedule[group_key]["kwargs"]["schedule_kind"]
== DEFAULT_GROUP_BATCH_SCHEDULE_KIND
)
assert schedule[group_key]["kwargs"]["group_id"] == "github"
interval_key = "boost-collector-interval-15min"
assert interval_key in schedule
assert (
schedule[interval_key]["task"]
== "boost_collector_runner.tasks.run_scheduled_collectors_task"
)
assert schedule[interval_key]["kwargs"]["schedule_kind"] == "interval"
assert schedule[interval_key]["kwargs"]["interval_minutes"] == 15
assert "group_id" not in schedule[interval_key]["kwargs"]
from datetime import timedelta
assert schedule[interval_key]["schedule"].run_every == timedelta(minutes=15)
def test_get_beat_schedule_with_all_schedule_types(tmp_path, settings):
"""get_beat_schedule with daily, weekly, monthly, on_release, interval yields correct entries per group and one per interval_minutes (no group_id)."""
from datetime import timedelta
yaml_path = tmp_path / "boost_collector_schedule.yaml"
yaml_path.write_text(
yaml.dump(
{
"groups": {
"github": {
"default_time": "16:10",
"tasks": [
{"command": "run_daily", "schedule": "daily"},
{
"command": "run_weekly",
"schedule": "weekly",
"on": "monday",
},
{
"command": "run_monthly",
"schedule": "monthly",
"on": 1,
},
{"command": "run_on_release", "schedule": "on_release"},
{
"command": "run_interval_15",
"schedule": "interval",
"minutes": 15,
},
],
},
"slack": {
"default_time": "16:30",
"tasks": [
{"command": "run_slack_daily", "schedule": "daily"},
{
"command": "run_slack_interval",
"schedule": "interval",
"minutes": 60,
},
],
},
},
}
),
encoding="utf-8",
)
settings.BOOST_COLLECTOR_SCHEDULE_YAML = str(yaml_path)
schedule = get_beat_schedule()
assert isinstance(schedule, dict)
# Group batch entries (default schedule kind) — one per group with non-interval tasks
for group_id, time_str, key_suffix in [
("github", "16-10", "16-10"),
("slack", "16-30", "16-30"),
]:
group_key = f"boost-collector-group-{group_id}-{key_suffix}"
assert group_key in schedule, f"missing {group_key}"
assert (
schedule[group_key]["kwargs"]["schedule_kind"]
== DEFAULT_GROUP_BATCH_SCHEDULE_KIND
)
assert schedule[group_key]["kwargs"]["group_id"] == group_id
# Interval entries — one per interval_minutes (no group_id)
interval_15_key = "boost-collector-interval-15min"
interval_60_key = "boost-collector-interval-60min"
assert interval_15_key in schedule
assert interval_60_key in schedule
assert schedule[interval_15_key]["kwargs"]["schedule_kind"] == "interval"
assert schedule[interval_15_key]["kwargs"]["interval_minutes"] == 15
assert "group_id" not in schedule[interval_15_key]["kwargs"]
assert schedule[interval_60_key]["kwargs"]["schedule_kind"] == "interval"
assert schedule[interval_60_key]["kwargs"]["interval_minutes"] == 60
assert "group_id" not in schedule[interval_60_key]["kwargs"]
assert schedule[interval_15_key]["schedule"].run_every == timedelta(minutes=15)
assert schedule[interval_60_key]["schedule"].run_every == timedelta(minutes=60)
def test_get_tasks_for_schedule_interval_scoped_by_group_id(tmp_path):
"""get_tasks_for_schedule(interval, ..., group_id=X) returns only that group's interval tasks; group_id=None returns all."""
path = tmp_path / "schedule.yaml"
path.write_text(
yaml.dump(
{
"groups": {
"g1": {
"default_time": "04:10",
"tasks": [
{
"command": "interval_g1",
"schedule": "interval",
"minutes": 15,
},
],
},
"g2": {
"default_time": "04:20",
"tasks": [
{
"command": "interval_g2",
"schedule": "interval",
"minutes": 15,
},
],
},
},
}
),
encoding="utf-8",
)
data = load_config(path)
tasks_g1 = get_tasks_for_schedule(
"interval",
interval_minutes=15,
group_id="g1",
data=data,
)
assert len(tasks_g1) == 1
assert tasks_g1[0][0] == "g1"
assert tasks_g1[0][1]["command"] == "interval_g1"
tasks_g2 = get_tasks_for_schedule(
"interval",
interval_minutes=15,
group_id="g2",
data=data,
)
assert len(tasks_g2) == 1
assert tasks_g2[0][0] == "g2"
assert tasks_g2[0][1]["command"] == "interval_g2"
tasks_all = get_tasks_for_schedule(
"interval",
interval_minutes=15,
group_id=None,
data=data,
)
assert len(tasks_all) == 2
commands = {t[1]["command"] for t in tasks_all}
assert commands == {"interval_g1", "interval_g2"}
@pytest.mark.django_db
def test_committed_schedule_yaml_loads_non_empty_beat_schedule(settings):
"""Repo ships config/boost_collector_schedule.yaml; Beat must not be empty on clone."""
repo_yaml = Path(settings.BASE_DIR) / "config" / "boost_collector_schedule.yaml"
assert (
repo_yaml.is_file()
), "committed schedule missing; add config/boost_collector_schedule.yaml"
settings.BOOST_COLLECTOR_SCHEDULE_YAML = repo_yaml
data = load_config(repo_yaml)
registered = get_commands()
for _group_id, group_data in (data.get("groups") or {}).items():
if not isinstance(group_data, dict):
continue
task_list = group_data.get("tasks") or []
for task in task_list:
if not isinstance(task, dict) or task.get("enabled") is False:
continue
cmd = task.get("command")
assert cmd in registered, f"unknown management command in YAML: {cmd!r}"
schedule = get_beat_schedule()
assert schedule, "CELERY_BEAT_SCHEDULE must not be empty when committed YAML exists"