def test_sanitize_query(api): q = Query('The Foo', type='movie', year='2000') assert api.sanitize_query(q) == Query('The Foo', type='unknown', year='2000') with pytest.raises(TypeError, match=r'^Not a Query instance: 123$'): api.sanitize_query(123)
async def test_WebDbSearchJob_initialize_sets_query_from_path(tmp_path, foodb): foodb.sanitize_query.return_value = Query('this query') job = webdb.WebDbSearchJob( home_directory=tmp_path, cache_directory=tmp_path, db=foodb, query='path/to/foo', ) assert job.query == Query('this query') assert foodb.sanitize_query.call_args_list == [ call(Query.from_path('path/to/foo')), ]
async def test_WebDbSearchJob_initialize_sets_no_id_ok(no_id_ok, exp_no_id_ok, tmp_path, foodb): job = webdb.WebDbSearchJob( home_directory=tmp_path, cache_directory=tmp_path, db=foodb, query=Query(title='The Foo', year=2010), no_id_ok=no_id_ok, ) assert job.no_id_ok == exp_no_id_ok assert foodb.sanitize_query.call_args_list == [ call(Query(title='The Foo', year=2010)), ]
async def test_search_result_title_original(api, store_response, mocker): mock_title_original = mocker.patch.object( api, 'title_original', AsyncMock(return_value='The Original Title')) results = await api.search(Query('Star Wars')) for result in results: assert await result.title_original() == 'The Original Title' assert sorted(mock_title_original.call_args_list) == [ call(result.id) for result in sorted(results, key=lambda r: r.id) ]
def test_WebDbSearchJob_search_before_finished(job, mocker): mocker.patch.object(job._searcher, 'search') mocker.patch.object(job._db, 'sanitize_query', return_value=Query('sanitized query', year='2020')) query_id = id(job.query) job.search('query string') assert job._db.sanitize_query.call_args_list == [ call(Query.from_string('query string')) ] assert job._searcher.search.call_args_list == [ call(job._db.sanitize_query.return_value) ] assert id(job.query) == query_id assert job.query.title == job._db.sanitize_query.return_value.title assert job.query.type == job._db.sanitize_query.return_value.type assert job.query.year == job._db.sanitize_query.return_value.year assert job.query.id == job._db.sanitize_query.return_value.id
def job(foodb, tmp_path, mocker): mocker.patch('upsies.jobs.webdb._Searcher', Mock(return_value=Mock(wait=AsyncMock()))) mocker.patch('upsies.jobs.webdb._InfoUpdater', Mock(return_value=Mock(wait=AsyncMock()))) job = webdb.WebDbSearchJob( home_directory=tmp_path, cache_directory=tmp_path, ignore_cache=False, db=foodb, query=Query('Mock Title'), ) assert job._db is foodb return job
async def test_search_handles_id_in_query(api, store_response): results = await api.search(Query(id=35256)) assert len(results) == 1 assert (await results[0].cast())[:3] == ('Son Ye Jin', 'Jung Hae In', 'Jang So Yun') assert results[0].countries == ('South Korea', ) assert results[0].id == 35256 assert results[0].genres == ('drama', 'romance') assert results[0].summary.startswith( 'Yoon Jin Ah is a single woman in her 30s who works') assert results[0].title == 'Something in the Rain' assert await results[0].title_english() == 'Something in the Rain' assert await results[0].title_original() == 'Bap Jal Sajuneun Yeppeun Nuna' assert results[0].type == ReleaseType.season assert results[ 0].url == 'https://www.tvmaze.com/shows/35256/something-in-the-rain' assert results[0].year == '2018'
def test_WebDbSearchJob_search_after_finished(job, mocker): mocker.patch.object(job._searcher, 'search') mocker.patch.object(job._db, 'sanitize_query', return_value=Query('sanitized query', year='2020')) original_query = { attr: getattr(job.query, attr) for attr in ('title', 'year', 'type', 'id') } job.finish() job.search('foo') assert job._searcher.search.call_args_list == [] assert job._db.sanitize_query.call_args_list == [] assert { attr: getattr(job.query, attr) for attr in ('title', 'year', 'type', 'id') } == original_query
async def test_search_handles_id_in_query(api, store_response): results = await api.search(Query(id='movie/525')) assert len(results) == 1 assert (await results[0].cast())[:3] == ('Dan Aykroyd', 'John Belushi', 'James Brown') assert results[0].countries == () assert await results[0].directors() == ('John Landis', ) assert results[0].id == 'movie/525' assert await results[0].genres() == ('music', 'comedy', 'action', 'crime') assert (await results[0].summary() ).startswith('Jake Blues, just released from prison') assert results[0].title == 'The Blues Brothers' assert await results[0].title_english() == 'The Blues Brothers' assert await results[0].title_original() == 'The Blues Brothers' assert results[0].type == ReleaseType.movie assert results[0].url == 'http://themoviedb.org/movie/525' assert results[0].year == '1980'
class TestDb(WebDbApiBase): name = 'foodb' label = 'FooDB' default_config = {} sanitize_query = Mock(return_value=Query('sanitized query')) search = AsyncMock() cast = AsyncMock() creators = AsyncMock() _countries = AsyncMock() directors = AsyncMock() genres = AsyncMock() poster_url = AsyncMock() rating = AsyncMock() rating_min = 0 rating_max = 10 _runtimes = AsyncMock() summary = AsyncMock() title_english = AsyncMock() title_original = AsyncMock() type = AsyncMock() url = AsyncMock() year = AsyncMock()
async def test_search_handles_id_in_query(id, title, title_english, title_original, type, year, cast, countries, directors, genres, summary, api, store_response): async def get_value(name): value = getattr(results[0], name) if not isinstance(value, str): value = await value() return value results = await api.search(Query(id=id)) assert len(results) == 1 assert results[0].id == id assert results[0].title == title assert results[0].type == type assert results[0].url == f'{imdb.ImdbApi._url_base}/title/{id}' assert results[0].year == year assert await get_value('title_english') == title_english assert await get_value('title_original') == title_original assert (await get_value('cast'))[:len(cast)] == cast assert await get_value('countries') == countries assert await get_value('directors') == directors assert await get_value('genres') == genres assert await get_value('summary') == summary
async def test_search_result_summary(title, summary, api, store_response): results = await api.search(Query('Star Wars')) results_dict = {r.title: r for r in results} assert summary in results_dict[title].summary
async def test_search_result_genres(title, exp_genres, api, store_response): results = await api.search(Query(title)) results_dict = {r.title: r for r in results} for kw in exp_genres: assert kw in results_dict[title].genres
async def test_search_result_countries(api, store_response): results = await api.search(Query('Star Wars')) for result in results: assert result.countries == ()
def __call__(self, *args, **kwargs): async def coro(_sup=super()): return _sup.__call__(*args, **kwargs) return coro() @pytest.fixture def api(): return imdb.ImdbApi() @pytest.mark.parametrize( argnames='query, exp_query', argvalues=( (Query('Foo and Bar'), Query('foo bar')), (Query('Foo & Bar'), Query('foo & bar')), ), ) def test_sanitize_query(query, exp_query, api, store_response): return_value = api.sanitize_query(query) assert return_value == exp_query @pytest.mark.parametrize( argnames= 'id, title, title_english, title_original, type, year, cast, countries, directors, genres, summary', argvalues=( ( 'tt3286052', "The Blackcoat's Daughter",
async def test_search_result_year(title, exp_year, api, store_response): results = await api.search(Query('Star Wars')) results_dict = {r.title: r for r in results} assert results_dict[title].year == exp_year
async def test_search_result_title(id, exp_title, api, store_response): results = await api.search(Query('Star Wars')) results_dict = {r.id: r for r in results} assert results_dict[id].title == exp_title
async def test_search_handles_non_unique_id_in_query(api, store_response): results = await api.search(Query(id='525')) assert len(results) == 2 assert results[0].id == 'movie/525' assert results[1].id == 'tv/525'
async def test_search_result_type(api, store_response): results = await api.search(Query('Star Wars')) for result in results: assert result.type is ReleaseType.series
async def test_search_returns_empty_list_if_title_is_empty( api, store_response): assert await api.search(Query('', year='2009')) == [] @pytest.mark.asyncio async def test_search_returns_list_of_SearchResults(api, store_response): results = await api.search(Query('Star Wars')) for result in results: assert isinstance(result, SearchResult) @pytest.mark.parametrize( argnames=('query', 'exp_titles'), argvalues=( (Query('Star Wars', year=2003), ('Star Wars: Clone Wars', )), (Query('Star Wars', year='2003'), ('Star Wars: Clone Wars', )), (Query('Star Wars', year=2014), ('Star Wars Rebels', )), (Query('Star Wars', year='1990'), ()), ), ids=lambda value: str(value), ) @pytest.mark.asyncio async def test_search_for_year(query, exp_titles, api, store_response): results = await api.search(query) titles = {r.title for r in results} assert titles == set(exp_titles) @pytest.mark.parametrize( argnames=('query', 'exp_titles'),
async def test_search_result_cast(title, exp_cast, api, store_response): results = await api.search(Query('Star Wars')) results_dict = {r.title: r for r in results} cast = await results_dict[title].cast() for member in exp_cast: assert member in cast
async def test_search_result_countries(title, exp_countries, api, store_response): results = await api.search(Query(title)) results_dict = {r.title: r for r in results} assert results_dict[title].countries == exp_countries
async def test_search_returns_list_of_SearchResults(api, store_response): results = await api.search(Query('Star Wars')) for result in results: assert isinstance(result, SearchResult)
async def test_search_result_directors(api, store_response): results = await api.search(Query('Star Wars')) for result in results: assert result.directors == ()
async def test_search_returns_empty_list_if_title_is_empty( api, store_response): assert await api.search(Query('', year='2009')) == []
async def test_search_returns_empty_list_if_title_is_empty( api, store_response): assert await api.search(Query('', year='2009')) == [] @pytest.mark.asyncio async def test_search_returns_list_of_SearchResults(api, store_response): results = await api.search(Query('Star Wars')) for result in results: assert isinstance(result, SearchResult) @pytest.mark.parametrize( argnames=('query', 'exp_top_result'), argvalues=( (Query('alien', year='1979'), { 'title': 'Alien', 'year': '1979' }), (Query('aliens', year='1986'), { 'title': 'Aliens', 'year': '1986' }), (Query('alien', year='1992'), { 'title': 'Alien³', 'year': '1992' }), ), ids=lambda value: str(value), ) @pytest.mark.asyncio