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_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_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_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_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_2_election_different_ridings() -> None: """Test riding_changes with 2 elections of totally different 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('r3', 'ndp', 1) e2.update_results('r4', 'pc', 1) j._history[date(2004, 5, 16)] = e2 j._history[date(2000, 2, 8)] = e1 res1 = j.riding_changes() assert res1 == [({'r1', 'r2'}, {'r3', 'r4'})]
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())]
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 simple_jurisdiction_setup() -> Jurisdiction: """Set up a simple Jurisdiction with a single Election and one result.""" j = Jurisdiction('Canada') res1 = StringIO(SHORT_FILE_CONTENTS) j.read_results(2000, 1, 2, res1) return j