-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_exceptions.py
More file actions
131 lines (102 loc) · 4 KB
/
test_exceptions.py
File metadata and controls
131 lines (102 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import json
from httpx import HTTPStatusError, Request, Response
from mpt_api_client.exceptions import (
MPTAPIError,
MPTHttpError,
transform_http_status_exception,
)
def test_http_error():
result = MPTHttpError(status_code=400, message="Bad request", body="Content")
assert result.status_code == 400
assert result.body == "Content"
assert str(result) == "HTTP 400: Bad request"
def test_http_error_not_found_from_mpt(): # noqa: WPS218
status_code = 400 # changed from 404 for testing purposes
api_status_code = 404
payload = {"message": "Resource not found", "statusCode": api_status_code}
message = (
"Client error '404 Resource Not Found' for url "
"'https://api.s1.show/public/public/v1/catalog/products?limit=100&offset=0'\n"
"For more information check: "
"https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404"
)
result = MPTAPIError(status_code=status_code, message=message, payload=payload)
assert result.status_code == status_code
assert result.payload == payload
assert result.status == api_status_code
assert result.title == "Resource not found"
assert result.detail == message
assert result.trace_id is None
assert result.errors is None
assert str(result) == f"404 Resource not found - {message} (no-trace-id)"
def test_api_error(): # noqa: WPS218
payload = {
"status": "400",
"title": "Bad Request",
"detail": "Invalid input",
"traceId": "abc123",
"errors": "Some error details",
}
result = MPTAPIError(status_code=400, message="Bad Request", payload=payload)
assert result.status_code == 400
assert result.payload == payload
assert result.status == "400"
assert result.title == "Bad Request"
assert result.detail == "Invalid input"
assert result.trace_id == "abc123"
assert result.errors == "Some error details"
def test_api_error_str_and_repr():
payload = {
"status": "400",
"title": "Bad Request",
"detail": "Invalid input",
"traceId": "abc123",
"errors": "Some error details",
}
result = MPTAPIError(status_code=400, message="Bad request", payload=payload)
assert str(result) == '400 Bad Request - Invalid input (abc123)\n"Some error details"'
assert repr(result) == (
"{'status': '400', 'title': 'Bad Request', 'detail': 'Invalid input', "
"'traceId': 'abc123', 'errors': 'Some error details'}"
)
def test_api_error_str_no_errors():
payload = {
"status": "400",
"title": "Bad Request",
"detail": "Invalid input",
"traceId": "abc123",
}
result = MPTAPIError(status_code=400, message="Bad request", payload=payload)
assert str(result) == "400 Bad Request - Invalid input (abc123)"
def test_transform_http_status_exception_api():
payload = {
"status": "400",
"title": "Bad Request",
"detail": "Invalid input",
"traceId": "abc123",
"errors": "Some error details",
}
response = Response(
status_code=400,
request=Request("GET", "http://test"),
content=json.dumps(payload).encode(),
headers={"content-type": "application/json"},
)
exc = HTTPStatusError("error", request=response.request, response=response)
result = transform_http_status_exception(exc)
assert isinstance(result, MPTAPIError)
assert result.status_code == 400
assert result.payload == payload
def test_transform_http_status_exception():
response = Response(
status_code=500,
request=Request("GET", "http://test"),
content=b"Internal Server Error",
headers={"content-type": "text/plain"},
)
exc = HTTPStatusError("Error message", request=response.request, response=response)
result = transform_http_status_exception(exc)
assert isinstance(result, MPTHttpError)
assert result.status_code == 500
assert result.body == "Internal Server Error"
assert str(result) == "HTTP 500: Error message"