|
| 1 | +from unittest.mock import MagicMock, patch |
| 2 | +from electionguard.tally import PlaintextTallySelection |
| 3 | +from electionguard_gui.services.plaintext_ballot_service import _get_contest_details |
| 4 | +from tests.base_test_case import BaseTestCase |
| 5 | + |
| 6 | + |
| 7 | +class TestPlaintextBallotService(BaseTestCase): |
| 8 | + """Test the ElectionDto class""" |
| 9 | + |
| 10 | + def test_zero_sections(self) -> None: |
| 11 | + # ARRANGE |
| 12 | + selections: list[PlaintextTallySelection] = [] |
| 13 | + selection_names: dict[str, str] = {} |
| 14 | + selection_write_ins: dict[str, bool] = {} |
| 15 | + parties: dict[str, str] = {} |
| 16 | + |
| 17 | + # ACT |
| 18 | + result = _get_contest_details( |
| 19 | + selections, selection_names, selection_write_ins, parties |
| 20 | + ) |
| 21 | + |
| 22 | + # ASSERT |
| 23 | + self.assertEqual(0, result["nonWriteInTotal"]) |
| 24 | + self.assertEqual(None, result["writeInTotal"]) |
| 25 | + self.assertEqual(0, len(result["selections"])) |
| 26 | + |
| 27 | + @patch("electionguard.tally.PlaintextTallySelection") |
| 28 | + def test_one_non_write_in(self, plaintext_tally_selection: MagicMock) -> None: |
| 29 | + # ARRANGE |
| 30 | + plaintext_tally_selection.object_id = "AL" |
| 31 | + plaintext_tally_selection.tally = 2 |
| 32 | + selections: list[PlaintextTallySelection] = [plaintext_tally_selection] |
| 33 | + selection_names: dict[str, str] = { |
| 34 | + "AL": "Abraham Lincoln", |
| 35 | + } |
| 36 | + selection_write_ins: dict[str, bool] = { |
| 37 | + "AL": False, |
| 38 | + } |
| 39 | + parties: dict[str, str] = { |
| 40 | + "AL": "National Union Party", |
| 41 | + } |
| 42 | + |
| 43 | + # ACT |
| 44 | + result = _get_contest_details( |
| 45 | + selections, selection_names, selection_write_ins, parties |
| 46 | + ) |
| 47 | + |
| 48 | + # ASSERT |
| 49 | + self.assertEqual(2, result["nonWriteInTotal"]) |
| 50 | + self.assertEqual(None, result["writeInTotal"]) |
| 51 | + self.assertEqual(1, len(result["selections"])) |
| 52 | + selection = result["selections"][0] |
| 53 | + self.assertEqual("Abraham Lincoln", selection["name"]) |
| 54 | + self.assertEqual(2, selection["tally"]) |
| 55 | + self.assertEqual("National Union Party", selection["party"]) |
| 56 | + self.assertEqual(1, selection["percent"]) |
| 57 | + |
| 58 | + @patch("electionguard.tally.PlaintextTallySelection") |
| 59 | + def test_one_write_in(self, plaintext_tally_selection: MagicMock) -> None: |
| 60 | + # ARRANGE |
| 61 | + plaintext_tally_selection.object_id = "ST" |
| 62 | + plaintext_tally_selection.tally = 1 |
| 63 | + selections: list[PlaintextTallySelection] = [plaintext_tally_selection] |
| 64 | + selection_names: dict[str, str] = {} |
| 65 | + selection_write_ins: dict[str, bool] = { |
| 66 | + "ST": True, |
| 67 | + } |
| 68 | + parties: dict[str, str] = {} |
| 69 | + |
| 70 | + # ACT |
| 71 | + result = _get_contest_details( |
| 72 | + selections, selection_names, selection_write_ins, parties |
| 73 | + ) |
| 74 | + |
| 75 | + # ASSERT |
| 76 | + self.assertEqual(0, result["nonWriteInTotal"]) |
| 77 | + self.assertEqual(1, result["writeInTotal"]) |
| 78 | + self.assertEqual(0, len(result["selections"])) |
| 79 | + |
| 80 | + @patch("electionguard.tally.PlaintextTallySelection") |
| 81 | + def test_zero_write_in(self, plaintext_tally_selection: MagicMock) -> None: |
| 82 | + # ARRANGE |
| 83 | + plaintext_tally_selection.object_id = "ST" |
| 84 | + plaintext_tally_selection.tally = 0 |
| 85 | + selections: list[PlaintextTallySelection] = [plaintext_tally_selection] |
| 86 | + selection_names: dict[str, str] = {} |
| 87 | + selection_write_ins: dict[str, bool] = { |
| 88 | + "ST": True, |
| 89 | + } |
| 90 | + parties: dict[str, str] = {} |
| 91 | + |
| 92 | + # ACT |
| 93 | + result = _get_contest_details( |
| 94 | + selections, selection_names, selection_write_ins, parties |
| 95 | + ) |
| 96 | + |
| 97 | + # ASSERT |
| 98 | + self.assertEqual(0, result["nonWriteInTotal"]) |
| 99 | + self.assertEqual(0, result["writeInTotal"]) |
| 100 | + self.assertEqual(0, len(result["selections"])) |
0 commit comments