import time import pytest from app.data import ISRCS from app.src.tests.insights.tests_common import markets_to_test, \ start_date, end_date from app.utils import get_insights_market, generate_excel_file, calculate_diff_percent QUERY = 'tiktokSummary' apollo_endpoint = "/dsp-api/delphi/tiktok/tracks/analytics?breakdowns=content_type_country%2Ctotals&metrics=creations%2Cvideo_views&isrc={0}&start_date={1}&end_date={2}&country_code={3}" @pytest.mark.parametrize("market", markets_to_test) def test_tiktokSummary_all(market, graphql, api_apollo): (insights_views, insights_creations, apollo_views, apollo_creations, diff_views, diff_creations) = [], [], [], [], [], [], for isrc in ISRCS: variables = {"isrc": isrc, "type": "ALL", "startDate": start_date, "endDate": end_date, "countries": get_insights_market(market) } response_insights = graphql.fetch_data(QUERY, variables) insights_items = response_insights['globalSoundRecordingByIsrc']['tiktok']['summary']['items'] insights_views_item = sum(item['views'] for item in insights_items) insights_creations_item = sum(item['creations'] for item in insights_items) insights_views.append(insights_views_item) insights_creations.append(insights_creations_item) response_apollo = api_apollo.get( apollo_endpoint.format(isrc, start_date, end_date, market)) if response_apollo['days_count'] > 0: apollo_breakdowns = response_apollo['breakdowns']['content_type_country'] apollo_views_pgc_item = sum(a for a in apollo_breakdowns['pgc'][market]['video_views'] if a is not None) \ if ('pgc' in apollo_breakdowns and apollo_breakdowns['pgc'] and market in apollo_breakdowns['pgc'] and apollo_breakdowns['pgc'][market]) else 0 apollo_creations_pgc_item = sum(a for a in apollo_breakdowns['pgc'][market]['creations'] if a is not None) \ if ('pgc' in apollo_breakdowns and apollo_breakdowns['pgc'] and market in apollo_breakdowns['pgc'] and apollo_breakdowns['pgc'][market]) else 0 apollo_views_ugc_item = sum(a for a in apollo_breakdowns['ugc'][market]['video_views'] if a is not None) \ if ('ugc' in apollo_breakdowns and apollo_breakdowns['ugc'] and market in apollo_breakdowns['ugc'] and apollo_breakdowns['ugc'][market]) else 0 apollo_creations_ugc_item = sum(a for a in apollo_breakdowns['ugc'][market]['creations'] if a is not None) \ if ('ugc' in apollo_breakdowns and apollo_breakdowns['ugc'] and market in apollo_breakdowns['ugc'] and apollo_breakdowns['ugc'][market]) else 0 else: apollo_views_pgc_item, apollo_creations_pgc_item, apollo_views_ugc_item, apollo_creations_ugc_item = 0, 0, 0, 0 apollo_views_item = apollo_views_pgc_item+apollo_views_ugc_item apollo_creations_item = apollo_creations_pgc_item+apollo_creations_ugc_item apollo_views.append(apollo_views_item) apollo_creations.append(apollo_creations_item) diff_views.append(calculate_diff_percent(apollo_views_item, insights_views_item)) diff_creations.append(calculate_diff_percent(apollo_creations_item, insights_creations_item)) time.sleep(0.5) data_dict = { "isrc": ISRCS, "apollo_views": apollo_views, "insights_views": insights_views, "change_views": diff_views, "apollo_creations": apollo_creations, "insights_creations": insights_creations, "change_creations": diff_creations, } generate_excel_file(data_dict, QUERY+'_all', [start_date, end_date, market])