from datetime import datetime import pytest from app.logger import log from app.utils import get_dates isrcs = ['US66W2401066', 'USSM12200612', 'USSM12401865', 'USSM12209777', 'USSM12404354'] @pytest.mark.parametrize('isrc', isrcs) def test_header(isrc, graphql): variables_go = { "isrc": isrc, "distributors": "theorchard,awal,sme", "orderBy": "streamsAllTime", "topSize": 5, "daysBack": -28, "countries": [], "topCountrySize": 10, "topStoreSize": 10 } variables_insights = { "isrc": isrc, "distributors": "theorchard,sme,awal" } response_go = graphql.fetch_data('GetGlobalSoundRecordingStreamingTrendGO', variables_go) response_insights = graphql.fetch_data('AggregateStreamsQuery', variables_insights) log.info(f"Response from GO: {response_go}") log.info(f"Response from Insights: {response_insights}") response_go_all_time = response_go['globalSoundRecordingByIsrc']['analytics']['streams']['aggregate']['allTime'] response_insights_all_time = response_insights['globalSoundRecordingByIsrc']['analytics']['aggregateStreams']['allTime'] assert response_go_all_time == response_insights_all_time, ( f"Mismatch in all-time streams for ISRC {isrc}: " f"GO={response_go_all_time}, " f"Insights={response_insights_all_time}" ) @pytest.mark.parametrize('isrc', isrcs) def test_pot(isrc, graphql): variables_go = { "isrc": isrc, "distributors": "theorchard,awal,sme", "orderBy": "streamsAllTime", "topSize": 5, "daysBack": -28, "countries": [], "topCountrySize": 10, "topStoreSize": 10 } variables_insights = { "days": -28, "startDate": "HIGHWATERMARK", "isrc": isrc, "countries": [], "storeIds": [], "distributors": "theorchard,sme,awal" } response_go = graphql.fetch_data("GetGlobalSoundRecordingStreamingTrendGO", variables_go) response_insights = graphql.fetch_data("SoundRecordingStreamsTotal", variables_insights) log.info(f"Response_Go: {response_go}") log.info(f"Response_Insights: {response_insights}") go_items = response_go['globalSoundRecordingByIsrc']['analytics']['streams']['aggregate']['items'] insights_items = response_insights['globalSoundRecordingByIsrc']['analytics']['streamsTotal']['items'] if not go_items or not insights_items: pytest.fail("Failed to retrieve items from the responses") if len(go_items) < 28 or len(insights_items) < 28: pytest.fail("Not enough data points in the response") go_date, go_value = go_items[27]['date'], go_items[27]['value'] insights_date, insights_value = insights_items[27]['date'], insights_items[27]['value'] assert go_date == insights_date, f"Dates do not match: {go_date} != {insights_date}" assert go_value == insights_value, f"Values do not match: {go_value} != {insights_value}" @pytest.mark.parametrize('isrc', isrcs) def test_tik_tok(isrc, graphql): common_vars = { "isrc": isrc, "countries": [], } data_availability = graphql.fetch_data('AnalyticsDataAvailability') end_date = data_availability['analyticsDataAvailability']['streams'] dates = get_dates(use_today=False, end_date=end_date) variables_go = {**common_vars, 'daysBack': 28, 'topSize': 5} variables_insights = { **common_vars, 'type': 'ALL', 'ids': [], 'startDate': dates['start_date'], 'endDate': dates['end_date']} response_go = graphql.fetch_data('TikTokCreationsGO', variables_go)[ 'globalSoundRecordingByIsrc']['tiktok'] response_insights = graphql.fetch_data('tiktokTimeseries', variables_insights)[ 'globalSoundRecordingByIsrc']['tiktok'] log.info(f"Response from GO: {response_go}") log.info(f"Response from Insights: {response_insights}") last_item_creations_insights = 0 last_item_creations_insights_date = None if response_insights['timeseries']['items']: last_item_creations_insights = \ response_insights['timeseries']['items'][-1]['creations'] last_item_creations_insights_date = \ response_insights['timeseries']['items'][-1]['date'] last_item_creations_go = ( next(i for i in response_go['timeseries']['items'] if i['date'] == last_item_creations_insights_date))['creations'] assert last_item_creations_go == last_item_creations_insights, ( f"Mismatch in TikTok creations for ISRC {isrc}: " f"GO={last_item_creations_go}, " f"Insights={last_item_creations_insights}" ) @pytest.mark.parametrize('isrc', isrcs) def test_top_charts(isrc, graphql): chart_ids = { 'Daily Top 200': "34f76bdb-4f5b-4917-a47e-02916f7291f5", 'Weekly Top 200': "2cca2f91-f4a1-4995-88f6-3bf8c7f62d3d", 'Daily Viral 100': "0abb8e9c-dfe9-4baf-9a8d-76566c3a09c4", 'Daily Top 100': "14f9b229-0ee1-480e-8d30-461ea37aac90" } go_query_variables = { "isrc": isrc, "platforms": ["APPLEMUSIC", "SPOTIFY"], "currentAppearances": True, "period": "_1_DAY" } insights_query_variables = { "isrc": isrc } response_go = graphql.fetch_data('GetSoundRecordingChartPlacements', go_query_variables) response_insights = graphql.fetch_data('SongMetadataQuery', insights_query_variables) log.info(f"Response from GO: {response_go}") log.info(f"Response from Insights: {response_insights}") sound_recording_id = response_insights['globalSoundRecordingByIsrc']['soundRecording']['id'] insights_rankings_query_variables = { "publicSoundRecordingId": sound_recording_id } response_insights_rankings = graphql.fetch_data('SongAggregatedRankingsV2Query', insights_rankings_query_variables) existing_go_charts = [] for chart_name in chart_ids.keys(): latest_date = next( (i['latestDate'] for i in response_go['globalSoundRecordingByIsrc'] ['analytics']['chartPlacements']['items'] if i['chart']['name'] == chart_name and i['countries']['items'][0]['countryCode'] == 'GLOBAL'), None) if latest_date: existing_go_charts.append(chart_name) for chart_name in existing_go_charts: insights_date_str = [ i['mostRecentTimestamp'] for i in response_insights_rankings ['publicSoundRecording']['chartAggregatedRankingsV2'] if i['chartId'] == chart_ids[chart_name] ] latest_insights_date = max([datetime.strptime(date, '%Y-%m-%d') for date in insights_date_str]) go_date_str = next( i['latestDate'] for i in response_go['globalSoundRecordingByIsrc'] ['analytics']['chartPlacements']['items'] if i['chart']['name'] == chart_name ) go_date = datetime.strptime(go_date_str, '%Y-%m-%d') assert latest_insights_date == go_date, (f"Mismatch in dates for {chart_name}: " f"Insights={latest_insights_date.date()}, " f"GO={go_date.date()}")