-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_candidate.py
executable file
·160 lines (140 loc) · 5.5 KB
/
test_candidate.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
#!/usr/bin/python2.7
"""Tests for candidate.py. Run them with py.test."""
import re
import unittest
import candidate
# pylint: disable=too-many-public-methods
class TestCandidate(unittest.TestCase):
"""Tests for candidate.py."""
def test_normalize_name(self):
"""Test translating names from various formats to Firstname Lastname."""
cases = {
"CATFACE, ALEX": "Alex Catface",
"BANANA, MABEL JR.": "Mabel Banana Jr",
"SLEEPERSOFA, LUCY DR": "Lucy Sleepersofa",
"BEAR, P III": "P Bear III",
"Some Ok Name": "Some Ok Name",
}
for k in cases:
self.assertEqual(candidate.normalize_name(k), cases[k])
def test_candidate_creation(self):
"""Test candidate creation from a dictionary."""
valid_cases = [
# Create a candidate
({"can_nam": "Some Person", "office": "senate", "party": "DEM",
"can_off_sta": "NM"},
{"name": "Some Person", "office": "senate", "party": "DEM", "state": "New Mexico"}
),
# Differently formatted name
({"can_nam": "PERSON, SOME", "office": "senate", "party": "DEM",
"can_off_sta": "NM"},
{"name": "Some Person", "office": "senate", "party": "DEM", "state": "New Mexico"}
),
# House candidate with a district
({"can_nam": "Some Person", "party": "Democratic", "office": "house", "district": "West Virginia 2"},
{"name": "Some Person", "party": "Democratic", "office": "house", "state": "West Virginia", "district": "2nd"},
),
# Invalid fields are retained (though not used).
({"can_nam": "PERSON, SOME", "office": "senate", "party": "DEM", "state": "AL",
"icecream_flavor": "banana"},
{"name": "Some Person", "office": "senate", "party": "DEM", "state": "Alabama",
"icecream_flavor": "banana",}
),
]
# Invalid cases raise a CandidateException
invalid_cases = [
# No specified office
({"can_nam": "Some Person", "party": "DEM", "can_off_sta": "NM"},
None,
),
# House candidate without a district
({"can_nam": "Some Person", "party": "DEM", "can_off_sta": "NM", "office": "house"},
None,
),
]
for k in valid_cases:
got = candidate.make_candidate(k[0])
self.assertEqual(k[1], got.data())
for k in invalid_cases:
try:
self.assertFalse(candidate.make_candidate(k[0]))
self.fail()
except candidate.CandidateException:
pass
def test_location(self):
"""Test district and state munging."""
cases = [
# input_state, input_district, expected_state, expected_district
["", "California 1", "California", "1st"],
["", "New York 2", "New York", "2nd"],
["", "New Mexico 3", "New Mexico", "3rd"],
["", "Florida 18", "Florida", "18th"],
["", "Wyoming at-large", "Wyoming", "at-large"],
["FL", "4", "Florida", "4th"],
["NM", "New Mexico 1", "New Mexico", "1st"],
["Washington", "", "Washington", ""],
["Iowa", "Iowa 1", "Iowa", "1st"],
# a valid state in the district name takes precedence
["NJ", "New York 2", "New York", "2nd"],
# and an invalid state in the district returns empty
["NJ", "New Spork 2", "", "2nd"], # error
["", "New Spork 2", "", "2nd"], # error
["Iowa", "not real", "Iowa", ""], # error
["", "", "", ""], # error
["Alberta", "", "", ""], # error
["XX", "Hawaii at-large", "Hawaii", "at-large"],
["", "Utah 1", "Utah", "1st"],
]
for k in cases:
got = candidate.normalize_location(k[0], k[1])
expected = (k[2], k[3])
self.assertEqual(got, expected)
def test_content(self):
"""Test wikipedia output."""
data = {"can_nam": "Some Person", "office": "house", "party": "DEM",
"can_off_sta": "NM", "can_off_dis": "New Mexico 7"}
got = candidate.make_candidate(data).wikipedia_content()
# fields could come out in any order, so we can't do a full match;
# we just check that something plausible came out.
expected_re = re.compile(
"^{{Infobox Officeholder\n.*| name = Some Person\n.*}}$")
match = expected_re.search(got)
self.assertTrue(match)
def test_wikipedia_html(self):
"""Test parsing wikipedia html.
Reads a test html file, test_house.html, in this directory and checks
for expected results.
"""
filename = "test_house.html"
got = []
for person in candidate.new_from_wikipedia_page(filename, "house"):
got.append(person.data())
expected_data1 = {
"district": u"2nd",
"name": u"Pageless One",
"state": u"Alabama",
u"representative": u"Name Two",
"reference_name": u'"Sen. Richard Shelby will face Republican challengers"',
"reference_url": u"http://www.montgomeryadvertiser.com/story/news/2015/11/07/sen-richard-shelby-face-republican-challengers/75318814",
"office": "house",
"party": "Democratic",
u"status": u'Incumbent running',
}
expected_data2 = {
"name": u"Pageless Two",
"state": u"Alaska",
u"district": u"at-large",
u"representative": u"Name Five",
"reference_name": u'"Steve Lindbeck announces run for Congress against Don Young"',
"reference_url": u"http://www.adn.com/article/20160407/steve-lindbeck-announces-run-congress-against-don-young",
"office": "house",
"party": "Democratic",
u"status": u'Incumbent running',
}
expected = [
expected_data1,
expected_data2,
]
self.assertEqual(got, expected)
if __name__ == 'main__':
unittest.main()