from http import HTTPStatus as http_status from typing import List from unittest.mock import AsyncMock import pytest from pytest_mock import MockerFixture from server.client.clients.apollo_api import ApolloApiClient from tests.api.playlists.v1.test_playlists import get_date, get_dates, get_playlists pytest_plugins = ["tests.clients.vendor_api.responses", "tests.clients.apollo_api.responses"] @pytest.mark.parametrize( "available_dates,playlists,outdated_indexes", ( (get_dates(1, 7, 10, 15, as_str=True), get_playlists(), [4, 5]), (get_dates(15, 10, 2, as_str=True), get_playlists(count=1), []), ), ) async def test_get_nmf_playlists( available_dates: List[str], playlists: List[dict], outdated_indexes: List[int], mocker: MockerFixture, auth: dict, client, ): last_updated = get_date(days_diff=2, as_str=True) async def apollo_send(url: str, *args, **kwargs): if url == "api/spotify/nmf/available-dates/": return available_dates elif url == "api/value/": return last_updated return playlists apollo_send_mock = mocker.patch.object(ApolloApiClient, "_send_request", side_effect=apollo_send) response = await client.get("/api/playlists/summary/nmf/", headers=auth) assert response.status == http_status.OK assert apollo_send_mock.call_count == 3 response = await response.json() assert response == { "available_dates": available_dates, "playlists": playlists, "outdated_playlists": [pl for i, pl in enumerate(playlists) if (i + 1) in outdated_indexes], "last_updated": last_updated, } async def test_nmf_completeness(mocker, client, auth): apollo_response = { "last_updated": "2021-12-12T23:31:25.446873", "markets_delayed": ["de", "fr", "au"] } async_apollo_mock = AsyncMock(return_value=apollo_response) get_apollo_mock = mocker.patch.object(ApolloApiClient, "_send_request", side_effect=async_apollo_mock) response = await client.get("/api/playlists/nmf/completeness/", headers=auth) assert response.status == http_status.OK get_apollo_mock.assert_called_once() response = await response.json() assert response == apollo_response