Skip to content

Commit

Permalink
add extra layer for responses fixture on api items test class (#47)
Browse files Browse the repository at this point in the history
* add extra layer for responses fixture on api items test class

* fix track item_key issue + update release history
  • Loading branch information
geo-martino authored Mar 18, 2024
1 parent 50fc009 commit 32d3689
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
4 changes: 4 additions & 0 deletions docs/release-history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=====
Expand Down
11 changes: 10 additions & 1 deletion tests/shared/remote/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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"""
Expand Down
15 changes: 6 additions & 9 deletions tests/spotify/api/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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()]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 32d3689

Please sign in to comment.