def testRepo(self): movRep = repo() with self.assertRaises(Exception): movRep.listElements() movRep.addElement(movie(1, "Titanic", "Nice", "Romance")) movRep.addElement(movie(2, "Avatar", "Good movie", "Sci-Fi")) movRep.addElement(movie(3, "The Matrix", "WoW", "Action")) movRep.addElement( movie(4, "The Wizard of Oz", "Excelent!", "Adventure")) movRep.addElement(movie(5, "Star Wars", "Exceptional!", "Sci-Fi")) with self.assertRaises(Exception): movRep.addElement(movie(2, "d", "d", "Romance")) movRep.removeElement(2) with self.assertRaises(Exception): movRep.removeElement(2) movRep.updateElement(3, movie(3, "The Matrix", "wow", "Sci-Fi")) self.assertEqual(movRep.getList()[3], movie(3, "The Matrix", "wow", "Sci-Fi")) with self.assertRaises(Exception): movRep.updateElement(2, movie(3, "The Matrix", "wow", "Sci-Fi")) with self.assertRaises(Exception): a = movRep.getByID(2)
def readFromFile(self, fileName): f = open(fileName, "r") s = f.read() List = json.loads(s) for i in List.keys(): obj = List[i] if self._instace == client: cl = client(obj['id'], obj['name']) self.addElement(cl) elif self._instace == movie: mv = movie(obj['id'], obj['title'], obj['description'], obj['genre']) self.addElement(mv) else: rentDate = date(int(obj['rentDate'][:4]), int(obj['rentDate'][5:7]), int(obj['rentDate'][8:])) dueDate = date(int(obj['dueDate'][:4]), int(obj['dueDate'][5:7]), int(obj['dueDate'][8:])) if obj['returnDate'] == '0': rt = rental(obj['movieID'], obj['clientID'], rentDate, dueDate) else: returnDate = date(int(obj['returnDate'][:4]), int(obj['returnDate'][5:7]), int(obj['returnDate'][8:])) rt = rental(obj['movieID'], obj['clientID'], rentDate, dueDate, returnDate) self.addElement(rt)
def updateMovie(self, params, recordForUndo=True): """ updates the title of a movie with a given ID input: the params read from the keyboard raise: error if there are missing parameters """ if params == "" or len(params) < 2: raise TypeError("Missing parameters!") ID = params[0] title = params[1] description = params[2] genre = params[3] self.validateNr(ID) if recordForUndo == True: mv = self._movieRepo.getByID(int(ID)) undo = functionCall(self.updateMovie, [ str(mv.getID()), mv.getTitle(), mv.getDescription(), mv.getGenre() ], False) redo = functionCall(self.updateMovie, [ID, title, description, genre], False) operation = op(redo, undo) self._undoController.recordOperation(operation) self._movieRepo.updateElement( int(ID), movie(int(ID), title, description, genre))
def testMovieObject(self): mv = movie(1, "Titanic", "cel mai bun", "Comedy") self.assertEqual(mv.getTitle(), "Titanic") self.assertEqual(mv.getDescription(), "cel mai bun") self.assertEqual(mv.getGenre(), "Comedy") mv.updateTitle("Fight Club") self.assertEqual(mv.getTitle(), "Fight Club") mv.updateDescription("smecher") self.assertEqual(mv.getDescription(), "smecher") mv.updateGenre("Action") mv.getGenre() == "Action" mv.updateID(2) a = str(mv) a = mv.getSearchStr() with self.assertRaises(Exception): mv.updateGenre("drama") with self.assertRaises(Exception): mv = movie(1, "Titanic", "creative movie", "drama") with self.assertRaises(Exception): mv = movie(1)
def buildList(self): for i in self._data: if self._instance == client: self.addElement(client(i[0], i[1])) elif self._instance == movie: self.addElement(movie(i[0], i[1], i[2], i[3])) else: rentDate = date(int(i[2][:4]), int(i[2][5:7]), int(i[2][8:])) dueDate = date(int(i[3][:4]), int(i[3][5:7]), int(i[3][8:])) if i[4] == '0': rt = rental(i[1], i[0], rentDate, dueDate) else: returnDate = date(int(i[4][:4]), int(i[4][5:7]), int(i[4][8:])) rt = rental(i[1], i[0], rentDate, dueDate, returnDate) self.addElement(rt)
def testRentObject(self): mv = movie(1, "Titanic", "Nice", "Romance") cl = client(1, "Poo Mircea") rent = rental(1, 1, date(2017, 11, 11), date(2017, 11, 13)) self.assertEqual(rent.getID(), rental.ID) self.assertEqual(rent.getMovieID(), 1) self.assertEqual(rent.getClientID(), 1) self.assertEqual(rent.getRentDate(), date(2017, 11, 11)) self.assertEqual(rent.getDueDate(), date(2017, 11, 13)) self.assertEqual(rent.getReturnDate(), 0) a = str(rent) rent.updateReturnDate(date(2017, 11, 14)) self.assertEqual(rent.getReturnDate(), date(2017, 11, 14)) a = str(rent) with self.assertRaises(Exception): a = rental(1, 1, date(2017, 11, 11), date(2017, 11, 10))
def readFromFile(self, fileName): f = open(fileName, "r") n = int(f.readline().strip()) for i in range(n): new = f.readline().strip() new = new.split(',') if self._instance == client: self.addElement(client(int(new[0]), new[1])) elif self._instance == movie: self.addElement(movie(int(new[0]), new[1], new[2], new[3])) else: if new[5] == "0": rt = rental(int(new[2]), int(new[1]), self.buildDate(new[3]), self.buildDate(new[4])) else: rt = rental(int(new[2]), int(new[1]), self.buildDate(new[3]), self.buildDate(new[4]), self.buildDate(new[5])) self.addElement(rt)
def addMovie(self, params, recordForUndo=True): """ adds a new movie to the store """ if params == "" or len(params) < 3: raise TypeError("Missing parameters!") ID = params[0] name = params[1] description = params[2] genre = params[3] self.validateNr(ID) mv = movie(int(ID), name, description, genre) self._movieRepo.addElement(mv) if recordForUndo == False: return undo = functionCall(self._movieRepo.removeElement, int(ID)) redo = functionCall(self.addMovie, [ID, name, description, genre], False) operation = op(redo, undo) self._undoController.recordOperation(operation)