-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathtest_address.py
More file actions
183 lines (132 loc) · 6.52 KB
/
test_address.py
File metadata and controls
183 lines (132 loc) · 6.52 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import pytest
from easypost.constant import (
_FILTERS_KEY,
_TEST_FAILED_INTENTIONALLY_ERROR,
NO_MORE_PAGES_ERROR,
)
from easypost.errors import ApiError
from easypost.models import Address
@pytest.mark.vcr()
def test_address_create(ca_address_1, test_client):
address = test_client.address.create(**ca_address_1)
assert isinstance(address, Address)
assert str.startswith(address.id, "adr_")
assert address.street1 == "388 Townsend St"
@pytest.mark.vcr()
def test_address_create_verify(incorrect_address, test_client):
"""
Test creating an address with the `verify` param.
"""
# Creating normally (without specifying "verify") will make the address and perform no verifications
address = test_client.address.create(**incorrect_address)
assert isinstance(address, Address)
assert hasattr(address.verifications, "delivery") is False
# Creating with verify would make the address and perform verifications
incorrect_address["verify"] = True
address = test_client.address.create(**incorrect_address)
assert isinstance(address, Address)
# Delivery verification assertions
assert address.verifications.delivery.success is False
assert address.verifications.delivery.details.to_dict() == {}
assert address.verifications.delivery.errors[0].code == "E.ADDRESS.NOT_FOUND"
assert address.verifications.delivery.errors[0].field == "address"
assert address.verifications.delivery.errors[0].suggestion is None
assert address.verifications.delivery.errors[0].message == "Address not found"
# Zip4 verification assertions
assert address.verifications.zip4.success is False
assert address.verifications.zip4.details is None
assert address.verifications.zip4.errors[0].code == "E.ADDRESS.NOT_FOUND"
assert address.verifications.zip4.errors[0].field == "address"
assert address.verifications.zip4.errors[0].suggestion is None
assert address.verifications.zip4.errors[0].message == "Address not found"
@pytest.mark.vcr()
def test_address_create_verify_strict(ca_address_1, test_client):
"""Test creating an address with the `verify_strict` param.
We purposefully pass in slightly incorrect data to get the corrected address back once verified.
"""
address_data = ca_address_1
address_data["verify_strict"] = True
address = test_client.address.create(**address_data)
assert isinstance(address, Address)
assert str.startswith(address.id, "adr_")
assert address.street1 == "388 TOWNSEND ST APT 20"
@pytest.mark.vcr()
def test_address_create_verify_array(incorrect_address, test_client):
"""
Test creating an address with the `verify` param as an array.
"""
# Creating normally (without specifying "verify") will make the address, perform no verifications
address = test_client.address.create(**incorrect_address)
assert isinstance(address, Address)
assert hasattr(address.verifications, "delivery") is False
# Creating with verify would make the address and perform verifications
incorrect_address["verify"] = [True]
address = test_client.address.create(**incorrect_address)
assert isinstance(address, Address)
assert address.verifications.delivery.success is False
@pytest.mark.vcr()
def test_address_create_and_verify(ca_address_1, test_client):
address = test_client.address.create_and_verify(**ca_address_1)
assert isinstance(address, Address)
assert str.startswith(address.id, "adr_")
assert address.street1 == "388 TOWNSEND ST APT 20"
@pytest.mark.vcr()
def test_address_retrieve(ca_address_1, test_client):
address = test_client.address.create(**ca_address_1)
retrieved_address = test_client.address.retrieve(address.id)
assert isinstance(retrieved_address, Address)
assert retrieved_address == address
@pytest.mark.vcr()
def test_address_all(page_size, test_client):
addresses = test_client.address.all(page_size=page_size)
addresses_array = addresses["addresses"]
assert len(addresses_array) <= page_size
assert addresses["has_more"] is not None
assert all(isinstance(address, Address) for address in addresses_array)
@pytest.mark.vcr()
def test_address_get_next_page(page_size, test_client):
try:
first_page = test_client.address.all(page_size=page_size)
next_page = test_client.address.get_next_page(addresses=first_page, page_size=page_size)
first_id_of_first_page = first_page["addresses"][0].id
first_id_of_second_page = next_page["addresses"][0].id
assert first_id_of_first_page != first_id_of_second_page
# Verify that the filters are being passed along for behind-the-scenes reference
assert first_page[_FILTERS_KEY] == next_page[_FILTERS_KEY]
except Exception as e:
if e.message != NO_MORE_PAGES_ERROR:
raise Exception(_TEST_FAILED_INTENTIONALLY_ERROR)
@pytest.mark.vcr()
def test_address_verify(ca_address_1, test_client):
"""Test verifying an already created address."""
address = test_client.address.create(**ca_address_1)
test_client.address.verify(address.id)
assert isinstance(address, Address)
assert str.startswith(address.id, "adr_")
assert address.street1 == "388 Townsend St"
@pytest.mark.vcr()
def test_address_verify_invalid_address(test_client):
"""Test verifying an already created address."""
with pytest.raises(ApiError) as error:
address = test_client.address.create(
street1="invalid",
)
test_client.address.verify(address.id)
assert str(error.value) == "Unable to verify address."
@pytest.mark.vcr()
def test_address_create_verify_carrier(incorrect_address, test_client):
"""Test creating an address with the `verify_carrier` param."""
incorrect_address["verify"] = True
incorrect_address["verify_carrier"] = "UPS"
address = test_client.address.create(**incorrect_address)
assert isinstance(address, Address)
assert address.verifications.delivery.errors[0].message == "Address not found"
assert address.verifications.zip4.errors[0].message == "Address not found"
@pytest.mark.vcr()
def test_address_create_and_verify_carrier(incorrect_address, test_client):
"""Test creating and verifying an address with the `verify_carrier` param."""
incorrect_address["verify_carrier"] = "UPS"
address = test_client.address.create_and_verify(**incorrect_address)
assert isinstance(address, Address)
assert address.verifications.delivery.errors[0].message == "Address not found"
assert address.verifications.zip4.errors[0].message == "Address not found"