def test_add_links_to_movies(self, session): expected = {'Solaris', "Akira Kurosawa's Dreams"} test_tag = 'Foreign' database.add_tag(test_tag) movie = database.MovieKeyTypedDict(title='Solaris', year=1972) database.add_movie_tag_link(test_tag, movie) movie = database.MovieKeyTypedDict(title="Akira Kurosawa's Dreams", year=1972) database.add_movie_tag_link(test_tag, movie) movies = (session.query(database.Movie.title) .filter(database.Movie.tags.any(tag=test_tag))) result = {movie.title for movie in movies} assert result == expected
def loaded_database(hamlet, solaris, dreams, revanche): """Provide a loaded database.""" database.connect_to_database(filename=':memory:') # noinspection PyProtectedMember movies = [database.Movie(**movie) for movie in (hamlet, solaris, dreams, revanche)] # noinspection PyProtectedMember with database._session_scope() as session: session.add_all(movies) database.add_tag('blue') database.add_movie_tag_link('blue', database.MovieKeyTypedDict(title='Hamlet', year=1996)) database.add_tag('yellow') database.add_movie_tag_link('yellow', database.MovieKeyTypedDict(title='Revanche', year=2008)) database.add_tag('green') database.add_movie_tag_link('green', database.MovieKeyTypedDict(title='Revanche', year=2008)) database.add_movie_tag_link('green', database.MovieKeyTypedDict(title='Solaris', year=1972))
def test_edit_movies_tag_raises_exception_movie_not_found(self, session): title_year = database.MovieKeyTypedDict(title='Non Existent Movie', year=1972) new_tags = ('green', 'yellow') old_tags = ('blue', 'yellow') with pytest.raises(exception.DatabaseSearchFoundNothing) as cm: database.edit_movie_tag_links(title_year, old_tags, new_tags) assert cm.typename == 'DatabaseSearchFoundNothing' assert cm.match("The movie Non Existent Movie, 1972 is not in the database.")
def test_edit_movies_tag(self, session): title_year = database.MovieKeyTypedDict(title='Revanche', year=2008) old_tags = ('green', 'yellow') new_tags = ('blue', 'yellow') database.edit_movie_tag_links(title_year, old_tags, new_tags) movies = database.find_movies(database.FindMovieTypedDict(title=title_year['title'], year=[str(title_year['year'])])) assert set(movies[0]['tags']) == {'blue', 'yellow'}
def test_del_tag(self, session): # Add a tag and links test_tag = 'Going soon' database.add_tag(test_tag) movie = database.MovieKeyTypedDict(title='Solaris', year=1972) database.add_movie_tag_link(test_tag, movie) movie = database.MovieKeyTypedDict(title='Hamlet', year=1996) database.add_movie_tag_link(test_tag, movie) # Delete the tag database.del_tag(test_tag) # Is the tag still there? with pytest.raises(sqlalchemy.orm.exc.NoResultFound): session.query(database.Tag).filter(database.Tag.tag == test_tag).one() # Do any movies still have the tag? assert (session.query(database.Movie) .filter(database.Movie.tags.any(tag=test_tag)) .all()) == []
def test_edit_movies_tag_logs_exception_movie_not_found(self, session, monkeypatch): calls = [] monkeypatch.setattr(database.logging, 'info', lambda msg: calls.append(msg)) title = 'Non Existent Movie' year = 1972 title_year = database.MovieKeyTypedDict(title=title, year=year) new_tags = ('green', 'yellow') old_tags = ('blue', 'yellow') with pytest.raises(exception.DatabaseSearchFoundNothing): database.edit_movie_tag_links(title_year, old_tags, new_tags) assert calls == [("An incomplete database session has been rolled back because of exception:\n" "NoResultFound"), f"The movie {title}, {year} is not in the database."]
def test_edit_tag(self, session): old_tag = 'old test tag' database.add_tag(old_tag) movie = database.MovieKeyTypedDict(title='Solaris', year=1972) database.add_movie_tag_link(old_tag, movie) old_tag_id, tag = (session.query(database.Tag.id, database.Tag.tag) .filter(database.Tag.tag == 'old test tag') .one()) new_tag = 'new test tag' database.edit_tag(old_tag, new_tag) new_tag_id, tag = (session.query(database.Tag.id, database.Tag.tag) .filter(database.Tag.tag == 'new test tag') .one()) assert old_tag_id == new_tag_id
def test_movie_tags(loaded_database, revanche): title_year = database.MovieKeyTypedDict(title=revanche['title'], year=revanche['year']) tags = database.movie_tags(title_year) assert set(tags) == {'yellow', 'green'}