Skip to content

Commit 1173936

Browse files
committed
Make respx-based tests more idiomatic
1 parent 747244c commit 1173936

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

tests/test_client.py

+10-18
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@
1414

1515
from .utils import get_fixture
1616

17-
RESPONSE_STATUS_MULTIPLE_ISSUES = httpx.Response(
18-
httpx.codes.OK,
19-
text=get_fixture("response-multiple-check-status-of-multiple-issues.json"),
20-
)
21-
2217
GITHUB_TOKEN = "ghp_abc123"
2318

19+
RESPONSE_STATUS_MULTIPLE_ISSUES = get_fixture("response-multiple-check-status-of-multiple-issues.json")
20+
2421

2522
@pytest.fixture
2623
def api_client():
@@ -32,14 +29,14 @@ def test_simple_client_init(api_client: ApiClient):
3229

3330

3431
def test_base_url_override(respx_mock: respx.mock):
35-
respx_mock.get(url="https://test/repos/foo/bar/import/issues").mock(return_value=RESPONSE_STATUS_MULTIPLE_ISSUES)
32+
respx_mock.get(url="https://test/repos/foo/bar/import/issues").respond(text=RESPONSE_STATUS_MULTIPLE_ISSUES)
3633

3734
api_client = ApiClient(http_client=HttpClient(token=GITHUB_TOKEN, base_url="https://test"))
3835
api_client.get_status_multiple("foo", "bar", datetime.now(tz=UTC))
3936

4037

4138
def test_headers_override(respx_mock: respx.mock):
42-
respx_mock.get(headers={"Foo": "Bar"}).mock(return_value=RESPONSE_STATUS_MULTIPLE_ISSUES)
39+
respx_mock.get(headers={"Foo": "Bar"}).respond(text=RESPONSE_STATUS_MULTIPLE_ISSUES)
4340

4441
api_client = ApiClient(http_client=HttpClient(token=GITHUB_TOKEN, headers={"Foo": "Bar"}))
4542
api_client.get_status_multiple("foo", "bar", datetime.now(tz=UTC))
@@ -48,7 +45,7 @@ def test_headers_override(respx_mock: respx.mock):
4845

4946

5047
def test_event_hooks_override(monkeypatch: MonkeyPatch, respx_mock: respx.mock):
51-
respx_mock.get().mock(return_value=RESPONSE_STATUS_MULTIPLE_ISSUES)
48+
respx_mock.get().respond(text=RESPONSE_STATUS_MULTIPLE_ISSUES)
5249

5350
mock_log_request, mock_log_response = MagicMock(), MagicMock()
5451
monkeypatch.setattr(HttpClient, "log_github_api_request", mock_log_request)
@@ -62,9 +59,7 @@ def test_event_hooks_override(monkeypatch: MonkeyPatch, respx_mock: respx.mock):
6259

6360

6461
def test_event_hooks_default(monkeypatch: MonkeyPatch, respx_mock: respx.mock):
65-
respx_mock.post("https://api.github.com/repos/foo/bar/import/issues").mock(
66-
return_value=httpx.Response(httpx.codes.BAD_GATEWAY)
67-
)
62+
respx_mock.post("https://api.github.com/repos/foo/bar/import/issues") % httpx.codes.BAD_GATEWAY
6863

6964
mock_log_request, mock_log_response = MagicMock(), MagicMock()
7065
monkeypatch.setattr(HttpClient, "log_github_api_request", mock_log_request)
@@ -92,7 +87,7 @@ def test_import_issue(api_client: ApiClient, respx_mock: respx.mock):
9287
method=HTTPMethod.POST,
9388
url="https://api.github.com/repos/owner/repository/import/issues",
9489
headers={"Authorization": f"Token {GITHUB_TOKEN}"} | HttpClient.HEADERS,
95-
).mock(return_value=httpx.Response(httpx.codes.ACCEPTED, text=import_response))
90+
).respond(status_code=httpx.codes.ACCEPTED, text=import_response)
9691

9792
response = api_client.import_issue(
9893
"owner",
@@ -107,24 +102,21 @@ def test_import_issue(api_client: ApiClient, respx_mock: respx.mock):
107102
def test_get_import_status(api_client: ApiClient, respx_mock: respx.mock):
108103
import_status_response = get_fixture("response-single-check-status-of-issue-import.json")
109104

110-
respx_mock.get("https://api.github.com/repos/jonmagic/foo/import/issues/3").mock(
111-
return_value=httpx.Response(httpx.codes.OK, text=import_status_response)
112-
)
105+
respx_mock.get("https://api.github.com/repos/jonmagic/foo/import/issues/3").respond(text=import_status_response)
113106

114107
response = api_client.get_status(HttpUrl("https://api.github.com/repos/jonmagic/foo/import/issues/3"))
115108
assert response == IssueImportStatusResponse.model_validate_json(import_status_response)
116109

117110

118111
def test_get_import_status_multiple(api_client: ApiClient, respx_mock: respx.mock):
119-
multiple_status_response = get_fixture("response-multiple-check-status-of-multiple-issues.json")
120112
since = datetime.now(tz=UTC)
121113

122114
respx_mock.get(
123115
httpx.URL(
124116
"https://api.github.com/repos/foo/bar/import/issues",
125117
params={"since": since.isoformat()},
126118
)
127-
).mock(return_value=RESPONSE_STATUS_MULTIPLE_ISSUES)
119+
).respond(text=RESPONSE_STATUS_MULTIPLE_ISSUES)
128120

129121
response = api_client.get_status_multiple("foo", "bar", since)
130-
assert response == [IssueImportStatusResponse.model_validate(json.loads(multiple_status_response)[0])]
122+
assert response == [IssueImportStatusResponse.model_validate(json.loads(RESPONSE_STATUS_MULTIPLE_ISSUES)[0])]

0 commit comments

Comments
 (0)