-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclient_test.py
More file actions
512 lines (402 loc) · 15.8 KB
/
client_test.py
File metadata and controls
512 lines (402 loc) · 15.8 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
from dataclasses import dataclass
from datetime import datetime, timezone
from unittest.mock import MagicMock, patch
import json
import pytest
from riverqueue import Client, InsertOpts, JobState, UniqueOpts
from riverqueue.client import unique_bitmask_from_states
from riverqueue.driver import DriverProtocol, ExecutorProtocol
import sqlalchemy
@pytest.fixture
def mock_driver() -> DriverProtocol:
return MagicMock(spec=DriverProtocol)
@pytest.fixture
def mock_exec(mock_driver) -> ExecutorProtocol:
# Don't try to mock a context manager. It will cause endless pain around the
# edges like swallowing raised exceptions.
class TrivialContextManager:
def __init__(self, with_val):
self.with_val = with_val
def __enter__(self):
return self.with_val
def __exit__(self, exc_type, exc_val, exc_tb):
pass
mock_exec = MagicMock(spec=ExecutorProtocol)
mock_driver.executor.return_value = TrivialContextManager(mock_exec)
return mock_exec
@pytest.fixture
def client(mock_driver) -> Client:
return Client(mock_driver)
def test_insert_with_only_args(client, mock_exec, simple_args):
mock_exec.job_insert_many.return_value = [("job_row", False)]
insert_res = client.insert(simple_args)
mock_exec.job_insert_many.assert_called_once()
assert insert_res.job == "job_row"
def test_insert_tx(mock_driver, client, simple_args):
mock_exec = MagicMock(spec=ExecutorProtocol)
mock_exec.job_insert_many.return_value = [("job_row", False)]
mock_tx = MagicMock(spec=sqlalchemy.Transaction)
def mock_unwrap_executor(tx: sqlalchemy.Transaction):
assert tx == mock_tx
return mock_exec
mock_driver.unwrap_executor.side_effect = mock_unwrap_executor
insert_res = client.insert_tx(mock_tx, simple_args)
mock_exec.job_insert_many.assert_called_once()
assert insert_res.job == "job_row"
def test_insert_with_insert_opts_from_args(client, mock_exec, simple_args):
mock_exec.job_insert_many.return_value = [("job_row", False)]
insert_res = client.insert(
simple_args,
insert_opts=InsertOpts(
max_attempts=23, priority=2, queue="job_custom_queue", tags=["job_custom"]
),
)
mock_exec.job_insert_many.assert_called_once()
assert insert_res.job == "job_row"
call_args = mock_exec.job_insert_many.call_args[0][0]
assert len(call_args) == 1
insert_args = call_args[0]
assert insert_args.max_attempts == 23
assert insert_args.priority == 2
assert insert_args.queue == "job_custom_queue"
assert insert_args.tags == ["job_custom"]
def test_insert_with_insert_opts_from_job(client, mock_exec):
@dataclass
class MyArgs:
kind = "my_args"
@staticmethod
def insert_opts() -> InsertOpts:
return InsertOpts(
max_attempts=23,
priority=2,
queue="job_custom_queue",
tags=["job_custom"],
)
@staticmethod
def to_json() -> str:
return "{}"
mock_exec.job_insert_many.return_value = [("job_row", False)]
insert_res = client.insert(
MyArgs(),
)
mock_exec.job_insert_many.assert_called_once()
assert insert_res.job == "job_row"
call_args = mock_exec.job_insert_many.call_args[0][0]
assert len(call_args) == 1
insert_args = call_args[0]
assert insert_args.max_attempts == 23
assert insert_args.priority == 2
assert insert_args.queue == "job_custom_queue"
assert insert_args.tags == ["job_custom"]
def test_insert_with_insert_opts_precedence(client, mock_exec, simple_args):
@dataclass
class MyArgs:
kind = "my_args"
@staticmethod
def insert_opts() -> InsertOpts:
return InsertOpts(
max_attempts=23,
priority=2,
queue="job_custom_queue",
tags=["job_custom"],
)
@staticmethod
def to_json() -> str:
return "{}"
mock_exec.job_insert_many.return_value = [("job_row", False)]
insert_res = client.insert(
simple_args,
insert_opts=InsertOpts(
max_attempts=17, priority=3, queue="my_queue", tags=["custom"]
),
)
mock_exec.job_insert_many.assert_called_once()
assert insert_res.job == "job_row"
call_args = mock_exec.job_insert_many.call_args[0][0]
assert len(call_args) == 1
insert_args = call_args[0]
assert insert_args.max_attempts == 17
assert insert_args.priority == 3
assert insert_args.queue == "my_queue"
assert insert_args.tags == ["custom"]
def test_insert_with_unique_opts_by_args(client, mock_exec, simple_args):
insert_opts = InsertOpts(unique_opts=UniqueOpts(by_args=True))
mock_exec.job_insert_many.return_value = [("job_row", False)]
insert_res = client.insert(simple_args, insert_opts=insert_opts)
mock_exec.job_insert_many.assert_called_once()
assert insert_res.job == "job_row"
# Check that the UniqueOpts were correctly processed
call_args = mock_exec.job_insert_many.call_args[0][0]
assert len(call_args) == 1
insert_params = call_args[0]
assert insert_params.kind == "simple"
@patch("datetime.datetime")
def test_insert_with_unique_opts_by_period(
mock_datetime, client, mock_exec, simple_args
):
mock_datetime.now.return_value = datetime(2024, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
insert_opts = InsertOpts(unique_opts=UniqueOpts(by_period=900))
mock_exec.job_insert_many.return_value = [("job_row", False)]
insert_res = client.insert(simple_args, insert_opts=insert_opts)
mock_exec.job_insert_many.assert_called_once()
assert insert_res.job == "job_row"
# Check that the UniqueOpts were correctly processed
call_args = mock_exec.job_insert_many.call_args[0][0]
assert len(call_args) == 1
insert_params = call_args[0]
assert insert_params.kind == "simple"
def test_insert_with_unique_opts_by_queue(client, mock_exec, simple_args):
insert_opts = InsertOpts(unique_opts=UniqueOpts(by_queue=True))
mock_exec.job_insert_many.return_value = [("job_row", False)]
insert_res = client.insert(simple_args, insert_opts=insert_opts)
mock_exec.job_insert_many.assert_called_once()
assert insert_res.job == "job_row"
# Check that the UniqueOpts were correctly processed
call_args = mock_exec.job_insert_many.call_args[0][0]
assert len(call_args) == 1
insert_params = call_args[0]
assert insert_params.kind == "simple"
# default unique states should all be set except for cancelled and discarded:
assert insert_params.unique_states == bytes([0b11110101])
def test_insert_with_unique_opts_by_state(client, mock_exec, simple_args):
# Turn on all unique states:
insert_opts = InsertOpts(
unique_opts=UniqueOpts(
by_state=[
JobState.AVAILABLE,
JobState.CANCELLED,
JobState.COMPLETED,
JobState.DISCARDED,
JobState.PENDING,
JobState.RETRYABLE,
JobState.RUNNING,
JobState.SCHEDULED,
]
)
)
mock_exec.job_insert_many.return_value = [("job_row", False)]
insert_res = client.insert(simple_args, insert_opts=insert_opts)
mock_exec.job_insert_many.assert_called_once()
assert insert_res.job == "job_row"
# Check that the UniqueOpts were correctly processed
call_args = mock_exec.job_insert_many.call_args[0][0]
assert len(call_args) == 1
insert_params = call_args[0]
assert insert_params.kind == "simple"
assert insert_params.unique_states == bytes([0b11111111])
def test_insert_with_unique_opts_by_args_true(client, mock_exec, simple_args):
"""Test that by_args=True uses full args with sorted keys"""
mock_exec.job_insert_many.return_value = [("job_row", False)]
# Call with by_args=True
insert_opts = InsertOpts(unique_opts=UniqueOpts(by_args=True))
insert_res = client.insert(simple_args, insert_opts=insert_opts)
mock_exec.job_insert_many.assert_called_once()
assert insert_res.job == "job_row"
# Verify the by_args=True was properly handled
call_args = mock_exec.job_insert_many.call_args[0][0]
assert len(call_args) == 1
insert_params = call_args[0]
assert insert_params.unique_key is not None
def test_insert_with_unique_opts_by_args_sorting(
client: Client, mock_exec: MagicMock
) -> None:
"""Test that different key order in args produces the same unique key"""
mock_exec.job_insert_many.side_effect = [
[("job_row1", False)],
[("job_row2", False)],
]
@dataclass
class JsonArgs:
kind: str = "ordered"
json_str: str = ""
def to_json(self) -> str:
return self.json_str
# Insert with different key orders
insert_opts = InsertOpts(unique_opts=UniqueOpts(by_args=True))
# Same data with different key orders
ordered_json = '{"a": 1, "b": 2, "c": 3}'
reverse_ordered_json = '{"c": 3, "b": 2, "a": 1}'
client.insert(JsonArgs(json_str=ordered_json), insert_opts=insert_opts)
client.insert(JsonArgs(json_str=reverse_ordered_json), insert_opts=insert_opts)
# Get the unique keys that were generated
call_args1 = mock_exec.job_insert_many.call_args_list[0][0][0] # type: ignore[index]
call_args2 = mock_exec.job_insert_many.call_args_list[1][0][0] # type: ignore[index]
# The unique keys should be identical despite different order in original JSON
assert call_args1[0].unique_key == call_args2[0].unique_key
def test_insert_with_unique_opts_by_args_partial_keys(
client: Client, mock_exec: MagicMock
) -> None:
"""Test that by_args with keys extracts only specified keys, even from nested objects"""
mock_exec.job_insert_many.return_value = [("job_row", False)]
@dataclass
class JsonArgs:
kind: str = "partial"
json_str: str = ""
def to_json(self) -> str:
return self.json_str
args1 = json.dumps(
{
"a": "value",
"b": "foo",
"c": {
"d": "bar",
},
"e": "ignore_this",
}
)
# Same data as args1 except for omitted `e`, and reordered keys. It's a duplicate:
args2 = json.dumps(
{
"c": {
"d": "bar",
},
"b": "foo",
"a": "value",
}
)
# Missing `c`, so it's not a duplicate:
args3 = json.dumps(
{
"a": "value",
"b": "foo",
"d": "something else", # Omitted
}
)
args4 = json.dumps(
{
"b": "foo",
"a": "value",
"e": "bar", # Omitted
}
)
# Filter by a, b, and c:
insert_opts = InsertOpts(unique_opts=UniqueOpts(by_args=["a", "b", "c"]))
client.insert(JsonArgs(json_str=args1), insert_opts=insert_opts)
client.insert(JsonArgs(json_str=args2), insert_opts=insert_opts)
client.insert(JsonArgs(json_str=args3), insert_opts=insert_opts)
client.insert(JsonArgs(json_str=args4), insert_opts=insert_opts)
# Parse args to verify filtering
call_args_1 = mock_exec.job_insert_many.call_args_list[0][0][0] # type: ignore[index]
insert_params_1 = call_args_1[0]
call_args_2 = mock_exec.job_insert_many.call_args_list[1][0][0] # type: ignore[index]
insert_params_2 = call_args_2[0]
call_args_3 = mock_exec.job_insert_many.call_args_list[2][0][0] # type: ignore[index]
insert_params_3 = call_args_3[0]
call_args_4 = mock_exec.job_insert_many.call_args_list[3][0][0] # type: ignore[index]
insert_params_4 = call_args_4[0]
# Check that the keys were filtered correctly
assert insert_params_1.unique_key == insert_params_2.unique_key
# args3 is missing `c`, so it's not a duplicate:
assert insert_params_1.unique_key != insert_params_3.unique_key
# args3 and args4 are both the same when only looking at the filtered keys:
assert insert_params_3.unique_key == insert_params_4.unique_key
def test_insert_kind_error(client):
@dataclass
class MyArgs:
pass
with pytest.raises(AttributeError) as ex:
client.insert(MyArgs())
assert "'MyArgs' object has no attribute 'kind'" == str(ex.value)
def test_insert_to_json_attribute_error(client):
@dataclass
class MyArgs:
kind = "my"
with pytest.raises(AttributeError) as ex:
client.insert(MyArgs())
assert "'MyArgs' object has no attribute 'to_json'" == str(ex.value)
def test_insert_to_json_none_error(client):
@dataclass
class MyArgs:
kind = "my"
@staticmethod
def to_json() -> None:
return None
with pytest.raises(AssertionError) as ex:
client.insert(MyArgs())
assert "args should return non-nil from `to_json`" == str(ex.value)
def test_tag_validation(client, mock_exec, simple_args):
mock_exec.job_insert_many.return_value = [("job_row", False)]
client.insert(
simple_args, insert_opts=InsertOpts(tags=["foo", "bar", "baz", "foo-bar-baz"])
)
with pytest.raises(AssertionError) as ex:
client.insert(simple_args, insert_opts=InsertOpts(tags=["commas,bad"]))
assert (
r"tags should be less than 255 characters in length and match regex \A[\w][\w\-]+[\w]\Z"
== str(ex.value)
)
with pytest.raises(AssertionError) as ex:
client.insert(simple_args, insert_opts=InsertOpts(tags=["a" * 256]))
assert (
r"tags should be less than 255 characters in length and match regex \A[\w][\w\-]+[\w]\Z"
== str(ex.value)
)
@pytest.mark.parametrize(
"description, input_states, postgres_bitstring",
[
# Postgres bitstrings are little-endian, so the MSB (AVAILABLE) is on the right.
("No states selected", [], bytes([0b00000000])),
("Single state - available", [JobState.AVAILABLE], bytes([0b00000001])),
("Single state - SCHEDULED", [JobState.SCHEDULED], bytes([0b10000000])),
("Single state - RUNNING", [JobState.RUNNING], bytes([0b01000000])),
(
"AVAILABLE and SCHEDULED",
[JobState.AVAILABLE, JobState.SCHEDULED],
bytes([0b10000001]),
),
(
"COMPLETED, PENDING, RETRYABLE",
[JobState.COMPLETED, JobState.PENDING, JobState.RETRYABLE],
bytes([0b00110100]),
),
(
"Default states",
[
JobState.AVAILABLE,
JobState.COMPLETED,
JobState.PENDING,
JobState.RETRYABLE,
JobState.RUNNING,
JobState.SCHEDULED,
],
bytes([0b11110101]),
),
(
"All states selected",
[
JobState.AVAILABLE,
JobState.CANCELLED,
JobState.COMPLETED,
JobState.DISCARDED,
JobState.PENDING,
JobState.RETRYABLE,
JobState.RUNNING,
JobState.SCHEDULED,
],
bytes([0b11111111]),
),
(
"AVAILABLE, COMPLETED, RETRYABLE, SCHEDULED",
[
JobState.AVAILABLE,
JobState.COMPLETED,
JobState.RETRYABLE,
JobState.SCHEDULED,
],
bytes([0b10100101]),
),
(
"Overlapping states",
[JobState.AVAILABLE, JobState.AVAILABLE],
bytes([0b00000001]),
),
("None input treated as empty", None, bytes([0b00000000])),
],
)
def test_unique_bitmask_from_states(description, input_states, postgres_bitstring):
if input_states is None:
input_states = []
result = unique_bitmask_from_states(input_states)
assert result == postgres_bitstring, (
f"{description} For states {input_states}, expected {postgres_bitstring}, got {result}"
)