Example #1
0
    def test_plurality(self):
        # make sure the shuffled sequence does not lose any elements
        tally_path = os.path.join(self.FIXTURES_PATH, self.PLURALITY)
        result = do_dirtally(tally_path)
        # print(json.dumps(result, indent=4))

        self.assertEqual(result['a'], 'result')
        self.assertEqual(result['total_votes'], 6)
        self.assertEqual(len(result['counts']), 1)

        question = result['counts'][0]
        self.assertEqual(question['tally_type'], Plurality.get_id())
        self.assertEqual(len(question['answers']), 2)
        alice = question['answers'][0]

        self.assertEqual(alice["total_count"], 1)
        self.assertEqual(alice["value"], "Alice")
        self.assertAlmostEqual(alice["total_count_percentage"], 100/3.0, 3)

        bob = question['answers'][1]

        self.assertEqual(bob["total_count"], 2)
        self.assertEqual(bob["value"], "Bob")
        self.assertAlmostEqual(bob["total_count_percentage"], 200/3.0, 3)

        self.assertEqual(len(question["winners"]), 1)
        self.assertEqual(question["winners"][0], "Bob")
        self.assertEqual(question["dirty_votes"], 3)
        self.assertEqual(question["invalid_votes"], 3)
        self.assertEqual(question["total_votes"], 3)
Example #2
0
    def test_hundred_thousand_votes(self):
        print("TEST MEEK-STV HUNDRED THOUSAND VOTES")
        tally_path = os.path.join(self.FIXTURES_PATH +
                                  '/meek-stv-hundred-thousand-votes')

        # We generate 100.000 random votes, where we want 10 candidates to have different weight than the others
        generateVotes(
            {
                4: 50,
                3: 20,
                15: 12,
                17: 5,
                20: 10,
                5: 13,
                8: 18,
                13: 4,
                16: 8,
                22: 11
            }, 100000, 24, tally_path)
        results = do_dirtally(tally_path)
        winners = serialize(results).split('"winners": ')[1]
        #Now we just check if the three candidates with more votes are the winners
        self.assertTrue("Eduardo Bondad" in winners)
        self.assertTrue("Borja Bethencourt" in winners)
        self.assertTrue("Juan Jesus Lopez Aguilar" in winners)
        '''You can check the different votes in the plaintexts_json in meek-stv-hundred-thousand-votes to check that
        the result is randomly generated. It even includes blank votes and invalid votes. The results are generated
        in a file as well if you want to check some statistics or just to see if they're being generated correctly.'''
        _write_file(tally_path + "/results_json", serialize(results))
Example #3
0
    def test_plurality(self):
        # make sure the shuffled sequence does not lose any elements
        tally_path = os.path.join(self.FIXTURES_PATH, self.PLURALITY)
        result = do_dirtally(tally_path)
        # print(json.dumps(result, indent=4))

        self.assertEqual(result['a'], 'result')
        self.assertEqual(result['total_votes'], 6)
        self.assertEqual(len(result['counts']), 1)

        question = result['counts'][0]
        self.assertEqual(question['tally_type'], Plurality.get_id())
        self.assertEqual(len(question['answers']), 2)
        alice = question['answers'][0]

        self.assertEqual(alice["total_count"], 1)
        self.assertEqual(alice["value"], "Alice")
        self.assertAlmostEqual(alice["total_count_percentage"], 100 / 3.0, 3)

        bob = question['answers'][1]

        self.assertEqual(bob["total_count"], 2)
        self.assertEqual(bob["value"], "Bob")
        self.assertAlmostEqual(bob["total_count_percentage"], 200 / 3.0, 3)

        self.assertEqual(len(question["winners"]), 1)
        self.assertEqual(question["winners"][0], "Bob")
        self.assertEqual(question["dirty_votes"], 3)
        self.assertEqual(question["invalid_votes"], 3)
        self.assertEqual(question["total_votes"], 3)
Example #4
0
    def test_borda(self):
        # from the variables passed as arguments, create a folder with the data
        # in a format usable for tests
        tally_path = test.desborda_test.create_desborda_test(test.desborda_test_data.test_desborda_1)
        try:
            results_path = os.path.join(tally_path, "results_json")
            results = do_dirtally(tally_path)
            serialized_results = file_helpers.serialize(results)
            should_results = file_helpers.read_file(results_path)
            # ====================================================== #
            #file_helpers.write_file('/agora/test/napas/shouldresults_json', should_results)
            #file_helpers.write_file('/agora/test/napas/results_json', serialized_results)

            #copied_results = copy.deepcopy(results['questions'][0]['answers'])
            #sorted_results = sorted(copied_results, key = lambda x: 62 if x['winner_position'] is None else x['winner_position'])
            #test_out = ""
            #for answer in sorted_results:
                #test_out += "%s, %i\n" % (answer['text'], answer['total_count'])
            #file_helpers.write_file('/agora/test/napas/test_out', test_out)
            # ====================================================== #
            self.assertEqual(serialized_results, should_results)
            # remove the temp test folder also in a successful test
            file_helpers.remove_tree(tally_path)
        except:
            # if there was an error, recover from the exception at least to 
            # remove the previously created temp folder for the test
            file_helpers.remove_tree(tally_path)
            raise
