Skip to content

Commit 5f7eaf1

Browse files
authored
Merge pull request #357 from EasyPost/test_alternative_error
test: alternative error format
2 parents 025260c + 0d315d2 commit 5f7eaf1

File tree

3 files changed

+117
-2
lines changed

3 files changed

+117
-2
lines changed

tests/cassettes/test_error_alternative_format.yaml

Lines changed: 73 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test_address.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_address_create_verify(incorrect_address, test_client):
2222
"""
2323
Test creating an address with the `verify` param.
2424
"""
25-
# Creating normally (without specifying "verify") will make the address, perform no verifications
25+
# Creating normally (without specifying "verify") will make the address and perform no verifications
2626
address = test_client.address.create(**incorrect_address)
2727

2828
assert isinstance(address, Address)
@@ -33,7 +33,23 @@ def test_address_create_verify(incorrect_address, test_client):
3333
address = test_client.address.create(**incorrect_address)
3434

3535
assert isinstance(address, Address)
36+
37+
# Delivery verification assertions
3638
assert address.verifications.delivery.success is False
39+
# TODO: details is not deserializing correctly, related to the larger "double EasyPostObject" wrapping issue
40+
# assert address.verifications.delivery.details == {}
41+
assert address.verifications.delivery.errors[0].code == "E.ADDRESS.NOT_FOUND"
42+
assert address.verifications.delivery.errors[0].field == "address"
43+
assert address.verifications.delivery.errors[0].suggestion is None
44+
assert address.verifications.delivery.errors[0].message == "Address not found"
45+
46+
# Zip4 verification assertions
47+
assert address.verifications.zip4.success is False
48+
assert address.verifications.zip4.details is None
49+
assert address.verifications.zip4.errors[0].code == "E.ADDRESS.NOT_FOUND"
50+
assert address.verifications.zip4.errors[0].field == "address"
51+
assert address.verifications.zip4.errors[0].suggestion is None
52+
assert address.verifications.zip4.errors[0].message == "Address not found"
3753

3854

3955
@pytest.mark.vcr()

tests/test_error.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,28 @@ def test_error(test_client):
1919
)
2020

2121

22+
@pytest.mark.vcr()
23+
def test_error_alternative_format(test_client, basic_claim):
24+
"""Tests that we assign properties of an error correctly when returned via the alternative format.
25+
26+
NOTE: Claims (among other things) uses the alternative errors format.
27+
"""
28+
try:
29+
claim_data = basic_claim
30+
claim_data["tracking_code"] = "123" # Intentionally pass a bad tracking code
31+
32+
test_client.claim.create(**claim_data)
33+
except ApiError as error:
34+
assert error.http_status == 404
35+
assert error.code == "NOT_FOUND"
36+
assert error.message == "The requested resource could not be found."
37+
assert error.errors[0] == "No eligible insurance found with provided tracking code."
38+
assert (
39+
error.http_body
40+
== '{"error": {"code": "NOT_FOUND", "errors": ["No eligible insurance found with provided tracking code."], "message": "The requested resource could not be found."}}' # noqa
41+
)
42+
43+
2244
def test_error_no_json():
2345
"""Tests if we don't have valid JSON that we don't set the JSON body of an error."""
2446
error = ApiError(message="", http_body="bad json")
@@ -55,7 +77,11 @@ def test_error_bad_format_message():
5577
message_data = {
5678
"errors": ["Bad format 1", "Bad format 2"],
5779
"bad_data": [
58-
{"first_message": "Bad format 3", "second_message": "Bad format 4", "thrid_message": "Bad format 5"}
80+
{
81+
"first_message": "Bad format 3",
82+
"second_message": "Bad format 4",
83+
"thrid_message": "Bad format 5",
84+
}
5985
],
6086
}
6187

0 commit comments

Comments
 (0)