Example #1
0
class TestElectionCandidateLogic(unittest.TestCase):
    def setUp(self):
        self.election = ElectionLogic()
        self.candidate = CandidateLogic()
        self.election_can = ElectionCandidateLogic()
        self.model_candidate1 = self.candidate.add(CandidateModel(None, 'Peter', 'URL', 'Vote for me'))
        self.model_candidate2 = self.candidate.add(CandidateModel(None, 'Gigi', 'URL', 'I rock'))
        self.model_election = self.election.add(ElectionModel(None, 'Election test'))
        self.model_election_can = self.election_can.add(
            ElectionCandidateModel(None, self.model_candidate1.id, self.model_election.id))
        self.model_election_can2 = self.election_can.add(ElectionCandidateModel(
            None, self.model_candidate2.id, self.model_election.id))

    def test_get_candidates_in_poll(self):
        candidates_in_poll = self.election_can.get_candidates_in_election(self.model_election.id)
        self.assertEqual(type(candidates_in_poll), list)
        id_lst = []
        for candidate in candidates_in_poll:
            id_lst.append(candidate.id)

        if self.model_candidate1.id not in id_lst:
            self.assertEqual(True, False)
        if self.model_candidate2.id not in id_lst:
            self.assertEqual(True, False)

    def test_get_error_candidates_in_poll(self):
        candidates_in_poll = self.election_can.get_candidates_in_election(0)
        self.assertEqual(candidates_in_poll, [])

    def tearDown(self):
        self.candidate.delete(self.model_candidate1.id)
        self.candidate.delete(self.model_candidate2.id)
        self.election.delete(self.model_election.id)
        self.election_can.delete(self.model_election_can.id)
        self.election_can.delete(self.model_election_can2.id)
Example #2
0
class TestCandidateLogic(unittest.TestCase):
    def setUp(self):
        self.candidate = CandidateLogic()
        self.model1 = CandidateModel(None, 'Peter', 'URL', 'Vote for me')
        self.model2 = CandidateModel(None, 'John', 'URL', 'I am the greatest')
        self.candidate.add(self.model1)
        self.candidate.add(self.model2)

    def test_get_candidates(self):
        candidates = self.candidate.get_candidates()
        self.assertEqual(type(candidates), list)
        self.assertGreaterEqual(len(candidates), 2)

    def test_get_candidate_by_id(self):
        candidate = self.candidate.get_candidate_by_id(self.model1.id)
        self.assertEqual(candidate, self.candidate.jsonify(self.model1))
        self.assertNotEqual(candidate, self.candidate.jsonify(self.model2))

    def test_get_error_candidate_by_id_(self):
        candidate = self.candidate.get_candidate_by_id(850000)
        self.assertEqual(candidate, None)

    def test_get_candidate_by_name(self):
        candidate = self.candidate.get_candidate_by_name("John")
        self.assertEqual(candidate, self.candidate.jsonify(self.model2))
        self.assertNotEqual(candidate, self.candidate.jsonify(self.model1))

    def test_get_error_candidate_by_name(self):
        candidate = self.candidate.get_candidate_by_name("")
        self.assertEqual(candidate, None)

    def tearDown(self):
        self.candidate.delete(self.model1.id)
        self.candidate.delete(self.model2.id)
Example #3
0
class TestElectionLogic(unittest.TestCase):
    def setUp(self):
        self.election_logic = ElectionLogic()
        self.candidate = CandidateLogic()
        self.election_can = ElectionCandidateLogic()
        self.election_data = ElectionData()
        self.model_candidate1 = CandidateModel(None, 'Peter', 'URL', 'Vote for me')
        self.model_candidate2 = CandidateModel(None, 'Gigi', 'URL', 'I rock')
        self.model_election = ElectionModel(None, 'Election test')
        self.election_logic.add(self.model_election)
        self.candidate.add(self.model_candidate1)
        self.candidate.add(self.model_candidate2)
        self.model_election_can = ElectionCandidateModel(None, self.model_candidate1.id, self.model_election.id)
        self.model_election_can2 = ElectionCandidateModel(None, self.model_candidate2.id, self.model_election.id)
        self.election_can.add(self.model_election_can)
        self.election_can.add(self.model_election_can2)
        self.model_candidate1_jsonify = self.candidate.jsonify(self.model_candidate1)
        self.model_candidate2_jsonify = self.candidate.jsonify(self.model_candidate2)

    def tearDown(self):
        self.candidate.delete(self.model_candidate1.id)
        self.candidate.delete(self.model_candidate2.id)
        self.election_logic.delete(self.model_election.id)
        self.election_can.delete(self.model_election_can.id)
        self.election_can.delete(self.model_candidate2.id)

    def test_get_election_by_id(self):
        election = self.election_logic.get_election(self.model_election.id)
        self.assertEqual(election, self.election_logic.jsonify(self.model_election))
        self.assertEqual(type(election), dict)

    def test_get_error_election(self):
        election = self.election_logic.get_election(999)
        self.assertEqual(election, None)
        election = self.election_logic.get_election('string')
        self.assertEqual(election, None)

    def test_get_all_elections(self):
        elections = self.election_logic.get_elections()
        self.assertEqual(type(elections), list)
        self.assertGreaterEqual(len(elections), 1)
        for x in elections:
            self.assertEqual(type(x), dict)

    def test_get_election_by_name(self):
        election = self.election_logic.get_election_by_name("Election test")
        self.assertEqual(type(election), dict)
        self.assertEqual(election, self.election_logic.jsonify(self.model_election))
        self.assertNotEqual(election, None)

    def test_get_error_election_by_name(self):
        election = self.election_logic.get_election_by_name("")
        self.assertEqual(election, None)
        self.assertNotEqual(type(election), dict)
        self.assertNotEqual(election, self.election_logic.jsonify(self.model_election))
        election = self.election_logic.get_election_by_name(1)
        self.assertEqual(election, None)
        self.assertNotEqual(type(election), dict)
        self.assertNotEqual(election, self.election_logic.jsonify(self.model_election))