Example #5
0
    def test_borda(self):
        # from the variables passed as arguments, create a folder with the data
        # in a format usable for tests
        tally_path = test.desborda_test.create_desborda_test(
            test.desborda_test_data.test_desborda_1)
        try:
            results_path = os.path.join(tally_path, "results_json")
            results = do_dirtally(tally_path)
            serialized_results = file_helpers.serialize(results)
            should_results = file_helpers.read_file(results_path)
            # ====================================================== #
            #file_helpers.write_file('/agora/test/napas/shouldresults_json', should_results)
            #file_helpers.write_file('/agora/test/napas/results_json', serialized_results)

            #copied_results = copy.deepcopy(results['questions'][0]['answers'])
            #sorted_results = sorted(copied_results, key = lambda x: 62 if x['winner_position'] is None else x['winner_position'])
            #test_out = ""
            #for answer in sorted_results:
            #test_out += "%s, %i\n" % (answer['text'], answer['total_count'])
            #file_helpers.write_file('/agora/test/napas/test_out', test_out)
            # ====================================================== #
            self.assertEqual(serialized_results, should_results)
            # remove the temp test folder also in a successful test
            file_helpers.remove_tree(tally_path)
        except:
            # if there was an error, recover from the exception at least to
            # remove the previously created temp folder for the test
            file_helpers.remove_tree(tally_path)
            raise
Example #6
0
 def _test_method(self, dirname):
     '''
     Generic method to do a tally
     '''
     tally_path = os.path.join(self.FIXTURES_PATH, dirname)
     results_path = os.path.join(tally_path, "results_json")
     results = do_dirtally(tally_path)
     should_results = _read_file(results_path)
     self.assertEqual(serialize(results), should_results)
     return results
Example #7
0
 def _test_method(self, dirname):
     '''
     Generic method to do a tally
     '''
     tally_path = os.path.join(self.FIXTURES_PATH, dirname)
     results_path = os.path.join(tally_path, "results_json")
     results = do_dirtally(tally_path)
     should_results = _read_file(results_path)
     self.assertEqual(serialize(results), should_results)
     return results
Example #8
0
    def test_meek_stv(self):
        # make sure the shuffled sequence does not lose any elements
        tally_path = os.path.join(self.FIXTURES_PATH, self.MEEK_STV)
        result = do_dirtally(tally_path)
        # print(json.dumps(result, indent=4))

        self.assertEqual(result['a'], 'result')
        self.assertEqual(result['total_votes'], 7)

        question = result['counts'][0]
        self.assertEqual(question["winners"], ["Edward", "Bob"])
        self.assertEqual(question["invalid_votes"], 3)
        self.assertEqual(question["total_votes"], 4)
Example #9
0
    def _test_method(self, dirname):
        """Agora-Voting:  Generic method to do a tally
        SOD: this method compares the results of applies do_dirtally method to a specific questions_json (in the
        path determined from tally_path) with the results save in the results_json file and, if they are the same
        result, returns one of them (in the method, it returns the variable called results but it could be
        should_results too)"""

        tally_path = os.path.join(self.FIXTURES_PATH, dirname)
        results = do_dirtally(tally_path)
        results_path = os.path.join(tally_path, "results_json")
        should_results = _read_file(results_path)
        self.assertEqual(serialize(results), should_results)
        return results
Example #10
0
    def _test_method(self, dirname):
        """Agora-Voting:  Generic method to do a tally
        SOD: this method compares the results of applies do_dirtally method to a specific questions_json (in the
        path determined from tally_path) with the results save in the results_json file and, if they are the same
        result, returns one of them (in the method, it returns the variable called results but it could be
        should_results too)"""

        tally_path = os.path.join(self.FIXTURES_PATH, dirname)
        results = do_dirtally(tally_path)
        results_path = os.path.join(tally_path, "results_json")
        should_results = _read_file(results_path)
        self.assertEqual(serialize(results), should_results)
        return results
