def test_runtime_string(): movie = Movie("Sharknado", 2020) assert movie.get_runtime_string() == '' movie.runtime_minutes = 61 assert movie.get_runtime_string() == '1h 1m' movie.runtime_minutes = 59 assert movie.get_runtime_string() == '59m'
def test_runtime(): movie = Movie("Sharknado", 2020) movie.runtime_minutes = 20 assert movie.runtime_minutes == 20 movie = Movie("Sharknado", 2020) with pytest.raises(ValueError): movie.runtime_minutes = 0 movie = Movie("Sharknado", 2020) movie.runtime_minutes = "20" assert movie.runtime_minutes == None
def test_add_review(): # Create instance movie = Movie("Test", 2020) movie.runtime_minutes = 120 user = User("Host", "password123") watch_simulation = MovieWatchingSimulation(movie, user) review = Review(user, movie, "wow!", 10) # Test works as intended watch_simulation.add_review(review, user) assert watch_simulation.reviews == [review] assert user.reviews == [review] # Test for invalid inputs user2 = "Host" review2 = "wow!" watch_simulation.add_review(review2, user) assert watch_simulation.reviews == [review] watch_simulation.add_review(review, user2) assert watch_simulation.reviews == [review] # Test for movie which isn't the movie being watched movie2 = Movie("Test2", 2020) review3 = Review(user, movie2, "wow!", 10) watch_simulation.add_review(review3, user) assert watch_simulation.reviews == [review] assert user.reviews == [review] # Test for user not in the party leaving a review user3 = User("Random User", "password123") review4 = Review(user3, movie, "hacked", 1) watch_simulation.add_review(review4, user3) assert watch_simulation.reviews == [review]
def test_add_user(): # Create instance movie = Movie("Test", 2020) movie.runtime_minutes = 120 user = User("Host", "password123") watch_simulation = MovieWatchingSimulation(movie, user) # Add users guest1 = User("Guest1", "password123") guest2 = User("Guest2", "password123") guest3 = User("Guest3", "password123") guest4 = "Guest4" # invalid # Good input watch_simulation.add_user(guest1) assert watch_simulation.users_watching == {user, guest1} assert movie in guest1.watched_movies # Good input watch_simulation.add_user(guest2) assert watch_simulation.users_watching == {user, guest1, guest2} assert movie in guest2.watched_movies # Good input watch_simulation.add_user(guest3) assert watch_simulation.users_watching == {user, guest1, guest2, guest3} assert movie in guest3.watched_movies # Invalid input watch_simulation.add_user(guest4) assert watch_simulation.users_watching == {user, guest1, guest2, guest3}
def test_init(): # Create instance movie = Movie("Test", 2020) movie.runtime_minutes = 120 user = User("Host", "password123") watch_simulation = MovieWatchingSimulation(movie, user) # Uses the properties to check valid init assert watch_simulation.movie == movie assert watch_simulation.reviews == [] assert watch_simulation.users_watching == {user} # Check movie was added to users watch list assert movie in user.watched_movies # create bad instances movie2 = "Test" user2 = "Host" with pytest.raises(TypeError): watch_simulation2 = MovieWatchingSimulation(movie2, user) watch_simulation3 = MovieWatchingSimulation(movie, user2)
def load_movie_file(repository: MemoryRepository, filename: str): with open(filename, mode="r", encoding="utf-8-sig") as csvfile: reader = csv.DictReader(csvfile) directors = dict() genres = dict() actors = dict() for row in reader: # Create and populate Movie object movie = Movie( title=row["Title"].strip(), release_year=int(row["Year"]) ) movie.description = row["Description"].strip() movie.runtime_minutes = int(row["Runtime (Minutes)"]) movie.rating = float(row["Rating"]) movie.votes = int(row["Votes"]) movie.revenue_in_millions = row["Revenue (Millions)"] movie.metascore = row["Metascore"] if "Image URL" in row: movie.image_link = row["Image URL"] # Add bidirectional link between director and movie director_name=row["Director"].strip() try: director = directors[director_name] except KeyError: director = Director(director_full_name=director_name) directors[director_name] = director make_movie_director_association(movie, director) # Add bidirectional link between movie and genres for name in row["Genre"].split(","): genre_name = name.strip() try: genre = genres[genre_name] except KeyError: genre = Genre(genre_name=genre_name) genres[genre_name] = genre make_movie_genre_association(movie, genre) # Add bidirectional link between actors and movies for name in row["Actors"].split(","): actor_name = name.strip() try: actor = actors[actor_name] except KeyError: actor = Actor(actor_full_name=actor_name) actors[actor_name] = actor make_movie_actor_association(movie, actor) # Add movie object to repository repository.add_movie(movie) # Add directors to repository for director in directors.values(): repository.add_director(director) # Add genres to repository for genre in genres.values(): repository.add_genre(genre) # Add actors to repository for actor in actors.values(): repository.add_actor(actor)