import json from unittest.mock import patch import pytest from analytics import product_handlers # noqa (registers routes on app) from analytics.connectors import redis from tests.unit.logic.test_product_timeseries import ( MOCK_COUNTRY_CODE_ROWS, MOCK_COUNTRY_CODE_SUMMARY_ROWS, ) def test_product_aggregated_streams_handler( client, insights_request_headers, request_context ): with patch( "analytics.logic.streams.get_aggregated_streams" ) as get_aggregated_streams: get_aggregated_streams.return_value = [] url = "/product/3859857/aggregated-streams?dimension=COUNTRY" 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 == {} def test_product_growth_periods_bulk_handler( client, insights_request_headers, request_context ): with patch( "analytics.logic.product_timeseries.get_product_growth_periods_bulk" ) as get_product_growth_periods_bulk: get_product_growth_periods_bulk.return_value = [] url = "/product/growth-periods-bulk" 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 == [] def test_product_time_series_handler(client, insights_request_headers, request_context): with patch( "analytics.logic.product_timeseries.get_product_timeseries" ) as get_product_timeseries: get_product_timeseries.return_value = {"items": []} url = "/product/3859857/timeseries?type=PRODUCT_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_product_time_series_summary_handler( client, insights_request_headers, request_context ): with patch( "analytics.logic.product_timeseries.get_product_timeseries" ) as get_product_timeseries: get_product_timeseries.return_value = {"items": []} url = "/product/3859857/summary?type=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_product_time_series_aggregated( client, insights_request_headers, request_context ): with patch( "analytics.logic.product_timeseries.ProductStreamsTimeSeries.execute", return_value=MOCK_COUNTRY_CODE_ROWS, ) as execute: url = "/product/3859857/timeseries?type=PRODUCT_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_product_time_series_summary_aggregated( client, insights_request_headers, request_context ): with patch( "analytics.logic.product_summary.ProductSummary.execute", return_value=MOCK_COUNTRY_CODE_SUMMARY_ROWS, ) as execute: url = "/product/3859857/summary?type=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", "skip_rate": 0.5, "skips": 30, "streams": 100, }, { "country_code": "GB", "skip_rate": 0.5, "skips": 30, "streams": 100, }, { "country_code": "DE", "skip_rate": 0.5, "skips": 30, "streams": 100, }, ] } def test_product_time_series_multi( client, insights_request_headers, request_context, mock_execute_orm ): url = "/product/3859857/timeseries?type=PRODUCT_STREAMS_BY_STORE&multi_product=true" # noqa response = client.get(url, headers=insights_request_headers) assert response.status_code == 200 sql = mock_execute_orm.call_args[0][0] sql = sql.text if hasattr(sql, "text") else sql assert "MULTI_PRIMARY_PRODUCTS" in sql url = "/product/3859857/timeseries?type=PRODUCT_STREAMS_BY_STORE&multi_product=false" # noqa response = client.get(url, headers=insights_request_headers) assert response.status_code == 200 sql = mock_execute_orm.call_args[0][0] sql = sql.text if hasattr(sql, "text") else sql assert "MULTI_PRIMARY_PRODUCTS" not in sql @pytest.mark.skip(reason="enable after implementing multi product queries") def test_product_time_series_summary_multi( client, insights_request_headers, request_context, mock_execute_orm ): url = "/product/3859857/summary?type=STORE&multi_product=true" # noqa response = client.get(url, headers=insights_request_headers) assert response.status_code == 200 sql = mock_execute_orm.call_args[0][0] sql = sql.text if hasattr(sql, "text") else sql assert "MULTI" in sql url = "/product/3859857/summary?type=STORE&multi_product=false" # noqa response = client.get(url, headers=insights_request_headers) assert response.status_code == 200 sql = mock_execute_orm.call_args[0][0] sql = sql.text if hasattr(sql, "text") else sql assert "MULTI" not in sql