Skip to content

Commit 331f282

Browse files
authored
Feature/label tests (#56)
* Added test for retry middleware. * Added test to schedules. Signed-off-by: Pavel Kirilin <[email protected]>
1 parent e98d825 commit 331f282

File tree

5 files changed

+66
-9
lines changed

5 files changed

+66
-9
lines changed

taskiq/kicker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ async def kiq( # noqa: D102
9292
self: "AsyncKicker[_FuncParams, Coroutine[Any, Any, _T]]",
9393
*args: _FuncParams.args,
9494
**kwargs: _FuncParams.kwargs,
95-
) -> AsyncTaskiqTask[_T]:
95+
) -> AsyncTaskiqTask[_T]: # pragma: no cover
9696
...
9797

9898
@overload
9999
async def kiq( # noqa: D102
100100
self: "AsyncKicker[_FuncParams, _ReturnType]",
101101
*args: _FuncParams.args,
102102
**kwargs: _FuncParams.kwargs,
103-
) -> AsyncTaskiqTask[_ReturnType]:
103+
) -> AsyncTaskiqTask[_ReturnType]: # pragma: no cover
104104
...
105105

106106
async def kiq( # noqa: C901

taskiq/scheduler/merge_functions.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ def only_unique(
3434
:param new_tasks: newly discovered tasks.
3535
:return: list of unique schedules.
3636
"""
37-
new_tasks = []
38-
new_tasks.extend(old_tasks)
37+
result = old_tasks
3938
for task in new_tasks:
40-
if task not in new_tasks:
41-
new_tasks.append(task)
42-
return new_tasks
39+
if task not in result:
40+
result.append(task)
41+
return result

taskiq/scheduler/scheduler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ def __init__(
3131
List["ScheduledTask"],
3232
] = preserve_all,
3333
refresh_delay: float = 30.0,
34-
) -> None:
34+
) -> None: # pragma: no cover
3535
self.broker = broker
3636
self.sources = sources
3737
self.refresh_delay = refresh_delay
3838
self.merge_func = merge_func
3939

40-
async def startup(self) -> None:
40+
async def startup(self) -> None: # pragma: no cover
4141
"""
4242
This method is called on startup.
4343
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import pytest
2+
3+
from taskiq.brokers.inmemory_broker import InMemoryBroker
4+
from taskiq.schedule_sources.label_based import LabelScheduleSource
5+
from taskiq.scheduler.scheduler import ScheduledTask
6+
7+
8+
@pytest.mark.anyio
9+
async def test_label_discovery() -> None:
10+
broker = InMemoryBroker()
11+
12+
@broker.task(
13+
task_name="test_task",
14+
schedule=[{"cron": "* * * * *"}],
15+
)
16+
def task() -> None:
17+
pass
18+
19+
source = LabelScheduleSource(broker)
20+
schedules = await source.get_schedules()
21+
assert schedules == [
22+
ScheduledTask(
23+
cron="* * * * *",
24+
task_name="test_task",
25+
labels={"schedule": [{"cron": "* * * * *"}]},
26+
args=[],
27+
kwargs={},
28+
),
29+
]
30+
31+
32+
@pytest.mark.anyio
33+
async def test_label_discovery_no_cron() -> None:
34+
broker = InMemoryBroker()
35+
36+
@broker.task(
37+
task_name="test_task",
38+
schedule=[{"args": ["* * * * *"]}],
39+
)
40+
def task() -> None:
41+
pass
42+
43+
source = LabelScheduleSource(broker)
44+
schedules = await source.get_schedules()
45+
assert schedules == []

tests/scheduler/test_merge_funcs.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from taskiq.scheduler.merge_functions import only_unique, preserve_all
2+
3+
4+
def test_preserve_all() -> None:
5+
first = [1, 2, 3]
6+
second = [3, 4, 5]
7+
assert preserve_all(first, second) == [1, 2, 3, 3, 4, 5] # type: ignore
8+
9+
10+
def test_only_unique() -> None:
11+
first = [1, 2, 3]
12+
second = [3, 4, 5]
13+
assert only_unique(first, second) == [1, 2, 3, 4, 5] # type: ignore

0 commit comments

Comments
 (0)