def test_watchlist_first_movie_in_watchlist(watchlist): assert str(watchlist.first_movie_in_watchlist()) == "<Movie Moana, 2016>" watchlist.remove_movie(Movie("Moana", 2016)) assert str(watchlist.first_movie_in_watchlist()) == "<Movie Ice Age, 2002>" watchlist.remove_movie(Movie("Ice Age", 2002)) assert str(watchlist.first_movie_in_watchlist()) == "<Movie Guardians of the Galaxy, 2012>" watchlist.remove_movie(Movie("Guardians of the Galaxy", 2012)) assert str(watchlist.first_movie_in_watchlist()) == "None"
def test_watchlist_remove_movie(watchlist): watchlist.remove_movie(Movie("Moana", 2016)) watchlist.remove_movie(Movie("Guardians of the Galaxy", 2012)) test_output = "" for movie in watchlist: test_output += str(movie) + "\n" assert test_output.strip() == "<Movie Ice Age, 2002>" assert str(watchlist) == "[<Movie Ice Age, 2002>]"
def test_watchlist_size(watchlist): assert watchlist.size() == 3 watchlist.add_movie(Movie("Fight Club", 1999)) watchlist.add_movie(Movie("Se7en", 1995)) watchlist.add_movie(Movie("Memento", 2001)) assert watchlist.size() == 6 watchlist.remove_movie(Movie("Fight Club", 1999)) watchlist.remove_movie(Movie("Se7en", 1995)) assert watchlist.size() == 4
def test_movie_remove_genre(): movie = Movie("Imception", 2011) genre = Genre("Weird") movie.add_actor(genre) assert len(movie.actors) == 1 movie.remove_actor(genre) assert len(movie.actors) == 0
def test_movie_remove_actor(): movie = Movie("Imception", 2011) actor = Actor("John Doe") movie.add_actor(actor) assert len(movie.actors) == 1 movie.remove_actor(actor) assert len(movie.actors) == 0
def test_genre_remove_movie(): genre = Genre("Weird") movie = Movie("Imception", 2011) genre.add_movie(movie) assert len(genre.movies) == 1 genre.remove_movie(movie) assert len(genre.movies) == 0
def test_director_remove_movie(): director = Director("Christopher Molan") movie = Movie("Imception", 2011) director.add_movie(movie) assert len(director.movies) == 1 director.remove_movie(movie) assert len(director.movies) == 0
def test_actor_remove_movie(): actor = Actor("John Doe") movie = Movie("Imception", 2011) actor.add_movie(movie) assert len(actor.movies) == 1 actor.remove_movie(movie) assert len(actor.movies) == 0
def test_user_remove_review(user): review = Review(arg_user=user, arg_movie=Movie("Imception", 2011), arg_text="It was pretty weird", arg_rating=6) user.add_review(review) assert len(user.reviews) == 1 user.remove_review(review) assert len(user.reviews) == 0
def read_csv_file(self): try: print("PROCESSING CSV FILE...") csvfile = open(self.file_name, encoding='utf-8-sig', newline='') reader = csv.DictReader(csvfile) for row in reader: # STEP ONE: Create isolated object references movie = Movie( arg_title=row['Title'].strip(), arg_year=int(row['Year'].strip()), arg_description=row['Description'].replace("\"", "'"), arg_runtime_minutes=int(row['Runtime (Minutes)']), arg_rating=float(row['Rating']), arg_votes=int(row['Votes'])) director = self.get_director(row['Director'].strip()) actors = [ self.get_actor(actor.strip()) for actor in row['Actors'].split(",") ] genres = [ self.get_genre(genre.strip()) for genre in row['Genre'].split(",") ] # STEP TWO: Populate object relationships director.add_movie(movie) movie.add_director(director) for actor in actors: actor.add_movie(movie) movie.add_actor(actor) for other_actor in actors: if other_actor is not actor: actor.add_actor_colleague(other_actor) for genre in genres: genre.add_movie(movie) movie.add_genre(genre) # STEP THREE: Store objects self.reader_movies.append(movie) self.reader_directors.add(director) self.reader_actors.update(set(actors)) self.reader_genres.update(set(genres)) csvfile.close() print("CSV FILE PROCESSED") except Exception as err: raise Exception(f"Error while reading CSV file:\n{err}")
def read_csv_file(self): try: csvfile = open(self.file_name, encoding='utf-8-sig', newline='') reader = csv.DictReader(csvfile) for row in reader: try: movie = Movie(row['Title'].strip(), int(row['Year'].strip())) movie.description = row['Description'] director = Director(row['Director'].strip()) movie.director = director director.add_movie(movie) actors = { Actor(actor.strip()) for actor in row['Actors'].split(",") } for actor in actors: movie.add_actor(actor) actor.add_movie(movie) genres = { Genre(genre.strip()) for genre in row['Genre'].split(",") } for genre in genres: movie.add_genre(genre) genre.add_movie(movie) movie.runtime_minutes = int(row['Runtime (Minutes)']) movie.rating = float(row['Rating']) movie.votes = int(row['Votes']) self.movies.append(movie) self.actors.update(set(actors)) self.directors.add(director) self.genres.update(set(genres)) except: continue # Skips movies with invalid formatting csvfile.close() except: raise Exception("Error while reading CSV file!")
def watchlist(user): watchlist = Watchlist(user.code) watchlist.add_movie(Movie("Moana", 2016)) watchlist.add_movie(Movie("Ice Age", 2002)) watchlist.add_movie(Movie("Guardians of the Galaxy", 2012)) return watchlist
def test_user_remove_review(user): review = Review(user, Movie("Imception", 2011), "It was pretty weird", 6) user.add_review(review) assert len(user.reviews) == 1 user.remove_review(review) assert len(user.reviews) == 0
def test_review_init(user): review = Review(user, Movie("Imception", 2011), "It was pretty weird", 6) assert str(review.user) == "<User bob>" assert str(review.movie) == "<Movie Imception, 2011>" assert str(review.text) == "It was pretty weird"
def test_review_init(user): review = Review(arg_user=user, arg_movie=Movie("Imception", 2011), arg_text="It was pretty weird", arg_rating=6) assert str(review.user) == "<User bob>" assert str(review.movie) == "<Movie Imception, 2011>" assert str(review.text) == "It was pretty weird"
def test_user_watch_movie(user): movie = Movie("Imception", 2011) movie.runtime_minutes = 120 user.watch_movie(movie) assert movie in user.watched_movies assert user.time_spent_watching_movies_minutes == movie.runtime_minutes
def load(self, engine): print("LOADING DATABASE...") conn = engine.raw_connection() cursor = conn.cursor() # STEP ONE: Retrieve entries from database cursor.execute("""SELECT * FROM actors""") actors = cursor.fetchall() cursor.execute("""SELECT * FROM directors""") directors = cursor.fetchall() cursor.execute("""SELECT * FROM genres""") genres = cursor.fetchall() cursor.execute("""SELECT * FROM movies""") movies = cursor.fetchall() cursor.execute("""SELECT * FROM reviews""") reviews = cursor.fetchall() cursor.execute("""SELECT * FROM users""") users = cursor.fetchall() cursor.execute("""SELECT * FROM watchlists""") watchlists = cursor.fetchall() # STEP TWO: Convert entries into isolated object references for row in actors: actor = Actor(row[1], list(), row[2], list(), row[3], row[4]) self.repo_actors.add(actor) for row in directors: director = Director(row[1], list(), row[2], row[3]) self.repo_directors.add(director) for row in genres: genre = Genre(row[1], list(), row[2], row[3]) self.repo_genres.add(genre) for row in movies: movie = Movie(row[1], row[2], row[3], None, row[4], list(), row[5], list(), row[6], row[7], list(), row[8], row[9], float(row[10]), row[11], row[12]) self.repo_movies.append(movie) for row in reviews: review = Review(None, row[1], None, row[2], row[3], row[4], row[5], row[6], row[7]) self.repo_reviews.append(review) for row in users: user = User(row[1], row[2], list(), row[3], list(), row[4], row[5], None, row[6], row[7]) self.repo_users.append(user) for row in watchlists: watchlist = Watchlist(row[1], list(), row[2], row[3]) self.repo_watchlists.append(watchlist) # STEP THREE: Populate object relationships for actor in self.repo_actors: for code in actor.movie_codes.split(","): if code != "": actor.add_movie(self.find_movie(code)) for code in actor.colleague_codes.split(","): if code != "": actor.add_actor_colleague(self.find_actor(code)) for director in self.repo_directors: for code in director.movie_codes.split(","): if code != "": director.add_movie(self.find_movie(code)) for genre in self.repo_genres: for code in genre.movie_codes.split(","): if code != "": genre.add_movie(self.find_movie(code)) for movie in self.repo_movies: movie.director = self.find_director(movie.director_code) for code in movie.actor_codes.split(","): if code != "": movie.add_actor(self.find_actor(code)) for code in movie.genre_codes.split(","): if code != "": movie.add_genre(self.find_genre(code)) for code in movie.review_codes.split(","): if code != "": movie.add_review(self.find_review(code)) for review in self.repo_reviews: review.user = self.find_user(review.user_code) review.movie = self.find_movie(review.movie_code) for user in self.repo_users: for code in user.watched_movie_codes.split(","): if code != "": user.watched_movies.append(self.find_movie( code)) # Can't use watch_movie() for this for code in user.review_codes.split(","): if code != "": user.add_review(self.find_review(code)) user.watchlist = self.find_watchlist(user.watchlist_code) for watchlist in self.repo_watchlists: for code in watchlist.movie_codes.split(","): if code != "": watchlist.add_movie(self.find_movie(code)) conn.commit() conn.close() print("DATABASE LOADED")