-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathtest_error.py
More file actions
91 lines (68 loc) · 3.24 KB
/
test_error.py
File metadata and controls
91 lines (68 loc) · 3.24 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
import pytest
from easypost.errors.api.api_error import ApiError
@pytest.mark.vcr()
def test_error(test_client):
"""Tests that we assign properties of an error correctly."""
try:
# Create a purposefully empty shipment so we can work with the returned error
_ = test_client.shipment.create()
except ApiError as error:
assert error.http_status == 422
assert error.code == "PARAMETER.REQUIRED"
assert error.message == "Missing required parameter."
assert error.errors[0] == {"field": "shipment", "message": "cannot be blank"}
assert (
error.http_body
== '{"error": {"code": "PARAMETER.REQUIRED", "message": "Missing required parameter.", "errors": [{"field": "shipment", "message": "cannot be blank"}]}}' # noqa
)
@pytest.mark.vcr()
def test_error_alternative_format(test_client, basic_claim):
"""Tests that we assign properties of an error correctly when returned via the alternative format.
NOTE: Claims (among other things) uses the alternative errors format.
"""
try:
claim_data = basic_claim
claim_data["tracking_code"] = "123" # Intentionally pass a bad tracking code
test_client.claim.create(**claim_data)
except ApiError as error:
assert error.http_status == 404
assert error.code == "NOT_FOUND"
assert error.message == "The requested resource could not be found."
assert error.errors[0] == "No eligible insurance found with provided tracking code."
assert (
error.http_body
== '{"error": {"code": "NOT_FOUND", "errors": ["No eligible insurance found with provided tracking code."], "message": "The requested resource could not be found."}}' # noqa
)
def test_error_no_json():
"""Tests if we don't have valid JSON that we don't set the JSON body of an error."""
error = ApiError(message="", http_body="bad json")
assert error.json_body is None
def test_error_list_message():
"""Tests that we concatenate error messages that are a list (they should be a string from the
API but aren't always so we protect against that here).
"""
error = ApiError(message=["Error1", "Error2"])
assert error.message == "Error1, Error2"
def test_error_dict_message():
"""Tests that we concatenate error messages that are a dict.
Error messages from the API should be a string but aren't always, we protect against that here.
"""
message_data = {"errors": ["Bad format 1", "Bad format 2"]}
error = ApiError(message=message_data)
assert error.message == "Bad format 1, Bad format 2"
def test_error_bad_format_message():
"""Tests that we concatenate error messages that has an invalid format.
Error messages from the API should be a string but aren't always, we protect against that here.
"""
message_data = {
"errors": ["Bad format 1", "Bad format 2"],
"bad_data": [
{
"first_message": "Bad format 3",
"second_message": "Bad format 4",
"thrid_message": "Bad format 5",
}
],
}
error = ApiError(message=message_data)
assert error.message == "Bad format 1, Bad format 2, Bad format 3, Bad format 4, Bad format 5"