def _loadFile(self): with open(self._fileName, "r") as f: try: json_clients = json.load(f) for c in json_clients: client = self.json_to_client(c) ClientRepository.add(self, client) except JSONDecodeError as je: raise RepoError("could not load file: " + str(je))
def _loadFile(self): try: f = open(self._fileName, "r") s = f.readline() while len(s) > 1: tok = s.split(",") tok[1] = tok[1].split("\n") client = Client(int(tok[0]), tok[1][0]) ClientRepository.add(self, client) s = f.readline() except Exception as e: raise RepoError("Error reading input file: " + str(e)) finally: f.close()
def add(self, client): ClientRepository.add(self, client) self._saveFile()
class Test_Client(unittest.TestCase): def setUp(self): self.__id = 3 self.__name = "Corina" self.__client = Client(self.__id, self.__name) self.__meanid = -5 self.__meanname = "" self.__meanclient = Client(self.__meanid, self.__meanname) self.__validator = ClientValidator() self.__repo = ClientRepository() self.__newname = "Korina" self.__updateclient = Client(self.__id, self.__newname) def tearDown(self): unittest.TestCase.tearDown(self) def test_model(self): self.assertEqual(self.__client.get_id(), self.__id) self.assertEqual(self.__client.get_name(), self.__name) self.__client.del_name() self.__client.set_name("Korina") self.assertEqual(self.__client.get_name(), "Korina") def test_validator(self): try: self.__validator.valid_client(self.__client) assert True except ValidError as ve: assert False with self.assertRaises(ValidError): self.__validator.valid_client(self.__meanclient) def test_repo(self): self.assertEqual(len(self.__repo), 0) self.__repo.add(self.__client) self.assertEqual(len(self.__repo), 1) with self.assertRaises(RepoError): self.__repo.add(self.__client) c = Client(5, "Adrian") with self.assertRaises(RepoError): self.__repo.remove(c) try: self.__repo.remove(self.__client) assert True except RepoError as re: assert False self.__repo.add(self.__client) with self.assertRaises(RepoError): self.__repo.update(c) try: self.__repo.update(self.__updateclient) assert True except RepoError as re: assert False try: assert self.__repo.list() == "ID \t Name \n3 \t Korina\n" except RepoError as re: assert False self.__repo.remove(self.__updateclient) with self.assertRaises(RepoError): assert self.__repo.list() == "ID \t Name \n" self.__repo.add(self.__client) self.assertEqual(self.__repo.find(3), self.__client) self.assertEqual(self.__repo.find(999), None) self.assertEqual(self.__repo.exists_client(self.__client), True) self.assertEqual(len(self.__repo.search_id(3)), 1) self.assertEqual(len(self.__repo.search_name("rina")), 1)