|
1 | | -from datetime import datetime, timedelta |
| 1 | +from datetime import timedelta |
2 | 2 | from typing import Union |
3 | 3 |
|
4 | 4 | import pytest |
5 | 5 |
|
6 | | -from taskiq.scheduler.scheduled_task import ScheduledTask |
| 6 | +from taskiq.scheduler.scheduled_task.validators import validate_interval_value |
7 | 7 |
|
8 | 8 |
|
9 | | -def test_valid_interval_tasks() -> None: |
10 | | - """Test that valid interval tasks are created successfully.""" |
11 | | - task1 = ScheduledTask( |
12 | | - task_name="test_task", |
13 | | - labels={}, |
14 | | - args=[], |
15 | | - kwargs={}, |
16 | | - interval=30, |
17 | | - ) |
18 | | - assert task1.interval == 30 |
19 | | - |
20 | | - task2 = ScheduledTask( |
21 | | - task_name="test_task", |
22 | | - labels={}, |
23 | | - args=[], |
24 | | - kwargs={}, |
25 | | - interval=timedelta(minutes=5), |
26 | | - ) |
27 | | - assert task2.interval == timedelta(minutes=5) |
28 | | - |
29 | | - task3 = ScheduledTask( |
30 | | - task_name="test_task", |
31 | | - labels={}, |
32 | | - args=[], |
33 | | - kwargs={}, |
34 | | - interval=1, |
35 | | - ) |
36 | | - assert task3.interval == 1 |
| 9 | +@pytest.mark.parametrize( |
| 10 | + "value", |
| 11 | + [ |
| 12 | + None, |
| 13 | + 1, |
| 14 | + 5, |
| 15 | + 3600, |
| 16 | + timedelta(seconds=1), |
| 17 | + timedelta(seconds=5), |
| 18 | + timedelta(hours=1, minutes=30), |
| 19 | + timedelta(seconds=1, microseconds=0), |
| 20 | + ], |
| 21 | +) |
| 22 | +def test_validate_interval_value_success(value: Union[int, timedelta, None]) -> None: |
| 23 | + validate_interval_value(value) |
37 | 24 |
|
38 | 25 |
|
39 | 26 | @pytest.mark.parametrize( |
40 | | - "interval", |
| 27 | + "value", |
41 | 28 | [ |
42 | 29 | 0, |
43 | | - -5, |
| 30 | + -1, |
44 | 31 | timedelta(seconds=0), |
45 | | - timedelta(seconds=0.5), |
46 | | - timedelta(microseconds=500000), |
47 | | - timedelta(seconds=-1), |
| 32 | + timedelta(milliseconds=999), |
| 33 | + timedelta(seconds=1.5), |
| 34 | + timedelta(seconds=0.999), |
| 35 | + timedelta(seconds=1, microseconds=1), |
48 | 36 | ], |
49 | 37 | ) |
50 | | -def test_invalid_interval_tasks(interval: Union[int, timedelta]) -> None: |
51 | | - """Test that invalid interval tasks raise ValueError.""" |
| 38 | +def test_validate_interval_value_fail(value: Union[int, timedelta, None]) -> None: |
52 | 39 | with pytest.raises(ValueError): |
53 | | - ScheduledTask( |
54 | | - task_name="test_task", |
55 | | - labels={}, |
56 | | - args=[], |
57 | | - kwargs={}, |
58 | | - interval=interval, |
59 | | - ) |
60 | | - |
61 | | - |
62 | | -def test_interval_validation_with_other_schedule_types() -> None: |
63 | | - """Test that interval validation works with other schedule types.""" |
64 | | - task1 = ScheduledTask( |
65 | | - task_name="test_task", |
66 | | - labels={}, |
67 | | - args=[], |
68 | | - kwargs={}, |
69 | | - cron="* * * * *", |
70 | | - interval=30, |
71 | | - ) |
72 | | - assert task1.interval == 30 |
73 | | - |
74 | | - task2 = ScheduledTask( |
75 | | - task_name="test_task", |
76 | | - labels={}, |
77 | | - args=[], |
78 | | - kwargs={}, |
79 | | - time=datetime.now(), |
80 | | - interval=timedelta(minutes=5), |
81 | | - ) |
82 | | - assert task2.interval == timedelta(minutes=5) |
83 | | - |
84 | | - with pytest.raises(ValueError, match="Interval must be at least 1 second"): |
85 | | - ScheduledTask( |
86 | | - task_name="test_task", |
87 | | - labels={}, |
88 | | - args=[], |
89 | | - kwargs={}, |
90 | | - cron="* * * * *", |
91 | | - interval=0, |
92 | | - ) |
| 40 | + validate_interval_value(value) |
0 commit comments