This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
test_new_england_reporting_units.py
246 lines (212 loc) · 7.84 KB
/
test_new_england_reporting_units.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
try:
set
except NameError:
from sets import Set as set
from elex.api import maps
import tests
class TestConnecticutRollups(tests.ElectionResultsTestCase):
"""
Connecticut townships not rolling up properly.
"""
data_url = 'tests/data/20160426_ct_rollups.json'
def test_ct_has_candidates(self):
self.assertNotEqual(len(self.candidate_reporting_units), 0)
def test_ct_has_all_counties(self):
ct_counties = set()
raw_counties = [
r for r in self.reporting_units if
r.statepostal == "CT" and
r.level == "county"
]
for c in raw_counties:
ct_counties.add(c.reportingunitname)
ct_mapped_counties = set(maps.FIPS_TO_STATE['CT'].keys())
self.assertEqual(len(ct_counties), len(ct_mapped_counties))
def test_ct_counties_match_townships(self):
ct_counties = [
r for r in self.reporting_units if
r.statepostal == "CT" and
r.level == "county"
]
for county in ct_counties:
races = set([
r.raceid for r in self.reporting_units if
r.statepostal == "CT" and
r.level == "township" and
r.fipscode == county.fipscode
])
for race in races:
townships = [
r.precinctstotal for r in self.reporting_units if
r.statepostal == "CT" and
r.level == "township" and
r.fipscode == county.fipscode and
r.raceid == race
]
self.assertEqual(county.precinctstotal, sum(townships))
class TestRhodeIslandEdgeCageReportingUnits(tests.ElectionResultsTestCase):
"""
Mail ballots listed as townships. Breaks rollups for RI.
"""
data_url = 'tests/data/20160426-ri_mail_ballots.json'
def test_existence_of_mail_ballots(self):
ri_results = [
r for r in self.reporting_units if
r.statepostal == "RI"
]
mail_ballots = []
for z in ri_results:
try:
if "C.D." in z.reportingunitname:
mail_ballots.append(z)
except TypeError:
pass
self.assertTrue(len(mail_ballots) > 0)
def test_mail_ballots_are_townships(self):
ri_results = [
r for r in self.reporting_units if
r.statepostal == "RI"
]
for z in ri_results:
try:
if "C.D." in z.reportingunitname:
self.assertTrue(z.level, "Township")
except TypeError:
pass
class TestMaineEdgeCaseReportingUnits(tests.ElectionResultsTestCase):
"""
Should get two reporting units from this Maine file: One is the
state level, the other is a level 'subunit' which is actually
the state level data as well. #228.
"""
data_url = 'tests/data/20160305_me_no_townships.json'
NE_STATES = maps.FIPS_TO_STATE.keys()
def test_number_of_reporting_units(self):
maine_results = [
r for r in self.reporting_units if r.raceid == '20852'
]
self.assertEqual(len(maine_results), 2)
def test_one_state_level_unit(self):
maine_results = [
r for r in self.reporting_units if r.raceid == '20852'
]
maine_state_level_units = [
r for r in maine_results if r.level == 'state'
]
self.assertEqual(len(maine_state_level_units), 1)
def test_one_subunit(self):
maine_results = [
r for r in self.reporting_units if r.raceid == '20852'
]
maine_subunits = [
r for r in maine_results if r.level == 'state'
]
self.assertEqual(len(maine_subunits), 1)
class TestNewEnglandReportingUnits(tests.ElectionResultsTestCase):
data_url = 'tests/data/20121106_me_fl_senate.json'
NE_STATES = maps.FIPS_TO_STATE.keys()
def test_number_of_races(self):
self.assertEqual(len(self.races), 2)
def test_results_parsing_florida(self):
florida_results = [
r for r in self.reporting_units if r.raceid == '10005'
]
self.assertEqual(len(florida_results), 68)
def test_results_parsing_maine(self):
maine_results = [
r for r in self.reporting_units if r.raceid == '20978'
]
self.assertEqual(len(maine_results), 516)
def test_florida_townships(self):
florida_results = [
r for r in self.reporting_units if r.raceid == '10005'
]
florida_townships = [
r for r in florida_results if r.level == 'township'
]
self.assertEqual(len(florida_townships), 0)
def test_florida_counties(self):
florida_results = [
r for r in self.reporting_units if r.raceid == '10005'
]
florida_counties = [r for r in florida_results if r.level == 'county']
self.assertEqual(len(florida_counties), len(florida_results) - 1)
def test_maine_townships(self):
maine_results = [
r for r in self.reporting_units if r.raceid == '20978'
]
maine_townships = [r for r in maine_results if r.level == 'township']
count_maine_counties = len(maps.FIPS_TO_STATE['ME'].keys())
count_maine_results_minus_state = len(maine_results) - 1
self.assertEqual(
len(maine_townships),
count_maine_results_minus_state - count_maine_counties
)
def test_maine_counties(self):
maine_results = [
r for r in self.reporting_units if r.raceid == '20978'
]
maine_counties = [
r for r in maine_results if r.level == 'county'
]
count_maine_counties = len(maps.FIPS_TO_STATE['ME'].keys())
self.assertEqual(len(maine_counties), count_maine_counties)
def test_maine_counties_have_statepostal(self):
maine_results = [
r for r in self.reporting_units if r.raceid == '20978'
]
maine_counties = [r for r in maine_results if r.level == 'county']
for c in maine_counties:
self.assertEqual(c.statepostal, 'ME')
def test_maine_counties_have_statename(self):
maine_results = [
r for r in self.reporting_units if r.raceid == '20978'
]
maine_counties = [r for r in maine_results if r.level == 'county']
for c in maine_counties:
self.assertEqual(c.statename, 'Maine')
def test_maine_county_votepcts_are_correct(self):
"""
From ticket 220.
"""
maine_results = [
r for r in self.results if r.raceid == '20978'
]
county_results = [
r for r in maine_results if r.level == 'county'
]
cumberland_results = [
r for r in county_results if r.fipscode == '23005'
]
self.assertNotEqual(
cumberland_results[0].votepct,
cumberland_results[1].votepct
)
def test_maine_county_votecounts_are_correct(self):
"""
From ticket 220.
"""
maine_results = [
r for r in self.results if r.raceid == '20978'
]
county_results = [
r for r in maine_results if r.level == 'county'
]
cumberland_results = [
r for r in county_results if r.fipscode == '23005'
]
self.assertNotEqual(
cumberland_results[0].votecount,
cumberland_results[1].votecount
)
def test_maine_counties_have_votecouts(self):
"""
From ticket 0179.
Vote count is camelcased.
"""
maine_results = [
r for r in self.reporting_units if r.raceid == '20978'
]
maine_counties = [r for r in maine_results if r.level == 'county']
for c in maine_counties:
self.assertFalse(c.votecount == 0)