def setUp(self): self.valid = Validator() self.bookRepo = RepoData() self.undoService = UndoService() self.rentalRepo = RepoData() self.book_service = BookService(self.valid, self.bookRepo, self.undoService, self.rentalRepo)
def __readAllFromFile(self): ''' we read all reantals from the file :return: ''' try: with open(self.__fileName, "r") as file: lines = file.readlines() for line in lines: if line != "": words = line.strip().split(",") if len(words) == 6: rental_id = int(words[0].strip()) book_id = int(words[1].strip()) client_id = int(words[2].strip()) rented_date = self.__computeDateFromFile( words[3].strip()) due_date = self.__computeDateFromFile( words[4].strip()) returned_date = self.__computeDateFromFile( words[5].strip()) rental = Rental(rental_id, book_id, client_id, rented_date, due_date, returned_date) RepoData.add(self, rental) except FileNotFoundError: print("Inexistent file : " + self.__fileName)
def __readBinaryFromFile(self): try: with open(self.file_name, "rb") as binary_file: list_of_books = pickle.load(binary_file) for book in list_of_books: RepoData.add(self, book) except FileNotFoundError: print("Inexistent file : " + self.file_name)
def __readAllBinaryFromFile(self): try: with open(self.__file_name, "rb") as binary_file: list_of_clients = pickle.load(binary_file) for client in list_of_clients: RepoData.add(self, client) except FileNotFoundError: print("Inexistent file : " + self.__file_name)
def setUp(self): self.clientRepo = RepoData() self.valid = Validator() self.UndoService = UndoService() self.rentalRepo = RepoData() self.bookRepo = RepoData() self.serviceClient = ClientService(self.valid, self.clientRepo, self.UndoService, self.rentalRepo, self.bookRepo)
def __readAllFromFile(self): try: with open(self.__fileName, "r") as file: lines = file.readlines() for line in lines: words = line.strip().split(',') if len(words) == 2: client_id = int(words[0].strip()) client_name = words[1].strip() client = Client(client_id, client_name) RepoData.add(self, client) except FileNotFoundError: print("Inexistent file : " + self.__fileName)
def __readAllBinaryFromFile(self): ''' we read all reantals from the file :return: ''' try: with open(self.__file_name, "rb") as binary_file: list_of_rentals = pickle.load(binary_file) for rentals in list_of_rentals: RepoData.add(self, rentals) except FileNotFoundError: print("Inexistent file : " + self.__file_name) except EOFError: list_of_rentals = []
def setUp(self): self.bookRepo = RepoData() self.clientRepo = RepoData() self.rentalRepo = RepoData() self.valid = Validator() self.undoService = UndoService() self.serviceBook = BookService(self.valid, self.bookRepo, self.undoService, self.rentalRepo) self.serviceClient = ClientService(self.valid, self.clientRepo, self.undoService, self.rentalRepo, self.bookRepo) self.serviceRental = RentalService(self.valid, self.rentalRepo, self.serviceBook, self.serviceClient, self.undoService)
def add(self, rental): RepoData.add(self, rental) self.__writeAllBinaryToFile()
def setUp(self): self.repo = RepoData()
class TestRepoData(TestCase): def setUp(self): self.repo = RepoData() def test_add(self): self.repo.add(1) self.repo.add('papa') self.assertEqual(len(self.repo), 2) self.assertRaises(RepoError, self.repo.add, 1) self.assertRaises(RepoError, self.repo.add, 'papa') def test_remove(self): self.repo.add(1) self.repo.add('papa') self.repo.remove(1) self.assertEqual(len(self.repo), 1) self.assertRaises(RepoError, self.repo.remove, 8888) def test_getAll(self): self.repo.add(1) self.repo.add('papa') self.assertEqual(2, len(self.repo))
def remove(self, book): RepoData.remove(self, book) self.__writeBinaryToFile()
class TestBookService(TestCase): def setUp(self): self.valid = Validator() self.bookRepo = RepoData() self.undoService = UndoService() self.rentalRepo = RepoData() self.book_service = BookService(self.valid, self.bookRepo, self.undoService, self.rentalRepo) def test_createBook(self): book = self.book_service.createBook(432, 'mara', 'op', 'slavici') self.assertEqual(book.get_book_id(), 432) self.assertEqual(book.get_author(), 'slavici') self.assertEqual(book.get_description(), 'op') self.assertEqual(book.get_title(), 'mara') self.assertEqual(book.get_availability(), True) def test_addBook(self): book1 = self.book_service.createBook(432, 'mara', 'op', 'slavici') book2 = self.book_service.createBook(442, 'mora cu noroc', 'super', 'slavici') self.book_service.addBook(book1) self.book_service.addBook(book2) self.assertEqual(len(self.bookRepo.getAll()), 2) def test_getAllBooks(self): book1 = self.book_service.createBook(432, 'mara', 'op', 'slavici') book2 = self.book_service.createBook(442, 'mora cu noroc', 'super', 'slavici') self.book_service.addBook(book1) self.book_service.addBook(book2) self.assertEqual(len(self.bookRepo.getAll()), 2) self.book_service.deleteBook(book2.get_book_id()) self.assertEqual(len(self.bookRepo.getAll()), 1) def test_searchBookById(self): book1 = self.book_service.createBook(432, 'mara', 'op', 'slavici') book2 = self.book_service.createBook(442, 'mora cu noroc', 'super', 'slavici') self.book_service.addBook(book1) self.book_service.addBook(book2) book = self.book_service.searchBookById(432) self.assertEqual(book, book1) def test_deleteBook(self): book1 = self.book_service.createBook(432, 'mara', 'op', 'slavici') book2 = self.book_service.createBook(442, 'mora cu noroc', 'super', 'slavici') self.book_service.addBook(book1) self.book_service.addBook(book2) self.assertEqual(len(self.bookRepo.getAll()), 2) self.book_service.deleteBook(book2.get_book_id()) self.assertEqual(len(self.bookRepo.getAll()), 1) self.book_service.addBook(book2) self.assertEqual(len(self.bookRepo.getAll()), 2) def test_setBookAvailable(self): book1 = self.book_service.createBook(432, 'mara', 'op', 'slavici') self.book_service.addBook(book1) self.book_service.setBookUnavailable(book1.get_book_id()) self.assertEqual(False, book1.get_availability()) self.book_service.setBookAvailable(book1.get_book_id()) self.assertEqual(True, book1.get_availability()) def test_searchBookByName(self): book1 = self.book_service.createBook(432, 'mara', 'op', 'slavici') book2 = self.book_service.createBook(442, 'mora cu noroc', 'super', 'slavici') self.book_service.addBook(book1) self.book_service.addBook(book2) book_lst = self.book_service.searchBookByName('mara') #print(book_lst[0].get_title()) self.assertEqual(book_lst, [book1]) def test_searchBookByAuthor(self): book1 = self.book_service.createBook(432, 'mara', 'op', 'slavici') book2 = self.book_service.createBook(442, 'mora cu noroc', 'super', 'slavici') self.book_service.addBook(book1) self.book_service.addBook(book2) book = self.book_service.searchBookByAuthor('slavici') self.assertEqual(book, book1) def test_searchBookByDescription(self): book1 = self.book_service.createBook(432, 'mara', 'op', 'slavici') book2 = self.book_service.createBook(442, 'mora cu noroc', 'super', 'slavici') self.book_service.addBook(book1) self.book_service.addBook(book2) book = self.book_service.searchBookByDescription('super') self.assertEqual(book, book2) def test_setBookUnavailable(self): book1 = self.book_service.createBook(432, 'mara', 'op', 'slavici') self.book_service.addBook(book1) self.book_service.setBookUnavailable(book1.get_book_id()) self.assertEqual(False, book1.get_availability()) def test_updateBook(self): book1 = self.book_service.createBook(432, 'mara', 'op', 'slavici') self.book_service.addBook(book1) self.book_service.updateBook(432, 'haha', 'gigi', 'oop') self.assertEqual(self.book_service.getAllBooks()[0].get_title(), 'haha') self.assertEqual(self.book_service.getAllBooks()[0].get_description(), 'gigi') self.assertEqual(self.book_service.getAllBooks()[0].get_author(), 'oop')
def __init__(self, fileName): RepoData.__init__(self) self.__fileName = fileName self.__readAllFromFile()
def add(self, book): RepoData.add(self, book) self.__writeBinaryToFile()
def setUp(self): self.repoBook = RepoData() self.repoClient = RepoData() self.repoRentals = RepoData()
class TestClientService(TestCase): def setUp(self): self.clientRepo = RepoData() self.valid = Validator() self.UndoService = UndoService() self.rentalRepo = RepoData() self.bookRepo = RepoData() self.serviceClient = ClientService(self.valid, self.clientRepo, self.UndoService, self.rentalRepo, self.bookRepo) def test_createClient(self): client = self.serviceClient.createClient(765, 'gigi') self.assertEqual(765, client.get_client_id()) self.assertEqual('gigi', client.get_name()) def test_addClient(self): client1 = self.serviceClient.createClient(765, 'gigi') client2 = self.serviceClient.createClient(769, 'popa') self.serviceClient.addClient(client1) self.serviceClient.addClient(client2) self.assertEqual(2, len(self.serviceClient.getAllClients())) def test_getAllClients(self): client1 = self.serviceClient.createClient(765, 'gigi') client2 = self.serviceClient.createClient(769, 'popa') self.serviceClient.addClient(client1) self.assertEqual(1, len(self.serviceClient.getAllClients())) self.serviceClient.addClient(client2) self.assertEqual(2, len(self.serviceClient.getAllClients())) def test_deleteClient(self): client1 = self.serviceClient.createClient(765, 'gigi') client2 = self.serviceClient.createClient(769, 'popa') self.serviceClient.addClient(client1) self.serviceClient.addClient(client2) self.assertEqual(2, len(self.serviceClient.getAllClients())) self.serviceClient.deleteClient(765) self.assertEqual(1, len(self.serviceClient.getAllClients())) def test_searchClientById(self): client1 = self.serviceClient.createClient(765, 'gigi') client2 = self.serviceClient.createClient(769, 'popa') self.serviceClient.addClient(client1) self.serviceClient.addClient(client2) client = self.serviceClient.searchClientById(769) self.assertEqual(client, client2) def test_searchClientByName(self): client1 = self.serviceClient.createClient(765, 'gigi') client2 = self.serviceClient.createClient(769, 'popa') self.serviceClient.addClient(client1) self.serviceClient.addClient(client2) client = self.serviceClient.searchClientByName('popa') self.assertEqual(client, client2) def test_updateClient(self): client1 = self.serviceClient.createClient(765, 'gigi') self.clientRepo.add(client1) self.serviceClient.updateClient(765, 'hagi') self.assertEqual('hagi', self.serviceClient.getAllClients()[0].get_name())
def remove(self, rental): RepoData.remove(self, rental) self.__writeAllBinaryToFile()
def remove(self, client): RepoData.remove(self, client) self.__writeAllBinaryToFile()
def add(self, client): RepoData.add(self, client) self.__writeAllBinaryToFile()
def __init__(self, file_name): RepoData.__init__(self) self.__file_name = file_name self.__readAllBinaryFromFile()
class TestRentalService(TestCase): def setUp(self): self.bookRepo = RepoData() self.clientRepo = RepoData() self.rentalRepo = RepoData() self.valid = Validator() self.undoService = UndoService() self.serviceBook = BookService(self.valid, self.bookRepo, self.undoService, self.rentalRepo) self.serviceClient = ClientService(self.valid, self.clientRepo, self.undoService, self.rentalRepo, self.bookRepo) self.serviceRental = RentalService(self.valid, self.rentalRepo, self.serviceBook, self.serviceClient, self.undoService) def test_createRental(self): self.bookRepo.add(Book(12, 'qwert', 'asdfgh', 'zxcvbn', True)) self.clientRepo.add(Client(43, 'MOhl ojsddan')) rental = self.serviceRental.createRental(777, 12, 43) self.assertEqual(777, rental.get_rental_id()) self.assertEqual(12, rental.get_book_id()) self.assertEqual(43, rental.get_client_id()) current_date = datetime.now() due_date = current_date + timedelta(days=14) day = current_date.day month = current_date.month year = current_date.year date_p = date(year, month, day) self.assertEqual(date_p, rental.get_rented_date()) day = due_date.day month = due_date.month year = due_date.year t = date(year, month, day) self.assertEqual(t, rental.get_due_date()) self.assertEqual(0, rental.get_returned_date()) def test_getAllRentals(self): self.bookRepo.add(Book(12, 'qwert', 'asdfgh', 'zxcvbn', True)) self.bookRepo.add(Book(56, 'mara', 'op', 'slavici', True)) self.clientRepo.add(Client(43, 'MOhl ojsddan')) self.clientRepo.add(Client(99, 'gicu jnjnj')) rental1 = self.serviceRental.createRental(777, 12, 43) self.serviceRental.addRental(rental1) self.assertEqual(1, len(self.serviceRental.getAllRentals())) rental2 = self.serviceRental.createRental(888, 56, 99) self.serviceRental.addRental(rental2) self.assertEqual(2, len(self.serviceRental.getAllRentals())) def test_addRental(self): self.bookRepo.add(Book(12, 'qwert', 'asdfgh', 'zxcvbn', True)) self.bookRepo.add(Book(56, 'mara', 'op', 'slavici', True)) self.clientRepo.add(Client(43, 'MOhl ojsddan')) self.clientRepo.add(Client(99, 'gicu jnjnj')) rental1 = self.serviceRental.createRental(777, 12, 43) self.serviceRental.addRental(rental1) self.assertEqual(1, len(self.serviceRental.getAllRentals())) rental2 = self.serviceRental.createRental(888, 56, 99) self.serviceRental.addRental(rental2) self.assertEqual(2, len(self.serviceRental.getAllRentals())) def test_deleteRental(self): self.bookRepo.add(Book(12, 'qwert', 'asdfgh', 'zxcvbn', True)) self.bookRepo.add(Book(56, 'mara', 'op', 'slavici', True)) self.clientRepo.add(Client(43, 'MOhl ojsddan')) self.clientRepo.add(Client(99, 'gicu jnjnj')) rental1 = self.serviceRental.createRental(777, 12, 43) self.serviceRental.addRental(rental1) rental2 = self.serviceRental.createRental(888, 56, 99) self.serviceRental.addRental(rental2) self.assertEqual(2, len(self.serviceRental.getAllRentals())) self.serviceRental.deleteRental(888) self.assertEqual(1, len(self.serviceRental.getAllRentals())) def test_searchRentalById(self): self.bookRepo.add(Book(12, 'qwert', 'asdfgh', 'zxcvbn', True)) self.bookRepo.add(Book(56, 'mara', 'op', 'slavici', True)) self.clientRepo.add(Client(43, 'MOhl ojsddan')) self.clientRepo.add(Client(99, 'gicu jnjnj')) rental1 = self.serviceRental.createRental(777, 12, 43) self.serviceRental.addRental(rental1) rental2 = self.serviceRental.createRental(888, 56, 99) self.serviceRental.addRental(rental2) rental = self.serviceRental.searchRentalById(777) self.assertEqual(rental, rental1) rental = self.serviceRental.searchRentalById(888) self.assertEqual(rental, rental2) def test_searchRentalByBookId(self): self.bookRepo.add(Book(12, 'qwert', 'asdfgh', 'zxcvbn', True)) self.bookRepo.add(Book(56, 'mara', 'op', 'slavici', True)) self.clientRepo.add(Client(43, 'MOhl ojsddan')) self.clientRepo.add(Client(99, 'gicu jnjnj')) rental1 = self.serviceRental.createRental(777, 12, 43) self.serviceRental.addRental(rental1) rental2 = self.serviceRental.createRental(888, 56, 99) self.serviceRental.addRental(rental2) rental = self.serviceRental.searchRentalByBookId(12) self.assertEqual(rental, rental1) rental = self.serviceRental.searchRentalByBookId(56) self.assertEqual(rental, rental2) def test_returnBook(self): book1 = Book(12, 'qwert', 'asdfgh', 'zxcvbn', True) self.serviceBook.addBook(book1) book2 = Book(56, 'mara', 'op', 'slavici', True) self.serviceBook.addBook(book2) self.serviceClient.addClient(Client(43, 'MOhl ojsddan')) self.serviceClient.addClient(Client(99, 'gicu jnjnj')) rental1 = self.serviceRental.createRental(777, 12, 43) rental2 = self.serviceRental.createRental(888, 56, 99) self.serviceRental.addRental(rental1) self.serviceRental.addRental(rental2) book3 = self.serviceBook.searchBookById(rental1.get_book_id()) self.assertEqual(book3.get_availability(), False) self.serviceRental.returnBook(12) book3 = self.serviceBook.searchBookById(rental1.get_book_id()) self.assertEqual(book3.get_availability(), True) book3 = self.serviceBook.searchBookById(rental2.get_book_id()) self.assertEqual(book3.get_availability(), False) self.serviceRental.returnBook(56) book3 = self.serviceBook.searchBookById(rental2.get_book_id()) self.assertEqual(book3.get_availability(), True) def test_updateRental(self): self.bookRepo.add(Book(12, 'qwert', 'asdfgh', 'zxcvbn', True)) self.serviceBook.addBook(Book(56, 'mara', 'op', 'slavici', True)) self.serviceClient.addClient(Client(43, 'MOhl ojsddan')) self.serviceClient.addClient(Client(99, 'gicu jnjnj')) rental1 = self.serviceRental.createRental(777, 12, 43) self.serviceRental.addRental(rental1) self.serviceRental.updateRental(777, 56, 43) self.assertEqual(56, self.serviceRental.getAllRentals()[0].get_book_id()) self.assertEqual(43, self.serviceRental.getAllRentals()[0].get_client_id())