from unittest.mock import AsyncMock from server.client.clients.dsp_api import DspApiClient from server.client.clients.apollo_api import ApolloApiClient from tests.helpers import read_json_from_file async def test_youtube_top_markets(client, auth, mocker): params = { "video_id": "youtube_niG3YMU6jFk", "start_date": "2022-05-21", "end_date": "2022-06-17", "limit": 15, } expected_json_response = { "items":[ { "percentage":18.0, "views":488854, "market":"mx" }, { "percentage":12.0, "views":330333, "market":"us" }, { "percentage":8.0, "views":212852, "market":"it" }, { "percentage":8.0, "views":207132, "market":"th" }, { "percentage":8.0, "views":200117, "market":"br" }, { "percentage":7.0, "views":194271, "market":"fr" }, { "percentage":6.0, "views":148166, "market":"es" }, { "percentage":4.0, "views":109272, "market":"gb" }, { "percentage":4.0, "views":95291, "market":"co" }, { "percentage":4.0, "views":92826, "market":"ar" }, { "percentage":3.0, "views":89362, "market":"de" }, { "percentage":3.0, "views":72902, "market":"pl" }, { "percentage":2.0, "views":63305, "market":"ca" }, { "percentage":2.0, "views":60746, "market":"au" }, { "percentage":2.0, "views":59004, "market":"cl" } ] } dsp_response = read_json_from_file("clients/dsp_api/youtube_top_markets") async_mock = AsyncMock(return_value=dsp_response) mocker.patch.object(DspApiClient, "_send_request", side_effect=async_mock) response = await client.get(f"/api/youtube/markets/top/", headers=auth, params=params) response_json = await response.json() assert response_json == expected_json_response async def test_youtube_markets(client, auth, mocker): params = { "video_id": "youtube_niG3YMU6jFk", "start_date": "2022-05-21", "end_date": "2022-06-17", } expected_json_response = { "items":[ { "full_name":"Argentina", "code":"ar", "id":40, "name":"Argentina" }, { "full_name":"Australia", "code":"au", "id":17, "name":"Australia" }, { "full_name":"Brazil", "code":"br", "id":19, "name":"Brazil" }, { "full_name":"Canada", "code":"ca", "id":16, "name":"Canada" }, { "full_name":"Chile", "code":"cl", "id":51, "name":"Chile" }, { "full_name":"Colombia", "code":"co", "id":52, "name":"Colombia" }, { "full_name":"France", "code":"fr", "id":11, "name":"France" }, { "full_name":"Germany", "code":"de", "id":15, "name":"Germany" }, { "full_name":"India", "code":"in", "id":49, "name":"India" }, { "full_name":"Indonesia", "code":"id", "id":34, "name":"Indonesia" }, { "full_name":"Italy", "code":"it", "id":20, "name":"Italy" }, { "full_name":"Mexico", "code":"mx", "id":25, "name":"Mexico" }, { "full_name":"the Philippines", "code":"ph", "id":67, "name":"Philippines" }, { "full_name":"Poland", "code":"pl", "id":23, "name":"Poland" }, { "full_name":"Russia", "code":"ru", "id":93, "name":"Russia" }, { "full_name":"Spain", "code":"es", "id":12, "name":"Spain" }, { "full_name":"Thailand", "code":"th", "id":89, "name":"Thailand" }, { "full_name":"Turkey", "code":"tr", "id":41, "name":"Turkey" }, { "full_name":"the United Kingdom", "code":"gb", "id":5, "name":"United Kingdom" }, { "full_name":"the United States", "code":"us", "id":4, "name":"United States" } ] } dsp_response = read_json_from_file("clients/dsp_api/youtube_top_markets") dsp_async_mock = AsyncMock(return_value=dsp_response) mocker.patch.object(DspApiClient, "_send_request", side_effect=dsp_async_mock) apollo_response = read_json_from_file("clients/apollo_api/apollo_markets") apollo_async_mock = AsyncMock(return_value=apollo_response) mocker.patch.object(ApolloApiClient, "_send_request", side_effect=apollo_async_mock) response = await client.get(f"/api/youtube/markets/", headers=auth, params=params) response_json = await response.json() assert response_json == expected_json_response