Example #11
0
    def test_meek_stv(self):
        # make sure the shuffled sequence does not lose any elements
        tally_path = os.path.join(self.FIXTURES_PATH, self.MEEK_STV)
        result = do_dirtally(tally_path)
        # print(json.dumps(result, indent=4))

        self.assertEqual(result['a'], 'result')
        self.assertEqual(result['total_votes'], 7)

        question = result['counts'][0]
        self.assertEqual(question["winners"], ["Edward", "Bob"])
        self.assertEqual(question["invalid_votes"], 3)
        self.assertEqual(question["total_votes"], 4)
Example #12
0
    def test_approval(self):
        # make sure the shuffled sequence does not lose any elements
        tally_path = os.path.join(self.FIXTURES_PATH, self.APPROVAL)
        result = do_dirtally(tally_path)
        # print(json.dumps(result, indent=4))

        self.assertEqual(result['a'], 'result')
        self.assertEqual(result['total_votes'], 7)

        question = result['counts'][0]
        # because plurality at large is not a preferential vote, the results are reversed wrt. to stv
        self.assertEqual(question["winners"], ["Bob", "Edward"])
        self.assertEqual(question["invalid_votes"], 3)
        self.assertEqual(question["total_votes"], 4)
Example #13
0
    def test_approval(self):
        # make sure the shuffled sequence does not lose any elements
        tally_path = os.path.join(self.FIXTURES_PATH, self.APPROVAL)
        result = do_dirtally(tally_path)
        # print(json.dumps(result, indent=4))

        self.assertEqual(result['a'], 'result')
        self.assertEqual(result['total_votes'], 7)

        question = result['counts'][0]
        # because plurality at large is not a preferential vote, the results are reversed wrt. to stv
        self.assertEqual(question["winners"], ["Bob", "Edward"])
        self.assertEqual(question["invalid_votes"], 3)
        self.assertEqual(question["total_votes"], 4)
Example #14
0
    def test_many_votes(self):
        print("TEST MEEK-STV MANY VOTES")
        tally_path = os.path.join(self.FIXTURES_PATH + '/meek-stv-manyVotes')

        # We generate 5238 random votes, where we want 3 candidates to have different weight than the others
        generateVotes({4: 50, 3: 20, 15: 12}, 5238, 24, tally_path)
        results = do_dirtally(tally_path)
        winners = serialize(results).split('"winners": ')[1]
        #Now we just check if the three candidates with more votes are the winners
        self.assertTrue("Eduardo Bondad" in winners)
        self.assertTrue("Juan Jesus Lopez Aguilar" in winners)
        self.assertTrue("Javier" in winners)
        '''You can check the different votes in the plaintexts_json in meek-stv-manyVotes to check that the result is
        randomly generated. It even includes blank votes and invalid votes. The results are generated in a file as well
        if you want to check some statistics or just to see if they're being generated correctly.'''
        _write_file(tally_path + "/results_json", serialize(results))
Example #15
0
    def test_many_votes(self):

        tally_path = os.path.join(self.FIXTURES_PATH + "/meek-stv-manyVotes")

        # We generate 5238 random votes, where we want 3 candidates to have different weight than the others
        generateVotes({4: 50, 3: 20, 15: 12}, 5238, 25, tally_path)
        results = do_dirtally(tally_path)
        winners = serialize(results).split('"winners": ')[1]
        # Now we just check if the three candidates with more votes are the winners
        self.assertTrue("Eduardo Bondad" in winners)
        self.assertTrue("Juan Jesus Lopez Aguilar" in winners)
        self.assertTrue("Javier" in winners)
        """You can check the different votes in the plaintexts_json in meek-stv-manyVotes to check that the result is
        randomly generated. It even includes blank votes and invalid votes. The results are generated in a file as well
        if you want to check some statistics or just to see if they're being generated correctly."""
        _write_file(tally_path + "/results_json", serialize(results))
Example #16
0
    def test_hundred_thousand_votes(self):
        print("TEST MEEK-STV HUNDRED THOUSAND VOTES")
        tally_path = os.path.join(self.FIXTURES_PATH+'/meek-stv-hundred-thousand-votes')

        # We generate 100.000 random votes, where we want 10 candidates to have different weight than the others
        generateVotes({4:50, 3:20, 15:12, 17:5, 20:10, 5:13, 8:18, 13:4, 16:8, 22:11}, 100000, 24, tally_path)
        results = do_dirtally(tally_path)
        winners = serialize(results).split('"winners": ')[1]
        #Now we just check if the three candidates with more votes are the winners
        self.assertTrue("Eduardo Bondad" in winners)
        self.assertTrue("Borja Bethencourt" in winners)
        self.assertTrue("Juan Jesus Lopez Aguilar" in winners)
        '''You can check the different votes in the plaintexts_json in meek-stv-hundred-thousand-votes to check that
        the result is randomly generated. It even includes blank votes and invalid votes. The results are generated
        in a file as well if you want to check some statistics or just to see if they're being generated correctly.'''
        _write_file(tally_path+"/results_json", serialize(results))
