class TestSorting(unittest.TestCase): Client1 = Client(1, "John") Client2 = Client(2, "Mike") Client3 = Client(3, "Nike") Movie1 = Movie(1, "Title1", "Desc1", "Genre1") Movie2 = Movie(2, "Title2", "Desc2", "Genre2") Movie3 = Movie(3, "Title3", "Desc3", "Genre3") def __reset(self): self.clients = Repository() self.movies = Repository() self.rentals = Repository() self.service = Service(self.clients, self.movies, self.rentals) self.service.addClient(["John"]) self.service.addClient(["Mike"]) self.service.addClient(["Nike"]) self.service.addMovie(["Title1", "Desc1", "Genre1"]) self.service.addMovie(["Title2", "Desc2", "Genre2"]) self.service.addMovie(["Title3", "Desc3", "Genre3"]) def testActiveClients(self): self.__reset() self.service.rentMovie([1, 1, date(2019, 10, 20), date(2019, 10, 29)]) self.service.rentMovie([2, 2, date(2019, 11, 20), date(2019, 11, 30)]) self.service.rentMovie([3, 1, date(2019, 10, 30), date(2019, 11, 12)]) result = self.service.mostActive(["client"]) self.assertEqual(result, [self.Client1, self.Client3, self.Client2]) def testRentedMovies(self): self.__reset() self.service.rentMovie([1, 1, date(2019, 10, 20), date(2019, 10, 29)]) self.service.rentMovie([2, 2, date(2019, 11, 20), date(2019, 11, 30)]) self.service.rentMovie([3, 1, date(2019, 10, 30), date(2019, 11, 12)]) result = self.service.mostActive(["movie"]) self.assertEqual(result, [self.Movie1, self.Movie2, self.Movie3]) def testLateRentals(self): self.__reset() self.service.rentMovie([1, 1, date(2019, 10, 20), date(2019, 10, 29)]) self.service.rentMovie([2, 2, date(2019, 11, 20), date(2019, 11, 24)]) self.service.rentMovie([3, 3, date(2018, 10, 20), date(2018, 10, 29)]) Rental1 = Rental(1, 1, 1, date(2019, 10, 20), date(2019, 10, 29), None) Rental2 = Rental(2, 2, 2, date(2019, 11, 20), date(2019, 11, 24), None) Rental3 = Rental(3, 3, 3, date(2018, 10, 20), date(2018, 10, 29), None) result = self.service.lateRentals([]) self.assertEqual(result, [Rental3, Rental1, Rental2]) self.service.returnMovie([3, 3, 3]) self.service.returnMovie([2, 2, 2]) self.service.returnMovie([1, 1, 1])
class TestClient(unittest.TestCase): def testAdd(self): self.clientRepo = ClientListGenerator().chooseClients() self.service = Service(self.clientRepo, [], []) self.lastID = self.clientRepo.ID self.service.addClient(["Hamilton"]) self.service.addClient(["Bottas"]) self.service.addClient(["Verstappen"]) self.service.addClient(["Leclerc"]) self.service.addClient(["Vettel"]) self.assertEqual(self.clientRepo.ID, self.lastID + 5) self.assertEqual(len(self.clientRepo), self.lastID + 5) self.assertEqual(self.clientRepo[self.lastID + 1], Client(self.lastID + 1, "Hamilton")) self.assertEqual(self.clientRepo[self.lastID + 2], Client(self.lastID + 2, "Bottas")) self.assertEqual(self.clientRepo[self.lastID + 3], Client(self.lastID + 3, "Verstappen")) self.assertEqual(self.clientRepo[self.lastID + 4], Client(self.lastID + 4, "Leclerc")) self.assertEqual(self.clientRepo[self.lastID + 5], Client(self.lastID + 5, "Vettel")) def testRemove(self): self.clientRepo = ClientListGenerator().chooseClients() self.lastID = self.clientRepo.ID #initially the nr of clients = ID of the last client = .ID del self.clientRepo[1] del self.clientRepo[2] del self.clientRepo[3] self.assertEqual(len(self.clientRepo), self.lastID - 3) self.assertEqual(self.clientRepo[4].ID, 4) self.assertEqual(self.clientRepo[5].ID, 5) self.assertEqual(self.clientRepo[6].ID, 6) del self.clientRepo[4] del self.clientRepo[5] del self.clientRepo[6] self.assertEqual(len(self.clientRepo), self.lastID - 6) self.assertEqual(self.clientRepo[7].ID, 7) self.assertEqual(self.clientRepo[8].ID, 8) self.assertEqual(self.clientRepo[9].ID, 9) def testUpdate(self): self.clientRepo = ClientListGenerator().chooseClients() self.lastID = self.clientRepo.ID #initially the nr of clients = ID of the last client = .ID self.service = Service(self.clientRepo, [], []) self.service.updateClient([1, "name", "Albon"]) self.service.updateClient([2, "name", "Gasly"]) self.service.updateClient([3, "name", "Kimi"]) self.service.updateClient([4, "name", "Ricciardo"]) self.service.updateClient([5, "name", "Sainz"]) self.assertEqual(len(self.clientRepo), self.lastID) self.assertEqual(self.clientRepo.ID, self.lastID) self.assertEqual(self.clientRepo[1], Client(1, "Albon")) self.assertEqual(self.clientRepo[2], Client(2, "Gasly")) self.assertEqual(self.clientRepo[3], Client(3, "Kimi")) self.assertEqual(self.clientRepo[4], Client(4, "Ricciardo")) self.assertEqual(self.clientRepo[5], Client(5, "Sainz"))
class TestSearch(unittest.TestCase): def testSearchClient(self): self.clients = Repository() self.service = Service(self.clients, [], Repository()) self.service.addClient(["John"]) self.service.addClient(["Mike"]) self.service.addClient(["zmaili"]) self.service.addClient(["Johnny"]) result = self.service.searchClients([2, True]) self.assertEqual(result, [self.clients[2]]) self.assertEqual(len(self.clients), 4) self.assertEqual(self.clients[2], Client(2, "Mike")) result = self.service.searchClients(["john", False]) self.assertEqual(result, [self.clients[1], self.clients[4]]) self.assertEqual(len(self.clients), 4) self.assertEqual(self.clients[1], Client(1, "John")) self.assertEqual(self.clients[4], Client(4, "Johnny")) result = self.service.searchClients(["luca", False]) self.assertEqual(result, []) self.assertEqual(len(self.clients), 4) self.assertRaises(EmptyError, self.service.searchClients, ["john", True]) self.service.removeClient([2]) self.assertRaises(EmptyError, self.service.searchClients, [2, True]) result = self.service.searchClients([3, True]) self.assertEqual(result, [self.clients[3]]) self.assertEqual(len(self.clients), 3) self.assertEqual(self.clients[3], Client(3, "zmaili")) def testSearchMovie(self): self.movies = Repository() self.service = Service([], self.movies, Repository()) self.service.addMovie(["title1", "desc1", "genre1"]) self.service.addMovie(["tiitle2", "deesc2", "geenre2"]) self.service.addMovie(["title 3", "desc 3", "genre 3"]) result = self.service.searchMovies(["title", False]) self.assertEqual(result, [self.movies[1], self.movies[3]]) self.assertEqual(len(self.movies), 3) self.assertEqual(self.movies[1], Movie(1, "title1", "desc1", "genre1")) self.assertEqual(self.movies[2], Movie(2, "tiitle2", "deesc2", "geenre2")) self.assertEqual(self.movies[3], Movie(3, "title 3", "desc 3", "genre 3")) result = self.service.searchMovies(["taitle", False]) self.assertEqual(result, []) self.assertEqual(len(self.movies), 3) self.service.removeMovie([2]) self.assertRaises(EmptyError, self.service.searchMovies, [2, True]) self.assertRaises(EmptyError, self.service.searchMovies, ["john", True]) result = self.service.searchMovies([3, True]) self.assertEqual(result, [self.movies[3]]) self.assertEqual(len(self.movies), 2) self.assertEqual(self.movies[1], Movie(1, "title1", "desc1", "genre1")) self.assertEqual(self.movies[3], Movie(3, "title 3", "desc 3", "genre 3")) result = self.service.searchMovies(["tITlE", False]) self.assertEqual(result, [self.movies[1], self.movies[3]]) self.assertEqual(len(self.movies), 2) self.assertEqual(self.movies[1], Movie(1, "title1", "desc1", "genre1")) self.assertEqual(self.movies[3], Movie(3, "title 3", "desc 3", "genre 3"))