Skip to content

Commit

Permalink
deal with empty ballots (and test correct behaviour)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Bals authored and jeriox committed Apr 2, 2024
1 parent 7258835 commit 54036d5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion myhpi/polls/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ def calculate_ranking(self):
names[option.pk] = option.name

for ballot in ballots:
current_votes[heapq.heappop(ballot)[1].option.pk].append(ballot)
if ballot:
current_votes[heapq.heappop(ballot)[1].option.pk].append(ballot)

def find_loosers():
threshold = min(map(lambda key: len(current_votes[key]), current_votes.keys()))
Expand Down
7 changes: 7 additions & 0 deletions myhpi/tests/polls/test_ranked_choice_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ def test_no_ballots(self):
[(1, "Alice", 0), (1, "Bob", 0), (1, "Charlie", 0), (1, "Dora", 0)],
)

def test_empty_ballots(self):
self.cast_ballots([[]])
self.assertEqual(
self.poll.calculate_ranking(),
[(1, "Alice", 0), (1, "Bob", 0), (1, "Charlie", 0), (1, "Dora", 0)],
)

def test_fast(self):
self.cast_ballots(([["alice", "bob"]] * 1000) + ([["bob", "alice", "charlie"]] * 900))

Expand Down

0 comments on commit 54036d5

Please sign in to comment.