from unittest.mock import patch import pytest from marshmallow import ValidationError from analytics.logic.tiktok import ( get_tiktok_aggregated_summary, get_tiktok_aggregated_time_series, get_tiktok_time_series, ) def test_tiktok_time_series_validation_error(): with pytest.raises(ValidationError): get_tiktok_time_series({}, {}) MOCK_ROWS_TIME_SERIES = [ { "comments": 50, "creations": 0, "date": "2018-12-29", "favorites": 0, "likes": 50, "shares": 50, "streams": 100, "views": 100, }, { "comments": 50, "creations": 0, "date": "2018-12-31", "favorites": 0, "likes": 50, "shares": 50, "streams": 100, "views": 100, }, { "comments": 50, "creations": 0, "date": "2019-01-01", "favorites": 0, "likes": 50, "shares": 50, "streams": 100, "views": 100, }, ] MOCK_COUNTRY_CODE_ROWS_TIME_SERIES = [ { "comments": 50, "creations": 0, "date": "2018-12-30", "favorites": 0, "likes": 50, "shares": 50, "streams": 100, "views": 100, "id": "GB", }, { "comments": 50, "creations": 0, "date": "2018-12-31", "favorites": 0, "likes": 50, "shares": 50, "streams": 100, "views": 100, "id": "GB", }, { "comments": 50, "creations": 0, "date": "2019-01-01", "favorites": 0, "likes": 50, "shares": 50, "streams": 100, "views": 100, "id": "GB", }, ] MOCK_CONTENT_TYPE_ROWS_TIME_SERIES = [ { "comments": 50, "creations": 0, "date": "2018-12-31", "favorites": 0, "likes": 50, "shares": 50, "streams": 100, "views": 100, "id": "PGC", }, { "date": "2019-01-01", "id": "PGC", "streams": 0, "views": 0, "likes": 0, "favorites": 0, "creations": 0, "shares": 0, "comments": 0, }, { "comments": 50, "creations": 0, "date": "2018-12-30", "favorites": 0, "likes": 50, "shares": 50, "streams": 100, "views": 100, "id": "UGC", }, { "date": "2018-12-31", "id": "UGC", "streams": 0, "views": 0, "likes": 0, "favorites": 0, "creations": 0, "shares": 0, "comments": 0, }, { "comments": 50, "creations": 0, "date": "2019-01-01", "favorites": 0, "likes": 50, "shares": 50, "streams": 100, "views": 100, "id": "UGC", }, ] def test_tiktok_time_series_total(): with patch( "analytics.logic.tiktok.TiktokTimeSeries.execute", return_value=MOCK_ROWS_TIME_SERIES, ) as execute: ts = get_tiktok_time_series( { "isrc": "QMFME2004132", "reverse_isrc": "2004132QMFME", "days_back": 2, "user_has_full_access": False, }, {}, ) assert execute.call_count == 1 assert ts == MOCK_ROWS_TIME_SERIES def test_tiktok_time_series_by_country(): with patch( "analytics.logic.tiktok.TiktokTimeSeries.execute", return_value=MOCK_COUNTRY_CODE_ROWS_TIME_SERIES, ) as execute: ts = get_tiktok_time_series( { "isrc": "QMFME2004132", "reverse_isrc": "2004132QMFME", "type": "BY_COUNTRY", "days_back": 2, "user_has_full_access": False, }, {}, ) assert execute.call_count == 1 assert ts == MOCK_COUNTRY_CODE_ROWS_TIME_SERIES def test_tiktok_time_series_by_content_type(): with patch( "analytics.logic.tiktok.TiktokTimeSeries.execute", return_value=MOCK_CONTENT_TYPE_ROWS_TIME_SERIES, ) as execute: ts = get_tiktok_time_series( { "isrc": "QMFME2004132", "reverse_isrc": "2004132QMFME", "type": "BY_CONTENT_TYPE", "days_back": 2, "user_has_full_access": False, }, {}, ) assert execute.call_count == 1 assert ts == MOCK_CONTENT_TYPE_ROWS_TIME_SERIES MOCK_ROWS_SUMMARY = [ { "comments": 50, "creations": 0, "favorites": 0, "likes": 50, "shares": 50, "streams": 100, "views": 100, } ] MOCK_COUNTRY_CODE_ROWS_SUMMARY = [ { "comments": 50, "creations": 0, "favorites": 0, "likes": 50, "shares": 50, "streams": 100, "views": 100, "id": "GB", }, { "comments": 50, "creations": 0, "favorites": 0, "likes": 50, "shares": 50, "streams": 100, "views": 100, "id": "DE", }, ] MOCK_CONTENT_TYPE_ROWS_SUMMARY = [ { "comments": 50, "creations": 0, "favorites": 0, "likes": 50, "shares": 50, "streams": 100, "views": 100, "id": "UGC", }, { "comments": 50, "creations": 0, "favorites": 0, "likes": 50, "shares": 50, "streams": 100, "views": 100, "id": "PGC", }, ] MOCK_AGGREGATED_SUMMARY_RESPONSE = [ { "result": '{\n "AGGREGATED_SUMMARY_BY_CONTENT_TYPE": [\n {\n "CONTENT_TYPE": "PGC",\n "CREATIONS_28_DAYS": 15097,\n "CREATIONS_7_DAYS": 3282,\n "CREATIONS_ALL_TIME": 251396,\n "VIEWS_28_DAYS": 98354799,\n "VIEWS_7_DAYS": 20897107,\n "VIEWS_ALL_TIME": 1783521941\n },\n {\n "CONTENT_TYPE": "UGC",\n "CREATIONS_28_DAYS": 38471,\n "CREATIONS_7_DAYS": 8006,\n "CREATIONS_ALL_TIME": 1740686,\n "VIEWS_28_DAYS": 158370182,\n "VIEWS_7_DAYS": 40109733,\n "VIEWS_ALL_TIME": 5174401606\n }\n ],\n "AGGREGATED_SUMMARY_TOTAL": {\n "CREATIONS_28_DAYS": 53568,\n "CREATIONS_7_DAYS": 11288,\n "CREATIONS_ALL_TIME": 1992082,\n "VIEWS_28_DAYS": 256724981,\n "VIEWS_7_DAYS": 61006840,\n "VIEWS_ALL_TIME": 6957923547\n },\n "ALL_OTHER_COUNTRIES_CREATIONS": {\n "CREATIONS_28_DAYS": 21145,\n "CREATIONS_7_DAYS": 4426,\n "CREATIONS_ALL_TIME": 749451\n },\n "ALL_OTHER_COUNTRIES_VIEWS": {\n "VIEWS_28_DAYS": 118642302,\n "VIEWS_7_DAYS": 27045833,\n "VIEWS_ALL_TIME": 3182260117\n },\n "TOP5_COUNTRIES_ORDERED_BY_ALL_TIME_CREATIONS": [\n {\n "COUNTRY_CODE": "MX",\n "CREATIONS_28_DAYS": 12926,\n "CREATIONS_7_DAYS": 2719,\n "CREATIONS_ALL_TIME": 446883\n },\n {\n "COUNTRY_CODE": "CO",\n "CREATIONS_28_DAYS": 4023,\n "CREATIONS_7_DAYS": 758,\n "CREATIONS_ALL_TIME": 230594\n },\n {\n "COUNTRY_CODE": "US",\n "CREATIONS_28_DAYS": 6708,\n "CREATIONS_7_DAYS": 1551,\n "CREATIONS_ALL_TIME": 225956\n },\n {\n "COUNTRY_CODE": "PE",\n "CREATIONS_28_DAYS": 5353,\n "CREATIONS_7_DAYS": 1144,\n "CREATIONS_ALL_TIME": 173073\n },\n {\n "COUNTRY_CODE": "ES",\n "CREATIONS_28_DAYS": 3413,\n "CREATIONS_7_DAYS": 690,\n "CREATIONS_ALL_TIME": 166125\n }\n ],\n "TOP5_COUNTRIES_ORDERED_BY_ALL_TIME_VIEWS": [\n {\n "COUNTRY_CODE": "MX",\n "VIEWS_28_DAYS": 58104836,\n "VIEWS_7_DAYS": 14778103,\n "VIEWS_ALL_TIME": 1725112908\n },\n {\n "COUNTRY_CODE": "US",\n "VIEWS_28_DAYS": 22722670,\n "VIEWS_7_DAYS": 6065763,\n "VIEWS_ALL_TIME": 786343353\n },\n {\n "COUNTRY_CODE": "PE",\n "VIEWS_28_DAYS": 30691456,\n "VIEWS_7_DAYS": 8228171,\n "VIEWS_ALL_TIME": 493535366\n },\n {\n "COUNTRY_CODE": "CO",\n "VIEWS_28_DAYS": 14999242,\n "VIEWS_7_DAYS": 2786675,\n "VIEWS_ALL_TIME": 420897333\n },\n {\n "COUNTRY_CODE": "ES",\n "VIEWS_28_DAYS": 11564475,\n "VIEWS_7_DAYS": 2102295,\n "VIEWS_ALL_TIME": 349774470\n }\n ]\n}' # noqa } ] # noqa MOCK_ROWS_AGGREGATED_SUMMARY = { "aggregated_summary_by_content_type": [ { "content_type": "pgc", "creations_28_days": 15097, "creations_7_days": 3282, "creations_all_time": 251396, "views_28_days": 98354799, "views_7_days": 20897107, "views_all_time": 1783521941, }, { "content_type": "ugc", "creations_28_days": 38471, "creations_7_days": 8006, "creations_all_time": 1740686, "views_28_days": 158370182, "views_7_days": 40109733, "views_all_time": 5174401606, }, ], "aggregated_summary_total": { "creations_28_days": 53568, "creations_7_days": 11288, "creations_all_time": 1992082, "views_28_days": 256724981, "views_7_days": 61006840, "views_all_time": 6957923547, }, "all_other_countries_creations": { "creations_28_days": 21145, "creations_7_days": 4426, "creations_all_time": 749451, }, "all_other_countries_views": { "views_28_days": 118642302, "views_7_days": 27045833, "views_all_time": 3182260117, }, "top5_countries_ordered_by_all_time_creations": [ { "country_code": "mx", "creations_28_days": 12926, "creations_7_days": 2719, "creations_all_time": 446883, }, { "country_code": "co", "creations_28_days": 4023, "creations_7_days": 758, "creations_all_time": 230594, }, { "country_code": "us", "creations_28_days": 6708, "creations_7_days": 1551, "creations_all_time": 225956, }, { "country_code": "pe", "creations_28_days": 5353, "creations_7_days": 1144, "creations_all_time": 173073, }, { "country_code": "es", "creations_28_days": 3413, "creations_7_days": 690, "creations_all_time": 166125, }, ], "top5_countries_ordered_by_all_time_views": [ { "country_code": "mx", "views_28_days": 58104836, "views_7_days": 14778103, "views_all_time": 1725112908, }, { "country_code": "us", "views_28_days": 22722670, "views_7_days": 6065763, "views_all_time": 786343353, }, { "country_code": "pe", "views_28_days": 30691456, "views_7_days": 8228171, "views_all_time": 493535366, }, { "country_code": "co", "views_28_days": 14999242, "views_7_days": 2786675, "views_all_time": 420897333, }, { "country_code": "es", "views_28_days": 11564475, "views_7_days": 2102295, "views_all_time": 349774470, }, ], } MOCK_AGGREGATED_TIMESERIES_RESPONSE = [ { "result": '{\n "ALL_OTHER_COUNTRIES_CREATIONS": [\n {\n "CREATIONS": 1432,\n "DOWNLOAD_ACTIVITY_DATE": "2018-12-31",\n "VIEWS": 1641189\n },\n {\n "CREATIONS": 1168,\n "DOWNLOAD_ACTIVITY_DATE": "2019-01-01",\n "VIEWS": 2109201\n }\n ],\n "ALL_OTHER_COUNTRIES_VIEWS": [\n {\n "CREATIONS": 1560,\n "DOWNLOAD_ACTIVITY_DATE": "2018-12-31",\n "VIEWS": 1847853\n },\n {\n "CREATIONS": 1275,\n "DOWNLOAD_ACTIVITY_DATE": "2019-01-01",\n "VIEWS": 1606883\n }\n ],\n "TOPN_COUNTRIES_ORDERED_BY_ALL_TIME_CREATIONS": [\n {\n "COUNTRY_CODE": "ES",\n "CREATIONS": 296,\n "DOWNLOAD_ACTIVITY_DATE": "2018-12-31",\n "VIEWS": 428791\n },\n {\n "COUNTRY_CODE": "ES",\n "CREATIONS": 229,\n "DOWNLOAD_ACTIVITY_DATE": "2019-01-01",\n "VIEWS": 245371\n },\n {\n "COUNTRY_CODE": "MX",\n "CREATIONS": 345,\n "DOWNLOAD_ACTIVITY_DATE": "2018-12-31",\n "VIEWS": 516125\n },\n {\n "COUNTRY_CODE": "MX",\n "CREATIONS": 263,\n "DOWNLOAD_ACTIVITY_DATE": "2019-01-01",\n "VIEWS": 500259\n }\n ],\n "TOPN_COUNTRIES_ORDERED_BY_ALL_TIME_VIEWS": [\n {\n "COUNTRY_CODE": "MX",\n "CREATIONS": 345,\n "DOWNLOAD_ACTIVITY_DATE": "2018-12-31",\n "VIEWS": 516125\n },\n {\n "COUNTRY_CODE": "MX",\n "CREATIONS": 263,\n "DOWNLOAD_ACTIVITY_DATE": "2019-01-01",\n "VIEWS": 500259\n },\n {\n "COUNTRY_CODE": "US",\n "CREATIONS": 168,\n "DOWNLOAD_ACTIVITY_DATE": "2018-12-31",\n "VIEWS": 222127\n },\n {\n "COUNTRY_CODE": "US",\n "CREATIONS": 122,\n "DOWNLOAD_ACTIVITY_DATE": "2019-01-01",\n "VIEWS": 747689\n }\n ]\n}' # noqa } ] # noqa MOCK_ROWS_AGGREGATED_TIMESERIES = { "all_other_countries_creations": [ {"creations": 1432, "download_activity_date": "2018-12-31", "views": 1641189}, {"creations": 1168, "download_activity_date": "2019-01-01", "views": 2109201}, ], "all_other_countries_views": [ {"creations": 1560, "download_activity_date": "2018-12-31", "views": 1847853}, {"creations": 1275, "download_activity_date": "2019-01-01", "views": 1606883}, ], "topn_countries_ordered_by_all_time_creations": [ { "country_code": "es", "creations": 296, "download_activity_date": "2018-12-31", "views": 428791, }, { "country_code": "es", "creations": 229, "download_activity_date": "2019-01-01", "views": 245371, }, { "country_code": "mx", "creations": 345, "download_activity_date": "2018-12-31", "views": 516125, }, { "country_code": "mx", "creations": 263, "download_activity_date": "2019-01-01", "views": 500259, }, ], "topn_countries_ordered_by_all_time_views": [ { "country_code": "mx", "creations": 345, "download_activity_date": "2018-12-31", "views": 516125, }, { "country_code": "mx", "creations": 263, "download_activity_date": "2019-01-01", "views": 500259, }, { "country_code": "us", "creations": 168, "download_activity_date": "2018-12-31", "views": 222127, }, { "country_code": "us", "creations": 122, "download_activity_date": "2019-01-01", "views": 747689, }, ], } def test_tiktok_summary(): with patch( "analytics.logic.tiktok.TiktokTimeSeries.execute", return_value=MOCK_ROWS_SUMMARY, ) as execute: ts = get_tiktok_time_series( { "isrc": "QMFME2004132", "reverse_isrc": "2004132QMFME", "summary": True, "user_has_full_access": False, }, {}, ) assert execute.call_count == 1 assert ts == MOCK_ROWS_SUMMARY def test_tiktok_summary_by_country(): with patch( "analytics.logic.tiktok.TiktokTimeSeries.execute", return_value=MOCK_COUNTRY_CODE_ROWS_SUMMARY, ) as execute: ts = get_tiktok_time_series( { "isrc": "QMFME2004132", "reverse_isrc": "2004132QMFME", "type": "BY_COUNTRY", "summary": True, "user_has_full_access": False, }, {}, ) assert execute.call_count == 1 assert ts == [ { "comments": 50, "id": "GB", "creations": 0, "favorites": 0, "likes": 50, "shares": 50, "streams": 100, "views": 100, }, { "comments": 50, "id": "DE", "creations": 0, "favorites": 0, "likes": 50, "shares": 50, "streams": 100, "views": 100, }, ] def test_tiktok_summary_by_content_type(): with patch( "analytics.logic.tiktok.TiktokTimeSeries.execute", return_value=MOCK_CONTENT_TYPE_ROWS_SUMMARY, ) as execute: ts = get_tiktok_time_series( { "isrc": "QMFME2004132", "reverse_isrc": "2004132QMFME", "type": "BY_CONTENT_TYPE", "summary": True, "user_has_full_access": False, }, {}, ) assert execute.call_count == 1 assert ts == [ { "comments": 50, "id": "UGC", "creations": 0, "favorites": 0, "likes": 50, "shares": 50, "streams": 100, "views": 100, }, { "comments": 50, "id": "PGC", "creations": 0, "favorites": 0, "likes": 50, "shares": 50, "streams": 100, "views": 100, }, ] MOCK_ROWS_AGGREGATE = [ { "creations_1_day": 38, "creations_28_days": 425, "creations_7_days": 89, "creations_all_time": 8986, "creations_growth_percentage_1_day": 0.0, "creations_growth_percentage_28_days": 0.0, "creations_growth_percentage_7_days": -0.315385, "views_1_day": 30326, "views_28_days": 484436, "views_7_days": 118957, "views_all_time": 11159315, "views_growth_percentage_1_day": 2.805974, "views_growth_percentage_28_days": -0.513805, "views_growth_percentage_7_days": -0.286075, } ] def test_tiktok_aggregated_summary(): with patch( "analytics.logic.tiktok.TiktokAggregatedSummary.execute", return_value=MOCK_AGGREGATED_SUMMARY_RESPONSE, ) as execute: agg = get_tiktok_aggregated_summary( { "isrc": "QMFME2004132", "reverse_isrc": "2004132QMFME", "user_has_full_access": False, }, {}, ) assert execute.call_count == 1 assert agg == MOCK_ROWS_AGGREGATED_SUMMARY def test_tiktok_aggregated_timeseries(): with patch( "analytics.logic.tiktok.TiktokAggregatedTimeSeries.execute", return_value=MOCK_AGGREGATED_TIMESERIES_RESPONSE, ) as execute: agg = get_tiktok_aggregated_time_series( { "isrc": "QMFME2004132", "reverse_isrc": "2004132QMFME", "days_back": 1, "user_has_full_access": False, }, {}, ) assert execute.call_count == 1 assert agg == MOCK_ROWS_AGGREGATED_TIMESERIES