from decimal import Decimal from unittest.mock import patch import pytest from marshmallow import ValidationError from analytics.logic.product_timeseries import ( get_product_growth_periods_bulk, get_product_timeseries, ) from analytics.utils.streams import breakdown_by_sos def test_get_product_timeseries_requires_product_id(): with pytest.raises(ValidationError): get_product_timeseries({}, {}) MOCK_COUNTRY_CODE_ROWS = [ { "date": "2022-02-23", "streams": 100, "skips": 30, "streams_with_skips": 30, "country_code": "GB", }, { "date": "2022-02-24", "streams": 100, "skips": 30, "streams_with_skips": 30, "country_code": "GB", }, { "date": "2022-02-23", "streams": 100, "skips": 30, "streams_with_skips": 30, "country_code": "DE", }, ] MOCK_COUNTRY_CODE_SUMMARY_ROWS = [ { "streams": 100, "skips": 30, "streams_with_skips": 30, "country_code": "GB", }, { "streams": 100, "skips": 30, "streams_with_skips": 30, "country_code": "GB", }, { "streams": 100, "skips": 30, "streams_with_skips": 30, "country_code": "DE", }, ] MOCK_GROWTH_PERIODS_ROWS = [ { "id": 3978857, "streams_1_day": 2, "growth_percentage_1_day": Decimal("0.000000"), "streams_7_days": 13, "growth_percentage_7_days": Decimal("-0.648649"), "streams_28_days": 99, "growth_percentage_28_days": Decimal("-0.307692"), }, { "id": 3978105, "streams_1_day": 0, "growth_percentage_1_day": Decimal("0.000000"), "streams_7_days": 1, "growth_percentage_7_days": Decimal("0.000000"), "streams_28_days": 1, "growth_percentage_28_days": Decimal("0.000000"), }, ] def test_get_product_timeseries_default(): with patch( "analytics.logic.product_timeseries.ProductStreamsTimeSeries.execute", return_value=MOCK_COUNTRY_CODE_ROWS, ): ts = get_product_timeseries({"product_id": 123}, {}) assert len(ts) == 3 def test_get_product_growth_periods_bulk(): with patch( "analytics.logic.product_timeseries.ProductBulkGrowthPeriods.execute", return_value=MOCK_GROWTH_PERIODS_ROWS, ): gp = get_product_growth_periods_bulk({"product_ids": [1, 2]}, {}) assert len(gp) == 2 def test_get_product_timeseries_summary_default(): with patch( "analytics.logic.product_timeseries.ProductStreamsTimeSeries.execute", return_value=MOCK_COUNTRY_CODE_SUMMARY_ROWS, ): ts = get_product_timeseries({"product_id": 123, "summary": True}, {}) assert len(ts) == 3 def test_get_product_timeseries_countries(): with patch( "analytics.logic.product_timeseries.ProductStreamsTimeSeries.execute", return_value=MOCK_COUNTRY_CODE_ROWS, ): ts = get_product_timeseries( {"product_id": 123, "type": "PRODUCT_STREAMS_BY_COUNTRY"}, {} ) assert len(ts) == 3 def test_get_product_timeseries_summary_countries(): with patch( "analytics.logic.product_timeseries.ProductStreamsTimeSeries.execute", return_value=MOCK_COUNTRY_CODE_SUMMARY_ROWS, ): ts = get_product_timeseries( {"product_id": 123, "type": "PRODUCT_STREAMS_BY_COUNTRY", "summary": True}, {}, ) assert len(ts) == 3 def test_breakdown_by_sos(): obj = { "id": 3859857, "streams": 1564970349, "streams_passive": 469443397, "streams_active": 534142333, "streams_collection": 499537827, "streams_unknown": 61846792, "saves": 30520940, "skips": 509755499, "downloads": 26408, "skip_rate": 0.24569776266652074, } assert breakdown_by_sos(obj, timeseries=False) == [ {"id": "active", "value": 534142333}, {"id": "passive", "value": 469443397}, {"id": "collection", "value": 499537827}, {"id": "unknown", "value": 61846792}, ] assert breakdown_by_sos(obj, timeseries=False, sources=["active"]) == [ {"id": "active", "value": 534142333} ]