from unittest.mock import patch import pytest from marshmallow import ValidationError from analytics.constants.account_summary_field_mappings import ( ACCOUNT_SUMMARY_FIELD_MAPPINGS, ) from analytics.logic.account_timeseries import ( _breakdown_by, _is_default_params, _map_row_key_to_field_from_query_type, get_account_summary, get_account_timeseries, ) def test_is_default_params(): # max_available_date is mocked to return 2019-01-01 assert ( _is_default_params( {"end_date": "2019-01-01", "start_date": "2018-12-05"}, "TOTAL" ) is True ) assert ( _is_default_params( {"end_date": "2019-01-01", "start_date": "2018-12-05"}, "SUBSCRIPTION" ) is True ) assert ( _is_default_params( {"end_date": "2019-01-02", "start_date": "2018-12-05"}, "TOTAL" ) is False ) assert ( _is_default_params( {"end_date": "2019-01-01", "start_date": "2018-12-05"}, "SOS" ) is False ) def test_get_account_timeseries_requires_account_id(): with pytest.raises(ValidationError): get_account_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", }, ] def test_get_account_timeseries_default(): with patch( "analytics.logic.account_timeseries.AccountStreamsTimeSeries.execute", return_value=MOCK_COUNTRY_CODE_ROWS, ): ts = get_account_timeseries({"account_id": 123}, {}) assert len(ts) == 3 def test_get_acccount_timeseries_countries(): with patch( "analytics.logic.account_timeseries.AccountStreamsTimeSeries.execute", return_value=MOCK_COUNTRY_CODE_ROWS, ): ts = get_account_timeseries( {"account_id": 123, "type": "ACCOUNT_STREAMS_BY_COUNTRY"}, {} ) assert len(ts) == 3 def test_get_account_summary_default(): with patch( "analytics.logic.account_timeseries.AccountSummary.execute", return_value=MOCK_COUNTRY_CODE_SUMMARY_ROWS, ): ts = get_account_summary({"account_id": 123, "account_type": "vendor"}, {}) assert len(ts) == 3 def test_breakdown_by(): 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, "sub_type_subscription": 35325325, "sub_type_adsupported": 41241324, "sub_type_midtier": 53545245, } assert _breakdown_by(obj, timeseries=False, by="subscription") == [ {"id": "subscription", "value": 35325325}, {"id": "adsupported", "value": 41241324}, {"id": "midtier", "value": 53545245}, ] assert _breakdown_by( obj, timeseries=False, by="subscription", sources=["midtier"] ) == [{"id": "midtier", "value": 53545245}] def test_map_row_key_to_field_from_query_type(): result_participant = {"id": "participant"} result_mapped_participant = _map_row_key_to_field_from_query_type( result_participant, "id", "PARTICIPANT", ACCOUNT_SUMMARY_FIELD_MAPPINGS ) assert result_mapped_participant == { "id": "participant", "global_participant_id": "participant", } result = {"id": "streams_active"} result_unmapped = _map_row_key_to_field_from_query_type( result, "id", "SOS", ACCOUNT_SUMMARY_FIELD_MAPPINGS ) assert result_unmapped == {"id": "streams_active"}