import copy from http import HTTPStatus as http_status from unittest.mock import AsyncMock import pytest from server.client.clients.vendor_api import VendorApiClient from tests.helpers import read_json_from_file NO_ITEM = "_no_item_" @pytest.mark.parametrize( "album_id,market,distributors_param,distributed_by,expected_is_sony", ( ("4QLAtpLNUsHEYrcHXmMIZZ", "us", None, "sme", True), ("4on00pcLDDmrTaBE52Z1Ev", "us", ["sme", "theorchard"], NO_ITEM, False), ("4on00pcLDDmrTaBE52Z1Ev", "us", None, "theorchard", False), ), ) async def test_get_spotify_album_with_tracks_by_album_id( mocker, client, auth, album_id, market, distributors_param, distributed_by, expected_is_sony): vendor_response = read_json_from_file("clients/vendor_api/spotify_album_with_tracks").get(album_id, {}) async_filtr_mock = AsyncMock(return_value=vendor_response) mocker.patch.object(VendorApiClient, "_send_request", side_effect=async_filtr_mock) _distributors_result = [] if distributed_by != NO_ITEM: _distributors_result.append({"distributed_by": distributed_by}) async_distributors_mock = AsyncMock(return_value=(_distributors_result, None)) distributors_mock = mocker.patch( "server.api.albums.spotify.album.get_distributors", side_effect=async_distributors_mock) expected_json_response = copy.deepcopy(vendor_response) expected_json_response.update(dict( distributed_by=distributed_by if distributed_by != NO_ITEM else None, is_sony=expected_is_sony )) params = {} if market: params["market"] = market if distributors_param: params["distributors"] = distributors_param response = await client.get(f"/api/albums/spotify/{album_id}/", params=params, headers=auth) assert response.status == http_status.OK response_json = await response.json() assert response_json == expected_json_response distributors_mock.assert_called()