From 39a4a37d7c6b6b7601dbc0d7bcd5aaa4dc09a938 Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Thu, 19 Sep 2024 19:03:30 -0700 Subject: [PATCH] Add tests --- comictalker/talkers/comicvine.py | 9 +++++---- testing/comicvine.py | 2 +- tests/comicvinetalker_test.py | 19 +++++++++++++++---- tests/conftest.py | 19 +++++++++++++------ tests/integration_test.py | 2 ++ 5 files changed, 36 insertions(+), 15 deletions(-) diff --git a/comictalker/talkers/comicvine.py b/comictalker/talkers/comicvine.py index 30b2250..abdafb8 100644 --- a/comictalker/talkers/comicvine.py +++ b/comictalker/talkers/comicvine.py @@ -393,11 +393,12 @@ class ComicVineTalker(ComicTalker): ), ) issue_found = True - else: + break + if not issues: needed_volumes.add(int(series_id)) # we got no results from cache, we definitely need to check online # If we didn't find the issue and we don't have all the issues we don't know if the issue exists, we have to check - if not issue_found and cvseries.get("count_of_issues") == len(issues): + if (not issue_found) and cvseries.get("count_of_issues") != len(issues): needed_volumes.add(int(series_id)) logger.debug("Found %d issues cached need %d issues", len(cached_results), len(needed_volumes)) @@ -405,9 +406,9 @@ class ComicVineTalker(ComicTalker): return cached_results series_filter = "" - for vid in series_id_list: + for vid in needed_volumes: series_filter += str(vid) + "|" - flt = f"volume:{series_filter},issue_number:{issue_number}" # CV uses volume to mean series + flt = f"volume:{series_filter[:-1]},issue_number:{issue_number}" # CV uses volume to mean series int_year = utils.xlate_int(year) if int_year is not None: diff --git a/testing/comicvine.py b/testing/comicvine.py index 54b2fa5..908420f 100644 --- a/testing/comicvine.py +++ b/testing/comicvine.py @@ -118,7 +118,7 @@ cv_volume_result: dict[str, Any] = { "results": { "aliases": None, "api_detail_url": "https://comicvine.gamespot.com/api/volume/4050-23437/", - "count_of_issues": 6, + "count_of_issues": 1, "date_added": "2008-10-16 05:25:47", "date_last_updated": "2012-01-18 17:21:57", "deck": None, diff --git a/tests/comicvinetalker_test.py b/tests/comicvinetalker_test.py index 8129c2a..aec57d4 100644 --- a/tests/comicvinetalker_test.py +++ b/tests/comicvinetalker_test.py @@ -46,13 +46,24 @@ def test_fetch_issue_data_by_issue_id(comicvine_api): assert result == testing.comicvine.cv_md -def test_fetch_issues_in_series_issue_num_and_year(comicvine_api): +def test_fetch_issues_in_series_issue_num_and_year(comicvine_api, cv_requests_get): results = comicvine_api.fetch_issues_by_series_issue_num_and_year([23437], "1", None) cv_expected = testing.comicvine.comic_issue_result.copy() - for r, e in zip(results, [cv_expected]): - assert r.series == e.series - assert r == e + assert results[0].series == cv_expected.series + assert results[0] == cv_expected + assert cv_requests_get.call_count == 2 + + results = comicvine_api.fetch_issues_by_series_issue_num_and_year([23437], "1", None) + + assert results[0].series == cv_expected.series + assert results[0] == cv_expected + assert cv_requests_get.call_count == 2 # verify caching works + + results = comicvine_api.fetch_issues_by_series_issue_num_and_year([23437], "2", None) + + assert not results + assert cv_requests_get.call_count == 2 # verify negative caching works cv_issue = [ diff --git a/tests/conftest.py b/tests/conftest.py index a1107bc..6ecba16 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -70,7 +70,7 @@ def no_requests(monkeypatch) -> None: @pytest.fixture -def comicvine_api(monkeypatch, cbz, comic_cache, mock_version, config) -> comictalker.talkers.comicvine.ComicVineTalker: +def cv_requests_get(monkeypatch, cbz, comic_cache) -> unittest.mock.Mock: # Any arguments may be passed and mock_get() will always return our # mocked object, which only has the .json() method or None for invalid urls. @@ -88,16 +88,18 @@ def comicvine_api(monkeypatch, cbz, comic_cache, mock_version, config) -> comict return comicvine.MockResponse(cv_result) if args[0].startswith("https://comicvine.gamespot.com/api/issue/4000-140529"): return comicvine.MockResponse(comicvine.cv_issue_result) + flt = kwargs.get("params", {}).get("filter", "").split(",") if ( args[0].startswith("https://comicvine.gamespot.com/api/issues/") and "params" in kwargs and "filter" in kwargs["params"] - and "23437" in kwargs["params"]["filter"] + and "volume:23437" in flt ): - cv_list = make_list(comicvine.cv_issue_result) - for cv in cv_list["results"]: - comicvine.filter_field_list(cv, kwargs) - return comicvine.MockResponse(cv_list) + if "issue_number" not in kwargs["params"]["filter"] or ("issue_number:1" in flt): + cv_list = make_list(comicvine.cv_issue_result) + for cv in cv_list["results"]: + comicvine.filter_field_list(cv, kwargs) + return comicvine.MockResponse(cv_list) if ( args[0].startswith("https://comicvine.gamespot.com/api/search") and "params" in kwargs @@ -126,6 +128,11 @@ def comicvine_api(monkeypatch, cbz, comic_cache, mock_version, config) -> comict # apply the monkeypatch for requests.get to mock_get monkeypatch.setattr(requests, "get", m_get) + return m_get + + +@pytest.fixture +def comicvine_api(monkeypatch, cv_requests_get, mock_version, config) -> comictalker.talkers.comicvine.ComicVineTalker: monkeypatch.setattr(comictalker.talkers.comicvine, "custom_limiter", Limiter(RequestRate(100, 1))) monkeypatch.setattr(comictalker.talkers.comicvine, "default_limiter", Limiter(RequestRate(100, 1))) diff --git a/tests/integration_test.py b/tests/integration_test.py index 05b9323..decabb3 100644 --- a/tests/integration_test.py +++ b/tests/integration_test.py @@ -52,6 +52,8 @@ def test_save( # This is inserted here because otherwise several other tests # unrelated to comicvine need to be re-worked + # the comicvine response is mocked to 1 for caching tests and adding the remaining 5 issues is more work + md_saved.issue_count = 1 md_saved.credits.insert( 1, comicapi.genericmetadata.Credit(