class TestRepository(unittest.TestCase): def setUp(self): self.repo = Repository() self.c = Client(12, 'alex') self.c1 = Client(121, 'ana') self.repo2 = FileRepository('testclient.txt', Client.readClientFromLine, Client.writeClientToLine) self.repo3 = FileRepository('testclient4.txt', Client.readClientFromLine, Client.writeClientToLine) def test_repo(self): self.assertEqual(len(self.repo), 0) self.repo.add(self.c) self.assertEqual(str(self.repo), '012. alex\n\n') self.assertEqual(len(self.repo), 1) self.assertRaises(RepositoryException, self.repo.add, self.c) self.assertEqual(len(self.repo.getAll()), 1) self.assertEqual(self.repo.findByID(12), 12) self.assertEqual(self.repo.findByID(13), -1) self.assertEqual(self.repo.get(12), self.c) self.assertRaises(RepositoryException, self.repo.get, 13) self.assertRaises(RepositoryException, self.repo.remove, 13) self.assertRaises(RepositoryException, self.repo.update, 13) self.repo.update(self.c) self.repo.remove(12) self.assertEqual(len(self.repo), 0) self.repo2.add(self.c) self.repo2.upd(self.c) self.repo2.rem(self.c.getID()) self.assertEqual(len(self.repo2), 1)
def testUtils(self): bList = [] self.assertTrue(len(bList) == 0) bookRepo = Repository() bc = BookController(bookRepo, Statistics(bookRepo)) bc.populateBookRepository() self.assertTrue(0 < len(bookRepo.getAll()) < 100)
class PickleRepository(Repository): def __init__(self, fileName): self.__fileName = fileName self.__repo = Repository() self.__clear(fileName) self.__loadFromFile() def add(self, elem): self.__repo.add(elem) self.__storeToFile() def remove(self, elem): self.__repo.remove(elem) self.__storeToFile() def removeById(self, id): self.__repo.removeById(id) self.__storeToFile() def update(self, elem): self.__repo.update(elem) self.__storeToFile() def __len__(self): return self.__repo.size() def getById(self, id): return self.__repo.getById(id) def findById(self, id): return self.__repo.findById(id) def getAll(self): return self.__repo.getAll() def existsById(self, id): return self.__repo.existsById(id) def size(self): return self.__repo.size() def __storeToFile(self): f = open(self.__fileName, "wb") pickle.dump(self.__repo.getAll(), f) f.close() def __loadFromFile(self): f = open(self.__fileName, "rb") try: self.__repo._elems = pickle.load(f) except EOFError: return [] f.close() def __clear(self, fileName): """ Clear the file :param fileName: :return: None """ open(fileName, "w").close()
class ControllerTest(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) undoController = UndoController() self.rentalList = Repository() self.bookList = Repository() self.clientList = Repository() self.rentalList.add( Rental(0, 0, 1, datetime.datetime.strptime("2017-10-10", '%Y-%m-%d'), datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'), "")) self.rentalList.add( Rental(1, 1, 1, datetime.datetime.strptime("2017-10-10", '%Y-%m-%d'), datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'), "")) self.bookList.add(Book(0, "book0", "desc0", "author0")) self.bookList.add(Book(1, "book1", "desc1", "author1")) self.bookList.add(Book(2, "book2", "desc2", "author2")) self.clientList.add(Client(0, "name0")) self.clientList.add(Client(1, "name1")) self.clientList.add(Client(2, "name2")) self.rentalController = RentalController(self.rentalList, self.bookList, self.clientList, undoController) self.bookController = BookController(self.bookList, undoController, self.rentalController) self.clientController = ClientController(self.clientList, undoController, self.rentalController) def tearDown(self): unittest.TestCase.tearDown(self) def testAddBook(self): self.bookController.addBook(3, "book3", "desc3", "author3") self.assertEqual(self.bookList.getAll(), [ Book(0, "book0", "desc0", "author0"), Book(1, "book1", "desc1", "author1"), Book(2, "book2", "desc2", "author2"), Book(3, "book3", "desc3", "author3") ]) def testAddClient(self): self.clientController.addClient(3, "name3") self.assertEqual(self.clientList.getAll(), [ Client(0, "name0"), Client(1, "name1"), Client(2, "name2"), Client(3, "name3") ]) def testAddRental(self): rentalDate = datetime.datetime.now().date() self.rentalController.addRental(2, 2, 1) self.assertEqual(self.rentalList.getAll(), [ Rental(0, 0, 1, datetime.datetime.strptime("2017-10-10", '%Y-%m-%d'), datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'), ""), Rental(1, 1, 1, datetime.datetime.strptime("2017-10-10", '%Y-%m-%d'), datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'), ""), Rental(2, 2, 1, rentalDate, rentalDate + datetime.timedelta(days=10), "") ]) try: self.rentalController.addRental( Rental(100, 1, datetime.datetime.strptime("2017-10-10", '%Y-%m-%d'), datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'))) self.rentalController.addRental( Rental(2, 200, datetime.datetime.strptime("2017-10-10", '%Y-%m-%d'), datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'))) except Exception: pass def testRemoveBook(self): self.bookController.removeBook(0) self.assertEqual(self.bookList.getAll(), [ Book(1, "book1", "desc1", "author1"), Book(2, "book2", "desc2", "author2") ]) self.bookController.removeBook(2) self.bookController.removeBook(1) self.assertEqual(self.bookList.getAll(), []) with self.assertRaises(Exception): self.bookController.removeBook(10) def testRemoveClient(self): self.clientController.removeClient(1) self.assertEqual( self.clientList.getAll(), [Client(0, "name0"), Client(2, "name2")]) with self.assertRaises(Exception): self.clientController.removeClient(100) def testRemoveRentalByClientId(self): self.rentalController.removeRentalByClientId(1) self.assertEqual(self.rentalList.getAll(), []) def testRemoveRentalByBookId(self): self.rentalController.removeRentalByBookId(0) self.assertEqual(self.rentalList.getAll(), [ Rental(1, 1, 1, datetime.datetime.strptime("2017-10-10", '%Y-%m-%d'), datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'), "") ]) def testUpdateBook(self): with self.assertRaises(InvalidIdException): self.bookController.updateBook(20, "book2", "author3", "desc4") self.bookController.updateBook(1, "Book1", "Author1", "Description1") self.assertEqual(self.bookList.get(1), Book(1, "Book1", "Author1", "Description1")) def testUpdateClients(self): with self.assertRaises(InvalidIdException): self.clientController.updateClient(20, "Name1") self.clientController.updateClient(1, "Name1") self.assertEqual(self.clientList.get(1), Client(1, "Name1")) def testReturnBook(self): with self.assertRaises(Exception): self.rentalController.returnBook(100) self.rentalController.returnBook(0) self.assertEqual( self.rentalList.get(0), Rental(0, 0, 1, datetime.datetime.strptime("2017-10-10", '%Y-%m-%d'), datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'), datetime.datetime.now().date())) def testGetBooks(self): self.assertEqual(self.bookController.getBooks(), [ Book(0, "book0", "desc0", "author0"), Book(1, "book1", "desc1", "author1"), Book(2, "book2", "desc2", "author2") ]) def testGetClients(self): self.assertEqual( self.clientController.getClients(), [Client(0, "name0"), Client(1, "name1"), Client(2, "name2")]) def testRentalVaidation(self): self.assertEqual(self.rentalController.rentalValidation(2, 1), True) with self.assertRaises(InvalidIdException): self.rentalController.rentalValidation(0, 7) self.rentalController.rentalValidation(13, 0) def testMostRentedBooks(self): newList = self.rentalController.mostRentedBooks() self.assertEqual(newList, [ BookRental(0, "book0", 1), BookRental(1, "book1", 1), BookRental(2, "book2", 0) ]) def testMostActiveClient(self): newList = self.rentalController.mostActiveClients() self.assertEqual(newList, [ClientActivity("name1", 20)]) def testMostRentedAuthor(self): newList = self.rentalController.mostRentedAuthors() self.assertEqual(newList, [ AuthorRental(0, "author0", 1), AuthorRental(1, "author1", 1), AuthorRental(2, "author2", 0) ]) def testSearchBooks(self): searchList = self.bookController.searchBooks("book") self.assertEqual(searchList, [ Book(0, "book0", "desc0", "author0"), Book(1, "book1", "desc1", "author1"), Book(2, "book2", "desc2", "author2") ]) self.bookList.add(Book(3, "book27", "desc3", "author4")) searchList1 = self.bookController.searchBooks("2") self.assertEqual(searchList1, [ Book(2, "book2", "desc2", "author2"), Book(3, "book27", "desc3", "author4") ]) def testSearchClients(self): searchList = self.clientController.searchClients("cl") self.assertEqual(searchList, []) searchList1 = self.clientController.searchClients("na") self.assertEqual( searchList1, [Client(0, "name0"), Client(1, "name1"), Client(2, "name2")])
class CSVRepository(Repository): def __init__(self, fileName, objectType): self.__fileName = fileName self.__repo = Repository() self.__clear(fileName) self.type = objectType self.__loadFromFile(objectType) def __clear(self, fileName): """ Clear the file :param fileName: :return: None """ open(fileName, "w").close() def add(self, elem): """ Write the updated list to the file :param elem: elem to be added :return: """ self.__repo.add(elem) self.__storeToFile(elem) def remove(self, elem): """ Remove elem from the repo :param elem: :return: """ self.__repo.remove(elem) self.__storeToFile(elem) def removeById(self, id): """ :param id: int :return: The element with the id id """ self.__repo.removeById(id) self.__storeToFile(self.type) #TODO def update(self, elem): """ Update an element :param elem: :return: None """ self.__repo.update(elem) self.__storeToFile(elem) def __len__(self): return len(self.__repo) def getById(self, id): """ :param id: int :return: The element with the id id """ return self.__repo.getById(id) def findById(self, id): """ :param id: int :return: the index of the elem with the id id """ return self.__repo.findById(id) def getAll(self): """ :return: a list of all elements """ return self.__repo.getAll() def existsById(self, id): """ :param id: int :return: True if the element with the id is in the repo """ return self.__repo.existsById(id) def size(self): """ :return: The number of elements in the repo """ return self.__repo.size() def __storeToFile(self, type): """ :param elemSample: element :return: None """ f = open(self.__fileName, "w") # handle each type of object accordingly if type == Book: for book in self.__repo.getAll(): output = str(book.getId()) + "," output += book.getTitle() + "," output += book.getDescription() + ";" output += book.getAuthor() output += "\n" f.write(output) if type == Client: for client in self.__repo.getAll(): output = str(client.getId()) + "," output += client.getName() output += "\n" f.write(output) if type == Rental: for rental in self.__repo.getAll(): output = str(rental.getRentalId()) + "," output += str(rental.getBookId()) + "," output += str(rental.getClientId()) + "," output += str(rental.getRentedDate()) + "," output += str(rental.getDueDate()) + "," output += str(rental.getReturnDate()) output += "\n" f.write(output) f.close() def __loadFromFile(self, type): """ :param sample: Element :return: None :raises RepositoryException when the file cannot be read for some reason """ try: f = open(self.__fileName, "r") line = f.readline().strip() while line != "": attrs = line.split(",") if type == Book: book = Book(int(attrs[0]), attrs[1], attrs[2], attrs[3]) self.__repo.add(book) line = f.readline().strip() if type == Client: client = Client(int(attrs[0]), attrs[1]) self.__repo.add(client) line = f.readline().strip() if type == Rental: rental = Rental( int(attrs[0]), int(attrs[1]), int(attrs[2]), createDateFromString(self.__reverseDate(attrs[3])), createDateFromString(self.__reverseDate(attrs[4])), createDateFromString(self.__reverseDate(attrs[5]))) self.__repo.add(rental) line = f.readline().strip() except IOError: raise RepositoryException() finally: f.close()