|
| 1 | +from os.path import dirname, join |
| 2 | +from unittest import TestCase |
| 3 | +import json |
| 4 | + |
| 5 | +from elex2.election_results import summarize |
| 6 | + |
| 7 | + |
| 8 | +class TestSummaryResults(TestCase): |
| 9 | + |
| 10 | + # Read the results of the parse_and_clean function stored in a test fixture |
| 11 | + json_file = open(join(dirname(__file__), 'sample_results_parsed.json'), 'rb') |
| 12 | + SAMPLE_RESULTS = json.load(json_file) |
| 13 | + # Q: Why aren't we just using the parse_and_clean method instead of |
| 14 | + # using a snapshot of that function's output? |
| 15 | + # A: To achieve better test isolation! |
| 16 | + |
| 17 | + # Q: Why aren't reading in the JSON in a setUp method? |
| 18 | + # A: setUp is called before each test method. This ensures we only |
| 19 | + # incur the overhead of reading in the JSON once. In python2.7 or newer, |
| 20 | + # you should use the setUpClass method instead of a class attribute. |
| 21 | + # http://docs.python.org/2/library/unittest.html#unittest.TestCase.setUpClass |
| 22 | + |
| 23 | + # We will, however, use the setUp method to call the summarize |
| 24 | + # funciton afresh before each of our test methods. |
| 25 | + def setUp(self): |
| 26 | + results = summarize(self.SAMPLE_RESULTS) |
| 27 | + self.race = results['President'] |
| 28 | + |
| 29 | + def test_racewide_vote_total(self): |
| 30 | + "Summary results should be annotated with total votes cast in race" |
| 31 | + self.assertEqual(self.race['all_votes'], 31) |
| 32 | + |
| 33 | + def test_candiate_vote_totals(self): |
| 34 | + "Summary candidates should reflect total votes from all counties" |
| 35 | + # Loop through candidates and find Smith rather than relying on |
| 36 | + # default sorting of candidates, which would make this test brittle |
| 37 | + # the implementation changed. |
| 38 | + smith = [cand for cand in self.race['candidates'] if cand['last_name'] == 'Smith'][0] |
| 39 | + self.assertEqual(smith['votes'], 15) |
| 40 | + |
| 41 | + def test_winner_has_flag(self): |
| 42 | + "Winner flag should be assigned to candidates with most votes" |
| 43 | + doe = [cand for cand in self.race['candidates'] if cand['last_name'] == 'Doe'][0] |
| 44 | + self.assertEqual(doe['winner'], 'X') |
| 45 | + |
| 46 | + def test_loser_has_no_winner_flag(self): |
| 47 | + "Winner flag should be not be assigned to candidate with that does not have highest vote total" |
| 48 | + smith = [cand for cand in self.race['candidates'] if cand['last_name'] == 'Smith'][0] |
| 49 | + self.assertEqual(smith['winner'], '') |
| 50 | + |
| 51 | + |
| 52 | +class TestTieRace(TestCase): |
| 53 | + |
| 54 | + # Q: Why do we need a new class and fixture for this race? |
| 55 | + # A: So that we can change the vote counts so that we have a tie, of course! |
| 56 | + # We don't *need* a new test class, but hey, why not? |
| 57 | + json_file = open(join(dirname(__file__), 'sample_results_parsed_tie_race.json'), 'rb') |
| 58 | + SAMPLE_RESULTS = json.load(json_file) |
| 59 | + |
| 60 | + def test_tie_race_winner_flags(self): |
| 61 | + "Winner flag should not be assigned to any candidate in a tie race" |
| 62 | + pass |
| 63 | + results = summarize(self.SAMPLE_RESULTS) |
| 64 | + race = results['President'] |
| 65 | + for cand in race['candidates']: |
| 66 | + self.assertEqual(cand['winner'], '') |
0 commit comments