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_summary @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, ), # valid countries can be passed ( { "account_id": 123, "views_type": "ALL", "type": "COUNTRY", "countries": ["DE", "CH", "AT"], "limit": 25.0, }, does_not_raise(), None, ), # empty countries can be passed ( { "account_id": 123, "views_type": "ALL", "type": "TOTAL", "countries": [], "limit": 25, }, 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_summary_input_args_raise_correct_error_or_none( example_input, expectation, message ): with expectation as e: get_account_video_summary(example_input, {}) assert message is None or message in str(e) MOCK_VIDEO_VIEWS_SUMMARY_TOTAL_ROWS = [ { "views": "404662", }, { "views": "330642", }, { "views": "290354", }, ] MOCK_VIDEO_VIEWS_SUMMARY_BY_AGG_KEY_ROWS = [ {"id": "4bb70d1e-9352-43ff-a1b6-666935b745fe", "views": 1462}, {"id": "30c7e01a-ae39-437f-9099-0a58d3966bc0", "views": 1550}, {"id": "8bf95dd6-8a1d-47bc-ba4b-bbea8dac9109", "views": 1623}, ] MOCK_VIDEO_VIEWS_SUMMARY_BY_SUBACCOUNT_ROWS = [ {"id": "456", "views": 102878, "label_id": "123"} ] MOCK_VIDEO_VIEWS_SUMMARY_BY_TRACK_FAMILY_ROWS = [ {"isrc": "QM4TW2296817", "prod_fam_no": None, "views": 31241, "label_id": "123"}, {"isrc": "QMFMF2375588", "prod_fam_no": 1234567, "views": 31241, "label_id": "123"}, ] def test_get_account_video_summary_total_returns_no_aggregation_key_total(): with patch( "analytics.logic.account_timeseries.AccountVideoSummary.execute", return_value=MOCK_VIDEO_VIEWS_SUMMARY_TOTAL_ROWS, ): ts = get_account_video_summary( {"account_id": 123, "views_type": "AD_SUPPORTED", "type": "TOTAL"}, {} ) first_key = ts[0] assert "views" in first_key assert len(first_key) == 1 def test_get_account_video_summary_total_returns_no_aggregation_key(): with patch( "analytics.logic.account_timeseries.AccountVideoSummary.execute", return_value=MOCK_VIDEO_VIEWS_SUMMARY_BY_AGG_KEY_ROWS, ): ts = get_account_video_summary( {"account_id": 123, "views_type": "ALL", "type": "PARTICIPANT"}, {} ) first_key = ts[0] assert "views" in first_key assert "global_participant_id" in first_key assert len(first_key) == 2 def test_get_acccount_video_summary_by_subaccount_returns_label_id_and_subaccount_id(): with patch( "analytics.logic.account_timeseries.AccountVideoSummary.execute", return_value=MOCK_VIDEO_VIEWS_SUMMARY_BY_SUBACCOUNT_ROWS, ): ts = get_account_video_summary( { "account_id": 123, "type": "SUBACCOUNT", "views_type": "ALL", }, {}, ) first_key = ts[0] assert len(ts) == 1 assert len(first_key) == 3 assert "views" in first_key assert "label_id" in first_key assert "subaccount_id" in first_key def test_get_acccount_video_summary_by_track_family_returns_required_fields(): with patch( "analytics.logic.account_timeseries.AccountVideoSummary.execute", return_value=MOCK_VIDEO_VIEWS_SUMMARY_BY_TRACK_FAMILY_ROWS, ): result = get_account_video_summary( {"account_id": 123, "type": "TRACK_FAMILY", "views_type": "ALL"}, {} ) first_key = result[0] assert len(result) == 2 assert len(first_key) == 3 assert "views" in first_key assert "isrc" in first_key assert "prod_fam_no" in first_key