def dict_to_movie(dict): movie = Movie(dict.title, dict.release_year) movie.rank = dict.rank movie.description = dict.description movie.director = dict_to_director(dict.director) movie.actors = dict_to_actors(dict.actors) movie.genres = dict_to_genres(dict.genres) movie.runtime_minutes = dict.runtime_minutes movie.rating = dict.rating return movie
def test_properties(movie): movie1 = Movie("Moana", 2016) movie1.title = "Hokage" # legal title assert repr(movie1) == "<Movie Hokage, 2016>" movie1.title = 1234 # illegal title assert repr(movie1) == "<Movie Hokage, 2016>" movie2 = Movie("Raikage", 2004) movie2.description = " Faster than speed of light for real " # legal description assert movie2.description == "Faster than speed of light for real" movie2.description = "" # illegal description assert movie2.description == "Faster than speed of light for real" movie3 = Movie("Moana", 2016) actor = Actor("Jacinda Adern") director = Director("Ron Clements") movie3.director = actor #illegal director assert movie3.director is None movie3.director = director #legal director assert repr(movie3.director) == "<Director Ron Clements>" actors = [ Actor("Auli'i Cravalho"), Actor("Dwayne Johnson"), Actor("Rachel House"), Actor("Temuera Morrison") ] for actor in actors: movie3.add_actor(actor) ##legal adding actor assert str( movie3.actors ) == "[<Actor Auli'i Cravalho>, <Actor Dwayne Johnson>, <Actor Rachel House>, <Actor Temuera Morrison>]" movie3.add_actor(director) ##illegal adding actor assert str( movie3.actors ) == "[<Actor Auli'i Cravalho>, <Actor Dwayne Johnson>, <Actor Rachel House>, <Actor Temuera Morrison>]" movie3.remove_actor(Actor("Rachel House")) ##legal remove actor assert str( movie3.actors ) == "[<Actor Auli'i Cravalho>, <Actor Dwayne Johnson>, <Actor Temuera Morrison>]" movie3.remove_actor(director) ##illegal remove actor assert str( movie3.actors ) == "[<Actor Auli'i Cravalho>, <Actor Dwayne Johnson>, <Actor Temuera Morrison>]" movie3.actors = Actor("Dwayne Johnson") ##test setter assert str(movie3.actors) == "[<Actor Dwayne Johnson>]" genres = [ Genre("Comedy"), Genre("Action"), Genre("Disney"), Genre("Romantic") ] for genre in genres: movie3.add_genre(genre) ##legal adding genre assert str( sorted(movie3.genres) ) == "[<Genre Action>, <Genre Comedy>, <Genre Disney>, <Genre Romantic>]" movie3.add_genre(director) ##illegal adding genre assert str( movie3.genres ) == "[<Genre Comedy>, <Genre Action>, <Genre Disney>, <Genre Romantic>]" movie3.remove_genre(Genre("Romantic")) ##legal remove genre assert str( movie3.genres) == "[<Genre Comedy>, <Genre Action>, <Genre Disney>]" movie3.remove_genre(director) ##illegal remove genre assert str( movie3.genres) == "[<Genre Comedy>, <Genre Action>, <Genre Disney>]" movie3.genres = Genre("Comedy") ##test setter assert str(movie3.genres) == "[<Genre Comedy>]" movie3.runtime_minutes = 107 ## legal runtime assert "Movie runtime: {} minutes".format( movie3.runtime_minutes) == "Movie runtime: 107 minutes" with pytest.raises(ValueError): movie3.runtime_minutes = -1 ## illegal runtime ###################################### test extension ###################################### movie3.rank = 185 ## legal rank assert movie3.rank == 185 with pytest.raises(ValueError): movie3.rank = -1 ## illegal rank movie3.rating = 8.1 ## legal rating assert movie3.rating == 8.1 with pytest.raises(ValueError): movie3.rating = 11 ## illegal rating movie3.votes = 107583 ## legal votes assert movie3.votes == 107583 with pytest.raises(ValueError): movie3.votes = -1 ## illegal votes movie3.revenue_millions = 510.365 ## legal revenue_millions assert movie3.revenue_millions == 510.365 with pytest.raises(ValueError): movie3.revenue_millions = -510.365 ## illegal revenue_millions movie3.metascore = 91.6 ## legal metascore assert movie3.metascore == 91.6 with pytest.raises(ValueError): movie3.metascore = -91.6 ## illegal metascore
def load_movies(data_path: str, repo: MemoryRepository): dataset_of_movies = [] dataset_of_actors = [] dataset_of_directors = [] dataset_of_genres = [] dictionary_genres = {} # extension dictionary_actors = {} # extension dictionary_directors = {} # extension dictionary_years = {} ignore_row = 0 for row in read_csv_file(os.path.join(data_path, 'Data1000Movies.csv')): if ignore_row == 0: ignore_row += 1 continue rank = int(row[0]) title = row[1] genres = row[2].split(',') description = row[3] director = Director(str(row[4])) actors = row[5].split(',') release_year = int(row[6]) runtime_minutes = int(row[7]) rating = float(row[8]) votes = int(row[9]) revenue_millions = row[10] metascore = row[11] image_hyperlink = row[12] # create movie object movie = Movie(title, release_year) dataset_of_movies.append(movie) if movie.year not in dictionary_years: dictionary_years[movie.year] = [movie] # extension else: dictionary_years[movie.year].append(movie) # add actors for actor in actors: actor_obj = Actor(actor) movie.add_actor(actor_obj) if actor_obj not in dataset_of_actors: dataset_of_actors.append(actor_obj) dictionary_actors[actor_obj] = [movie] # extension else: dictionary_actors[actor_obj].append(movie) # extension # add director movie.director = director if director not in dataset_of_directors: dataset_of_directors.append(director) dictionary_directors[director] = [movie] else: dictionary_directors[director].append(movie) # add genre for genre in genres: genre_obj = Genre(genre) movie.add_genre(genre_obj) if genre_obj not in dataset_of_genres: dataset_of_genres.append(genre_obj) dictionary_genres[genre_obj] = [movie] # extension else: dictionary_genres[genre_obj].append(movie) # extension # add description movie.description = description # add runtime movie.runtime_minutes = runtime_minutes # add rank movie.rank = rank # add rating movie.rating = rating # add votes movie.votes = votes # add revenue_million movie.revenue_millions = revenue_millions # add metascore movie.metascore = metascore # add metascore movie.image_hyperlink = image_hyperlink # Add the Movie to the repository. repo.add_movie(movie) repo.add_year(dictionary_years) repo.add_genres(dictionary_genres) repo.add_director(dictionary_directors) repo.add_actor(dictionary_actors)