-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathtest_us.py
More file actions
167 lines (102 loc) · 3.91 KB
/
test_us.py
File metadata and controls
167 lines (102 loc) · 3.91 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
from itertools import chain
import jellyfish # type: ignore
import pytest # type: ignore
import pytz
import us
# attribute
def test_attribute():
for state in us.STATES_AND_TERRITORIES:
assert state == getattr(us.states, state.abbr)
def test_valid_timezones():
for state in us.STATES_AND_TERRITORIES:
if state.capital:
assert pytz.timezone(state.capital_tz)
for tz in state.time_zones:
assert pytz.timezone(tz)
# During migration from SQLite to Python classes, a duplicate
# time zone had been found
assert len(state.time_zones) == len(set(state.time_zones))
# maryland lookup
def test_fips():
assert us.states.lookup("24") == us.states.MD
assert us.states.lookup("51") != us.states.MD
def test_abbr():
assert us.states.lookup("MD") == us.states.MD
assert us.states.lookup("md") == us.states.MD
assert us.states.lookup("VA") != us.states.MD
assert us.states.lookup("va") != us.states.MD
def test_name():
assert us.states.lookup("Maryland") == us.states.MD
assert us.states.lookup("maryland") == us.states.MD
assert us.states.lookup("Maryland", field="name") == us.states.MD
assert us.states.lookup("maryland", field="name") is None
assert us.states.lookup("murryland") == us.states.MD
assert us.states.lookup("Virginia") != us.states.MD
# lookups
def test_abbr_lookup():
for state in us.STATES:
assert us.states.lookup(state.abbr) == state
def test_fips_lookup():
for state in us.STATES:
assert us.states.lookup(state.fips) == state
def test_name_lookup():
for state in us.STATES:
assert us.states.lookup(state.name) == state
def test_obsolete_lookup():
for state in us.OBSOLETE:
assert us.states.lookup(state.name) is None
def test_deep_lookup():
assert us.states.lookup("Cal.", field="name", deep_lookup=True) == us.states.CA
assert us.states.lookup(" Mary ", field="name", deep_lookup=True) == us.states.MD
assert us.states.lookup("The New York State ", field="name", deep_lookup=True) == us.states.NY
assert us.states.lookup(" a State of Washington", field="name", deep_lookup=True) == us.states.WA
assert us.states.lookup("State of the Washington", field="name", deep_lookup=True) == us.states.WA
# test metaphone
def test_jellyfish_metaphone():
for state in chain(us.STATES_AND_TERRITORIES, us.OBSOLETE):
assert state.name_metaphone == jellyfish.metaphone(state.name)
# mappings
def test_mapping():
states = us.STATES[:5]
assert us.states.mapping("abbr", "fips", states=states) == dict(
(s.abbr, s.fips) for s in states
)
def test_obsolete_mapping():
mapping = us.states.mapping("abbr", "fips")
for state in us.states.OBSOLETE:
assert state.abbr not in mapping
def test_custom_mapping():
mapping = us.states.mapping("abbr", "fips", states=[us.states.DC, us.states.MD])
assert len(mapping) == 2
assert "DC" in mapping
assert "MD" in mapping
# known bugs
def test_kentucky_uppercase():
assert us.states.lookup("kentucky") == us.states.KY
assert us.states.lookup("KENTUCKY") == us.states.KY
def test_wayoming():
assert us.states.lookup("Wyoming") == us.states.WY
assert us.states.lookup("Wayoming") is None
def test_dc():
assert us.states.DC not in us.STATES
# shapefiles
@pytest.mark.skip
def test_head():
import requests
for state in us.STATES_AND_TERRITORIES:
for url in state.shapefile_urls().values():
resp = requests.head(url)
assert resp.status_code == 200
# counts
def test_obsolete():
assert len(us.OBSOLETE) == 3
def test_states():
assert len(us.STATES) == 50
def test_territories():
assert len(us.TERRITORIES) == 5
def test_contiguous():
# Lower 48
assert len(us.STATES_CONTIGUOUS) == 48
def test_continental():
# Lower 48 + Alaska
assert len(us.STATES_CONTINENTAL) == 49