import unittest from arrow import get as arrow_get, Arrow from voting import Quorum, Vote, Voting, Result voters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] class TestVoter(unittest.TestCase): def setUp(self) -> None: self.voting = Voting(title="Test", quorum=Quorum(), voters=voters) def test_fail_with_no_voters(self): self.assertRaises(RuntimeError, Voting, title="Test", quorum=Quorum(), voters=[],) def test_one_voter(self): Voting(title="Test", quorum=Quorum(), voters=['a']) def test_vote_before_end(self): self.voting.vote(voter='a', vote=Vote.JA) def test_vote_after_end(self): self.voting.days = 0 self.assertRaises(RuntimeError, self.voting.vote, voter='a', vote=Vote.JA) def test_unknown_voter(self): self.assertRaises(ValueError, self.voting.vote, voter='z', vote=Vote.JA) def test_known_voter(self): self.voting.vote('a', Vote.JA) class TestResult(unittest.TestCase): def setUp(self) -> None: self.timestamp = str(Arrow.utcnow()) self.voting = Voting(title="Test", quorum=Quorum(), voters=voters) self.voting.start = self.timestamp self.result = Result(self.voting) def test_result_data(self): res = self.result.result() assert res['state'] == "LAUFEND" assert res['voting']['title'] == 'Test' assert res['voting']['quorum']['kind'] == 'NONE' assert res['voting']['quorum']['value'] == 0 assert res['voting']['voters'] == voters assert res['voting']['start'] == self.timestamp assert res['voting']['days'] == 7 assert res['voting']['votes'] == {} assert res['votes'] == {'JA': 0, 'NEIN': 0, 'ENTHALTUNG': 0} assert res['quorum_reached'] == True def test_result_data_with_votes(self): self.voting.vote(voter='a', vote=Vote.JA) res = self.result.result() assert res['voting']['votes'] == {'a': 'JA'} assert res['votes'] == {'JA': 1, 'NEIN': 0, 'ENTHALTUNG': 0} def test_result_data_with_outcome_accept(self): self.voting.vote(voter='a', vote=Vote.JA) self.voting.days = 0 res = self.result.result() assert res['state'] == 'ANGENOMMEN' def test_result_data_with_outcome_declined(self): self.voting.vote(voter='a', vote=Vote.NEIN) self.voting.days = 0 res = self.result.result() assert res['state'] == 'ABGELEHNT' if __name__ == '__main__': unittest.main()