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)
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))
class ElectionCandidateLogic(Logic): def __init__(self): super().__init__() self.candidates = CandidateLogic() def safe_delete_candidate(self, candidate_id: int) -> None: data = self.get_all() new_data = [] for el_candidate in data: if el_candidate.candidate_id != candidate_id: new_data.append(el_candidate) self.save(new_data) self.candidates.delete(candidate_id) def delete_election_candidates(self, election_id): """Delete candidates in election""" data = self.get_all() new_data = [] for i in range(len(data)): if data[i].election_id != election_id: new_data.append(data[i]) self.save(new_data) def get_candidates_in_election(self, election_id): """Returns all candidates in a specific election""" candidate_ids = [] all_candidates = [] # Fetch all candidate id's for given election_id for x in self.get_all(): if x.election_id == election_id: candidate_ids.append(x.candidate_id) # Fetch all candidate models for candidate id's # Það væri betra að fetcha all einu sinni hér ertu að ýtra yfir alla kandídata aftur og aftur for _id in candidate_ids: all_candidates.append(self.candidates.get(_id)) return all_candidates def jsonify(self, models): if models: return self.candidates.jsonify(models) return [] def jsonify_with_candidate_image(self, models): if models: return self.candidates.jsonify_with_image(models) return []
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)