import pytest from app.logger import log from app.utils import get_dates upcs = ['887396891937', '843436099121', '884385674991', '885686735961', '196006132958'] def find_by_date(items, date): return next((item for item in items if item['date'] == date), None) @pytest.mark.parametrize("upc", upcs) def test_header(upc, graphql): variables_go = { "upc": upc, "distributors": "theorchard,awal,sme", "orderBy": "streamsAllTime", "topSize": 5, "daysBack": -28, "countries": [] } variables_insights = { "upc": upc, "multiProduct": False, "distributors": "theorchard,sme,awal" } response_go = graphql.fetch_data('GetProduct', variables_go) response_insights = graphql.fetch_data('ProductAggregateStreamsQuery', variables_insights) log.info(f"Response from GO: {response_go}") log.info(f"Response from Insights: {response_insights}") response_go_all_time = response_go['globalProductByUpc']['catalogProduct']['analytics']['aggregateStreams']['allTime'] response_go_growth_percentage = response_go['globalProductByUpc']['catalogProduct']['analytics']['aggregateStreams']['growthPercentage7Days']['growthPercentage'] response_insights_all_time = response_insights['globalProductByUpc']['catalogProduct']['analytics']['aggregateStreams']['allTime'] response_insights_growth_percentage = response_insights['globalProductByUpc']['catalogProduct']['analytics']['aggregateStreams']['growthPercentage7Days']['growthPercentage'] assert response_go_all_time == response_insights_all_time, ( f"GO={response_go['globalProductByUpc']['catalogProduct']['analytics']}", f"Insights={response_insights['globalProductByUpc']['catalogProduct']['analytics']}" ) assert response_go_growth_percentage == response_insights_growth_percentage, ( f"GO={response_go['globalProductByUpc']['catalogProduct']['analytics']}", f"Insights={response_insights['globalProductByUpc']['catalogProduct']['analytics']}" ) @pytest.mark.parametrize("upc", upcs) def test_pot_store(upc, graphql): stores_map = { '286': 'Spotify', '1': 'Apple Music', '187': 'Amazon Music', '708': 'Pandora', '348': 'Deezer' } # Get data from GO variables_go = { "upc": upc, "distributors": "theorchard,awal,sme", "orderBy": "streamsAllTime", "topSize": 5, "daysBack": -28, "countries": [] } response_go = graphql.fetch_data('GetProduct', variables_go) log.info(f"Response from GO: {response_go}") # Get initial insights variables_insights = { "limit": 50, "upc": upc, "type": "STORE", "startDate": get_dates()['start_date'], "endDate": get_dates()['end_date'], "countries": [], "stores": [], "multiProduct": False, "distributors": [], "orderBy": "streams", "orderDir": "desc" } response_insights = graphql.fetch_data('ProductSummaryQuery', variables_insights) log.info(f"Response from Insights: {response_insights}") # Process GO data response_go_all_time = response_go['globalProductByUpc']['catalogProduct']['aggregatedStreamsByStore']['byDimension']['topNTimeseries'] response_go_all_time_dict = { k: store['timeseries'] for store in response_go_all_time for k, v in stores_map.items() if v == store['id'] } # Process initial insights response response_insights_ids = [item['id'] for item in response_insights['globalProductByUpc']['catalogProduct']['summary']['items']][:5] response_insights_all_time_dict = {} # Fetch and compare detailed insights for each ID for id_ in response_insights_ids: variables_insights = { "upc": upc, "type": "PRODUCT_STREAMS_BY_STORE", "startDate": get_dates()['start_date'], "endDate": get_dates()['end_date'], "stores": [], "countries": [], "distributors": [], "ids": id_, "multiProduct": False } second_response_insights = graphql.fetch_data('ProductTimeseriesQuery', variables_insights) log.info(f"Second response from Insights: {second_response_insights}") response_insights_all_time_dict[id_] = \ second_response_insights['globalProductByUpc']['catalogProduct']['timeseries']['items'] # Final comparison of data for id_, items in response_go_all_time_dict.items(): insights_items = response_insights_all_time_dict.get(id_) if not insights_items: print(f"ID {id_} not found in Insights data.") continue for item in items: corresponding_item = find_by_date(insights_items, item['date']) if corresponding_item: match = item['value'] == corresponding_item['value'] assert match, \ (f"ID: {id_}, Date: {item['date']}, GO value: {item['value']}, " f"Insights value: {corresponding_item['value']}, Match: {match}") else: print( f"No corresponding item found for ID {id_} on date {item['date']}") @pytest.mark.parametrize("upc", upcs) def test_pot_sos(upc, graphql): # Get data from GO variables_go = { "upc": upc, "distributors": "theorchard,awal,sme", "orderBy": "streamsAllTime", "topSize": 5, "daysBack": -28, "countries": [] } response_go = graphql.fetch_data('GetProduct', variables_go) log.info(f"Response from GO: {response_go}") # Get initial insights variables_insights = { "limit": 50, "upc": upc, "type": "SOS", "startDate": get_dates()['start_date'], "endDate": get_dates()['end_date'], "countries": [], "stores": [], "multiProduct": False, "distributors": [], "orderBy": "streams", "orderDir": "desc" } response_insights = graphql.fetch_data('ProductSummaryQuery', variables_insights) log.info(f"Response from Insights: {response_insights}") # Process GO data response_go_all_time = response_go['globalProductByUpc']['catalogProduct']['aggregatedStreamsBySos']['byDimension']['topNTimeseries'] response_go_all_time_dict = {} for item in response_go_all_time: response_go_all_time_dict[item['id']] = item['timeseries'] # Process initial insights response response_insights_ids = [item['id'] for item in response_insights['globalProductByUpc']['catalogProduct']['summary']['items']][:5] response_insights_all_time_dict = {} # Fetch and compare detailed insights for each ID for id_ in response_insights_ids: variables_insights = { "upc": upc, "type": "PRODUCT_STREAMS_BY_SOS", "startDate": get_dates()['start_date'], "endDate": get_dates()['end_date'], "stores": [], "countries": [], "distributors": [], "ids": id_, "multiProduct": False } second_response_insights = graphql.fetch_data('ProductTimeseriesQuery', variables_insights) log.info(f"Second response from Insights: {second_response_insights}") response_insights_all_time_dict[f'streams_{id_}'] = \ second_response_insights['globalProductByUpc']['catalogProduct']['timeseries']['items'] # Final comparison of data for id_, items in response_go_all_time_dict.items(): insights_items = response_insights_all_time_dict.get(id_) if not insights_items: print(f"ID {id_} not found in Insights data.") continue for item in items: corresponding_item = find_by_date(insights_items, item['date']) if corresponding_item: match = item['value'] == corresponding_item['value'] assert match, \ (f"ID: {id_}, Date: {item['date']}, GO value: {item['value']}, " f"Insights value: {corresponding_item['value']}, Match: {match}") else: print( f"No corresponding item found for ID {id_} on date {item['date']}") @pytest.mark.parametrize("upc", upcs) def test_pot_country(upc, graphql): # Get data from GO variables_go = { "upc": upc, "distributors": "theorchard,awal,sme", "orderBy": "streamsAllTime", "topSize": 5, "daysBack": -28, "countries": [] } response_go = graphql.fetch_data('GetProduct', variables_go) log.info(f"Response from GO: {response_go}") # Get initial insights variables_insights = { "limit": 50, "upc": upc, "type": "COUNTRY", "startDate": get_dates()['start_date'], "endDate": get_dates()['end_date'], "countries": [], "stores": [], "multiProduct": False, "distributors": [], "orderBy": "streams", "orderDir": "desc" } response_insights = graphql.fetch_data('ProductSummaryQuery', variables_insights) log.info(f"Response from Insights: {response_insights}") # Process GO data response_go_all_time = response_go['globalProductByUpc']['catalogProduct']['aggregatedStreamsByCountry']['byDimension']['topNTimeseries'] response_go_all_time_dict = {} for item in response_go_all_time: response_go_all_time_dict[item['id'].upper()] = item['timeseries'] # Process initial insights response response_insights_ids = [item['id'] for item in response_insights['globalProductByUpc']['catalogProduct']['summary']['items']][:5] response_insights_all_time_dict = {} # Fetch and compare detailed insights for each ID for id_ in response_insights_ids: variables_insights = { "upc": upc, "type": "PRODUCT_STREAMS_BY_COUNTRY", "startDate": get_dates()['start_date'], "endDate": get_dates()['end_date'], "stores": [], "countries": [], "distributors": [], "ids": id_, "multiProduct": False } second_response_insights = graphql.fetch_data('ProductTimeseriesQuery', variables_insights) log.info(f"Second response from Insights: {second_response_insights}") response_insights_all_time_dict[f'{id_}'] = \ second_response_insights['globalProductByUpc']['catalogProduct']['timeseries']['items'] # Final comparison of data for id_, items in response_go_all_time_dict.items(): insights_items = response_insights_all_time_dict.get(id_) if not insights_items: print(f"ID {id_} not found in Insights data.") continue for item in items: corresponding_item = find_by_date(insights_items, item['date']) if corresponding_item: match = item['value'] == corresponding_item['value'] assert match, \ (f"ID: {id_}, Date: {item['date']}, GO value: {item['value']}, " f"Insights value: {corresponding_item['value']}, Match: {match}") else: print( f"No corresponding item found for ID {id_} on date {item['date']}") @pytest.mark.parametrize("upc", ['887396891937', '196006132958']) def test_tracklist(upc, graphql): variables_go = { "upc": upc, "distributors": "theorchard,awal,sme", "orderBy": "streamsAllTime", "topSize": 5, "daysBack": -28, "countries": [] } variables_insights = { "limit": 50, "upc": upc, "type": "SOUND_RECORDING", "countries": [], "stores": [], "multiProduct": False, "distributors": [], "orderBy": "streams", "orderDir": "desc" } response_go = graphql.fetch_data('GetProduct', variables_go) response_insights = graphql.fetch_data('ProductSummaryQuery', variables_insights) log.info(f"Response from GO: {response_go}") log.info(f"Response from Insights: {response_insights}") response_go_dict = {} response_insights_dict = {} for track in response_go['globalProductByUpc']['catalogProduct']['tracks']: response_go_dict[track['isrc']] = track['analytics']['streams']['aggregate']['allTime'] for track in response_insights['globalProductByUpc']['catalogProduct']['summary']['items']: response_insights_dict[track['id']] = track['streams'] assert response_go_dict == response_insights_dict