from contextlib import nullcontext as does_not_raise from datetime import date from unittest.mock import patch import pytest from marshmallow import ValidationError from analytics.logic.account_timeseries import get_account_video_timeseries @pytest.mark.parametrize( "example_input,expectation,message", [ # account_id is required ({}, pytest.raises(ValidationError), "Missing data for required field."), # type and views_type are required ( {"account_id": 123}, pytest.raises(ValidationError), "Missing data for required field.", ), # views_type is required ( {"account_id": 123, "type": "COUNTRY"}, pytest.raises(ValidationError), "Missing data for required field.", ), # invalid input for views_type ( {"account_id": 123, "type": "COUNTRY", "views_type": "POTATO"}, pytest.raises(KeyError), "POTATO", ), # invalid date throws ( { "account_id": 123, "views_type": "ALL", "type": "TOTAL", "end_date": "2019-32-32", "start_date": "2018-12-05", }, pytest.raises(ValidationError), "Not a valid date.", ), # valid date can be passed ( { "account_id": 123, "views_type": "ALL", "type": "TOTAL", "end_date": "2019-01-01", "start_date": "2018-12-05", "limit": 25.0, }, does_not_raise(), None, ), # track family can be processed correctly ( { "account_id": 123, "views_type": "ALL", "type": "TRACK_FAMILY", "countries": [], "limit": 25, }, does_not_raise(), None, ), ], ) def test_get_account_video_timeseries_input_args_raise_correct_error_or_none( example_input, expectation, message ): with expectation as e: get_account_video_timeseries(example_input, {}) assert message is None or message in str(e) MOCK_VIDEO_VIEWS_TS_TOTAL_ROWS = [ { "download_activity_date": "2024-03-01", "views": "404662", }, { "download_activity_date": "2024-03-02", "views": "330642", }, { "download_activity_date": "2024-03-03", "views": "290354", }, { "download_activity_date": "2024-03-04", "views": "485136", }, { "download_activity_date": "2024-03-05", "views": "289391", }, { "download_activity_date": "2024-03-06", "views": "242157", }, { "download_activity_date": "2024-03-07", "views": "349650", }, ] MOCK_VIDEO_VIEWS_TS_BY_AGG_KEY_ROWS = [ { "id": "4bb70d1e-9352-43ff-a1b6-666935b745fe", "download_activity_date": "2024-04-05", "views": 1462, }, { "id": "30c7e01a-ae39-437f-9099-0a58d3966bc0", "download_activity_date": "2024-04-05", "views": 1550, }, { "id": "8bf95dd6-8a1d-47bc-ba4b-bbea8dac9109", "download_activity_date": "2024-04-05", "views": 1623, }, { "id": "f9f9c4e8-d129-4a17-a355-7bae7239394b", "download_activity_date": "2024-04-05", "views": 1668, }, { "id": "168111fc-7c9b-4490-9e44-60f38903d7d7", "download_activity_date": "2024-04-05", "views": 1668, }, { "id": "36f4d034-6854-4a68-a67f-b93ad6509ec6", "download_activity_date": "2024-04-05", "views": 2737, }, { "id": "a6ef90ad-0fff-4c86-8723-958c9957a9a6", "download_activity_date": "2024-04-05", "views": 2737, }, { "id": "985651ca-256f-40cf-ac85-ec51299f88f8", "download_activity_date": "2024-04-05", "views": 4231, }, { "id": "9892c6bb-c12e-4ea5-a221-a1baf0356817", "download_activity_date": "2024-04-05", "views": 5835, }, { "id": "243192da-f0d2-405c-b9ac-9509eedca035", "download_activity_date": "2024-04-05", "views": 7596, }, { "id": "9710eda7-7979-4788-a19f-5fb059453867", "download_activity_date": "2024-04-05", "views": 14520, }, { "id": "c41c8306-d2e6-410f-9e34-f9c421baa426", "download_activity_date": "2024-04-05", "views": 19238, }, {"id": "Others", "download_activity_date": "2024-04-05", "views": 19959}, { "id": "57af83b0-73a6-4d7f-8a50-548ba9519a14", "download_activity_date": "2024-04-05", "views": 67787, }, { "id": "b1212850-df41-415e-be27-73f93c5257a0", "download_activity_date": "2024-04-05", "views": 83958, }, { "id": "5f24373c-103b-4d7f-a9e2-2606c53ac7e4", "download_activity_date": "2024-04-05", "views": 102878, }, ] MOCK_VIDEO_VIEWS_TS_BY_SUBACCOUNT_ROWS = [ { "id": "456", "download_activity_date": "2024-04-05", "views": 102878, "label_id": 123, } ] MOCK_VIDEO_VIEWS_TS_BY_TRACK_FAMILY_ROWS = [ { "isrc": "QM4TW2296817", "prod_fam_no": None, "views": 31241, "label_id": "123", "download_activity_date": "2024-04-05", }, { "isrc": "QM4TW2296817", "prod_fam_no": None, "views": 12341, "label_id": "123", "download_activity_date": "2024-04-07", }, { "isrc": "QMFMF2375588", "prod_fam_no": 1234567, "views": 43112, "label_id": "123", "download_activity_date": "2024-04-06", }, { "isrc": "QMFMF2375588", "prod_fam_no": 1234567, "views": 35672, "label_id": "123", "download_activity_date": "2024-04-08", }, ] def test_get_account_video_timeseries_total_returns_no_aggregation_key_total(): with patch( "analytics.logic.account_timeseries.AccountVideoTimeseries.execute", return_value=MOCK_VIDEO_VIEWS_TS_TOTAL_ROWS, ): ts = get_account_video_timeseries( {"account_id": 123, "views_type": "AD_SUPPORTED", "type": "TOTAL"}, {} ) first_key = ts[0] assert "date" in first_key assert "views" in first_key assert len(first_key) == 2 def test_get_account_video_timeseries_total_returns_no_aggregation_key(): with patch( "analytics.logic.account_timeseries.AccountVideoTimeseries.execute", return_value=MOCK_VIDEO_VIEWS_TS_BY_AGG_KEY_ROWS, ): ts = get_account_video_timeseries( {"account_id": 123, "views_type": "ALL", "type": "PARTICIPANT"}, {} ) first_key = ts[0] assert "date" in first_key assert "views" in first_key assert "global_participant_id" in first_key assert len(first_key) == 3 def test_get_acccount_video_timeseries_by_subaccount_returns_label_id_and_subaccount_id(): with patch( "analytics.logic.account_timeseries.AccountVideoTimeseries.execute", return_value=MOCK_VIDEO_VIEWS_TS_BY_SUBACCOUNT_ROWS, ): ts = get_account_video_timeseries( { "account_id": 123, "type": "SUBACCOUNT", "views_type": "ALL", }, {}, ) assert len(ts) == 1 assert len(ts[0]) == 4 def test_get_account_video_timeseries_track_family_returns_required_fields(): with patch( "analytics.logic.account_timeseries.AccountVideoTimeseries.execute", return_value=MOCK_VIDEO_VIEWS_TS_BY_TRACK_FAMILY_ROWS, ): result = get_account_video_timeseries( {"account_id": 123, "views_type": "ALL", "type": "TRACK_FAMILY"}, {} ) first_item = result[0] assert len(result) == 4 assert "date" in first_item assert "views" in first_item assert "isrc" in first_item assert "prod_fam_no" in first_item assert len(first_item) == 4