Exemple #1
0
    def perform_count(self, candidate_ids, votes_as_json, max_grade=4):
        """
        votes_as_json is a list of tuples, each one being an individual users votes.
        And each vote is a tuple with a numeric grade for each candidate, in the natural
        order of the candidates.
        """
        aggregator = VoteAggregator(candidate_ids, max_grade + 1)
        aggregated_votes = aggregator.aggregate(votes_as_json)

        scheme = MajorityJudgementCount()
        succeeded, result = scheme.sort_candidates(aggregated_votes)
        return (
            succeeded,
            { x[0]: self._candidate_dict(x[1], n) for n, x in enumerate(result) },
            (result[0][0],)
        )
Exemple #2
0
    def test_complex_tie_breaker(self):
        rioja = ('Rioja', (2, 2, 6, 1, 2))  #PPAAGGGGGGVEE, PPEE
        bordeaux = ('Bordeaux', (2, 2, 6, 2, 1))  #PPAAGGGGGGVVE, PPVE
        tempranillo = ('Tempranillo', (1, 3, 6, 2, 1))  #PAAAGGGGGGVVE, PAVE

        input_data = (rioja, bordeaux, tempranillo)
        expected = (tempranillo, rioja, bordeaux)

        success, actual_output = MajorityJudgementCount().sort_candidates(
            input_data)
        self.assertEqual(expected, actual_output)
        self.assertTrue(success)
Exemple #3
0
    def test_simple_tie_breaker(self):
        pizza = ('Pizza', (2, 2, 1))  #PPAAG, PPAG, PAG
        burger = ('Burger', (0, 3, 2))  #AAAGG, AAGG, AGG
        veggie = ('Veggie', (2, 1, 2))  #PPAGG, PPGG, PGG

        input_data = (pizza, burger, veggie)
        expected_output = (burger, veggie, pizza)

        success, actual_output = MajorityJudgementCount().sort_candidates(
            input_data)
        self.assertEqual(expected_output, actual_output)
        self.assertTrue(success)
Exemple #4
0
    def test_two_identical_losers(self):
        castle = ('Castle', (2, 2, 1))  #PPAAG
        fort = ('Fort', (0, 0, 5))  #GGGGG
        country_house = ('Country house', (2, 2, 1))  #PPAAG

        input_data = (castle, fort, country_house)
        expected_output = (fort, castle, country_house)

        actual_output = MajorityJudgementCount().sort_candidates(input_data)
        self.fail(
            "TODO: Sort this test and implementation out - we need to do something better when there are two losers"
        )
Exemple #5
0
    def test_basic_small_case(self):
        pizza = ('Pizza', (1, 1, 3))
        burger = ('Burger', (3, 0, 2))
        veggie = ('Veggie', (2, 2, 1))

        input_data = (pizza, burger, veggie)

        expected_output = (pizza, veggie, burger)

        success, actual_output = MajorityJudgementCount().sort_candidates(
            input_data)
        self.assertEqual(expected_output, actual_output)
        self.assertTrue(success)
Exemple #6
0
    def test_basic_larger_case(self):
        chinese = ('Chinese', (12, 40, 6, 8, 7, 26))
        pizza = ('Pizza', (2, 10, 21, 4, 17, 45))
        indian = ('Indian', (10, 4, 33, 33, 2, 17))
        burger = ('Burger', (6, 12, 40, 26, 7, 8))

        input_data = (chinese, pizza, indian, burger)

        expected_output = (pizza, indian, burger, chinese)

        success, actual_output = MajorityJudgementCount().sort_candidates(
            input_data)
        self.assertEqual(expected_output, actual_output)
        self.assertTrue(success)
Exemple #7
0
    def test_incomplete_vote(self):
        """
        We are expecting the input data to have votes for each of the candidates
        (i.e. any padding with 0 or defaulting to 'reject' should happen before
        it gets to us. If the data is not complete in this way, we raise an
        IncompleteVoteError.
        """
        pizza = ('Pizza', (3, 1, 1))
        burger = ('Burger', (2, 0))
        veggie = ('Veggie', (1, 2, 2))

        input_data = (pizza, burger, veggie)

        with self.assertRaises(IncompleteVoteError):
            MajorityJudgementCount().sort_candidates(input_data)
Exemple #8
0
    def test_two_identical_winners(self):
        """
        When the two highest ranked candidates have identical vote
        distributions we cannot pick a winner and an exception should be
        raised.
        """
        smith = ('L. Smith', (2, 7, 5, 8, 3))
        jones = ('Q. Jones', (11, 9, 2, 3, 0))
        rogers = ('P. Rogers', (2, 7, 5, 8, 3))

        input_data = (smith, jones, rogers)

        success, actual_output = MajorityJudgementCount().sort_candidates(
            input_data)
        self.assertIn(actual_output[0], [smith, rogers])
        self.assertIn(actual_output[1], [smith, rogers])
        self.assertEqual(actual_output[2], jones)
        self.assertFalse(success)
Exemple #9
0
    def test_even_number_of_voters_different_medians(self):
        """
        In this case, there are 6 voters.

        If we used the 4th item for the median, result order would be red, blue, green.
        But, if we use the 3th item for the median, result would be   blue, red, green.

        The second is the *correct* way.
        """
        red = ('Red party', (3, 0, 3))  #PPPGGG
        blue = ('Blue party', (2, 2, 2))  #PPAAGG
        green = ('Green party', (4, 0, 2))  #PPPPGG

        input_data = (red, blue, green)
        expected_output = (blue, red, green)

        success, actual_output = MajorityJudgementCount().sort_candidates(
            input_data)
        self.assertEqual(expected_output, actual_output)
        self.assertTrue(success)