Ejemplo n.º 1
0
    def test_election(self):
        """Test the public ``add_candidate'' function provided by the
        Election() class.
        """

        el = Election()

        con1 = Constituency("test")
        con2 = Constituency("test2")

        party1 = Party("party1")
        party2 = Party("party2")

        candidate1 = Candidate("candidate1", party1, con1)
        candidate2 = Candidate("candidate2", party2, con1)
        candidate3 = Candidate("candidate3", party2, con2)

        el.add_candidate(candidate1)
        self.assertTrue(candidate1 in el.candidates)
        self.assertTrue(party1 in el.parties)
        self.assertTrue(con1 in el.constituencies)

        with self.assertRaises(AssertionError):
            el.add_candidate(candidate1)

        el.add_candidate(candidate2)
        self.assertTrue(candidate2 in el.candidates)
        self.assertTrue(party1 in el.parties)
        self.assertTrue(con1 in el.constituencies)
        self.assertEqual(len(el.candidates), 2)
        self.assertEqual(len(el.parties), 2)
        self.assertEqual(len(el.constituencies), 1)

        el.add_candidate(candidate3)
        self.assertTrue(candidate3 in el.candidates)
        self.assertTrue(party2 in el.parties)
        self.assertTrue(con2 in el.constituencies)
        self.assertEqual(len(el.candidates), 3)
        self.assertEqual(len(el.parties), 2)
        self.assertEqual(len(el.constituencies), 2)
Ejemplo n.º 2
0
    def test_election(self):
        """Test the public ``add_candidate'' function provided by the
        Election() class.
        """

        el = Election()

        con1 = Constituency("test")
        con2 = Constituency("test2")

        party1 = Party("party1")
        party2 = Party("party2")

        candidate1 = Candidate("candidate1", party1, con1)
        candidate2 = Candidate("candidate2", party2, con1)
        candidate3 = Candidate("candidate3", party2, con2)

        el.add_candidate(candidate1)
        self.assertTrue(candidate1 in el.candidates)
        self.assertTrue(party1 in el.parties)
        self.assertTrue(con1 in el.constituencies)

        with self.assertRaises(AssertionError):
            el.add_candidate(candidate1)

        el.add_candidate(candidate2)
        self.assertTrue(candidate2 in el.candidates)
        self.assertTrue(party1 in el.parties)
        self.assertTrue(con1 in el.constituencies)
        self.assertEqual(len(el.candidates), 2)
        self.assertEqual(len(el.parties), 2)
        self.assertEqual(len(el.constituencies), 1)

        el.add_candidate(candidate3)
        self.assertTrue(candidate3 in el.candidates)
        self.assertTrue(party2 in el.parties)
        self.assertTrue(con2 in el.constituencies)
        self.assertEqual(len(el.candidates), 3)
        self.assertEqual(len(el.parties), 2)
        self.assertEqual(len(el.constituencies), 2)
def election_from_csv(filename):
    """
    Create an Election object from a csv file of election data.
    """

    election = None

    with open(filename, 'rb') as csvfile:
        reader = csv.reader(csvfile, delimiter=',', quotechar='"')

        # create an Election object
        election = Election()

        constituencies = {}
        parties = {}
        candidates = {}

        for row in reader:
            # extract the fields from the CSV data
            constituencyName = row[0]
            candidateName = row[1]
            partyName = row[2]
            voteCount = int(row[3])

            # reconcile the constituency
            if constituencyName not in constituencies:
                con = Constituency(constituencyName)
                constituencies[constituencyName] = con
            else:
                con = constituencies[constituencyName]

            # reconcile the party
            if partyName not in parties:
                par = Party(partyName)
                parties[partyName] = par
            else:
                par = parties[partyName]

            # reconcile the candidate
            if (candidateName, partyName, constituencyName) \
                    not in candidates:
                can = Candidate(candidateName,
                                par,
                                con,
                                voteCount)
                candidates[(candidateName,
                            partyName,
                            constituencyName)] = can
            else:
                raise ValueError(
                    """A candidate with the same details already exists""")

            # add the candidate to the constituency
            con.add_candidate(can)

            # add the candidate to the party
            par.add_candidate(can)

            # add the candidate to the election
            election.add_candidate(can)

    return election