Example #4
0
class CandidateInterface(Interface):
    def __init__(self):
        super().__init__()
        self.candidate_logic = CandidateLogic()
        self.election_candidate_logic = ElectionCandidateLogic()
        self.auth_methods = ["createElectable", "deleteElectable", "getUserElectables",
                             "createCandidate", "createParty"]

    def createElectable(self, args: dict, auth) -> dict:
        desired_args = ["name", "description"]
        optional_args = ["imageUrl"]
        try:
            req_args = self.get_args_or_key_error(desired_args, args)
        except KeyError as error:
            return self.set_msg_or_error(str(error), 400)

        optional_args = self.get_possible_args(optional_args, args)
        image_url = ""
        if optional_args["imageUrl"] is not None:
            image_url = optional_args["imageUrl"]

        new_electable = CandidateModel(id=None, name=req_args["name"], image_url=image_url, info=req_args["bio"])
        electable = self.candidate_logic.add(new_electable)
        ret_str = self.create_relation(auth, electable.id)
        if ret_str == "":
            return self.set_msg_or_error(self.candidate_logic.jsonify(electable), 0)
        self.candidate_logic.delete(electable)
        return self.set_msg_or_error(ret_str, 401)

    def createCandidate(self, args: dict, auth) -> dict:
        return self.createElectable(args, auth)

    def createParty(self, args: dict, auth) -> dict:
        return self.createElectable(args, auth)

    def deleteElectable(self, args: dict, auth) -> dict:
        desired_args = ["electableID"]
        try:
            req_args = self.get_args_or_key_error(desired_args, args)
        except KeyError as error:
            return self.set_msg_or_error(str(error), 400)

        electable_id = self.convert_id(req_args["electableID"])
        if electable_id is None:
            return self.set_msg_or_error("electableID invalid.", 400)

        ret_str = self.delete_relation(auth, electable_id)
        if ret_str != "":
            return self.set_msg_or_error(ret_str, 401)

        self.election_candidate_logic.safe_delete_candidate(electable_id)
        return self.set_msg_or_error("Successfully removed electable.", 0)

    def getUserElectables(self, args: dict, auth) -> dict:
        if not auth.logged_in:
            return self.set_msg_or_error("Must be logged in.", 401)
        id_list = self.get_user_creations(auth)
        electables = self.candidate_logic.get_all()
        ret_models = []
        for electable in electables:
            if electable.id in id_list:
                ret_models.append(electable)

        return self.set_msg_or_error(self.candidate_logic.jsonify(ret_models), 0)

    def getElectables(self, args: dict) -> dict:
        """Returns all candidates for a given election. Returns an empty array if no election with this ID exists,
        or the election has no candidate."""
        # Return empty array if no election with this id exists
        # return [Candidate]
        desired_args = ['electionID']
        try:
            req_args = self.get_args_or_key_error(desired_args, args)
        except KeyError as error:
            return self.set_msg_or_error(str(error), 400)

        election_id = self.convert_id(req_args['electionID'])
        if not election_id:
            return self.set_msg_or_error('electionID must be a number.', 400)

        election_candidates = self.election_candidate_logic.get_candidates_in_election(election_id)
        if not election_candidates:
            return self.set_msg_or_error([], 0)
        return self.set_msg_or_error(self.election_candidate_logic.jsonify(election_candidates), 0)

    def getElectableDetails(self, args: dict) -> dict:
        """Returns a single candidate with the given ID. Returns an empty string if no candidate with this ID exists."""
        # return Candidate
        # return empty string if no candidate
        desired_args = ['electableID']
        try:
            req_args = self.get_args_or_key_error(desired_args, args)
        except KeyError as error:
            return self.set_msg_or_error(str(error), 400)

        candidate_id = self.convert_id(req_args['electableID'])
        if not candidate_id:
            return self.set_msg_or_error('electableID must be a number.', 400)

        candidate = self.candidate_logic.get(_id=candidate_id)
        if not candidate:
            return self.set_msg_or_error("Electable does not exist.", 404)
        return self.set_msg_or_error(self.candidate_logic.jsonify(candidate), 0)

    def getAllCandidates(self, args: dict):
        candidates = self.candidate_logic.get_all()
        if not candidates:
            return self.set_msg_or_error([], 0)
        return self.set_msg_or_error(self.candidate_logic.jsonify(candidates), 0)

    def getAllParties(self, args: dict):
        # not available in our system
        return self.set_msg_or_error([], 0)