From 32d36898ebea042f3791427cc0221b748340fb37 Mon Sep 17 00:00:00 2001 From: George <41969151+geo-martino@users.noreply.github.com> Date: Mon, 18 Mar 2024 18:42:31 -0400 Subject: [PATCH] add extra layer for responses fixture on api items test class (#47) * add extra layer for responses fixture on api items test class * fix track item_key issue + update release history --- docs/release-history.rst | 4 ++++ tests/shared/remote/api.py | 11 ++++++++++- tests/spotify/api/test_item.py | 15 ++++++--------- tests/test_report.py | 2 +- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/docs/release-history.rst b/docs/release-history.rst index 332bcbe0..b6e7c9ae 100644 --- a/docs/release-history.rst +++ b/docs/release-history.rst @@ -48,6 +48,10 @@ Changed * Made :py:func:`.load_tracks` and :py:func:`.load_playlists` utility functions more DRY * Move :py:meth:`.TagReader.load` from :py:class:`.LocalTrack` to super class :py:class:`.TagReader` * Major refactoring and restructuring to local and remote modules to add composition +* :py:meth:`.SpotifyAPI.extend_items` now skips on responses that are already fully extended +* :py:meth:`.SpotifyArtist.load` now uses the base `load` method from :py:class:`.SpotifyCollectionLoader` + meaning it now takes full advantage of the item filtering this method offers. + As part of this, the base method was made more generic to accommodate all :py:class:`.SpotifyObject` types 0.8.1 ===== diff --git a/tests/shared/remote/api.py b/tests/shared/remote/api.py index d61b3509..d39b7e44 100644 --- a/tests/shared/remote/api.py +++ b/tests/shared/remote/api.py @@ -29,7 +29,7 @@ def object_factory(self) -> RemoteObjectFactory: raise NotImplementedError @pytest.fixture - def responses(self, object_type: RemoteObjectType, api_mock: RemoteMock) -> dict[str, dict[str, Any]]: + def _responses(self, object_type: RemoteObjectType, api_mock: RemoteMock) -> dict[str, dict[str, Any]]: """Yields valid responses mapped by ID for a given ``object_type`` as a pytest.fixture""" api_mock.reset_mock() # tests check the number of requests made source = api_mock.item_type_map[object_type] @@ -38,6 +38,15 @@ def responses(self, object_type: RemoteObjectType, api_mock: RemoteMock) -> dict return {response[self.id_key]: deepcopy(response) for response in source} + @pytest.fixture + def responses(self, _responses: dict[str, dict[str, Any]], key: str) -> dict[str, dict[str, Any]]: + """ + Yields valid responses mapped by ID for a given ``object_type`` as a pytest.fixture. + This method can be overridden to provide finer-grained filtering + on the initial response provided by ``_responses``. + """ + return _responses + @pytest.fixture def response(self, responses: dict[str, dict[str, Any]]) -> dict[str, Any]: """Yields a random valid response from a given set of ``responses`` as a pytest.fixture""" diff --git a/tests/spotify/api/test_item.py b/tests/spotify/api/test_item.py index e46f1d18..e34d8894 100644 --- a/tests/spotify/api/test_item.py +++ b/tests/spotify/api/test_item.py @@ -33,6 +33,11 @@ def object_factory(self) -> SpotifyObjectFactory: """Yield the object factory for Spotify objects as a pytest.fixture""" return SpotifyObjectFactory() + # noinspection PyMethodOverriding + @pytest.fixture + def responses(self, _responses: dict[str, dict[str, Any]], key: str) -> dict[str, dict[str, Any]]: + return {id_: response for id_, response in _responses.items() if key is None or response[key]["total"] > 3} + @staticmethod def reduce_items(response: dict[str, Any], key: str, api: SpotifyAPI, api_mock: SpotifyMock, pages: int = 3) -> int: """ @@ -554,16 +559,8 @@ def test_get_items_many_response( object_factory: SpotifyObjectFactory, ): if object_type in api.collection_item_map: - valid_responses = {} for id_, response in responses.items(): - try: - self.reduce_items(response=response, key=key, api=api, api_mock=api_mock) - valid_responses[id_] = response - except AssertionError: # filter out invalid collections i.e. those with too few items - continue - - responses = valid_responses - assert len(responses) > 3 + self.reduce_items(response=response, key=key, api=api, api_mock=api_mock) factory = object_factory[object_type] original = [factory(deepcopy(response), skip_checks=True) for response in responses.values()] diff --git a/tests/test_report.py b/tests/test_report.py index a6f7877a..3d914758 100644 --- a/tests/test_report.py +++ b/tests/test_report.py @@ -110,7 +110,7 @@ def test_report_missing_tags(local_library: LocalLibrary): if choice([True, False]): track.bpm = None if choice([True, False]): - track.item_key = None + track.key = None if choice([True, False]): track.disc_total = None