def test_riding_winners_multiple_party_same_vote() -> None: """Test riding_winner for multiple party that same vote""" e = Election(date(2000, 2, 8)) e.update_results('r1', 'ndp', 45) e.update_results('r1', 'np', 45) e.update_results('r1', 'ncp', 45) e.update_results('r1', 'nap', 45) res1 = e.riding_winners('r1') assert res1.sort() == ['np', 'ndp', 'ncp', 'nap'].sort()
def test_party_history_1_election_all_vote() -> None: """Test party_wins for 1 election and all vote.""" e1 = Election(date(2000, 2, 8)) e1.update_results('r1', 'ndp', 0) e1.update_results('r1', 'lib', 0) e1.update_results('r1', 'pc', 10) e1.update_results('r2', 'lib', 0) e1.update_results('r2', 'pc', 200) e1.update_results('r3', 'ndp', 0) e1.update_results('r3', 'pc', 50) j = Jurisdiction('Canada') j._history[date(2000, 2, 8)] = e1 res1 = j.party_history('pc') assert res1 == {date(2000, 2, 8): 1.0}
def test_badlogin(self): with self.assertRaises(BadCredentialsError): Election( electiondate=self.electiondate, username="******", password="******" )
def test_baddate(self): with self.assertRaises(FileDoesNotExistError): Election( electiondate=self.baddate, username=self.username, password=self.password, )
def test_party_seats_0_votes() -> None: """Test party_seats for each party has 0 votes""" e = Election(date(2000, 2, 8)) e.update_results('r1', 'ndp', 0) e.update_results('r2', 'np', 1) res1 = e.party_seats() assert res1 == {'ndp': 0, 'np': 1}
def test_party_seats_2_riding_2_party() -> None: """Test party_seats for 2 riding and 2 party""" e = Election(date(2000, 2, 8)) e.update_results('r1', 'ndp', 19) e.update_results('r2', 'np', 69) res1 = e.party_seats() assert res1 == {'ndp': 1, 'np': 1}
def test_popular_vote_0_votes() -> None: """Test popular_vote for each party has 0 votes""" e = Election(date(2000, 2, 8)) e.update_results('r1', 'ndp', 0) e.update_results('r2', 'np', 0) res1 = e.popular_vote() assert res1 == {'ndp': 0, 'np': 0}
def test_popular_vote_2_riding_2_party() -> None: """Test popular_vote for 2 riding and 2 party""" e = Election(date(2000, 2, 8)) e.update_results('r1', 'ndp', 19) e.update_results('r2', 'np', 69) res1 = e.popular_vote() assert res1 == {'ndp': 19, 'np': 69}
def test_riding_winners_2_party_different_vote() -> None: """Test riding_winner for two party that has different vote""" e = Election(date(2000, 2, 8)) e.update_results('r1', 'ndp', 45) e.update_results('r1', 'np', 74) res1 = e.riding_winners('r1') assert res1 == ['np']
def test_party_seats_1_riding_2_party_tie() -> None: """Test party_seats for two tied party in 1 riding""" e = Election(date(2000, 2, 8)) e.update_results('r1', 'ndp', 20) e.update_results('r1', 'np', 20) res1 = e.party_seats() assert res1 == {'ndp': 0, 'np': 0}
def test_election_winners_1_riding_2_party_tie() -> None: """Test election_winners for two tied party in 1 riding""" e = Election(date(2000, 2, 8)) e.update_results('r1', 'ndp', 20) e.update_results('r1', 'np', 20) res1 = e.election_winners() assert res1.sort() == ['ndp', 'np'].sort()
def test_election_winners_0_votes() -> None: """Test election_winners for each party has 0 votes""" e = Election(date(2000, 2, 8)) e.update_results('r1', 'ndp', 0) e.update_results('r2', 'np', 0) res1 = e.election_winners() assert res1.sort() == ['ndp', 'np'].sort()
def test_simple_election_riding_winners_tie() -> None: """Test Election.riding_winners with a tie Election.""" e = Election(date(2000, 2, 8)) e.update_results('r1', 'ndp', 1) e.update_results('r1', 'lib', 1) e.update_results('r1', 'pc', 1) assert e.riding_winners('r1') == ['ndp', 'lib', 'pc']
def test_election_winners_2_riding_2_party() -> None: """Test election_winners for 2 riding and 2 party""" e = Election(date(2000, 2, 8)) e.update_results('r1', 'ndp', 19) e.update_results('r2', 'np', 69) res1 = e.election_winners() assert res1.sort() == ['ndp', 'np'].sort()
def test_update_results_0_votes() -> None: """Test update_result for not existed and existed riding""" e = Election(date(2000, 2, 8)) e.update_results('r1', 'ndp', 0) res1 = e.results_for('r1', 'ndp') assert res1 == 0 assert 'r1' in e._ridings and 'r1' in e._results assert 'ndp' in e._parties and 'ndp' in e._results['r1'] e.update_results('r1', 'nap', 0) res2 = e.results_for('r1', 'nap') assert res2 == 0 assert 'r1' in e._ridings and 'r1' in e._results assert 'nap' in e._parties and 'nap' in e._results['r1']
def test_complex_election_popular_vote() -> None: """Test Election.popular_vote with a simple Election.""" e = Election(date(2000, 2, 8)) e.update_results('r1', 'ndp', 1) e.update_results('r1', 'lib', 1) e.update_results('r1', 'pc', 0) e.update_results('r2', 'ndp', 3) e.update_results('r2', 'lib', 2) e.update_results('r2', 'pc', 0) e.update_results('r2', 'green', 1) assert e.popular_vote() == {'ndp': 4, 'lib': 3, 'green': 1, 'pc': 0}
def test_riding_changes_4_election_recuring_ridings() -> None: """Test riding_changes with 3 elections of same ridings""" j = Jurisdiction('Canada') e1 = Election(date(2000, 2, 8)) e1.update_results('r1', 'ndp', 1) e1.update_results('r5', 'lib', 1) e1.update_results('r6', 'pc', 1) e1.update_results('r2', 'pc', 1) e1.update_results('r2', 'lib', 1) e1.update_results('r2', 'green', 1) e1.update_results('r2', 'ndp', 1) e2 = Election(date(2004, 5, 16)) e2.update_results('r3', 'ndp', 1) e2.update_results('r4', 'pc', 1) e3 = Election(date(2005, 5, 16)) e3.update_results('r1', 'ndp', 1) e3.update_results('r2', 'pc', 1) e4 = Election(date(2006, 5, 16)) e4.update_results('r3', 'ndp', 1) e4.update_results('r2', 'pc', 1) e4.update_results('r1', 'pc', 1) e4.update_results('r4', 'pc', 1) e4.update_results('r5', 'pc', 1) j._history[date(2006, 5, 16)] = e4 j._history[date(2005, 5, 16)] = e3 j._history[date(2004, 5, 16)] = e2 j._history[date(2000, 2, 8)] = e1 res1 = j.riding_changes() assert res1 == [({'r1', 'r2', 'r5', 'r6'}, {'r3', 'r4'}), ({'r3', 'r4'}, {'r1', 'r2'}), (set(), {'r3', 'r4', 'r5'})]
def test_riding_changes_3_election_same_ridings() -> None: """Test riding_changes with 3 elections of same ridings""" j = Jurisdiction('Canada') e1 = Election(date(2000, 2, 8)) e1.update_results('r1', 'ndp', 1) e1.update_results('r1', 'lib', 1) e1.update_results('r1', 'pc', 1) e1.update_results('r2', 'pc', 1) e1.update_results('r2', 'lib', 1) e1.update_results('r2', 'green', 1) e1.update_results('r2', 'ndp', 1) e2 = Election(date(2004, 5, 16)) e2.update_results('r1', 'ndp', 1) e2.update_results('r2', 'pc', 1) e3 = Election(date(2005, 5, 16)) e3.update_results('r1', 'ndp', 1) e3.update_results('r2', 'pc', 1) j._history[date(2005, 5, 16)] = e3 j._history[date(2004, 5, 16)] = e2 j._history[date(2000, 2, 8)] = e1 res1 = j.riding_changes() assert res1 == [(set(), set()), (set(), set())]
def test_riding_changes_3_election_different_ridings() -> None: """Test riding_changes with 3 elections of totally different ridings""" j = Jurisdiction('Canada') e1 = Election(date(2000, 2, 8)) e1.update_results('r1', 'ndp', 1) e1.update_results('r2', 'ndp', 1) e2 = Election(date(2004, 5, 16)) e2.update_results('r3', 'ndp', 1) e2.update_results('r4', 'pc', 1) e3 = Election(date(2005, 5, 16)) e3.update_results('r5', 'ndp', 1) e3.update_results('r6', 'pc', 1) j._history[date(2005, 5, 16)] = e3 j._history[date(2004, 5, 16)] = e2 j._history[date(2000, 2, 8)] = e1 res1 = j.riding_changes() assert res1 == [({'r1', 'r2'}, {'r3', 'r4'}), ({'r3', 'r4'}, {'r5', 'r6'})]
def test_riding_changes_1_election() -> None: """Test riding_changes with 1 election""" j = Jurisdiction('Canada') e1 = Election(date(2000, 2, 8)) e1.update_results('r1', 'ndp', 1) e1.update_results('r1', 'lib', 1) e1.update_results('r1', 'pc', 1) e1.update_results('r2', 'pc', 1) e1.update_results('r2', 'lib', 1) e1.update_results('r2', 'green', 1) e1.update_results('r2', 'ndp', 1) j._history[date(2000, 2, 8)] = e1 res1 = j.riding_changes() assert res1 == []
def test_party_history_1_election_random_vote() -> None: """Test party_wins for 1 election and random vote.""" e1 = Election(date(2000, 2, 8)) e1.update_results('r1', 'ndp', 14) e1.update_results('r1', 'lib', 22) e1.update_results('r1', 'pc', 35) e1.update_results('r2', 'lib', 10) e1.update_results('r2', 'pc', 20) e1.update_results('r3', 'ndp', 199) e1.update_results('r3', 'pc', 100) j = Jurisdiction('Canada') j._history[date(2000, 2, 8)] = e1 res1 = j.party_history('pc') assert res1 == {date(2000, 2, 8): 0.3875}
def test_party_wins_2_election_all_tie() -> None: """Test party_wins for 1 election and the party losing.""" e1 = Election(date(2000, 2, 8)) e1.update_results('r1', 'ndp', 10) e1.update_results('r1', 'lib', 100) e1.update_results('r1', 'pc', 100) e1.update_results('r2', 'lib', 10) e1.update_results('r2', 'pc', 20) e1.update_results('r3', 'ndp', 200) e1.update_results('r3', 'pc', 100) e2 = Election(date(2004, 5, 16)) e2.update_results('r1', 'ndp', 10) e2.update_results('r1', 'lib', 2) e2.update_results('r2', 'lib', 30) e2.update_results('r2', 'ndp', 5) j = Jurisdiction('Canada') j._history[date(2000, 2, 8)] = e1 j._history[date(2004, 5, 16)] = e2 res1 = j.party_wins('ndp') assert res1 == [date(2000, 2, 8), date(2004, 5, 16)]
def test_party_wins_1_election_lose() -> None: """Test party_wins for 1 election and the party losing.""" e1 = Election(date(2000, 2, 8)) e1.update_results('r1', 'ndp', 1) e1.update_results('r1', 'lib', 2) e1.update_results('r1', 'pc', 3) e1.update_results('r2', 'lib', 10) e1.update_results('r2', 'pc', 20) e1.update_results('r3', 'ndp', 200) e1.update_results('r3', 'pc', 100) j = Jurisdiction('Canada') j._history[date(2000, 2, 8)] = e1 res1 = j.party_wins('lib') assert res1 == []
def test_riding_winners_1_party_zero_vote() -> None: """Test riding_winner for one party that has 0 votes""" e = Election(date(2000, 2, 8)) e.update_results('r1', 'ndp', 0) res1 = e.riding_winners('r1') assert res1 == ['ndp']
def test_election(self): self.election = Election( electiondate=self.electiondate, username=self.username, password=self.password, ) # 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_popular_vote_1_riding_1_party() -> None: """Test popular_vote for 1 riding and 1 party""" e = Election(date(2000, 2, 8)) e.update_results('r1', 'ndp', 19) res1 = e.popular_vote() assert res1 == {'ndp': 19}
def test_party_seats_1_riding_1_party() -> None: """Test party_seats for 1 riding and 1 party""" e = Election(date(2000, 2, 8)) e.update_results('r1', 'ndp', 19) res1 = e.party_seats() assert res1 == {'ndp': 1}
def test_election_winners_1_riding_1_party() -> None: """Test election_winners for 1 riding and 1 party""" e = Election(date(2000, 2, 8)) e.update_results('r1', 'ndp', 20) res1 = e.election_winners() assert res1 == ['ndp']
def test_one_party_one_riding_read_results() -> None: """Test Election.read_results with a file with a single line.""" file = StringIO(SHORT_FILE_CONTENTS) e = Election(date(2012, 10, 30)) e.read_results(file) assert e.popular_vote() == {'Liberal': 113}
def test_election_winners_empty() -> None: """Test election_winners for a election with no vote""" e = Election(date(2000, 2, 8)) res1 = e.election_winners() assert res1.sort() == ['ndp', 'np'].sort()