-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_clear.py
More file actions
253 lines (211 loc) · 6.62 KB
/
test_clear.py
File metadata and controls
253 lines (211 loc) · 6.62 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
import asyncio
from datetime import datetime, timedelta, timezone
from unittest.mock import AsyncMock
import pytest
from typer.testing import CliRunner
from docket.cli import app
from docket.docket import Docket
@pytest.fixture(autouse=True)
async def empty_docket(docket: Docket):
"""Ensure that the docket starts empty"""
await docket.clear()
async def test_clear_command_empty_docket(docket: Docket, runner: CliRunner):
"""Should clear empty docket and report 0 tasks cleared"""
result = await asyncio.get_running_loop().run_in_executor(
None,
runner.invoke,
app,
[
"clear",
"--url",
docket.url,
"--docket",
docket.name,
],
)
assert result.exit_code == 0, result.output
assert "Cleared 0 tasks" in result.output
async def test_clear_command_with_immediate_tasks(
docket: Docket, runner: CliRunner, the_task: AsyncMock
):
"""Should clear immediate tasks and report count"""
docket.register(the_task)
await docket.add(the_task)("arg1")
await docket.add(the_task)("arg2")
await docket.add(the_task)("arg3")
result = await asyncio.get_running_loop().run_in_executor(
None,
runner.invoke,
app,
[
"clear",
"--url",
docket.url,
"--docket",
docket.name,
],
)
assert result.exit_code == 0, result.output
assert "Cleared 3 tasks" in result.output
snapshot = await docket.snapshot()
assert len(snapshot.future) == 0
assert len(snapshot.running) == 0
async def test_clear_command_with_scheduled_tasks(
docket: Docket, runner: CliRunner, the_task: AsyncMock
):
"""Should clear scheduled tasks and report count"""
docket.register(the_task)
future = datetime.now(timezone.utc) + timedelta(seconds=60)
await docket.add(the_task, when=future)("scheduled1")
await docket.add(the_task, when=future + timedelta(seconds=1))("scheduled2")
result = await asyncio.get_running_loop().run_in_executor(
None,
runner.invoke,
app,
[
"clear",
"--url",
docket.url,
"--docket",
docket.name,
],
)
assert result.exit_code == 0, result.output
assert "Cleared 2 tasks" in result.output
snapshot = await docket.snapshot()
assert len(snapshot.future) == 0
assert len(snapshot.running) == 0
async def test_clear_command_with_mixed_tasks(
docket: Docket, runner: CliRunner, the_task: AsyncMock, another_task: AsyncMock
):
"""Should clear both immediate and scheduled tasks"""
docket.register(the_task)
docket.register(another_task)
future = datetime.now(timezone.utc) + timedelta(seconds=60)
await docket.add(the_task)("immediate1")
await docket.add(another_task)("immediate2")
await docket.add(the_task, when=future)("scheduled1")
await docket.add(another_task, when=future + timedelta(seconds=1))("scheduled2")
result = await asyncio.get_running_loop().run_in_executor(
None,
runner.invoke,
app,
[
"clear",
"--url",
docket.url,
"--docket",
docket.name,
],
)
assert result.exit_code == 0, result.output
assert "Cleared 4 tasks" in result.output
snapshot = await docket.snapshot()
assert len(snapshot.future) == 0
assert len(snapshot.running) == 0
async def test_clear_command_with_keyed_tasks(
docket: Docket, runner: CliRunner, the_task: AsyncMock
):
"""Should clear tasks with keys"""
docket.register(the_task)
await docket.add(the_task, key="task1")("arg1")
await docket.add(the_task, key="task2")("arg2")
result = await asyncio.get_running_loop().run_in_executor(
None,
runner.invoke,
app,
[
"clear",
"--url",
docket.url,
"--docket",
docket.name,
],
)
assert result.exit_code == 0, result.output
assert "Cleared 2 tasks" in result.output
snapshot = await docket.snapshot()
assert len(snapshot.future) == 0
async def test_clear_command_basic_functionality(
docket: Docket, runner: CliRunner, the_task: AsyncMock
):
"""Should clear tasks via CLI command"""
docket.register(the_task)
# Add some tasks to clear
await docket.add(the_task)("task1")
future = datetime.now(timezone.utc) + timedelta(seconds=60)
await docket.add(the_task, when=future)("scheduled_task")
result = await asyncio.get_running_loop().run_in_executor(
None,
runner.invoke,
app,
[
"clear",
"--url",
docket.url,
"--docket",
docket.name,
],
)
assert result.exit_code == 0, result.output
assert "Cleared" in result.output
snapshot_after_clear = await docket.snapshot()
assert len(snapshot_after_clear.future) == 0
async def test_clear_command_preserves_strikes(
docket: Docket, runner: CliRunner, the_task: AsyncMock
):
"""Should not affect strikes when clearing"""
docket.register(the_task)
await docket.strike("the_task")
await docket.add(the_task)("arg1")
result = await asyncio.get_running_loop().run_in_executor(
None,
runner.invoke,
app,
[
"clear",
"--url",
docket.url,
"--docket",
docket.name,
],
)
assert result.exit_code == 0, result.output
assert "Cleared" in result.output
# Strikes should still be in effect - clear doesn't affect strikes
async def test_clear_command_with_custom_url(runner: CliRunner):
"""Should handle custom Redis URL"""
result = await asyncio.get_running_loop().run_in_executor(
None,
runner.invoke,
app,
[
"clear",
"--url",
"redis://nonexistent:12345/0",
"--docket",
"test-docket",
],
)
assert result.exit_code != 0
assert result.exit_code != 0
async def test_clear_command_with_custom_docket_name(
docket: Docket, runner: CliRunner, the_task: AsyncMock
):
"""Should handle custom docket name"""
docket.register(the_task)
await docket.add(the_task)("test")
result = await asyncio.get_running_loop().run_in_executor(
None,
runner.invoke,
app,
[
"clear",
"--url",
docket.url,
"--docket",
docket.name,
],
)
assert result.exit_code == 0, result.output
assert "Cleared 1 tasks" in result.output