-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_bulk_random_election.py
52 lines (42 loc) · 1.99 KB
/
test_bulk_random_election.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Import Unit Test & Candidate Class
import unittest
from SVT.Election import Election, Result
from SVT.Candidate import Candidate
from SVT.Ballot import Ballot
import sys
import os
import random
import inspect
# Used to fix path issues
sys.path.append(os.getcwd())
class TestBulkElection(unittest.TestCase):
def test_bulk_random(self):
''' Test a set of random ballots for a 10 candidate election. '''
# Initializes test election
election_name = "Test Election"
test_election = Election(election_name)
# Creates lists for generating random candidates.
first_names = ["Alice", "Bob", "Charlie", "David", "Eva", "Frank", "Grace", "Henry", "Isabella", "Jack"]
last_names = ["Smith", "Johnson", "Williams", "Jones", "Brown", "Garcia", "Miller", "Davis", "Rodriguez", "Martinez"]
foods = ["pizza", "sushi", "pasta", "burger", "tacos", "ramen", "steak", "salad", "curry", "seafood"]
# Creates 10 random candidates.
for i in range(10):
first_name = random.choice(first_names)
last_name = random.choice(last_names)
full_name = f"{first_name} {last_name}"
favorite_food = random.choice(foods)
statement = f"My favorite food is {favorite_food}!"
person = {"name": full_name, "statement": statement}
test_election.add_candidate(Candidate(person["name"], person["statement"], "https://github.com/jaickler/SVT"))
# Creates and casts 1000 ballots with random scores for each candidate.
for i in range(0, 1000):
temp_score:list[int] = []
for j in range(0,10):
temp_score.append(random.randint(0,5))
test_election.cast_ballot(Ballot(temp_score))
# Tallies the votes.
test_result = test_election.tally_votes()
# Checks that the tally_votes function returns a valid Result object
self.assertTrue(inspect.isclass(type(test_result)))
if __name__ == '__main__':
unittest.main()