def test_update_results(self): with test_database(test_db, [Race, Candidate]): data.load_races('data/tests/init_races.json') data.load_candidates('data/tests/init_candidates.json') data.load_updates('data/tests/update.json') race = Race.get(Race.race_id == '38529-OR') candidate_4848 = Candidate.get(Candidate.candidate_id == '4848-OR') candidate_4642 = Candidate.get(Candidate.candidate_id == '4642-OR') candidate_4979 = Candidate.get(Candidate.candidate_id == '4979-OR') self.assertEqual(race.precincts_reporting, 1970) self.assertEqual(race.precincts_total, 2288) self.assertTrue(race.is_reporting()) self.assertEqual(candidate_4848.vote_count, 150000) self.assertEqual(candidate_4642.vote_count, 200000) self.assertEqual(candidate_4979.vote_count, 250000) self.assertEqual(race.count_votes(), 600000) self.assertEqual(candidate_4848.vote_percent(), Decimal('25.0')) self.assertAlmostEqual(candidate_4642.vote_percent(), Decimal('33.333'), 3) self.assertAlmostEqual(candidate_4979.vote_percent(), Decimal('41.667'), 3) # Results does not call races self.assertFalse(candidate_4848.ap_winner) self.assertFalse(candidate_4642.ap_winner) self.assertFalse(candidate_4979.ap_winner)
def test_count_votes(self): with test_database(test_db, [Race, Candidate], create_tables=True): data.load_races('data/tests/init_races.json') data.load_candidates('data/tests/init_candidates.json') data.load_updates('data/tests/update.json') race = Race.get(Race.race_id == '38529-OR') self.assertTrue(race.is_reporting()) self.assertEqual(race.count_votes(), 600000)
def test_top_choices(self): with test_database(test_db, [Race, Candidate], create_tables=True): data.load_races('data/tests/init_races.json') data.load_candidates('data/tests/init_candidates.json') data.load_updates('data/tests/update.json') race = Race.get(Race.race_id == '27456-MO') top_choices = race.top_choices() self.assertEqual(top_choices[0].last_name, 'Yes') self.assertEqual(top_choices[1].last_name, 'No')
def test_top_candidates(self): with test_database(test_db, [Race, Candidate], create_tables=True): data.load_races('data/tests/init_races.json') data.load_candidates('data/tests/init_candidates.json') data.load_updates('data/tests/update.json') race = Race.get(Race.race_id == '38529-OR') top_candidates = race.top_candidates() self.assertEqual(top_candidates[0].party, 'Dem') self.assertEqual(top_candidates[1].party, 'GOP')
def test_load_candidates(self): with test_database(test_db, [Race, Candidate]): data.load_races('data/tests/init_races.json') data.load_candidates('data/tests/init_candidates.json') candidate = Candidate.select().get() self.assertEqual(candidate.first_name, 'Aelea') self.assertEqual(candidate.last_name, 'Christofferson') self.assertEqual(candidate.party, 'Dem') self.assertIsNotNone(candidate.race) self.assertEqual(candidate.candidate_id, '4848-OR')
def test_precincts_reporting_percent(self): with test_database(test_db, [Race, Candidate], create_tables=True): data.load_races('data/tests/init_races.json') data.load_candidates('data/tests/init_candidates.json') data.load_updates('data/tests/update.json') race = Race.select().get() race.precincts_reporting = 700 race.precincts_total = 1100 race.save() self.assertAlmostEqual(race.precincts_reporting_percent(), Decimal('63.636'), 3)
def test_vote_percent(self): with test_database(test_db, [Race, Candidate], create_tables=True): data.load_races('data/tests/init_races.json') data.load_candidates('data/tests/init_candidates.json') data.load_updates('data/tests/update.json') candidate_4848 = Candidate.get(Candidate.candidate_id == '4848-OR') candidate_4642 = Candidate.get(Candidate.candidate_id == '4642-OR') candidate_4979 = Candidate.get(Candidate.candidate_id == '4979-OR') self.assertEqual(candidate_4848.vote_percent(), Decimal('25.0')) self.assertAlmostEqual(candidate_4642.vote_percent(), Decimal('33.333'), 3) self.assertAlmostEqual(candidate_4979.vote_percent(), Decimal('41.667'), 3)
def test_has_incumbent(self): with test_database(test_db, [Race, Candidate], create_tables=True): data.load_races('data/tests/init_races.json') data.load_candidates('data/tests/init_candidates.json') race = Race.get(Race.race_id == '38529-OR') self.assertFalse(race.has_incumbent()) incumbent = race.candidates.get() incumbent.incumbent = True incumbent.save() self.assertTrue(race.has_incumbent())
def test_get_winning_party(self): with test_database(test_db, [Race, Candidate], create_tables=True): data.load_races('data/tests/init_races.json') data.load_candidates('data/tests/init_candidates.json') race = Race.get(Race.race_id == '38529-OR') self.assertIsNone(race.get_winning_party()) race.ap_called = True race.accept_ap_call = True race.save() winner = race.candidates.select()[0] winner.party = 'GOP' winner.ap_winner = True winner.save() self.assertEqual(race.get_winning_party(), 'gop') winner = race.candidates.select()[0] winner.party = 'Dem' winner.ap_winner = True winner.save() self.assertEqual(race.get_winning_party(), 'dem') winner = race.candidates.select()[0] winner.party = 'Lib' winner.ap_winner = True winner.save() self.assertEqual(race.get_winning_party(), 'other') race.accept_ap_call = False race.save() self.assertIsNone(race.get_winning_party()) race.npr_called = True race.save() npr_winner = race.candidates.select()[1] npr_winner.party = 'GOP' npr_winner.npr_winner = True npr_winner.save() self.assertEqual(race.get_winning_party(), 'gop')
def test_is_winner(self): with test_database(test_db, [Race, Candidate], create_tables=True): data.load_races('data/tests/init_races.json') data.load_candidates('data/tests/init_candidates.json') race = Race.get(Race.race_id == '38529-OR') candidate = race.candidates.get() candidate.ap_winner = True candidate.save() self.assertFalse(race.is_called()) self.assertFalse(candidate.is_winner()) race.ap_called = True race.npr_called = False race.accept_ap_call = True race.save() # Hack so the race updates override the cached FK candidate.race = race self.assertTrue(race.is_called()) self.assertTrue(candidate.is_winner()) candidate.ap_winner = False candidate.save() self.assertTrue(race.is_called()) self.assertFalse(candidate.is_winner()) race.accept_ap_call = False race.save() self.assertFalse(race.is_called()) race.npr_called = True race.save() self.assertTrue(race.is_called()) self.assertFalse(candidate.is_winner()) candidate.npr_winner = True self.assertTrue(candidate.is_winner())
def test_update_calls(self): with test_database(test_db, [Race, Candidate]): data.load_races('data/tests/init_races.json') data.load_candidates('data/tests/init_candidates.json') data.load_calls('data/tests/calls.json') race = Race.get(Race.race_id == '38529-OR') candidate_4848 = Candidate.get(Candidate.candidate_id == '4848-OR') candidate_4642 = Candidate.get(Candidate.candidate_id == '4642-OR') candidate_4979 = Candidate.get(Candidate.candidate_id == '4979-OR') self.assertTrue(race.is_called()) self.assertTrue(race.ap_called) self.assertEqual(race.ap_called_time, datetime(2014, 9, 25, 17, 8, 14)) self.assertEqual(race.get_called_time(), datetime(2014, 9, 25, 17, 8, 14)) self.assertFalse(candidate_4848.ap_winner) self.assertFalse(candidate_4642.ap_winner) self.assertTrue(candidate_4979.ap_winner)
def test_update_times(self): with test_database(test_db, [Race, Candidate]): data.load_races('data/tests/init_races.json') data.load_candidates('data/tests/init_candidates.json') data.load_updates('data/tests/update.json') race = Race.get(Race.race_id == '38529-OR') first_last_updated = race.last_updated data.load_updates('data/tests/update.json') race = Race.get(Race.race_id == '38529-OR') last_updated = race.last_updated self.assertEqual(first_last_updated, last_updated) data.load_updates('data/tests/update-votes.json') race = Race.get(Race.race_id == '38529-OR') last_updated = race.last_updated self.assertEqual(first_last_updated, last_updated) data.load_updates('data/tests/update-precincts.json') race = Race.get(Race.race_id == '38529-OR') last_updated = race.last_updated self.assertEqual(first_last_updated, last_updated)
def test_party_changed(self): with test_database(test_db, [Race, Candidate], create_tables=True): data.load_races('data/tests/init_races.json') data.load_candidates('data/tests/init_candidates.json') race = Race.get() self.assertIsNone(race.party_changed()) race.ap_called = True race.previous_party = 'gop' race.save() winner = race.candidates.select()[0] winner.party = 'Dem' winner.ap_winner = True winner.save() self.assertEqual(race.get_winning_party(), 'dem') self.assertTrue(race.party_changed()) race.previous_party = 'dem' race.save() self.assertFalse(race.party_changed())