def test_all(self): person_repository = PersonRepository() pers_cont = PersonController(person_repository) p1 = Person(1, "nonvaccinated", "healthy") p2 = Person(1, "vaccinated", "healthy") p3 = Person(1, "nonvaccinated", "healthy") pers_cont.add(p1) pers_cont.add(p2) pers_cont.add(p3) self.assertEqual(p1, p3) self.assertNotEqual(p1, p2) self.assertEqual(len(pers_cont.return_all()), 3)
def test_update(self): p = Person(1, "John", "0756705316", "street 1") self.__repository.save(p) p2 = Person(1, "John1", "0000000000", "street2") self.__repository.update(p2) self.assertEqual(self.__repository.find_by_id(1), p2, "find_by_id(1) should be equal to p2") p3 = Person("Invalid", 12341, 131, 131) self.assertRaises(RepositoryException, self.__repository.update, p3) p4 = Person(5, "Inexistent", "0000000000", "street 5") self.assertRaises(IdNotFoundException, self.__repository.update, p4)
def test_save(self): p = Person(1, "John", "0756705316", "street 1") self.__repository.save(p) # check if the size of the repository has changed self.assertEqual(len(self.__repository.get_all()), 1, "repository length should be 1") p2 = Person(1, "John1", "0756705415", "street 2") # check for DuplicateIdException self.assertRaises(DuplicateIdException, self.__repository.save, p2) # check for ValidatorException p3 = Person("invalid", 123, "00", 12) self.assertRaises(RepositoryException, self.__repository.save, p3)
def test_find_by_id(self): p = Person(1, "John", "0756705316", "street 1") self.__repository.save(p) self.assertEqual(self.__repository.find_by_id(1), p, "find_by_id(1) should be equal to p") self.assertEqual(self.__repository.find_by_id(2), None, "find_by_id(2) should be equal to None")
def read_entity(file_name): with open(file_name,'r') as f: line = f.read() line = line.split(" ") entity = Person(int(line[0]),line[1],line[2],line[3]) return entity return None
def update(self,id,name,phone_number,address): """Update a person entity from the reposity. Args: id(uint): the id of the person entity to be updated name(str): the new name of the person phone_number(str): the new phone number of the person address(str): the new address of the person Returns: None """ self.__person_repository.update(Person(id,name,phone_number,address))
def test_delete_by_id(self): p = Person(1, "John", "0756705316", "street 1") self.__repository.save(p) self.__repository.delete_by_id(p.entity_id) self.assertEqual(len(self.__repository.get_all()), 0, "repository size should be 0") self.assertRaises(IdNotFoundException, self.__repository.delete_by_id, 100)
def add(self,id,name,phone_number,address): """Add a new person entity to the person repository. Args: id(uint): the id of the person name(str): the name of the person phone_number(str): the phone number of the person address(str): the address of the person Returns: None """ self.__person_repository.save(Person(id,name,phone_number,address))
def modifyPerson(self, idPers, name, address): ''' Modifies a person if it exists :param idPers: :return: returns the new Person ''' try: newPerson = Person(idPers, name, address) self.__rep.updateElem(newPerson) return newPerson except ValidatorException as ex: print(ex.args)
def __readFromFile(self): try: f = open(self.__fName, 'r') line = f.readline().strip() while line != "": args = line.split(",") person = Person(args[0], args[1], args[2]) PersonRepository.add(self, person) line = f.readline().strip() except IOError: raise FileRepoException("sth is not ok... ") finally: f.close()
def test_person_validator(self): # invalid id check person = Person("string", "John", "0756705316", "street 1") self.assertRaises(PersonValidatorException, PersonValidator.validate, person) person = Person(1, 123, "0756705316", "street 1") self.assertRaises(PersonValidatorException, PersonValidator.validate, person) person = Person(1, "John", 1234, "street 1") self.assertRaises(PersonValidatorException, PersonValidator.validate, person) person = Person(1, "John", "a123456789", "street 1") self.assertRaises(PersonValidatorException, PersonValidator.validate, person) person = Person(1, "John", "0756705316", 123) self.assertRaises(PersonValidatorException, PersonValidator.validate, person)
def createPerson(self, idPers, name, addr): ''' store a person :param idPers: string :param name: string :param addr: string :return: the person :post: student added to the repository :raise: RepositoryException - if person already exists :raise: ValidationException - if person fields are invalid ''' #create a person object person = Person(idPers, name, addr) #store person into using repository self.__rep.store(person) return person
def ui_add_person(self): id = len(self._person_controller.return_all()) + 1 immunization = "nonvaccinated" status = "healthy" p = Person(id, immunization, status) self._person_controller.add(p)
def add_person(self): person_id = input("person id:") if not Common.get_type(person_id) is int: raise ValueError("Person id must be an integer.") person_id = int(person_id) name = input("name:") phone_number = input("phone number:") address = input("address:") CallStack.add_undo_operation(UndoHandlers.ADD_PERSON_HANDLER,self.__person_controller,Person(person_id,name,phone_number,address)) self.__person_controller.add(person_id,name,phone_number,address)
def test_eq(self): self.assertFalse(self.__person==Person(2,"lol","0000000000","street nr 2"),"Persons should not be equal.")
def test_get_all(self): p = Person(1, "John", "0756705316", "street 1") self.__repository.save(p) self.assertEqual(len(self.__repository.get_all()), 1, "repository size should be 1")
def setUp(self): super().setUp() self.__person = Person(1,"Johny","0756705316","street nr1")