Skip to content

Commit 51c2851

Browse files
authored
Make tests more terse + fix typing on UniqueOpts (#13)
I'm getting to a place soon where I'm going to have to add a new set of tests for asynchronous operations, so in preparation for that, here we go through and make the tests more terse in a few ways: * Remove time mocks for tests they're not needed (most of them). * Remove local variables that aren't needed (instead just use the value directly). i.e. `args` can just be `JobArgs()` values, `UniqueOpts` can just go right onto `InsertOpts`. * Replace the name `result` with the more specific `insert_res` (suggesting it's a result for an insert rather than an arbitrary result). Also found a bad value for `by_args` in one test and realized that it wasn't being detected because the fields on `UniqueOpts` didn't have type annotations on them. Add type annotations to make this impossible in the future.
1 parent a761187 commit 51c2851

File tree

3 files changed

+62
-115
lines changed

3 files changed

+62
-115
lines changed

src/riverqueue/client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from dataclasses import dataclass
22
from datetime import datetime, timezone, timedelta
3-
from typing import Any, Optional, Protocol, Tuple, List, Callable
3+
from typing import Any, Literal, Optional, Protocol, Tuple, List, Callable
44

55
from .driver import GetParams, JobInsertParams, DriverProtocol, ExecutorProtocol
66
from .model import InsertResult
@@ -37,10 +37,10 @@ class InsertOpts:
3737

3838
@dataclass
3939
class UniqueOpts:
40-
by_args: Optional[Any] = None
41-
by_period: Optional[Any] = None
42-
by_queue: Optional[Any] = None
43-
by_state: Optional[Any] = None
40+
by_args: Optional[Literal[True]] = None
41+
by_period: Optional[int] = None
42+
by_queue: Optional[Literal[True]] = None
43+
by_state: Optional[list[str]] = None
4444

4545

4646
class Client:

tests/client_test.py

Lines changed: 25 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,17 @@ def client(mock_driver) -> Client:
3737
return Client(mock_driver)
3838

3939

40-
@patch("datetime.datetime")
41-
def test_insert_with_only_args(mock_datetime, client, mock_exec):
42-
mock_datetime.now.return_value = datetime(2024, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
43-
40+
def test_insert_with_only_args(client, mock_exec):
4441
mock_exec.job_get_by_kind_and_unique_properties.return_value = None
4542
mock_exec.job_insert.return_value = "job_row"
4643

47-
result = client.insert(SimpleArgs())
44+
insert_res = client.insert(SimpleArgs())
4845

4946
mock_exec.job_insert.assert_called_once()
50-
assert result.job == "job_row"
47+
assert insert_res.job == "job_row"
5148

5249

53-
@patch("datetime.datetime")
54-
def test_insert_tx(mock_datetime, mock_driver, client):
55-
mock_datetime.now.return_value = datetime(2024, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
56-
50+
def test_insert_tx(mock_driver, client):
5751
mock_exec = MagicMock(spec=ExecutorProtocol)
5852
mock_exec.job_get_by_kind_and_unique_properties.return_value = None
5953
mock_exec.job_insert.return_value = "job_row"
@@ -66,47 +60,38 @@ def mock_unwrap_executor(tx: sqlalchemy.Transaction):
6660

6761
mock_driver.unwrap_executor.side_effect = mock_unwrap_executor
6862

69-
result = client.insert_tx(mock_tx, SimpleArgs())
63+
insert_res = client.insert_tx(mock_tx, SimpleArgs())
7064

7165
mock_exec.job_insert.assert_called_once()
72-
assert result.job == "job_row"
73-
66+
assert insert_res.job == "job_row"
7467

75-
@patch("datetime.datetime")
76-
def test_insert_with_opts(mock_datetime, client, mock_exec):
77-
mock_datetime.now.return_value = datetime(2024, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
7868

79-
args = SimpleArgs()
80-
insert_opts = InsertOpts(queue="high_priority", unique_opts=None)
69+
def test_insert_with_opts(client, mock_exec):
70+
insert_opts = InsertOpts(queue="high_priority")
8171

8272
mock_exec.job_get_by_kind_and_unique_properties.return_value = None
8373
mock_exec.job_insert.return_value = "job_row"
8474

85-
result = client.insert(args, insert_opts=insert_opts)
75+
insert_res = client.insert(SimpleArgs(), insert_opts=insert_opts)
8676

8777
mock_exec.job_insert.assert_called_once()
88-
assert result.job == "job_row"
78+
assert insert_res.job == "job_row"
8979

9080
# Check that the InsertOpts were correctly passed to make_insert_params
9181
call_args = mock_exec.job_insert.call_args[0][0]
9282
assert call_args.queue == "high_priority"
9383

9484

95-
@patch("datetime.datetime")
96-
def test_insert_with_unique_opts_by_args(mock_datetime, client, mock_exec):
97-
mock_datetime.now.return_value = datetime(2024, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
98-
99-
args = SimpleArgs()
100-
unique_opts = UniqueOpts(by_args=True)
101-
insert_opts = InsertOpts(unique_opts=unique_opts)
85+
def test_insert_with_unique_opts_by_args(client, mock_exec):
86+
insert_opts = InsertOpts(unique_opts=UniqueOpts(by_args=True))
10287

10388
mock_exec.job_get_by_kind_and_unique_properties.return_value = None
10489
mock_exec.job_insert.return_value = "job_row"
10590

106-
result = client.insert(args, insert_opts=insert_opts)
91+
insert_res = client.insert(SimpleArgs(), insert_opts=insert_opts)
10792

10893
mock_exec.job_insert.assert_called_once()
109-
assert result.job == "job_row"
94+
assert insert_res.job == "job_row"
11095

11196
# Check that the UniqueOpts were correctly processed
11297
call_args = mock_exec.job_insert.call_args[0][0]
@@ -117,59 +102,47 @@ def test_insert_with_unique_opts_by_args(mock_datetime, client, mock_exec):
117102
def test_insert_with_unique_opts_by_period(mock_datetime, client, mock_exec):
118103
mock_datetime.now.return_value = datetime(2024, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
119104

120-
args = SimpleArgs()
121-
unique_opts = UniqueOpts(by_period=900)
122-
insert_opts = InsertOpts(unique_opts=unique_opts)
105+
insert_opts = InsertOpts(unique_opts=UniqueOpts(by_period=900))
123106

124107
mock_exec.job_get_by_kind_and_unique_properties.return_value = None
125108
mock_exec.job_insert.return_value = "job_row"
126109

127-
result = client.insert(args, insert_opts=insert_opts)
110+
insert_res = client.insert(SimpleArgs(), insert_opts=insert_opts)
128111

129112
mock_exec.job_insert.assert_called_once()
130-
assert result.job == "job_row"
113+
assert insert_res.job == "job_row"
131114

132115
# Check that the UniqueOpts were correctly processed
133116
call_args = mock_exec.job_insert.call_args[0][0]
134117
assert call_args.kind == "simple"
135118

136119

137-
@patch("datetime.datetime")
138-
def test_insert_with_unique_opts_by_queue(mock_datetime, client, mock_exec):
139-
mock_datetime.now.return_value = datetime(2024, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
140-
141-
args = SimpleArgs()
142-
unique_opts = UniqueOpts(by_queue=True)
143-
insert_opts = InsertOpts(unique_opts=unique_opts)
120+
def test_insert_with_unique_opts_by_queue(client, mock_exec):
121+
insert_opts = InsertOpts(unique_opts=UniqueOpts(by_queue=True))
144122

145123
mock_exec.job_get_by_kind_and_unique_properties.return_value = None
146124
mock_exec.job_insert.return_value = "job_row"
147125

148-
result = client.insert(args, insert_opts=insert_opts)
126+
insert_res = client.insert(SimpleArgs(), insert_opts=insert_opts)
149127

150128
mock_exec.job_insert.assert_called_once()
151-
assert result.job == "job_row"
129+
assert insert_res.job == "job_row"
152130

153131
# Check that the UniqueOpts were correctly processed
154132
call_args = mock_exec.job_insert.call_args[0][0]
155133
assert call_args.kind == "simple"
156134

157135

158-
@patch("datetime.datetime")
159-
def test_insert_with_unique_opts_by_state(mock_datetime, client, mock_exec):
160-
mock_datetime.now.return_value = datetime(2024, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
161-
162-
args = SimpleArgs()
163-
unique_opts = UniqueOpts(by_state=["available", "running"])
164-
insert_opts = InsertOpts(unique_opts=unique_opts)
136+
def test_insert_with_unique_opts_by_state(client, mock_exec):
137+
insert_opts = InsertOpts(unique_opts=UniqueOpts(by_state=["available", "running"]))
165138

166139
mock_exec.job_get_by_kind_and_unique_properties.return_value = None
167140
mock_exec.job_insert.return_value = "job_row"
168141

169-
result = client.insert(args, insert_opts=insert_opts)
142+
insert_res = client.insert(SimpleArgs(), insert_opts=insert_opts)
170143

171144
mock_exec.job_insert.assert_called_once()
172-
assert result.job == "job_row"
145+
assert insert_res.job == "job_row"
173146

174147
# Check that the UniqueOpts were correctly processed
175148
call_args = mock_exec.job_insert.call_args[0][0]

tests/driver/riversqlalchemy/sqlalchemy_driver_test.py

Lines changed: 32 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,21 @@ def client(driver: riversqlalchemy.Driver) -> Client:
2525
return Client(driver)
2626

2727

28-
@patch("datetime.datetime")
29-
def test_insert_with_only_args(mock_datetime, client, driver):
30-
mock_datetime.now.return_value = datetime(2024, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
31-
args = SimpleArgs()
32-
result = client.insert(args)
33-
assert result.job
28+
def test_insert_with_only_args(client, driver):
29+
insert_res = client.insert(SimpleArgs())
30+
assert insert_res.job
3431

3532

36-
@patch("datetime.datetime")
37-
def test_insert_tx(mock_datetime, client, driver, engine):
38-
mock_datetime.now.return_value = datetime(2024, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
39-
33+
def test_insert_tx(client, driver, engine):
4034
with engine.begin() as tx:
4135
args = SimpleArgs()
42-
result = client.insert_tx(tx, args)
43-
assert result.job
36+
insert_res = client.insert_tx(tx, args)
37+
assert insert_res.job
4438

4539
job = driver.unwrap_executor(tx).job_get_by_kind_and_unique_properties(
4640
GetParams(kind=args.kind)
4741
)
48-
assert job == result.job
42+
assert job == insert_res.job
4943

5044
with engine.begin() as tx2:
5145
job = driver.unwrap_executor(tx2).job_get_by_kind_and_unique_properties(
@@ -56,61 +50,41 @@ def test_insert_tx(mock_datetime, client, driver, engine):
5650
tx.rollback()
5751

5852

59-
@patch("datetime.datetime")
60-
def test_insert_with_opts(mock_datetime, client, driver):
61-
mock_datetime.now.return_value = datetime(2024, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
62-
args = SimpleArgs()
53+
def test_insert_with_opts(client, driver):
6354
insert_opts = InsertOpts(queue="high_priority", unique_opts=None)
64-
result = client.insert(args, insert_opts=insert_opts)
65-
assert result.job
66-
55+
insert_res = client.insert(SimpleArgs(), insert_opts=insert_opts)
56+
assert insert_res.job
6757

68-
@patch("datetime.datetime")
69-
def test_insert_with_unique_opts_by_args(mock_datetime, client, driver):
70-
mock_datetime.now.return_value = datetime(2024, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
7158

72-
args = SimpleArgs()
73-
unique_opts = UniqueOpts(by_args=args)
74-
insert_opts = InsertOpts(unique_opts=unique_opts)
75-
result = client.insert(args, insert_opts=insert_opts)
76-
assert result.job
77-
result2 = client.insert(args, insert_opts=insert_opts)
78-
assert result.job == result2.job
59+
def test_insert_with_unique_opts_by_args(client, driver):
60+
insert_opts = InsertOpts(unique_opts=UniqueOpts(by_args=True))
61+
insert_res = client.insert(SimpleArgs(), insert_opts=insert_opts)
62+
assert insert_res.job
63+
insert_res2 = client.insert(SimpleArgs(), insert_opts=insert_opts)
64+
assert insert_res.job == insert_res2.job
7965

8066

8167
@patch("datetime.datetime")
8268
def test_insert_with_unique_opts_by_period(mock_datetime, client, driver):
8369
mock_datetime.now.return_value = datetime(2024, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
8470

85-
args = SimpleArgs()
86-
unique_opts = UniqueOpts(by_period=900)
87-
insert_opts = InsertOpts(unique_opts=unique_opts)
88-
result = client.insert(args, insert_opts=insert_opts)
89-
result2 = client.insert(args, insert_opts=insert_opts)
90-
assert result.job == result2.job
91-
92-
93-
@patch("datetime.datetime")
94-
def test_insert_with_unique_opts_by_queue(mock_datetime, client, driver):
95-
mock_datetime.now.return_value = datetime(2024, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
71+
insert_opts = InsertOpts(unique_opts=UniqueOpts(by_period=900))
72+
insert_res = client.insert(SimpleArgs(), insert_opts=insert_opts)
73+
insert_res2 = client.insert(SimpleArgs(), insert_opts=insert_opts)
74+
assert insert_res.job == insert_res2.job
9675

97-
args = SimpleArgs()
98-
unique_opts = UniqueOpts(by_queue=True)
99-
insert_opts = InsertOpts(unique_opts=unique_opts)
100-
result = client.insert(args, insert_opts=insert_opts)
101-
assert result.job
102-
result2 = client.insert(args, insert_opts=insert_opts)
103-
assert result.job == result2.job
10476

77+
def test_insert_with_unique_opts_by_queue(client, driver):
78+
insert_opts = InsertOpts(unique_opts=UniqueOpts(by_queue=True))
79+
insert_res = client.insert(SimpleArgs(), insert_opts=insert_opts)
80+
assert insert_res.job
81+
insert_res2 = client.insert(SimpleArgs(), insert_opts=insert_opts)
82+
assert insert_res.job == insert_res2.job
10583

106-
@patch("datetime.datetime")
107-
def test_insert_with_unique_opts_by_state(mock_datetime, client, driver):
108-
mock_datetime.now.return_value = datetime(2024, 6, 1, 12, 0, 0, tzinfo=timezone.utc)
10984

110-
args = SimpleArgs()
111-
unique_opts = UniqueOpts(by_state=["available", "running"])
112-
insert_opts = InsertOpts(unique_opts=unique_opts)
113-
result = client.insert(args, insert_opts=insert_opts)
114-
assert result.job
115-
result2 = client.insert(args, insert_opts=insert_opts)
116-
assert result.job == result2.job
85+
def test_insert_with_unique_opts_by_state(client, driver):
86+
insert_opts = InsertOpts(unique_opts=UniqueOpts(by_state=["available", "running"]))
87+
insert_res = client.insert(SimpleArgs(), insert_opts=insert_opts)
88+
assert insert_res.job
89+
insert_res2 = client.insert(SimpleArgs(), insert_opts=insert_opts)
90+
assert insert_res.job == insert_res2.job

0 commit comments

Comments
 (0)