class TestClient(TestCase): def setUp(self): self.client = ClientDAO("Dani") def tearDown(self): self.client = None def test_init(self): with self.assertRaises(ValueError): testClient = ClientDAO(1) with self.assertRaises(ValueError): testClient = ClientDAO([]) self.assertEqual(self.client.getName(), "Dani", 'client name got wrong') self.assertRaises(TypeError, lambda: self.client.getId(), 'default client id not None') def test_setClientId(self): self.client.setClientId(5) self.assertEqual(self.client.getId(), 5, "client id set wrong") def test_setWrongId(self): self.assertRaises(ValueError, lambda: self.client.setClientId(-3)) self.assertRaises(ValueError, lambda: self.client.setClientId([])) self.assertRaises(ValueError, lambda: self.client.setClientId("asd")) self.assertRaises(ValueError, lambda: self.client.setClientId(3.5)) self.assertRaises(ValueError, lambda: self.client.setClientId({})) def test_setClientIdSecondTime(self): self.client.setClientId(6) with self.assertRaises(AlreadySetException): self.client.setClientId(9)
def __loadRepo(self): self.__loadFileReadMode() for line in self.__file: splitLine = line.split() clientToAdd = ClientDAO(splitLine[1]) clientToAdd.setClientId(int(splitLine[0])) super().addClientWithId(clientToAdd) self.__closeFile()
def test_undoUpdateClient(self): updatedClient = ClientDAO("aaa") updatedClient.setClientId(0) self.undoRunner.addCommandToUndo(["update", "0", "aaa"], self.clientController, self.stack, "client", self.commandsStack) self.clientController.updateClientWithId(0, updatedClient) self.assertEqual(self.clientController.getClientWithId(0).getName(), "aaa") self.undoRunner.undo(self.clientController, self.movieController, self.rentalController, self.stack) self.assertEqual(self.clientController.getClientWithId(0).getName(), "Dani")
def test_updateClient(self): self.clientFileRepository.addClient(ClientDAO("damo")) self.clientFileRepository.addClient(ClientDAO("ancu")) testClient1 = ClientDAO("ancu") testClient1.setClientId(0) testClient2 = ClientDAO("ancu") testClient2.setClientId(1) self.clientFileRepository.updateClientWithId(0, ClientDAO("ancu")) self.assertEqual(self.clientFileRepository.getList(), [testClient1, testClient2])
def test_removeClient(self): self.clientFileRepository.addClient(ClientDAO("damo")) self.clientFileRepository.addClient(ClientDAO("ancu")) self.clientFileRepository.removeClientWithId(1) self.assertEqual(len(self.clientFileRepository.getList()), 1) testClient = ClientDAO("damo") testClient.setClientId(0) self.assertEqual(self.clientFileRepository.getList(), [testClient]) with self.assertRaises(ObjectNotInCollectionException): self.clientFileRepository.removeClientWithId(5)
def test_sortByAttribute(self): client1 = ClientDAO("a") client1.setClientId(1) client2 = ClientDAO("b") client2.setClientId(0) testList = [client1, client2] sortListByObjectAttribute(testList, lambda a, b: True if a < b else False, lambda a: a.getId()) self.assertEqual(testList, [client2, client1]) sortListByObjectAttribute(testList, lambda a, b: True if a > b else False, lambda a: a.getId()) self.assertEqual(testList, [client1, client2])
def test_updateClient(self): printer = Printer() printer.printList(self.clientRepo.getList()) print("ccccccccccc") self.clientRepo.addClient(ClientDAO("damo")) self.clientRepo.addClient(ClientDAO("ancu")) testClient1 = ClientDAO("ancu") testClient1.setClientId(0) testClient2 = ClientDAO("ancu") testClient2.setClientId(1) self.clientRepo.updateClientWithId(0, ClientDAO("ancu")) self.assertEqual(self.clientRepo.getList(), [testClient1, testClient2])
def test_removeClient(self): printer = Printer() printer.printList(self.clientRepo.getList()) print("bbbbb") self.clientRepo.addClient(ClientDAO("damo")) self.clientRepo.addClient(ClientDAO("ancu")) self.clientRepo.removeClientWithId(1) self.assertEqual(len(self.clientRepo.getList()), 1) testClient = ClientDAO("damo") testClient.setClientId(0) self.assertEqual(self.clientRepo.getList(), [testClient]) with self.assertRaises(ObjectNotInCollectionException): self.clientRepo.removeClientWithId(5)
def test_hasClientWithId(self): client1 = ClientDAO("aaa") client1.setClientId(1) client2 = ClientDAO("bb") client2.setClientId(2) client3 = ClientDAO("958") client3.setClientId(3) self.clientFileRepository.addClientWithId(client1) self.clientFileRepository.addClientWithId(client2) self.clientFileRepository.addClientWithId(client3) self.assertEqual(self.clientFileRepository.hasClientWithId(1), True) self.assertEqual(self.clientFileRepository.hasClientWithId(4), False)
def test_getList(self): client1 = ClientDAO("aaa") client1.setClientId(1) client2 = ClientDAO("bb") client2.setClientId(2) client3 = ClientDAO("958") client3.setClientId(4) self.clientFileRepository.addClientWithId(client1) self.clientFileRepository.addClientWithId(client2) self.clientFileRepository.addClientWithId(client3) self.assertEqual(self.clientFileRepository.getList(), [client1, client2, client3])
def addClient(self, command, clientController): # return "client" "add" "client id" "client name" client = ClientDAO(command[3]) client.setClientId(int(command[2])) clientController.addClientWithId(client)
def updateClient(self, command, clientController): # return "client" "update" "client id" "old name" updatedClient = ClientDAO(command[3]) updatedClient.setClientId(int(command[2])) clientController.updateClientWithId(int(command[2]), updatedClient)