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 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 _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 _saveFile(self): try: f = open(self._fileName, "w") for c in ClientRepository.get_clients(self): f.write(str(c.get_id()) + "," + c.get_name() + "\n") except Exception as e: raise RepoError("Error writing file: " + str(e)) finally: f.close()
def setUp(self): self.__id_b = 23 self.__title = "FP" self.__description = "work on your assignments daily" self.__author = "Arthur Molnar" self.__book = Book(self.__id_b, self.__title, self.__author, self.__description) self.__id_cl = 3 self.__name = "Corina" self.__client = Client(self.__id_cl, self.__name) self.__rid = 4 self.__bid = 8 self.__cid = 2 self.__rdate = "25.11.2018" self.__duedate = "2.12.2018" self.__returdate = "28.11.2018" self.__rental = Rental(self.__rid, self.__bid, self.__cid, self.__rdate, self.__duedate, self.__returdate) self.__bvalid = BookValidator() self.__cvalid = ClientValidator() self.__rvalid = RentalValidator() self.__brepo = BookRepository() self.__crepo = ClientRepository() self.__rrepo = RentalRepository() self.__undoserv = UndoService() self.__rental_serv = RentalService( self.__rrepo, self.__rvalid, self.__brepo, self.__crepo, self.__undoserv, ) self.__book_serv = BookService(self.__brepo, self.__bvalid, self.__undoserv, self.__rental_serv) self.__client_serv = ClientService(self.__crepo, self.__cvalid, self.__undoserv, self.__rental_serv)
def update(self, client): c = ClientRepository.update(self, client) self._saveFile() return c
def remove(self, client): c = ClientRepository.remove(self, client) self._saveFile() return c
def add(self, client): ClientRepository.add(self, client) self._saveFile()
def __init__(self, fileName="clients.txt"): ClientRepository.__init__(self) self._fileName = fileName self._loadFile()
f = open("settings") tokens = f.read().split("\n") for tok in tokens: t = tok.split("=") settings[t[0].strip()] = t[1].strip() return settings settings = readSettings() book_repo = None if settings["repo_type"] == "memory": book_repo = BookRepository() client_repo = ClientRepository() rental_repo = RentalRepository() elif settings["repo_type"] == "text": book_repo = BookTextFileRepo(settings["book_repo_file"]) client_repo = ClientTextFileRepo(settings["client_repo_file"]) rental_repo = RentalTextFileRepo(settings["rental_repo_file"]) elif settings["repo_type"] == "binaryfile": # print("its here") book_repo = BookBinaryFileRepo(settings["book_repo_file"]) client_repo = ClientBinaryFileRepo(settings["client_repo_file"]) rental_repo = RentalBinaryFileRepo(settings["rental_repo_file"]) elif settings["repo_type"] == "json": book_repo = Book_JSON_Repo(settings["book_repo_file"]) client_repo = Client_JSON_Repo(settings["client_repo_file"]) rental_repo = Rental_JSON_Repo(settings["rental_repo_file"])
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)
def __init__(self, fileName="clients.pickle"): ClientRepository.__init__(self) self._fileName = fileName self._loadBinaryFile()