Skip to content
This repository was archived by the owner on Oct 14, 2024. It is now read-only.

Commit 07d118e

Browse files
committed
Define reusable constants in tests
1 parent c3fd641 commit 07d118e

File tree

2 files changed

+30
-59
lines changed

2 files changed

+30
-59
lines changed

tests/conftest.py

Lines changed: 27 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
from .helpers import MockTransport, MockErrorObject, MockResponseObject, OfficeLocation
1515

16+
@pytest.fixture
17+
def sample_headers():
18+
return {"Content-Type": "application/json"}
1619

1720
@pytest.fixture
1821
def auth_provider():
@@ -55,70 +58,36 @@ def mock_error_500_map():
5558
}
5659

5760
@pytest.fixture
58-
def mock_apierror_map():
61+
def mock_apierror_map(sample_headers):
5962
return {
60-
"400": APIError(
61-
"Resource not found",
62-
400,
63-
{
64-
'cache-control': 'private',
65-
'transfer-encoding': 'chunked',
66-
'content-type': 'application/json'
67-
},
68-
),
69-
"500": APIError(
70-
"Custom Internal Server Error",
71-
500,
72-
{
73-
'cache-control': 'private',
74-
'transfer-encoding': 'chunked',
75-
'content-type': 'application/json'
76-
},
77-
),
63+
"400": APIError("Resource not found", 400, sample_headers),
64+
"500": APIError("Custom Internal Server Error", 500, sample_headers)
7865
}
7966

8067
@pytest.fixture
81-
def mock_apierror_XXX_map():
82-
return {
83-
"XXX": APIError(
84-
"OdataError",
85-
400,
86-
{
87-
'cache-control': 'private',
88-
'transfer-encoding': 'chunked',
89-
'content-type': 'application/json'
90-
},
91-
),
92-
}
68+
def mock_apierror_XXX_map(sample_headers):
69+
return {"XXX": APIError("OdataError",400, sample_headers)}
9370

9471
@pytest.fixture
95-
def mock_request_adapter():
96-
resp = httpx.Response(
97-
json={'error': 'not found'}, status_code=404, headers={"Content-Type": "application/json"}
98-
)
72+
def mock_request_adapter(sample_headers):
73+
resp = httpx.Response(json={'error': 'not found'}, status_code=404, headers=sample_headers)
9974
mock_request_adapter = AsyncMock
10075
mock_request_adapter.get_http_response_message = AsyncMock(return_value=resp)
10176

102-
10377
@pytest.fixture
104-
def simple_error_response():
105-
return httpx.Response(
106-
json={'error': 'not found'}, status_code=404, headers={"Content-Type": "application/json"}
107-
)
108-
78+
def simple_error_response(sample_headers):
79+
return httpx.Response(json={'error': 'not found'}, status_code=404, headers=sample_headers)
10980

11081
@pytest.fixture
111-
def simple_success_response():
112-
return httpx.Response(
113-
json={'message': 'Success!'}, status_code=200, headers={"Content-Type": "application/json"}
114-
)
82+
def simple_success_response(sample_headers):
83+
return httpx.Response(json={'message': 'Success!'}, status_code=200, headers=sample_headers)
11584

11685

11786
@pytest.fixture
118-
def mock_user_response(mocker):
87+
def mock_user_response(mocker, sample_headers):
11988
return httpx.Response(
12089
200,
121-
headers={"Content-Type": "application/json"},
90+
headers=sample_headers,
12291
json={
12392
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
12493
"businessPhones": ["+1 205 555 0108"],
@@ -183,25 +152,25 @@ def mock_users_response(mocker):
183152

184153

185154
@pytest.fixture
186-
def mock_primitive_collection_response(mocker):
155+
def mock_primitive_collection_response(sample_headers):
187156
return httpx.Response(
188-
200, json=[12.1, 12.2, 12.3, 12.4, 12.5], headers={"Content-Type": "application/json"}
157+
200, json=[12.1, 12.2, 12.3, 12.4, 12.5], headers=sample_headers
189158
)
190159

191160

192161
@pytest.fixture
193-
def mock_primitive(mocker):
162+
def mock_primitive():
194163
resp = MockResponseObject()
195164
return resp
196165

197166

198167
@pytest.fixture
199-
def mock_primitive_response(mocker):
200-
return httpx.Response(200, json=22.3, headers={"Content-Type": "application/json"})
168+
def mock_primitive_response(sample_headers):
169+
return httpx.Response(200, json=22.3, headers=sample_headers)
201170

202171

203172
@pytest.fixture
204-
def mock_primitive_response_bytes(mocker):
173+
def mock_primitive_response_bytes():
205174
return httpx.Response(
206175
200,
207176
content=b'Hello World',
@@ -213,7 +182,7 @@ def mock_primitive_response_bytes(mocker):
213182

214183

215184
@pytest.fixture
216-
def mock_primitive_response_with_no_content(mocker):
185+
def mock_primitive_response_with_no_content():
217186
return httpx.Response(
218187
200,
219188
headers={
@@ -224,13 +193,13 @@ def mock_primitive_response_with_no_content(mocker):
224193

225194

226195
@pytest.fixture
227-
def mock_primitive_response_with_no_content_type_header(mocker):
196+
def mock_primitive_response_with_no_content_type_header():
228197
return httpx.Response(200, content=b'Hello World')
229198

230199

231200
@pytest.fixture
232-
def mock_no_content_response(mocker):
233-
return httpx.Response(204, json="Radom JSON", headers={"Content-Type": "application/json"})
201+
def mock_no_content_response(sample_headers):
202+
return httpx.Response(204, json="Radom JSON", headers=sample_headers)
234203

235204

236205
tracer = trace.get_tracer(__name__)
@@ -242,7 +211,7 @@ def mock_otel_span():
242211

243212

244213
@pytest.fixture
245-
def mock_cae_failure_response(mocker):
214+
def mock_cae_failure_response():
246215
auth_header = """Bearer authorization_uri="https://login.windows.net/common/oauth2/authorize",
247216
client_id="00000003-0000-0000-c000-000000000000",
248217
error="insufficient_claims",

tests/test_httpx_request_adapter.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
from .helpers import MockResponseObject
1919

20+
APPLICATION_JSON = "application/json"
2021
BASE_URL = "https://graph.microsoft.com"
2122

2223

24+
2325
def test_create_request_adapter(auth_provider):
2426
request_adapter = HttpxRequestAdapter(auth_provider)
2527
assert request_adapter._authentication_provider is auth_provider
@@ -53,7 +55,7 @@ def test_get_serialization_writer_factory(request_adapter):
5355

5456
def test_get_response_content_type(request_adapter, simple_success_response):
5557
content_type = request_adapter.get_response_content_type(simple_success_response)
56-
assert content_type == "application/json"
58+
assert content_type == APPLICATION_JSON
5759

5860

5961
def test_set_base_url_for_request_information(request_adapter, request_info):

0 commit comments

Comments
 (0)