def test_saving_genred_movie(empty_session): movie = make_movie() genre = make_genre() # Establish the bidirectional relationship between the movie and the Genre. make_genre_association(movie, genre) # Persist the Movie (and Genre). # Note: it doesn't matter whether we add the Genre or the Movie. They are connected # bidirectionally, so persisting either one will persist the other. empty_session.add(movie) empty_session.commit() # Test test_saving_of_movie() checks for insertion into the movies table. rows = list(empty_session.execute('SELECT id FROM movies')) movie_key = rows[0][0] # Check that the genres table has a new record. rows = list(empty_session.execute('SELECT id, name FROM genres')) genre_key = rows[0][0] assert rows[0][1] == "News" # Check that the movie_genres table has a new record. rows = list( empty_session.execute('SELECT movie_id, genre_id from movie_genres')) movie_foreign_key = rows[0][0] genre_foreign_key = rows[0][1] assert movie_key == movie_foreign_key assert genre_key == genre_foreign_key
def test_make_tag_associations(movie, genre, actor): make_genre_association(movie, genre) make_actor_association(movie, actor) # Check that the Article knows about the Tag. assert movie.genred() assert movie.is_genre(genre) assert movie.acted() assert movie.is_acted_by(actor) # check that the Tag knows about the Article. assert genre.is_applied_to(movie) assert movie in genre.genre_movie assert actor.is_applied_to(movie) assert movie in actor.actor_movie
def test_make_genre_association(movie, genre): make_genre_association(movie, genre) assert genre.is_applied_to(movie) assert movie in genre.genre_movies
def load_movies_and_genres(data_path: str, repo: MemoryRepository): genres = dict() actors = dict() directors = dict() for data_row in read_csv_file(os.path.join(data_path, 'Data1000Movies.csv')): movie_key = int(data_row[0]) movie_genres = data_row[2].split(',') movie_actors = data_row[5].split(',') movie_director = data_row[4].strip() for genre in movie_genres: genre = genre.strip() if genre not in genres.keys(): genres[genre] = list() genres[genre].append(movie_key) for actor in movie_actors: actor = actor.strip() if actor not in actors.keys(): actors[actor] = list() actors[actor].append(movie_key) if movie_director not in directors.keys(): directors[movie_director] = list() directors[movie_director].append(movie_key) movie = Movie(title=data_row[1], description=data_row[3], year=data_row[6], runtime_minutes=data_row[7], rating=data_row[8], votes=data_row[9], revenue_millions=data_row[10], meta_score=data_row[11], id=movie_key) repo.add_movie(movie) for genre_name in genres.keys(): genre = Genre(genre_name) for movie_id in genres[genre_name]: movie = repo.get_movie(movie_id) make_genre_association(movie, genre) repo.add_genre(genre) for actor_name in actors.keys(): actor = Actor(actor_name) for movie_id in actors[actor_name]: movie = repo.get_movie(movie_id) make_actor_association(movie, actor) repo.add_actor(actor) for director_name in directors.keys(): director = Director(director_name) for movie_id in directors[director_name]: movie = repo.get_movie(movie_id) make_director_association(movie, director) repo.add_director(director)
def load_movies_and_genres_and_actors_and_directors(data_path: str, repo: MemoryRepository): genres = dict() actors = dict() directors = dict() for data_row in read_csv_file(os.path.join(data_path, 'Data1000Movies.csv')): movie_key = int(data_row[0]) number_of_genres = 2 movie_genres = sorted(data_row[number_of_genres].strip().split(',')) number_of_actors = 5 movie_actors = sorted(data_row[number_of_actors].strip().split(',')) number_of_directors = 4 movie_directors = sorted(data_row[number_of_directors].strip().split(',')) # Add any new tags; associate the current article with tags. for genre in movie_genres: if genre not in genres.keys(): genres[genre] = list() genres[genre].append(movie_key) # del data_row[number_of_genres] for actor in movie_actors: if actor not in actors.keys(): actors[actor] = list() actors[actor].append(movie_key) for director in movie_directors: if director not in directors.keys(): directors[director] = list() directors[director].append(movie_key) # Create Article object. movie = Movie( name=data_row[1], year1=int(data_row[6]), rank=int(data_row[0]) ) # Add the Article to the repository. repo.add_movie(movie) # Create Tag objects, associate them with Articles and add them to the repository. for genre_name in genres.keys(): genre = Genre(genre_name) for movie_rank in genres[genre_name]: movie = repo.get_movie(movie_rank) make_genre_association(movie, genre) repo.add_genre(genre) for actor_name in actors.keys(): actor = Actor(actor_name) for movie_rank in actors[actor_name]: movie = repo.get_movie(movie_rank) make_actor_association(movie, actor) repo.add_actor(actor) for director_name in directors.keys(): director = Director(director_name) for movie_rank in directors[director_name]: movie = repo.get_movie(movie_rank) make_director_association(movie, director) repo.add_director(director)
def test_make_genre_associations_with_movie_already_genred(movie, genre): make_genre_association(movie, genre) with pytest.raises(ModelException): make_genre_association(movie, genre)