def test_election_basic(self): rules = ElectionRules() rules["constituency_seats"] = [1, 2] rules["constituency_adjustment_seats"] = [2, 1] rules["constituency_names"] = ["I", "II"] rules["parties"] = ["A", "B"] votes = [[501, 400], [301, 200]] election = Election(rules, votes) election.run() self.assertEqual(election.results, [[2, 1], [2, 1]])
def setUp(self): self.rules = ElectionRules() self.rules["debug"] = True self.rules["show_entropy"] = True self.rules["parties"] = ["A", "B"] self.rules["adjustment_method"] = "alternating-scaling" self.rules["constituency_names"] = ["I", "II"] self.rules["constituency_seats"] = [2, 3] self.rules["constituency_adjustment_seats"] = [1, 2] self.votes = [[500, 400], [300, 200]] self.election = Election(self.rules, self.votes) self.election.run()
def test_election_basic2(self): rules = ElectionRules() rules["constituency_seats"] = [1, 1] rules["constituency_adjustment_seats"] = [1, 1] rules["constituency_names"] = ["I", "II"] rules["parties"] = ["A", "B", "C"] rules["divisor"] = "dhondt" rules["adjustment_threshold"] = 0.0 votes = [[4000, 2000, 3500], [3000, 1000, 2500]] election = Election(rules, votes) election.run() self.assertEqual(election.results, [[1, 1, 0], [1, 0, 1]])
def test_max_entropy_calculation(self): rules = ElectionRules() rules["debug"] = True rules["show_entropy"] = True rules["parties"] = ["A", "B"] rules["adjustment_method"] = "alternating-scaling" rules["constituency_names"] = ["I", "II"] rules["constituency_seats"] = [2, 3] rules["constituency_adjustment_seats"] = [1, 2] votes = [[500, 400], [300, 200]] election = Election(rules, votes) election.run() self.assertEqual(round(election.entropy(), 2), 42.95)
def test_get_results(self): rules = ElectionRules() rules["parties"] = ["A", "B"] rules["adjustment_method"] = "alternating-scaling" rules["constituency_names"] = ["I", "II"] rules["constituency_seats"] = [2, 3] rules["constituency_adjustment_seats"] = [1, 2] votes = [[500, 400], [300, 200]] election = Election(rules, votes) election.run() res = election.get_results_dict() self.assertEqual(res["rules"], rules) self.assertEqual(res["seat_allocations"], [[2, 1], [3, 2]])
def __init__(self, rules, votes): self.rules = rules self.votes = votes self.variates = [] self.election = Election(self.rules, votes) for j in self.rules["simulation_variate"]: self.variates.append(SIMULATION_VARIATES[j](self.election))
def test_entropy_depenency_on_divisor(self): dd_entropy = self.election.entropy() ds_entropy = entropy(self.votes, self.election.results, sainte_lague_gen) self.rules["primary_divider"] = "sainte-lague" self.rules["adj_determine_divider"] = "sainte-lague" self.rules["adj_alloc_divider"] = "sainte-lague" self.sl_election = Election(self.rules, self.votes) self.sl_election.run() ss_entropy = self.sl_election.entropy() sd_entropy = entropy(self.votes, self.sl_election.results, dhondt_gen) self.assertNotEqual(ds_entropy, dd_entropy) self.assertNotEqual(ss_entropy, dd_entropy) self.assertNotEqual(ss_entropy, sd_entropy) self.assertNotEqual(ds_entropy, sd_entropy) self.assertEqual(round(dd_entropy, 2), 42.95) self.assertEqual(round(ds_entropy, 2), 41.22) self.assertEqual(round(ss_entropy, 2), 41.22) self.assertEqual(round(sd_entropy, 2), 42.95)
class TestEntropy(unittest.TestCase): def setUp(self): self.rules = ElectionRules() self.rules["debug"] = True self.rules["show_entropy"] = True self.rules["parties"] = ["A", "B"] self.rules["adjustment_method"] = "alternating-scaling" self.rules["constituency_names"] = ["I", "II"] self.rules["constituency_seats"] = [2, 3] self.rules["constituency_adjustment_seats"] = [1, 2] self.votes = [[500, 400], [300, 200]] self.election = Election(self.rules, self.votes) self.election.run() def test_entropy_calculation(self): self.assertEqual(round(self.election.entropy(), 2), 42.95) def test_entropy_depenency_on_divisor(self): dd_entropy = self.election.entropy() ds_entropy = entropy(self.votes, self.election.results, sainte_lague_gen) self.rules["primary_divider"] = "sainte-lague" self.rules["adj_determine_divider"] = "sainte-lague" self.rules["adj_alloc_divider"] = "sainte-lague" self.sl_election = Election(self.rules, self.votes) self.sl_election.run() ss_entropy = self.sl_election.entropy() sd_entropy = entropy(self.votes, self.sl_election.results, dhondt_gen) self.assertNotEqual(ds_entropy, dd_entropy) self.assertNotEqual(ss_entropy, dd_entropy) self.assertNotEqual(ss_entropy, sd_entropy) self.assertNotEqual(ds_entropy, sd_entropy) self.assertEqual(round(dd_entropy, 2), 42.95) self.assertEqual(round(ds_entropy, 2), 41.22) self.assertEqual(round(ss_entropy, 2), 41.22) self.assertEqual(round(sd_entropy, 2), 42.95)