Example #17
0
 def test_desborda_blank_invalid(self):
     # from the variables passed as arguments, create a folder with the data
     # in a format usable for tests
     tally_path = test.desborda_test.create_desborda_test(test.desborda_test_data.test_desborda_2)
     try:
         results_path = os.path.join(tally_path, "results_json")
         results = do_dirtally(tally_path)
         serialized_results = file_helpers.serialize(results)
         should_results = file_helpers.read_file(results_path)
         self.assertEqual(serialized_results, should_results)
         # remove the temp test folder also in a successful test
         file_helpers.remove_tree(tally_path)
     except:
         # if there was an error, recover from the exception at least to 
         # remove the previously created temp folder for the test
         file_helpers.remove_tree(tally_path)
         raise
Example #18
0
 def test_meek_stvMultipleQuestions(self):
     print("TEST MEEK-STV MULTIPLE QUESTIONS")
     tally_path = os.path.join(self.FIXTURES_PATH+'/meek-stv-multipleQuestions')
     results = do_dirtally(tally_path)
     index = 0
     for question in results['questions']:
         varQuestion = question['question']
         varWinners = question['winners']
         varBlankVotes = question['totals']['blank_votes']
         print("Question: " + varQuestion)
         print("Blank Votes: " + str(varBlankVotes))
         print("Winners:")
         i = 1
         for winner in varWinners:
             stringI = str(i)
             print(stringI + ". " + winner)
             i = i+1
     print("----------------")
Example #19
0
 def test_desborda_blank_invalid(self):
     # from the variables passed as arguments, create a folder with the data
     # in a format usable for tests
     tally_path = test.desborda_test.create_desborda_test(
         test.desborda_test_data.test_desborda_2)
     try:
         results_path = os.path.join(tally_path, "results_json")
         results = do_dirtally(tally_path)
         serialized_results = file_helpers.serialize(results)
         should_results = file_helpers.read_file(results_path)
         self.assertEqual(serialized_results, should_results)
         # remove the temp test folder also in a successful test
         file_helpers.remove_tree(tally_path)
     except:
         # if there was an error, recover from the exception at least to
         # remove the previously created temp folder for the test
         file_helpers.remove_tree(tally_path)
         raise
Example #20
0
 def test_meek_stvMultipleQuestions(self):
     print("TEST MEEK-STV MULTIPLE QUESTIONS")
     tally_path = os.path.join(self.FIXTURES_PATH +
                               '/meek-stv-multipleQuestions')
     results = do_dirtally(tally_path)
     index = 0
     for question in results['questions']:
         varQuestion = question['question']
         varWinners = question['winners']
         varBlankVotes = question['totals']['blank_votes']
         print("Question: " + varQuestion)
         print("Blank Votes: " + str(varBlankVotes))
         print("Winners:")
         i = 1
         for winner in varWinners:
             stringI = str(i)
             print(stringI + ". " + winner)
             i = i + 1
     print("----------------")
Example #21
0
    def test_invalid_votesMeekstv(self):

        tally_path = os.path.join(self.FIXTURES_PATH + "/meek-stv-invalidVotes")
        results = do_dirtally(tally_path)
        self.assertTrue('"null_votes": 2,' in serialize(results))
Example #22
0
 def test_invalid_votesMeekstv(self):
     print("TEST MEEK-STV INVALID VOTES")
     tally_path = os.path.join(self.FIXTURES_PATH+'/meek-stv-invalidVotes')
     results = do_dirtally(tally_path)
     self.assertTrue('"null_votes": 2,' in serialize(results))
Example #23
0
 def test_blank_votesMeekstv(self):
     print("TEST MEEK-STV BLANK VOTES")
     tally_path = os.path.join(self.FIXTURES_PATH + '/meek-stv-blankVotes')
     results = do_dirtally(tally_path)
     self.assertTrue('"blank_votes": 1,' in serialize(results))
Example #24
0
 def test_invalid_votesMeekstv(self):
     print("TEST MEEK-STV INVALID VOTES")
     tally_path = os.path.join(self.FIXTURES_PATH +
                               '/meek-stv-invalidVotes')
     results = do_dirtally(tally_path)
     self.assertTrue('"null_votes": 2,' in serialize(results))
Example #25
0
    def test_blank_votesMeekstv(self):

        tally_path = os.path.join(self.FIXTURES_PATH+'/meek-stv-blankVotes')
        results = do_dirtally(tally_path)
        self.assertTrue('"blank_votes": 1,' in serialize(results))