-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_addresses.py
211 lines (177 loc) · 7.17 KB
/
test_addresses.py
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import pytest
import uuid
from datetime import timedelta
from service.models import AddressSegment, Person
from service.server import db
@pytest.fixture
def seed_person():
person = Person(
first_name="John",
last_name="Doe",
email="[email protected]",
date_of_birth="1978-06-12",
)
db.session.add(person)
db.session.commit()
db.session.refresh(person)
yield person
@pytest.fixture
def seed_address_segment(seed_person):
address_segment = AddressSegment(
street_one="123 Main Street",
city="San Francisco",
state="CA",
zip_code="94613",
start_date="2021-01-01",
person_id=seed_person.id,
)
db.session.add(address_segment)
db.session.commit()
db.session.refresh(address_segment)
yield address_segment
def test_create_address_with_validation_error(test_context, client, seed_person):
with test_context:
response = client.put(f"/api/persons/{seed_person.id}/address", json={})
assert response.status_code == 422
assert response.json == {
"errors": {
"json": {
"city": ["Missing data for required field."],
"start_date": ["Missing data for required field."],
"state": ["Missing data for required field."],
"street_one": ["Missing data for required field."],
"zip_code": ["Missing data for required field."],
}
}
}
def test_create_single_valid_address(test_context, client, seed_person):
with test_context:
response = client.put(
f"/api/persons/{seed_person.id}/address",
json={
"start_date": "2021-01-01",
"street_one": "123 Main Street",
"city": "San Francisco",
"state": "CA",
"zip_code": "94613",
},
)
assert response.status_code == 200
def test_get_address(test_context, client, seed_address_segment):
with test_context:
response = client.get(f"/api/persons/{seed_address_segment.person_id}/address")
assert response.status_code == 200
def test_get_address_no_person(test_context, client):
with test_context:
response = client.get(f"/api/persons/{uuid.uuid4()}/address")
assert response.status_code == 404
assert response.json == {"error": "person does not exist"}
def test_get_address_no_address_exists(test_context, client, seed_person):
with test_context:
response = client.get(f"/api/persons/{seed_person.id}/address")
assert response.status_code == 404
assert response.json == {
"error": "person does not have an address, please create one"
}
def test_create_initial_address_segment(test_context, client, seed_person):
with test_context:
response = client.put(
f"/api/persons/{seed_person.id}/address",
json={
"street_one": "123 Main Street",
"city": "San Francisco",
"state": "CA",
"zip_code": "94613",
"start_date": "2021-06-15",
},
)
assert response.status_code == 200
assert response.json == {
"street_one": "123 Main Street",
"street_two": None,
"city": "San Francisco",
"state": "CA",
"zip_code": "94613",
"start_date": "2021-06-15",
"end_date": None,
}
def test_implementation_create_and_update_address(
test_context, client, seed_address_segment
):
with test_context:
response = client.put(
f"/api/persons/{seed_address_segment.person_id}/address",
json={
"street_one": "1 California Street",
"city": "San Francisco",
"state": "CA",
"zip_code": "94111",
"start_date": "2021-06-15",
},
)
assert response.status_code == 200
assert response.json == {
"street_one": "1 California Street",
"street_two": None,
"city": "San Francisco",
"state": "CA",
"zip_code": "94111",
"start_date": "2021-06-15",
"end_date": None,
}
# TODO: Extension One
# def test_extension_one_validation_of_same_start_date(test_context, client, seed_address_segment):
# with test_context:
# # Update the existing address using the person_id and start_date from the
# # already existing, seed_address_segment
# response = client.put(
# f"/api/persons/{seed_address_segment.person_id}/address", json={
# "street_one": "123 Main Street",
# "city": "San Francisco",
# "state": "CA",
# "zip_code": "94613",
# "start_date": seed_address_segment.start_date.isoformat()
# }
# )
# assert response.status_code == 422
# assert response.json == {
# "error": "Address segment already exists with start_date 2021-01-01"
# }
# TODO: Extension Two
# def test_extension_two_get_address_by_date(test_context, client, seed_address_segment):
# with test_context:
# start_date = seed_address_segment.start_date
# new_start_date = start_date + timedelta(days=90)
# response = client.put(
# f"/api/persons/{seed_address_segment.person_id}/address", json={
# "street_one": "1 California Street",
# "city": "San Francisco",
# "state": "CA",
# "zip_code": "94111",
# "start_date": new_start_date.isoformat()
# }
# )
# assert response.status_code == 200
# first_get_response = client.get(f"/api/persons/{seed_address_segment.person_id}/address", query_string={"date": start_date.isoformat()})
# assert first_get_response.status_code == 200
# assert first_get_response.json["street_one"] == seed_address_segment.street_one
# second_get_response = client.get(f"/api/persons/{seed_address_segment.person_id}/address?date={new_start_date.isoformat()}")
# assert second_get_response.status_code == 200
# assert second_get_response.json["street_one"] == "1 California Street"
# TODO: Extension Three
# def test_extension_three_merge_contiguous_equal_addresses(test_context, client, seed_address_segment):
# with test_context:
# start_date = seed_address_segment.start_date
# new_start_date = start_date + timedelta(days=90)
# response = client.put(
# f"/api/persons/{seed_address_segment.person_id}/address", json={
# "street_one": seed_address_segment.street_one,
# "city": seed_address_segment.city,
# "state": seed_address_segment.state,
# "zip_code": seed_address_segment.zip_code,
# "start_date": new_start_date.isoformat()
# }
# )
# assert response.status_code == 200
# person = Person.query.get(seed_address_segment.person_id)
# assert len(person.address_segments) == 1