def test_update_client(self): repoClients = Repo("Repoclients:") repoClients.add(Client(20, "John Wright")) repoClients.add(Client(40, "Andrei Ivan")) repoClients.update(Client(40, "New")) assert(repoClients.get_all()[1].clientName == "New")
def test_search_unique(self): repoRental = RepositoryRentals("Reporentals:") repoRental.add(Rental(1, Book(1, "Title1", "Author1"), Client(1, "Name1"), datetime.date(2019, 10, 5), None)) repoRental.add(Rental(2, Book(2, "Title2", "Author2"), Client(2, "Name2"), datetime.date(2019, 10, 6), None)) self.assertRaises(RepositoryError, lambda : repoRental.search_unique(Rental(1, Book(2, "Title1", "Author1"), Client(1, "Name1"), datetime.date(2019, 10, 5), None))) self.assertRaises(RepositoryError, lambda : repoRental.search_unique(Rental(12, Book(1, "Title1", "Author1"), Client(1, "Name1"), datetime.date(2019, 10, 5), None)))
def test_search_client_name(self): repoClients = RepositoryClients("Repoclients:") repoClients.add(Client(1, "Name1")) repoClients.add(Client(12, "Name12")) assert(len(repoClients.searchClientName("aM")) == 2) self.assertRaises(RepositoryError, lambda : repoClients.searchClientName("8"))
def test_rental_remove_book(self): repoRental = RepositoryRentals("Reporentals:") repoRental.add(Rental(1, Book(1, "Title1", "Author1"), Client(1, "Name1"), datetime.date(2019, 10, 5), None)) repoRental.add(Rental(2, Book(2, "Title2", "Author2"), Client(2, "Name2"), datetime.date(2019, 10, 6), None)) repoRental.remove_book(2) assert(repoRental.size() == 1)
def test_validate_client(self): validatorClients = ValidateClient() c = Client(-2, "Ionut") self.assertRaises(ValidationError, lambda:validatorClients.validate_client(c)) c = Client(2, "") self.assertRaises(ValidationError, lambda:validatorClients.validate_client(c))
def initFromMemory(self): self.listOfBooks.extend([ Book("1", "B", "E"), Book("2", "E", "x"), Book("5", "po", "k"), Book("4", "T", "J"), Book("3", "z", "E"), Book("7", "Iz", "Pzz") ]) self.listOfClients.extend([ Client("1", "jo"), Client("2", "Richy"), Client("3", "Iu"), Client("4", "Jaz"), Client("5", "Qwe") ]) self.listOfRentedBooks.extend([ Rental("1", "1", "1", datetime.datetime(2012, 12, 5), datetime.datetime(2012, 12, 10)), Rental("2", "1", "4", datetime.datetime(2012, 12, 22), datetime.datetime(2013, 1, 22)), Rental("3", "1", "2", datetime.datetime(2012, 12, 29), datetime.datetime(2013, 12, 10)), Rental("3", "2", "5", datetime.datetime(2010, 9, 20), datetime.datetime(2011, 1, 1)) ]) self.mostRentedBooks = {} self.mostRentedAuthor = {} self.mostActiveClients = {}
def test_remove_client(self): repoClients = Repo("Repoclients:") repoClients.add(Client(20, "John Wright")) repoClients.add(Client(40, "Andrei Ivan")) repoClients.remove(Client(40, None)) assert(repoClients.size() == 1) assert(repoClients.get_all()[0].clientID == 20)
def testClient_again(self): c = Client(2, 'Pop MIhnea', 20) with self.assertRaises(ValueError): c.Age = 17 try: c.Age = 17 assert False # should have raised an error except ValueError: assert True # we are ei okay except Exception: assert False # a different exception was raised
def test_create_client(self): clientId = 15 clientName = "George" c = Client(clientId, clientName) assert(c.clientID == 15) assert(c.clientName == "George") c1 = Client(15, None) c1.clientName = "Andrew" assert(c == c1)
def test_most_authors(self): repoBooks = RepositoryBooks("Repobooks:") repoRental = RepositoryRentals("Reporentals:") repoBooks.add(Book(1, "Title1", "Author1")) repoBooks.add(Book(2, "Title2", "Author2")) repoRental.add(Rental(1, Book(1, "Title1", "Author1"), Client(1, "Name1"), datetime.date(2019, 10, 5), None)) repoRental.add(Rental(2, Book(2, "Title2", "Author2"), Client(2, "Name2"), datetime.date(2019, 10, 6), None)) a = repoBooks.get_all_authors() alist = repoRental.most_authors(a) assert(len(a) == len(alist))
def test_most_clients(self): repoClients = RepositoryClients("Repoclients:") repoRental = RepositoryRentals("Reporentals:") repoClients.add(Client(1, "Name1")) repoClients.add(Client(2, "Name2")) repoRental.add(Rental(1, Book(1, "Title1", "Author1"), Client(1, "Name1"), datetime.date(2019, 10, 5), None)) repoRental.add(Rental(2, Book(2, "Title2", "Author2"), Client(2, "Name2"), datetime.date(2019, 10, 6), datetime.date(2019, 10, 15))) c = repoClients.get_all() clist = repoRental.most_clients(c) assert(len(c) == len(clist))
def test_add_client(self): repoClients = Repo("Repoclients:") repoClients.add(Client(20, "John Wright")) repoClients.add(Client(40, "Andrei Ivan")) assert(repoClients.size() == 2) assert(repoClients.get_all()[0].clientID == 20) assert(repoClients.get_all()[1].clientID == 40) assert(repoClients.get_all()[0].clientName == "John Wright") assert(repoClients.get_all()[1].clientName == "Andrei Ivan")
def create (self, clientID, name): ''' Adds a new client to the repository ''' client = Client(clientID, name) self._validator.validate(client) self._repository.add(client)
def _loadFile(self): ''' loads the repo file ''' try: f = open(self._filename, "r") line = f.readline() while len(line) > 2: tok = line.split(",") date = datetime.strptime(tok[3], '%Y-%m-%d %H:%M:%S') ''' yeah, month, day = map(int, tok[3].split("-")) date = datetime.date(yeah, month, day) ''' book = Book(int(tok[1]), "", "", "") client = Client(int(tok[0]), "") ret = tok[4].strip() RentalRepository.rent_book(self, client, book, date) if str(ret) != "None": ''' yeah, month, day = map(int, ret.split("/")) date = datetime.date(yeah, month, day) ''' date = datetime.strptime(ret, '%Y-%m-%d %H:%M:%S') RentalRepository.return_book(self, client, book, date) line = f.readline() except IOError: raise Exception("Invalid file... :(") finally: f.close()
def test_validate_rental(self): validatorRental = ValidateRental() with self.assertRaises(ValidationError) as context: validatorRental.validate_rental(Rental(-2, Book(2, "Title2", "Author2"), Client(2, "Name2"), datetime.date(2019, 10, 6), datetime.date(2018, 10, 5))) self.assertTrue("\n[VALIDATION ERROR] -> Invalid Rental ID! Invalid returned Date!" in str(context.exception)) self.assertRaises(ValidationError, lambda : validatorRental.validate_rental(Rental(-2, Book(2, "Title2", "Author2"), Client(2, "Name2"), datetime.date(2019, 10, 6), datetime.date(2018, 10, 5))))
def test_client_repo(): client1 = Client(1, "Scott Fitzgerald") client2 = Client(2, "Shuji Sogabe") repo = ClientRepository() repo.add(client1) listClients = repo.getAll() assert len(listClients) == 1 assert listClients[0].get_name() == "Scott Fitzgerald" repo.add(client2) listClients = repo.getAll() assert len(listClients) == 2 assert listClients[1].get_name() == "Shuji Sogabe" assert listClients[0].get_id() == 1 assert listClients[1].get_id() == 2 repo.remove(client1) listBooks = repo.getAll() assert len(listClients) == 1 assert listBooks[0].get_name() == "Shuji Sogabe" assert listBooks[0].get_id() == 2 try: repo.remove(Client(34, "til")) assert True except IndexError: pass except ValueError: pass assert repo.find(client1) == False assert repo.find(client2) == True assert repo.find(Client(1, "Shuji Sogabe")) == False repo.update(Client(2, "Atlus")) listClients = repo.getAll() assert listClients[0].get_name() == "Atlus" assert listClients[0].get_id() == 2 try: repo.update(client1) assert False except IndexError: pass #test_client_repo()
def add_client(self, name): ''' adds a new client to the list of clients :param name: name of client :return:nothing, appends to the list ''' c = Client(name) self._clients.append(c)
def generateClients(self, dimension, idBasis): result = [] names = self.generateNames() randomNames = random.sample(names, dimension) for i in range(dimension): c = Client(idBasis + i, randomNames[i]) result.append(c) return result
def add_client(self, clientId, clientName, u): #adds a new client to the list of clients #clientID = (int) the client ID, clientName = (str) the name of the client client = Client(clientId, clientName) self._validatorClient.validate_client(client) self._clientsList.add(client) if u == True: UndoManager.register_operation(self, UndoHandler.ADD_CLIENT, int(clientId), clientName)
def read_data(self): #reads all the data from the sql table self._entities = [] cmd = 'SELECT * FROM ' + self._table_name self._cursor.execute(cmd) line = self._cursor.fetchone() while line is not None: obj = Client(int(line[0]), line[1]) self._entities.append(obj) line = self._cursor.fetchone()
def remove_client(self, clientId, u): #removes a client from the list of clients #clientId = (str) the client ID which has to be removed client = Client(clientId, None) client = self._clientsList.search(client) #print(client.clientName) self._clientsList.remove(client) if u == True: rlist = self._repoRental.remove_client(int(clientId)) UndoManager.register_operation(self, UndoHandler.REMOVE_CLIENT, clientId, client.clientName, rlist)
def search(self, keyobj): #returns an object with a specific key (keyobj) from the repo #raises a RepositoryError if the id is not in the repo #print(str(keyobj.bookID)) cmd = "SELECT * FROM " + self._table_name + " WHERE Id=" + str( keyobj.clientID) + ";" self._cursor.execute(cmd) line = self._cursor.fetchone() if line is None: raise RepositoryError(self._name + " Inexistent ID!\n") return Client(int(line[0]), line[1])
def read_data(self): #reads all the data from the sql table self._entities = [] cmd = 'SELECT * FROM ' + self._table_name self._cursor.execute(cmd) line = self._cursor.fetchone() while line is not None: if line[4] is not None: obj = Rental( int(line[0]), Book(int(line[1]), None, None), Client(int(line[2]), None), datetime.datetime.strptime(line[3], '%Y-%m-%d').date(), datetime.datetime.strptime(line[4], '%Y-%m-%d').date()) else: obj = Rental( int(line[0]), Book(int(line[1]), None, None), Client(int(line[2]), None), datetime.datetime.strptime(line[3], '%Y-%m-%d').date(), None) self._entities.append(obj) line = self._cursor.fetchone()
def update_client(self, clientId, clientName, u): #updates a client with a new Title and a new Author #clientId = (str) the client ID which has to be updated #clientName = (str) the new client Name client = Client(clientId, clientName) self._validatorClient.validate_client(client) client2 = self._clientsList.search(client) self._clientsList.update(client) if u == True: UndoManager.register_operation(self, UndoHandler.UPDATE_CLIENT, client2.clientID, client2.clientName)
def search(self, keyobj): #returns an object with a specific key (keyobj) from the repo #raises a RepositoryError if the id is not in the repo #print(str(keyobj.bookID)) cmd = "SELECT * FROM " + self._table_name + " WHERE Id=" + str( keyobj.rentalID) + ";" self._cursor.execute(cmd) line = self._cursor.fetchone() if line is None: raise RepositoryError(self._name + " Inexistent ID!\n") if line[4] is not None: obj = Rental( int(line[0]), Book(int(line[1]), None, None), Client(int(line[2]), None), datetime.datetime.strptime(line[3], '%Y-%m-%d').date(), datetime.datetime.strptime(line[4], '%Y-%m-%d').date()) else: obj = Rental( int(line[0]), Book(int(line[1]), None, None), Client(int(line[2]), None), datetime.datetime.strptime(line[3], '%Y-%m-%d').date(), None) return obj
def _loadFile(self): try: #self._fileName = "D:\\INFO2018-2019\\~INFO\\FP\\Assignments\\AICIASS5-7\\57\\repository\\" + self._fileName f = open(self._fileName, "r") s = f.readline() while len(s) > 1: tok = s.split(",") client = Client(int(tok[0]), tok[1]) ClientRepository.addClient(self, client) s = f.readline() except Exception as e: raise RepositoryError("The file cannot be open") finally: f.close()
def _loadFile(self): try: f = open(self._filename, "rb") while True: try: rent = pickle.load(f) client = Client(rent.get_clientID(), "") book = Book(rent.get_bookID(), "", "", "") RentalRepository.rent_book(self, client, book, rent.get_rented_date()) RentalRepository.return_book(self, client, book, rent.get_return_date()) except EOFError: break except IOError: raise Exception("Invalid file... :(") finally: f.close()
def _loadFile(self): ''' loads the repo file ''' try: f = open(self._filename, "r") line = f.readline() while len(line) > 2: tok = line.split(",") client = Client(int(tok[0]), tok[1].strip()) ClientRepository.add(self, client) line = f.readline() except IOError: raise Exception("Invalid file... :(") finally: f.close()
def test_repo(): client1 = Client(1, "Scott Fitzgerald") book1 = Book(1, "Persona 3", "3rd entry in the series", "Shuji Sogabe") book2 = Book(2, "Persona 5", "5th entry in the series", "Atlus") repo = RentalRepository() repo.rent_book(client1, book1, datetime(2018, 11, 3)) listRent = repo.getAll() assert len(listRent) == 1 assert listRent[0].get_clientID() == 1 assert listRent[0].get_bookID() == 1 assert listRent[0].get_rentalID() == 0 assert str(listRent[0].get_rented_date()) == "2018-11-03 00:00:00" assert str(listRent[0].get_due_date()) == "2018-11-17 00:00:00" assert str(listRent[0].get_return_date()) == "None" repo.return_book(client1, book1, datetime(2018, 11, 10)) listRent = repo.getAll()
def add_rental(self, rentalId, bookId, clientId, rentedDate, returnedDate, u): #adds a new rental to the list of rentals #rentalID = (int) the rental ID #bookID = (int) the book ID #clientID = (int) the client ID #rentedDate = (date) the rentedDate #returnedDate = (date) the returnedDate rent = Rental(rentalId, Book(bookId, None, None), Client(clientId, None), rentedDate, returnedDate) book = self._booksList.search(rent.book) client = self._clientsList.search(rent.client) rent = Rental(rentalId, book, client, rentedDate, returnedDate) self._validatorRental.validate_rental(rent) self._repoRental.search_unique(rent) self._repoRental.add(rent) if u == True: UndoManager.register_operation(self, UndoHandler.ADD_RENTAL, int(rentalId), int(bookId), int(clientId), rentedDate, returnedDate)