Skip to content

Commit 97c72f1

Browse files
committed
Make respx-based tests more idiomatic
1 parent d60fb30 commit 97c72f1

File tree

4 files changed

+12
-57
lines changed

4 files changed

+12
-57
lines changed

pyproject.toml

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ path = "src/trac_rpc/__about__.py"
4444
line-length = 120
4545
target-version = "py313"
4646

47-
4847
[tool.ruff.lint]
4948
select = [
5049
"C4", # flake8-comprehensions

tests/test_client.py

+6-18
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def test_event_hooks_default(monkeypatch: MonkeyPatch, respx_mock: respx.mock):
7777

7878

7979
def test_raise_for_status(api_client: ApiClient, respx_mock: respx.mock):
80-
respx_mock.post(url=TRAC_RPC_URL).mock(return_value=httpx.Response(status_code=httpx.codes.BAD_GATEWAY))
80+
respx_mock.post(url=TRAC_RPC_URL) % httpx.codes.BAD_GATEWAY
8181

8282
with pytest.raises(httpx.HTTPStatusError):
8383
api_client.get_api_version()
@@ -98,11 +98,9 @@ def test_event_hooks_override(monkeypatch: MonkeyPatch, respx_mock: respx.mock):
9898

9999

100100
def test_raise_on_error(api_client: ApiClient, respx_mock: respx.mock):
101-
respx_mock.post(TRAC_RPC_URL).mock(
102-
return_value=httpx.Response(
103-
status_code=httpx.codes.OK, # sic!
104-
text=get_fixture("trac-response-rpc-error.json"),
105-
)
101+
respx_mock.post(TRAC_RPC_URL).respond(
102+
status_code=httpx.codes.OK, # sic!
103+
text=get_fixture("trac-response-rpc-error.json"),
106104
)
107105

108106
with pytest.raises(TracRpcError) as excinfo:
@@ -116,24 +114,14 @@ def test_raise_on_error(api_client: ApiClient, respx_mock: respx.mock):
116114

117115

118116
def test_invalid_success_response(api_client: ApiClient, respx_mock: respx.mock):
119-
respx_mock.post().mock(
120-
return_value=httpx.Response(
121-
status_code=httpx.codes.OK,
122-
text="""{"error": null, "result": null, "id": null}""",
123-
)
124-
)
117+
respx_mock.post().respond(text="""{"error": null, "result": null, "id": null}""")
125118

126119
with pytest.raises(ValidationError, match=r".+ invalid success response .+"):
127120
api_client.get_api_version()
128121

129122

130123
def test_invalid_error_response(api_client: ApiClient, respx_mock: respx.mock):
131-
respx_mock.post().mock(
132-
return_value=httpx.Response(
133-
status_code=httpx.codes.OK,
134-
text="""{"error": {"message": "", "code": 123, "name": ""}, "result": null, "id": 123}""",
135-
)
136-
)
124+
respx_mock.post().respond(text="""{"error": {"message": "", "code": 123, "name": ""}, "result": null, "id": 123}""")
137125

138126
with pytest.raises(ValidationError, match=r".+ invalid error response .+"):
139127
api_client.get_api_version()

tests/test_methods.py

+5-31
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import json
22
from datetime import datetime
33

4-
import httpx
54
import pytest
65
import respx
76

@@ -22,12 +21,7 @@
2221
],
2322
)
2423
def test_query_tickets(kwargs: dict, expected: str, api_client: ApiClient, respx_mock: respx.mock):
25-
respx_mock.post().mock(
26-
return_value=httpx.Response(
27-
status_code=httpx.codes.OK,
28-
text="""{"error": null, "result": [3, 2, 1], "id": null}""",
29-
)
30-
)
24+
respx_mock.post().respond(text="""{"error": null, "result": [3, 2, 1], "id": null}""")
3125

3226
def get_last_request_params() -> str:
3327
(param,) = json.loads(respx_mock.calls.last.request.content)["params"]
@@ -38,12 +32,7 @@ def get_last_request_params() -> str:
3832

3933

4034
def test_get_ticket_attachments(api_client: ApiClient, respx_mock: respx.mock):
41-
respx_mock.post().mock(
42-
return_value=httpx.Response(
43-
status_code=httpx.codes.OK,
44-
text=get_fixture("trac-get-ticket-attachments-response.json"),
45-
)
46-
)
35+
respx_mock.post().respond(text=get_fixture("trac-get-ticket-attachments-response.json"))
4736

4837
(attachment,) = api_client.get_ticket_attachments(1)
4938
assert respx_mock.calls.last.request.content == b'{"id":null,"method":"ticket.listAttachments","params":[1]}'
@@ -58,12 +47,7 @@ def test_get_ticket_attachments(api_client: ApiClient, respx_mock: respx.mock):
5847

5948

6049
def test_get_ticket_changelog(api_client: ApiClient, respx_mock: respx.mock):
61-
respx_mock.post().mock(
62-
return_value=httpx.Response(
63-
status_code=httpx.codes.OK,
64-
text=get_fixture("trac-get-ticket-changelog-response.json"),
65-
)
66-
)
50+
respx_mock.post().respond(text=get_fixture("trac-get-ticket-changelog-response.json"))
6751

6852
changelog = api_client.get_ticket_changelog(1)
6953
assert respx_mock.calls.last.request.content == b'{"id":null,"method":"ticket.changeLog","params":[1]}'
@@ -113,12 +97,7 @@ def test_get_ticket_changelog(api_client: ApiClient, respx_mock: respx.mock):
11397

11498

11599
def test_get_ticket(api_client: ApiClient, respx_mock: respx.mock):
116-
respx_mock.post().mock(
117-
return_value=httpx.Response(
118-
status_code=httpx.codes.OK,
119-
text=get_fixture("trac-get-ticket-response.json"),
120-
)
121-
)
100+
respx_mock.post().respond(text=get_fixture("trac-get-ticket-response.json"))
122101

123102
assert api_client.get_ticket(1) == TracTicketProperties(
124103
id=1,
@@ -145,12 +124,7 @@ def test_get_ticket(api_client: ApiClient, respx_mock: respx.mock):
145124

146125

147126
def test_get_ticket_last_field_change(api_client: ApiClient, respx_mock: respx.mock):
148-
respx_mock.post().mock(
149-
return_value=httpx.Response(
150-
status_code=httpx.codes.OK,
151-
text=get_fixture("trac-get-ticket-changelog-response.json"),
152-
)
153-
)
127+
respx_mock.post().respond(text=get_fixture("trac-get-ticket-changelog-response.json"))
154128

155129
last_comment = api_client.get_ticket_last_field_change(1, "comment")
156130
assert last_comment == TracTicketChangelogEntry(

tests/test_models.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import httpx
21
import respx
32

43
from trac_rpc.client import ApiClient
@@ -9,12 +8,7 @@
98

109

1110
def test_custom_string_type(api_client: ApiClient, respx_mock: respx.mock):
12-
respx_mock.post().mock(
13-
return_value=httpx.Response(
14-
status_code=httpx.codes.OK,
15-
text=get_fixture("trac-get-milestone-response.json"),
16-
)
17-
)
11+
respx_mock.post().respond(text=get_fixture("trac-get-milestone-response.json"))
1812

1913
milestone = api_client.get_milestone(" milestone2 ", TracMilestone[TracStrippedStr])
2014
assert milestone.name == "milestone2"

0 commit comments

Comments
 (0)