Skip to content

test: update responses stream usage #733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 26 additions & 45 deletions test/unit/test_cloudant_base_error_augment.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class MockResponse(TypedDict):
body: Union[dict[str,Any], str, bytes, None]
headers: dict[str, str]
status: int
stream: bool

class ExpectedResponse(TypedDict, total=False):
body: Union[dict[str,Any], str, bytes, None]
Expand Down Expand Up @@ -85,10 +84,10 @@ def _run_test(
mock_response: MockResponse = {
'body': _error_reason_body,
'headers': _default_mock_headers,
'status': 444,
'stream': False
'status': 444
},
expected_response: ExpectedResponse = None
expected_response: ExpectedResponse = None,
stream=False
) -> Union[Exception, None]:

if expected_response is None:
Expand Down Expand Up @@ -122,7 +121,7 @@ def _run_test(
)
if expect_raises:
with self.assertRaisesRegex(ApiException, expected_response['message']) as e_ctx:
self._make_request(method, mock_response['stream'])
self._make_request(method, stream)
caught_exception = e_ctx.exception
actual_status_code = caught_exception.status_code
actual_headers = caught_exception.http_response.headers
Expand All @@ -139,7 +138,7 @@ def _run_test(
actual_body = caught_exception.http_response.text
else:
try:
service_response = self._make_request(method, mock_response['stream'])
service_response = self._make_request(method, stream)
actual_status_code = service_response.get_status_code()
actual_headers = service_response.get_headers()
actual_body = service_response.get_result()
Expand Down Expand Up @@ -189,8 +188,7 @@ def test_augment_error(self):
mock_response={
'body': self._error_only_body,
'headers': self._content_type_header,
'status': 444,
'stream': False
'status': 444
},
expected_response={
'body': self._error_only_body | self._errors_error_only
Expand All @@ -202,8 +200,7 @@ def test_augment_error_with_trace(self):
mock_response={
'body': self._error_only_body,
'headers': self._default_mock_headers,
'status': 444,
'stream': False
'status': 444
},
expected_response={
'body': self._error_only_body | self._errors_error_only | self._trace
Expand All @@ -215,8 +212,7 @@ def test_augment_error_reason(self):
mock_response={
'body': self._error_reason_body,
'headers': self._content_type_header,
'status': 444,
'stream': False
'status': 444
},
expected_response={
'body': self._error_reason_body | self._errors_error_reason
Expand All @@ -228,8 +224,7 @@ def test_augment_error_reason_with_trace(self):
mock_response={
'body': self._error_reason_body,
'headers': self._default_mock_headers,
'status': 444,
'stream': False
'status': 444
},
expected_response={
'body': self._error_reason_body | self._errors_error_reason | self._trace
Expand All @@ -241,21 +236,20 @@ def test_augment_error_reason_stream(self):
mock_response={
'body': self._error_reason_body,
'headers': self._default_mock_headers,
'status': 444,
'stream': True
'status': 444
},
expected_response={
'body': self._error_reason_body | self._errors_error_reason | self._trace
}
},
stream = True
)

def test_augment_json_charset(self):
self._run_test(
mock_response={
'body': self._error_reason_body,
'headers': self._request_id_header | {'content-type': 'application/json; charset=utf-8'},
'status': 444,
'stream': False
'status': 444
},
expected_response={
'body': self._error_reason_body | self._errors_error_reason | self._trace
Expand All @@ -267,8 +261,7 @@ def test_augment_no_header(self):
mock_response={
'body': self._error_reason_body,
'headers': self._content_type_header,
'status': 444,
'stream': False
'status': 444
},
expected_response={
'body': self._error_reason_body | self._errors_error_reason
Expand All @@ -280,8 +273,7 @@ def test_no_augment_success(self):
mock_response={
'body': {'_id': self._doc_id, '_rev': '1-abc', 'foo': 'bar'},
'headers': self._default_mock_headers,
'status': 200,
'stream': False
'status': 200
}
)

Expand All @@ -291,8 +283,7 @@ def test_no_augment_head(self):
mock_response={
'body': None,
'headers': self._default_mock_headers,
'status': 444,
'stream': False
'status': 444
},
expected_response={
'body': b'', # no body content for HEAD
Expand All @@ -305,8 +296,7 @@ def test_no_augment_id_only(self):
mock_response={
'body': {},
'headers': self._default_mock_headers,
'status': 444,
'stream': False
'status': 444
},
expected_response={
'body': {},
Expand All @@ -324,8 +314,7 @@ def test_no_augment_existing_trace(self):
mock_response={
'body': test_body,
'headers': self._default_mock_headers,
'status': 429,
'stream': False
'status': 429
},
expected_response={
'body': test_body,
Expand All @@ -347,8 +336,7 @@ def test_no_augment_existing_errors_no_id(self):
mock_response={
'body': test_body,
'headers': self._content_type_header,
'status': 403,
'stream': False
'status': 403
},
expected_response={
'body': test_body,
Expand All @@ -369,8 +357,7 @@ def test_augment_trace_existing_errors(self):
mock_response={
'body': test_body,
'headers': self._default_mock_headers,
'status': 403,
'stream': False
'status': 403
},
expected_response={
'body': test_body | self._trace
Expand All @@ -383,8 +370,7 @@ def test_no_augment_non_json(self):
mock_response={
'body': test_body,
'headers': self._request_id_header | {'content-type': 'text/plain'},
'status': 400,
'stream': False
'status': 400
},
expected_response={
'body': test_body,
Expand All @@ -398,8 +384,7 @@ def test_no_augment_no_content_type(self):
mock_response={
'body': test_body,
'headers': self._request_id_header,
'status': 444,
'stream': False
'status': 444
},
expected_response={
'body': test_body,
Expand All @@ -413,8 +398,7 @@ def test_no_augment_no_error(self):
mock_response={
'body': test_body,
'headers': self._default_mock_headers,
'status': 400,
'stream': False
'status': 400
},
expected_response={
'body': test_body,
Expand All @@ -428,8 +412,7 @@ def test_no_augment_no_error_no_header(self):
mock_response={
'body': test_body,
'headers': self._content_type_header,
'status': 400,
'stream': False
'status': 400
},
expected_response={
'body': test_body,
Expand All @@ -443,8 +426,7 @@ def test_no_augment_invalid_json(self):
mock_response={
'body': test_body,
'headers': self._content_type_header,
'status': 400,
'stream': False
'status': 400
},
expected_response={
'body': test_body,
Expand All @@ -460,8 +442,7 @@ def test_augment_error_empty_reason_with_trace(self):
mock_response={
'body': test_body,
'headers': self._default_mock_headers,
'status': 444,
'stream': False
'status': 444
},
expected_response={
'body': test_body | self._errors_error_only | self._trace,
Expand Down