Exemple #1
0
    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)
Exemple #2
0
    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)
Exemple #3
0
    def test_ballot_measures_extra(self):
        with test_database(test_db, [Race,]):
            data.load_races('data/tests/init_races.json')
            data.load_ballot_measures_extra('data/ballot-measures-extra.csv', quiet=True)

            race = Race.get(Race.race_id == '27456-MO')
            self.assertEqual(race.ballot_measure_description, 'Teacher performance evaluation (Amendment 3)')
Exemple #4
0
    def test_senate_extra(self):
        with test_database(test_db, [Race,]):
            data.load_races('data/tests/init_races.json')
            data.load_senate_extra('data/senate-extra.csv', quiet=True)

            race = Race.get(Race.race_id == '38145-OK')
            self.assertEqual(race.previous_party, 'gop')
Exemple #5
0
    def test_house_extra(self):
        with test_database(test_db, [Race,]):
            data.load_races('data/tests/init_races.json')
            data.load_house_extra('data/house-extra.csv', quiet=True)

            race = Race.get(Race.race_id == '38529-OR')
            self.assertEqual(race.previous_party, 'gop')
            self.assertFalse(race.featured_race)
    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)
Exemple #7
0
    def test_senate_extra(self):
        with test_database(test_db, [
                Race,
        ]):
            data.load_races('data/tests/init_races.json')
            data.load_senate_extra('data/senate-extra.csv', quiet=True)

            race = Race.get(Race.race_id == '38145-OK')
            self.assertEqual(race.previous_party, 'gop')
    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')
Exemple #9
0
    def test_house_extra(self):
        with test_database(test_db, [
                Race,
        ]):
            data.load_races('data/tests/init_races.json')
            data.load_house_extra('data/house-extra.csv', quiet=True)

            race = Race.get(Race.race_id == '38529-OR')
            self.assertEqual(race.previous_party, 'gop')
            self.assertFalse(race.featured_race)
Exemple #10
0
    def test_closing_times(self):
        with test_database(test_db, [Race,]):
            data.load_races('data/tests/init_races.json')
            data.load_closing_times('data/closing-times.csv')

            house_race = Race.get(Race.race_id == '38529-OR')
            self.assertEqual(house_race.poll_closing_time, datetime(2014, 11, 4, 23, 0, 0))

            senate_race = Race.get(Race.race_id == '38145-OK')
            self.assertEqual(senate_race.poll_closing_time, datetime(2014, 11, 4, 20, 0, 0))
    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')
Exemple #12
0
    def test_ballot_measures_extra(self):
        with test_database(test_db, [
                Race,
        ]):
            data.load_races('data/tests/init_races.json')
            data.load_ballot_measures_extra('data/ballot-measures-extra.csv',
                                            quiet=True)

            race = Race.get(Race.race_id == '27456-MO')
            self.assertEqual(race.ballot_measure_description,
                             'Teacher performance evaluation (Amendment 3)')
    def test_is_reporting(self):
        with test_database(test_db, [Race, Candidate], create_tables=True):
            data.load_races('data/tests/init_races.json')
            race = Race.get()

            self.assertFalse(race.is_reporting())

            race.precincts_reporting = 100
            race.save()

            self.assertTrue(race.is_reporting())
Exemple #14
0
    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')
Exemple #15
0
    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) 
Exemple #18
0
    def test_closing_times(self):
        with test_database(test_db, [
                Race,
        ]):
            data.load_races('data/tests/init_races.json')
            data.load_closing_times('data/closing-times.csv')

            house_race = Race.get(Race.race_id == '38529-OR')
            self.assertEqual(house_race.poll_closing_time,
                             datetime(2014, 11, 4, 23, 0, 0))

            senate_race = Race.get(Race.race_id == '38145-OK')
            self.assertEqual(senate_race.poll_closing_time,
                             datetime(2014, 11, 4, 20, 0, 0))
    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')
Exemple #21
0
    def test_load_races(self):
        """
        Test loading races from intermediary file.
        """
        with test_database(test_db, [Race, Candidate], create_tables=True):
            data.load_races('data/tests/init_races.json')

            race = Race.get(Race.race_id == '38529-OR')

            self.assertEqual(race.state_postal, 'OR')
            self.assertEqual(race.office_id, 'H')
            self.assertEqual(race.office_name, 'U.S. House')
            self.assertEqual(race.seat_name, "District 2")
            self.assertEqual(race.seat_number, 2)
            self.assertEqual(race.race_id, '38529-OR')
            self.assertEqual(race.race_type, 'G')
Exemple #22
0
    def test_load_races(self):
        """
        Test loading races from intermediary file.
        """
        with test_database(test_db, [Race, Candidate], create_tables=True):
            data.load_races('data/tests/init_races.json')

            race = Race.get(Race.race_id == '38529-OR')

            self.assertEqual(race.state_postal, 'OR')
            self.assertEqual(race.office_id, 'H')
            self.assertEqual(race.office_name, 'U.S. House')
            self.assertEqual(race.seat_name, "District 2")
            self.assertEqual(race.seat_number, 2)
            self.assertEqual(race.race_id, '38529-OR')
            self.assertEqual(race.race_type, 'G')
    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())
Exemple #24
0
    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)
Exemple #25
0
    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_get_called_time(self):
        with test_database(test_db, [Race, Candidate], create_tables=True):
            data.load_races('data/tests/init_races.json')

            race = Race.get()

            self.assertIsNone(race.get_called_time())

            race.ap_called_time = datetime(2014, 1, 1)
            race.save()

            self.assertEqual(race.get_called_time(), datetime(2014, 1, 1))

            race.accept_ap_call = False
            race.save()

            self.assertIsNone(race.get_called_time())

            race.npr_called_time = datetime(2014, 2, 2)
            race.save()

            self.assertEqual(race.get_called_time(), datetime(2014, 2, 2))
    def test_is_called(self):
        with test_database(test_db, [Race, Candidate], create_tables=True):
            data.load_races('data/tests/init_races.json')

            race = Race.get()

            self.assertFalse(race.is_called())

            race.ap_called = True
            race.save()

            self.assertTrue(race.is_called())

            race.accept_ap_call = False
            race.save()

            self.assertFalse(race.is_called())

            race.npr_called = True
            race.save()
            
            self.assertTrue(race.is_called())
Exemple #28
0
    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)
Exemple #29
0
    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())