import json from unittest.mock import patch from analytics.connectors import redis from tests.unit.logic.test_account_timeseries import MOCK_COUNTRY_CODE_ROWS from tests.unit.logic.test_account_top_content import MOCK_TOP_CONTENT_ROWS def test_account_time_series_handler(client, insights_request_headers, request_context): with patch( "analytics.logic.account_timeseries.get_account_timeseries" ) as get_account_timeseries: get_account_timeseries.return_value = {"items": []} url = ( "/account/3859857/timeseries?account_type=vendor" "&type=ACCOUNT_STREAMS_BY_STORE" ) response = client.get(url, headers=insights_request_headers) assert response.status_code == 200 response_payload = json.loads(response.data.decode("utf-8")) assert response_payload == {"items": []} def test_account_time_series_aggregated( client, insights_request_headers, request_context ): with patch( "analytics.logic.account_timeseries.AccountStreamsTimeSeries.execute", return_value=MOCK_COUNTRY_CODE_ROWS, ) as execute: url = ( "/account/3859857/timeseries?account_type=vendor" "&type=ACCOUNT_STREAMS_BY_STORE" ) redis.client.flushall() # flush Fakeredis cache response = client.get(url, headers=insights_request_headers) assert response.status_code == 200 response_payload = json.loads(response.data.decode("utf-8")) assert execute.call_count == 1 assert response_payload == { "items": [ { "country_code": "GB", "date": "2022-02-23", "skip_rate": 0.5, "skips": 30, "streams": 100, }, { "country_code": "GB", "date": "2022-02-24", "skip_rate": 0.5, "skips": 30, "streams": 100, }, { "country_code": "DE", "date": "2022-02-23", "skip_rate": 0.5, "skips": 30, "streams": 100, }, ] } def test_account_summary_handler(client, insights_request_headers, request_context): with patch( "analytics.logic.account_timeseries.get_account_summary" ) as get_account_summary: get_account_summary.return_value = {"items": []} url = "/account/3859857/summary?account_type=vendor&type=TOTAL" response = client.get(url, headers=insights_request_headers) assert response.status_code == 200 response_payload = json.loads(response.data.decode("utf-8")) assert response_payload == {"items": []} def test_account_products_handler(client, insights_request_headers, request_context): with patch("analytics.logic.account_products.get_products") as get_products: get_products.return_value = {"items": []} url = "/account/3859857/products?account_type=vendor&order_by=streams_all_time" response = client.get(url, headers=insights_request_headers) assert response.status_code == 200 response_payload = json.loads(response.data.decode("utf-8")) assert response_payload == {"items": []} def test_account_top_content_handler(client, insights_request_headers, request_context): with patch( "analytics.logic.account_top_content.AccountTopContentStreams.execute", return_value=MOCK_TOP_CONTENT_ROWS, ) as execute: url = "/account/3859857/top-content?account_type=vendor&start_date=2021-01-01&end_date=2023-01-01" response = client.get(url, headers=insights_request_headers) assert response.status_code == 200 response_payload = json.loads(response.data.decode("utf-8")) assert execute.call_count == 1 assert response_payload == { "topn_artists": [ { "global_participant_id": "03f1db0c-be3c-4619-8491-bcb6d1288fb5", "value": 22082366027, } ], "topn_products": [{"product_id": 3191557, "value": 9580398167}], "topn_songs": [{"isrc": "usqx91601347", "value": 4198647216}], "topn_stores": [{"store_id": 286, "value": 4297651896}], } def test_account_top_content_handler_all_time( client, insights_request_headers, request_context ): with patch( "analytics.logic.account_top_content.AccountTopContentAllTimeStreams.execute", return_value=MOCK_TOP_CONTENT_ROWS, ) as execute: url = "/account/3859857/top-content?account_type=vendor" response = client.get(url, headers=insights_request_headers) assert response.status_code == 200 response_payload = json.loads(response.data.decode("utf-8")) assert execute.call_count == 1 assert response_payload == { "topn_artists": [ { "global_participant_id": "03f1db0c-be3c-4619-8491-bcb6d1288fb5", "value": 22082366027, } ], "topn_products": [{"product_id": 3191557, "value": 9580398167}], "topn_songs": [{"isrc": "usqx91601347", "value": 4198647216}], "topn_stores": [{"store_id": 286, "value": 4297651896}], }