示例#1
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'})]
示例#2
0
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}
示例#3
0
 def test_baddate(self):
     with self.assertRaises(FileDoesNotExistError):
         Election(
             electiondate=self.baddate,
             username=self.username,
             password=self.password,
         )
示例#4
0
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}
示例#5
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}
示例#6
0
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}
示例#7
0
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()
示例#8
0
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()
示例#9
0
def test_riding_winners_2_party_same_vote() -> None:
    """Test riding_winner for two party that has same vote"""
    e = Election(date(2000, 2, 8))
    e.update_results('r1', 'ndp', 45)
    e.update_results('r1', 'np', 45)
    res1 = e.riding_winners('r1')
    assert res1.sort() == ['np', 'ndp'].sort()
示例#10
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()
示例#11
0
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']
示例#12
0
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']
示例#13
0
 def test_badlogin(self):
     with self.assertRaises(BadCredentialsError):
          Election(
             electiondate=self.electiondate,
             username="******",
             password="******"
         )
示例#14
0
def test_update_results_multiple_value() -> 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('r2', 'nap', 12)
    res2 = e.results_for('r2', 'nap')
    assert res2 == 12
    assert 'r2' in e._ridings and 'r2' in e._results
    assert 'nap' in e._parties and 'nap' in e._results['r2']
    e.update_results('r2', 'nap', 32)
    res2 = e.results_for('r2', 'nap')
    assert res2 == 44
    assert 'r2' in e._ridings and 'r2' in e._results
    assert 'nap' in e._parties and 'nap' in e._results['r2']
    e.update_results('r1', 'nap', 2)
    res2 = e.results_for('r1', 'nap')
    assert res2 == 2
    assert 'r1' in e._ridings and 'r1' in e._results
    assert 'nap' in e._parties and 'nap' in e._results['r1']
    e.update_results('r1', 'ndp', 2)
    res2 = e.results_for('r1', 'ndp')
    assert res2 == 2
    assert 'r1' in e._ridings and 'r1' in e._results
    assert 'ndp' in e._parties and 'ndp' in e._results['r1']
示例#15
0
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}
示例#16
0
def test_simple_election_win_tie() -> None:
    e = Election(date(2000, 2, 8))
    e.update_results('r1', 'ndp', 1)
    e.update_results('r1', 'lib', 1)
    e.update_results('r1', 'pc', 1)
    e.update_results('r2', 'ndp', 1)
    e.update_results('r2', 'lib', 1)
    assert e.election_winners() == ['ndp', 'lib', 'pc']
示例#17
0
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'})]
示例#18
0
def test_results_for_party_in_two_ridings() -> None:
    """Test results_for for a party in two ridings"""
    e = Election(date(2000, 2, 8))
    e.update_results('r1', 'ndp', 2)
    e.update_results('r2', 'ndp', 4)
    res1 = e.results_for('r1', 'ndp')
    res2 = e.results_for('r2', 'ndp')
    assert res1 == 2
    assert res2 == 4
示例#19
0
def test_riding_changes_2_election_same_ridings() -> None:
    """Test riding_changes with 2 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)
    j._history[date(2004, 5, 16)] = e2
    j._history[date(2000, 2, 8)] = e1
    res1 = j.riding_changes()
    assert res1 == [(set(), set())]
示例#20
0
def test_riding_winners_multiple_party_different_vote() -> None:
    """Test riding_winner for multiple party that has different vote"""
    e = Election(date(2000, 2, 8))
    e.update_results('r1', 'ndp', 4)
    e.update_results('r1', 'np', 5)
    e.update_results('r1', 'ncp', 45)
    e.update_results('r1', 'nap', 59)
    res1 = e.riding_winners('r1')
    assert res1 == ['nap']
示例#21
0
def test_riding_winners_multiple_party_same_vote_except_largest() -> 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', 55)
    res1 = e.riding_winners('r1')
    assert res1 == ['nap']
示例#22
0
def test_riding_winners_multiple_party_same_0_vote() -> None:
    """Test riding_winner for multiple party that same vote"""
    e = Election(date(2000, 2, 8))
    e.update_results('r1', 'ndp', 0)
    e.update_results('r1', 'np', 0)
    e.update_results('r1', 'ncp', 0)
    e.update_results('r1', 'nap', 0)
    res1 = e.riding_winners('r1')
    assert res1.sort() == ['np', 'ndp', 'ncp', 'nap'].sort()
示例#23
0
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}
示例#24
0
def test_riding_of_multiple_ridings() -> None:
    """Test Election.riding_of with multiple ridings"""
    e = Election(date(2000, 2, 8))
    e.update_results('r1', 'ndp', 1)
    res1 = e.ridings_of()
    assert res1 == ['r1']
    e.update_results('r2', 'ndp', 10)
    e.update_results('r3', 'ncp', 51)
    e.update_results('r4', 'nap', 14)
    res2 = e.ridings_of()
    assert res2 == ['r1', 'r2', 'r3', 'r4']
示例#25
0
def simple_election_setup() -> Election:
    """Set up a simple Election with two ridings and three parties"""
    e = Election(date(2000, 2, 8))
    e.update_results('r1', 'ndp', 1234)
    e.update_results('r1', 'lib', 1345)
    e.update_results('r1', 'pc', 1456)
    e.update_results('r2', 'ndp', 300)
    e.update_results('r2', 'lib', 200)
    e.update_results('r2', 'pc', 100)

    return e
示例#26
0
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)]
示例#27
0
def test_riding_of_1_vote() -> None:
    """Test Election.riding_of with riding of 1 votes"""
    e = Election(date(2000, 2, 8))
    e.update_results('r1', 'ndp', 1)
    res1 = e.ridings_of()
    assert res1 == ['r1']
    e.update_results('r2', 'ndp', 1)
    e.update_results('r1', 'ncp', 1)
    e.update_results('r2', 'nap', 1)
    res2 = e.ridings_of()
    assert res2 == ['r1', 'r2']
示例#28
0
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']
示例#29
0
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 == []
示例#30
0
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}