def test_constituency_with_results(self): """Summary with results should have a table.""" add_constituency_result_line('X, 10, C') r = self.client.get('/constituencies') self.assertEqual(r.status_code, 200) soup = BeautifulSoup(r.data, 'html.parser') self.assertIs(soup.find(id='no-results'), None) self.assertIsNot(soup.find(id='results-table'), None)
def test_simple_usage(self): """Simple addition of result line should work.""" add_constituency_result_line('C1, 10, P1, 20, P2') db.session.commit() q = Voting.query.filter(Voting.constituency_id == 1) self.assertEqual(q.count(), 2) self.assertEqual(q.filter(Voting.party_id == 'P1').count(), 1) self.assertEqual(q.filter(Voting.party_id == 'P2').count(), 1) self.assertEqual(q.filter(Voting.party_id == 'P1').first().count, 10) self.assertEqual(q.filter(Voting.party_id == 'P2').first().count, 20)
def test_basic(self): """Basic usage gives sane results.""" # C ends up with 1 seat, L with 3 add_constituency_result_line('A, 10, C, 20, L') add_constituency_result_line('B, 30, C, 40, L') add_constituency_result_line('C, 10, C, 50, L') add_constituency_result_line('D, 20, C, 2, L') add_constituency_result_line('E') p, tot = query.party_totals().filter(Party.id == 'L').one() self.assertEqual(tot, 3) p, tot = query.party_totals().filter(Party.id == 'C').one() self.assertEqual(tot, 1)
def test_replaces_prior_result(self): """Adding a result replaces the previous one.""" add_constituency_result_line('C1, 10, P1, 20, P2') db.session.commit() add_constituency_result_line('C1, 12, P3, 11, P1') db.session.commit() q = Voting.query.filter(Voting.constituency_id == 1) self.assertEqual(q.count(), 2) self.assertEqual(q.filter(Voting.party_id == 'P1').count(), 1) self.assertEqual(q.filter(Voting.party_id == 'P2').count(), 0) self.assertEqual(q.filter(Voting.party_id == 'P3').count(), 1) self.assertEqual(q.filter(Voting.party_id == 'P1').first().count, 11) self.assertEqual(q.filter(Voting.party_id == 'P3').first().count, 12)
def test_creates_constituency(self): """An unknown constituency is created automatically.""" add_constituency_result_line( 'New, and exciting, constituency, 10, P1, 20, P2') db.session.commit() c = Constituency.query.filter( Constituency.name == 'New, and exciting, constituency').first() self.assertIsNot(c, None) q = Voting.query.filter(Voting.constituency == c) self.assertEqual(q.count(), 2) self.assertEqual(q.filter(Voting.party_id == 'P1').count(), 1) self.assertEqual(q.filter(Voting.party_id == 'P2').count(), 1) self.assertEqual(q.filter(Voting.party_id == 'P1').first().count, 10) self.assertEqual(q.filter(Voting.party_id == 'P2').first().count, 20)
def test_winners_correct(self): """Test that the winners are correct.""" add_constituency_result_line('A, 10, C, 20, L') add_constituency_result_line('B') add_constituency_result_line('C, 200, C, 100, L') c, v, max_v, tot_v = (query.constituency_winners().filter( Constituency.name == 'A').one()) self.assertEqual(v.party.id, 'L') self.assertEqual(max_v, 20) self.assertEqual(tot_v, 30) c, v, max_v, tot_v = (query.constituency_winners().filter( Constituency.name == 'B').one()) self.assertIs(v, None) self.assertIs(max_v, None) self.assertIs(tot_v, None) c, v, max_v, tot_v = (query.constituency_winners().filter( Constituency.name == 'C').one()) self.assertEqual(v.party.id, 'C') self.assertEqual(max_v, 200) self.assertEqual(tot_v, 300)
def test_result_for_every_constituency(self): """All constituencies get winners even if no results.""" add_constituency_result_line('A, 10, C, 20, L') add_constituency_result_line('B') self.assertEqual(query.constituency_winners().count(), 2)
def test_multiple_party_fails(self): """Multiple results for one party are not allowed.""" with self.assertRaises(ValueError): add_constituency_result_line('C1, 10, P1, 20, P1')
def test_non_existent_party_fails(self): """An non-existent party is invalid""" with self.assertRaises(ValueError): add_constituency_result_line('C1, 10, NOTEXIST')
def test_empty_constituency_fails(self): """An empty constituency name is invalid""" with self.assertRaises(ValueError): add_constituency_result_line(', 10, P1, 20, P2')
def test_no_results_adds_constituency(self): """A constituency with no results is still added.""" add_constituency_result_line('XXX') db.session.commit() self.assertEqual( Constituency.query.filter(Constituency.name == 'XXX').count(), 1)
def test_export(self): """Can get back a line from export.""" add_constituency_result_line('X, 10, C') r = self.client.get('/export/results') self.assertEqual(r.status_code, 200) self.assertEqual(r.data.decode('utf8').strip(), 'X, 10, C')