-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sdk.py
More file actions
321 lines (239 loc) · 8.92 KB
/
Copy pathtest_sdk.py
File metadata and controls
321 lines (239 loc) · 8.92 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
import datetime
import warnings
from http import HTTPStatus
from unittest import mock
import pytest
from taskbadger import Action, EmailIntegration, StatusEnum, WebhookIntegration, create_task
from taskbadger.exceptions import TaskbadgerException
from taskbadger.internal.models import (
PatchedTaskRequest,
TaskRequest,
)
from taskbadger.internal.types import UNSET, Response
from taskbadger.mug import Badger
from taskbadger.sdk import Task, init
from tests.utils import task_for_test
@pytest.fixture(autouse=True)
def _init_skd():
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
init("org", "project", "token")
@pytest.fixture()
def settings():
return Badger.current.settings
@pytest.fixture()
def patched_get():
with mock.patch("taskbadger.sdk.task_get.sync") as get:
yield get
@pytest.fixture()
def patched_create():
with mock.patch("taskbadger.sdk.task_create.sync_detailed") as create:
yield create
@pytest.fixture()
def patched_update():
with mock.patch("taskbadger.sdk.task_partial_update.sync_detailed") as update:
yield update
def test_get(patched_get):
data = {"a": 1}
api_task = task_for_test(data=data)
patched_get.return_value = api_task
fetched_task = Task.get("test_id")
assert fetched_task.id == api_task.id
assert fetched_task.data == data
def test_create(settings, patched_create):
api_task = task_for_test()
patched_create.return_value = Response(HTTPStatus.OK, b"", {}, api_task)
action = Action("success", integration=EmailIntegration(to="me@example.com"))
data = {"a": 1}
task = Task.create(
name="task name",
status=StatusEnum.PRE_PROCESSING,
value=13,
data=data,
max_runtime=10,
stale_timeout=2,
actions=[action],
)
assert task.id == api_task.id
request = TaskRequest(
name="task name",
status=StatusEnum.PRE_PROCESSING,
value=13,
value_max=UNSET,
data=data,
max_runtime=10,
stale_timeout=2,
)
request.additional_properties = {
"actions": [
{
"trigger": "success",
"integration": "email",
"config": {"to": "me@example.com"},
}
]
}
patched_create.assert_called_with(
client=mock.ANY,
organization_slug="org",
project_slug="project",
body=request,
)
def test_before_create_update_task(settings, patched_create):
def before_create(task):
tags = task.setdefault("tags", {})
tags["new"] = "tag"
return task
settings.before_create = before_create
api_task = task_for_test()
patched_create.return_value = Response(HTTPStatus.OK, b"", {}, api_task)
task = create_task(name="task name")
assert task.id == api_task.id
request = TaskRequest.from_dict(
{
"name": "task name",
"status": StatusEnum.PENDING,
"tags": {"new": "tag"},
}
)
assert patched_create.call_args[1]["body"] == request
def test_before_create_filter(settings, patched_create):
def before_create(_):
return None
settings.before_create = before_create
with pytest.raises(TaskbadgerException):
create_task(name="task name")
patched_create.assert_not_called()
def test_update_status(settings, patched_update):
api_task = task_for_test()
task = Task(api_task)
patched_update.return_value = Response(HTTPStatus.OK, b"", {}, api_task)
task.update_status(StatusEnum.PRE_PROCESSING)
# expected request
_verify_update(settings, patched_update, status=StatusEnum.PRE_PROCESSING)
def test_update_data(settings, patched_update):
api_task = task_for_test()
task = Task(api_task)
patched_update.return_value = Response(HTTPStatus.OK, b"", {}, api_task)
task.update(data={"a": 1})
# expected request
_verify_update(settings, patched_update, data={"a": 1})
def test_increment_value(settings, patched_update):
api_task = task_for_test()
task = Task(api_task)
patched_update.return_value = Response(HTTPStatus.OK, b"", {}, task_for_test(value=10))
task.increment_value(10)
_verify_update(settings, patched_update, value=10)
task.increment_value(5)
_verify_update(settings, patched_update, value=15)
def test_ping(settings, patched_update):
task = Task(task_for_test())
updated_at = task.updated
patched_update.return_value = Response(HTTPStatus.OK, b"", {}, task_for_test())
assert not task.ping(rate_limit=1)
assert len(patched_update.call_args_list) == 0
assert task.ping()
_verify_update(settings, patched_update)
assert task.updated > updated_at
assert not task.ping(rate_limit=1)
assert len(patched_update.call_args_list) == 1
task._task.updated = task._task.updated - datetime.timedelta(seconds=1)
assert task.ping(rate_limit=1)
assert len(patched_update.call_args_list) == 2
def test_update_value_rate_limit(settings, patched_update):
task = Task(task_for_test(value=1))
updated_at = task.updated
patched_update.return_value = Response(HTTPStatus.OK, b"", {}, task_for_test())
assert not task.update_value(2, rate_limit=1)
assert len(patched_update.call_args_list) == 0
assert task.update_value(2)
_verify_update(settings, patched_update, value=2)
assert task.updated > updated_at
assert not task.update_value(3, rate_limit=1)
assert len(patched_update.call_args_list) == 1
task._task.updated = task._task.updated - datetime.timedelta(seconds=1)
assert task.update_value(3, rate_limit=1)
assert len(patched_update.call_args_list) == 2
def test_update_value_value_step(settings, patched_update):
task = Task(task_for_test(value=1))
patched_update.return_value = Response(HTTPStatus.OK, b"", {}, task_for_test(value=4))
task.update_value(4, value_step=5)
assert len(patched_update.call_args_list) == 0
task.update_value(4)
_verify_update(settings, patched_update, value=4)
task.update_value(8, value_step=5)
assert len(patched_update.call_args_list) == 1
task.update_value(9, value_step=5)
assert len(patched_update.call_args_list) == 2
def test_update_value_min_interval_both(settings, patched_update):
task = Task(task_for_test(value=1))
patched_update.return_value = Response(HTTPStatus.OK, b"", {}, task_for_test(value=4))
# neither checks pass
task.update_value(4, rate_limit=1, value_step=5)
assert len(patched_update.call_args_list) == 0
# value check passes
task.update_value(6, rate_limit=1, value_step=5)
_verify_update(settings, patched_update, value=6)
# neither checks pass
task.update_value(8, rate_limit=1, value_step=5)
assert len(patched_update.call_args_list) == 1
# time check passes
task._task.updated = task._task.updated - datetime.timedelta(seconds=1)
task.update_value(6, rate_limit=1, value_step=5)
assert len(patched_update.call_args_list) == 2
def test_update_timeouts(settings, patched_update):
api_task = task_for_test()
task = Task(api_task)
patched_update.return_value = Response(HTTPStatus.OK, b"", {}, task_for_test(max_runtime=10, stale_timeout=2))
task.update(max_runtime=10, stale_timeout=2)
_verify_update(settings, patched_update, max_runtime=10, stale_timeout=2)
def test_add_actions(settings, patched_update):
api_task = task_for_test()
task = Task(api_task)
patched_update.return_value = Response(HTTPStatus.OK, b"", {}, api_task)
task.add_actions(
[
Action("*/10%,success,error", integration=EmailIntegration(to="me@example.com")),
Action("cancelled", integration=WebhookIntegration(id="webhook:123")),
]
)
# expected request
_verify_update(
settings,
patched_update,
actions=[
{
"trigger": "*/10%,success,error",
"integration": "email",
"config": {"to": "me@example.com"},
},
{"trigger": "cancelled", "integration": "webhook:123", "config": {}},
],
)
def test_action_validation():
WebhookIntegration(id="webhook:123")
with pytest.raises(TaskbadgerException):
WebhookIntegration(id="email:123")
def _verify_update(settings, patched_update, **kwargs):
actions = kwargs.pop("actions", None)
request_params = {
"name": UNSET,
"status": UNSET,
"value": UNSET,
"value_max": UNSET,
"data": UNSET,
}
request_params.update(kwargs)
if kwargs.get("data"):
request_params["data"] = kwargs["data"]
request = PatchedTaskRequest(**request_params)
if actions:
request.additional_properties = {"actions": actions}
# verify expected call
patched_update.assert_called_with(
client=mock.ANY,
organization_slug="org",
project_slug="project",
id=mock.ANY,
body=request,
)