This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
forked from datadesk/python-elections
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.py
259 lines (233 loc) · 12.5 KB
/
tests.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
247
248
249
250
251
252
253
254
255
256
257
258
259
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Tests out python-elections
These tests were written using the Los Angeles Times' login, which gives it state
level access for California and nationwide access elsewhere. If you have a different
access level, this will prove to be a problem.
We need to work this out somehow. If you have any bright ideas let me know.
"""
import os
import unittest
from elections import AP
from datetime import date, datetime
from elections.ap import Nomination, StateDelegation
from elections.ap import Candidate, Race, ReportingUnit, Result, State
from elections.ap import FileDoesNotExistError, BadCredentialsError
class BaseTest(unittest.TestCase):
def setUp(self):
self.client = AP(
os.environ['AP_USERNAME'],
os.environ['AP_PASSWORD']
)
class APTest(BaseTest):
def test_badlogin(self):
client = AP("foo", "bar")
self.assertRaises(BadCredentialsError, client.get_state, os.environ['TEST_STATE'])
def test_badstate(self):
self.assertRaises(FileDoesNotExistError, self.client.get_state, "XYZ")
def test_county_aggregates(self):
self.state = self.client.get_state(os.environ['TEST_STATE'])
county_list = self.state.counties
self.assertEqual(type(county_list), type([]))
[self.assertEqual(type(i), ReportingUnit) for i in county_list]
[self.assertEqual(i.is_state, False) for i in county_list]
def test_state_reporting_unit(self):
"""
Makes sure Wyoming only has one 'state'-identified RU.
"""
self.state = self.client.get_state(os.environ['TEST_STATE'])
self.assertEqual(type(self.state.races[0].state), ReportingUnit)
def test_getstate(self):
# Pull state
self.state = self.client.get_state(os.environ['TEST_STATE'])
# Races
race_list = self.state.races
self.assertTrue(isinstance(race_list, list))
self.assertTrue(len(race_list) > 0)
self.assertTrue(isinstance(race_list[0], Race))
self.assertEqual(self.state.get_race(race_list[0].ap_race_number), race_list[0])
self.assertRaises(KeyError, self.state.get_race, 'foo')
race = self.state.races[0]
self.assertTrue(isinstance(race.ap_race_number, basestring))
self.assertTrue(isinstance(race.office_name, basestring))
self.assertTrue(isinstance(race.office_description, basestring))
self.assertTrue(isinstance(race.office_id, basestring))
self.assertTrue(isinstance(race.seat_name, basestring))
self.assertTrue(isinstance(race.seat_number, basestring))
self.assertTrue(isinstance(race.scope, basestring))
self.assertTrue(isinstance(race.date, date))
self.assertTrue(isinstance(race.num_winners, int))
self.assertTrue(isinstance(race.race_type, basestring))
self.assertTrue(isinstance(race.party, basestring))
self.assertTrue(isinstance(race.uncontested, bool))
self.assertTrue(isinstance(race.name, basestring))
self.assertTrue(
isinstance(race.race_type_name, basestring) or
isinstance(race.race_type_name, type(None))
)
self.assertTrue(isinstance(race.is_primary, bool))
self.assertTrue(isinstance(race.is_caucus, bool))
self.assertTrue(isinstance(race.is_general, bool))
# Reporting units
ru_list = self.state.reporting_units
self.assertTrue(isinstance(ru_list, list))
self.assertTrue(len(ru_list) > 0)
self.assertTrue(isinstance(ru_list[0], ReportingUnit))
self.assertEqual(self.state.get_reporting_unit(ru_list[0].key), ru_list[0])
self.assertRaises(KeyError, self.state.get_reporting_unit, 'foo')
self.assertTrue(isinstance(ru_list[0], ReportingUnit))
self.assertTrue(isinstance(ru_list[0].ap_number, basestring))
self.assertTrue(isinstance(ru_list[0].name, basestring))
self.assertTrue(isinstance(ru_list[0].abbrev, basestring))
self.assertTrue(isinstance(ru_list[0].fips, basestring))
ru_list[0].num_reg_voters
self.assertTrue(isinstance(ru_list[0].precincts_total, int))
self.assertTrue(isinstance(ru_list[0].precincts_reporting, type(None)))
self.assertTrue(isinstance(ru_list[0].precincts_reporting_percent, type(None)))
ru_list = self.state.races[0].reporting_units
self.assertTrue(isinstance(ru_list, list))
self.assertTrue(len(ru_list) > 0)
for ru in ru_list:
self.assertTrue(isinstance(ru, ReportingUnit))
self.assertTrue(isinstance(ru.ap_number, basestring))
self.assertTrue(isinstance(ru.name, basestring))
self.assertTrue(isinstance(ru.abbrev, basestring))
self.assertTrue(isinstance(ru.fips, basestring))
ru.num_reg_voters
self.assertTrue(isinstance(ru.precincts_total, (int, type(None))))
self.assertTrue(isinstance(ru.precincts_reporting, (int, type(None))))
self.assertTrue(isinstance(ru.precincts_reporting_percent, (float, type(None))))
if ru.results:
self.assertTrue(isinstance(ru.results[0], Result))
# Results
for result in ru.results:
self.assertTrue(isinstance(result, Result))
self.assertTrue(isinstance(result.candidate, Candidate))
self.assertEqual(result.reporting_unit, ru)
self.assertTrue(isinstance(result.vote_total, int))
try:
self.assertTrue(isinstance(result.vote_total_percent, float))
except:
self.assertTrue(isinstance(result.vote_total_percent, type(None)))
# Counties
county_list = self.state.races[0].counties
self.assertEqual(type(county_list), type([]))
self.assertEqual(type(county_list[0]), ReportingUnit)
self.assertEqual(county_list[0].is_state, False)
# State
state = self.state.races[0].state
self.assertEqual(type(state), ReportingUnit)
self.assertEqual(state.is_state, True)
# Candidates
cand_list = self.state.races[0].candidates
self.assertTrue(isinstance(race.candidates, list))
self.assertTrue(len(cand_list) > 0)
for cand in cand_list:
self.assertTrue(isinstance(cand, Candidate))
self.assertTrue(isinstance(cand.first_name, basestring))
self.assertTrue(isinstance(cand.middle_name, basestring))
self.assertTrue(isinstance(cand.last_name, basestring))
self.assertTrue(isinstance(cand.abbrev_name, basestring))
self.assertTrue(isinstance(cand.suffix, basestring))
self.assertTrue(isinstance(cand.use_suffix, bool))
self.assertTrue(isinstance(cand.ap_natl_number, basestring))
self.assertTrue(isinstance(cand.ap_polra_number, basestring))
self.assertTrue(isinstance(cand.ap_pol_number, basestring))
self.assertTrue(isinstance(cand.party, basestring))
self.assertTrue(isinstance(cand.is_winner, bool))
self.assertTrue(isinstance(cand.is_runoff, bool))
self.assertTrue(isinstance(cand.is_incumbent, bool))
#self.assertTrue(isinstance(cand.delegates, int))
self.assertTrue(isinstance(cand.name, basestring))
# FTP hits
self.assertEqual(self.client._ftp_hits, 1)
def test_getstates(self):
# Pull states, using the state twice since that's all we have access to.
self.first_two = self.client.get_states(os.environ['TEST_STATE'], os.environ['TEST_STATE'])
self.assertEqual(type(self.first_two), type([]))
self.assertEqual(len(self.first_two), 2)
[self.assertEqual(type(i), State) for i in self.first_two]
# FTP hits
self.assertEqual(self.client._ftp_hits, 1)
# def test_topofticket(self):
# # The 2012 general election
# self.nov6 = self.client.get_topofticket()
# self.assertEqual(len(self.nov6.filter_races(office_name='President')), 52)
# self.assertEqual(len(self.nov6.filter_races(office_name='President', state_postal='CO')), 1)
# # Test custom properties
# self.assertEqual(len(self.nov6.states), 51)
# [self.assertEqual(type(i), ReportingUnit) for i in self.nov6.states]
# # Pull some bum dates
# self.assertRaises(FileDoesNotExistError, self.client.get_topofticket, "2011-02-07")
# self.assertRaises(ValueError, self.client.get_topofticket, 'abcdef')
# # Test the results against a get_state method to verify they are the same
# self.tt = self.client.get_topofticket()
# self.st = self.client.get_state(os.environ['TEST_STATE'])
# # self.tt = self.tt.filter_races(office_name='President', state_postal=os.environ['TEST_STATE'])[0]
# # self.st = self.st.filter_races(office_name='President', state_postal=os.environ['TEST_STATE'])[0]
# # self.assertEqual(
# # [i.vote_total for i in self.tt.state.results],
# # [i.vote_total for i in self.st.state.results]
# # )
# refs = self.tt.filter_races(is_referendum=True)
# [self.assertTrue(i.is_referendum)
# for i in refs if i.office_name in [
# 'Amendment',
# 'Initiative',
# 'Issue',
# 'Measure',
# 'Proposition',
# 'Question',
# 'Referendum'
# ]
# ]
# def test_presidential_summary(self):
# self.nov6 = self.client.get_presidential_summary()
# self.assertEqual(len(self.nov6.states), 51)
# self.assertEqual(len([self.nov6.nationwide]), 1)
# self.assertEqual(self.nov6.nationwide.electoral_votes_total, 538)
# self.assertEqual(sum([i.electoral_votes_total for i in self.nov6.states]), 538)
# [self.assertTrue(isinstance(i,ReportingUnit)) for i in self.nov6.counties]
# [self.assertTrue(isinstance(i.electoral_votes_total,int))
# for i in self.nov6.nationwide.results]
# for state in self.nov6.states:
# [self.assertTrue(isinstance(i.electoral_votes_total,int))
# for i in state.results]
# for county in self.nov6.counties:
# [self.assertTrue(isinstance(i.vote_total,int))
# for i in county.results]
# self.districts = self.client.get_presidential_summary(districts=True)
# self.assertEqual(len(self.districts.districts), 5)
# self.assertEqual(len(self.nov6.districts), 0)
def test_congressional_trends(self):
self.trends = self.client.get_congressional_trends()
for chamber_name in ('house', 'senate'):
chamber = getattr(self.trends, chamber_name)
self.assertEqual(isinstance(chamber.dem_net_change, int), True)
self.assertEqual(isinstance(chamber.gop_net_change, int), True)
self.assertEqual(isinstance(chamber.others_net_change, int), True)
self.assertEqual(isinstance(chamber.dem_won_total, int), True)
self.assertEqual(isinstance(chamber.gop_won_total, int), True)
self.assertEqual(isinstance(chamber.others_won_total, int), True)
self.assertEqual(isinstance(chamber.dem_leading, int), True)
self.assertEqual(isinstance(chamber.gop_leading, int), True)
self.assertEqual(isinstance(chamber.others_leading, int), True)
self.assertEqual(isinstance(chamber.dem_current_total, int), True)
self.assertEqual(isinstance(chamber.gop_current_total, int), True)
self.assertEqual(isinstance(chamber.others_current_total, int), True)
self.assertEqual(isinstance(chamber.dem_holdovers, int), True)
self.assertEqual(isinstance(chamber.gop_holdovers, int), True)
self.assertEqual(isinstance(chamber.others_holdovers, int), True)
self.assertEqual(isinstance(chamber.dem_insufficient, int), True)
self.assertEqual(isinstance(chamber.gop_insufficient, int), True)
self.assertEqual(isinstance(chamber.others_insufficient, int), True)
# def test_delegate_summary(self):
# self.delsum = self.client.get_delegate_summary()
# self.assertEqual(len(self.delsum), 2)
# [self.assertEqual(type(i), Nomination) for i in self.delsum]
# [self.assertEqual(type(i), Candidate) for i in self.delsum[0].candidates]
# [self.assertEqual(type(i), StateDelegation) for i in self.delsum[0].states]
# [self.assertEqual(type(i), Candidate) for i in self.delsum[0].states[0].candidates]
if __name__ == '__main__':
unittest.main()