import time import pytest from app.api_client import ApiClient from app.config import INSIGHTS, APOLLO from app.gql_client import GraphQLClient from app.snowflake import SnowflakeClient from app.spotify_client import SpotifyApiClient @pytest.fixture(scope='session') def api_insights(): return ApiClient(INSIGHTS) @pytest.fixture(scope='session') def api_apollo(): return ApiClient(APOLLO) @pytest.fixture() def spotify(): return SpotifyApiClient() @pytest.fixture(scope='session') def snowflake(): snowflake_client = SnowflakeClient() yield snowflake_client snowflake_client.close_connection() @pytest.fixture(scope='session') def graphql(api_insights): return GraphQLClient(api_insights) @pytest.fixture(autouse=True) def add_delay_between_tests(): yield time.sleep(3) @pytest.fixture def test_results(): return { "ISRC": None, "ARTIST": None, "PRODUCT": None, "COMMENTS": [] } def _fetch_and_parametrize(metafunc, graphql, fixture, query, data_path, id_key, name_key, offset=0, limit=50): base_variables = { "orderBy": "streams_1_day", "orderDir": "DESC", "countries": [], "labelIds": [], "globalParticipantIds": [], "subaccountIds": [], "distributors": "theorchard,awal,sme", "brandsFilter": {"brandUuids": []}, "offset": offset, "limit": limit } response = graphql.fetch_data(query, base_variables) # Traverse nested keys data = response for key in data_path: data = data[key] # Create readable test IDs ids = [] for item in data: identifier = item.get(id_key, "no-id") name = item.get(name_key, "unknown")[:20] ids.append(f"{identifier} {name}") metafunc.parametrize(fixture, data, ids=ids) def pytest_generate_tests(metafunc): api_insights = ApiClient(INSIGHTS) graphql = GraphQLClient(api_insights) if "participant" in metafunc.fixturenames: _fetch_and_parametrize( metafunc, graphql, fixture="participant", query="TopParticipants", data_path=["topGlobalParticipantsResults", "participants"], id_key="id", name_key="name" ) elif "product" in metafunc.fixturenames: _fetch_and_parametrize( metafunc, graphql, fixture="product", query="TopProducts", data_path=["topProductResults", "products"], id_key="upc", name_key="productName" ) elif "record" in metafunc.fixturenames: _fetch_and_parametrize( metafunc, graphql, fixture="record", query="TopGlobalSoundRecordings", data_path=["topGlobalSoundRecordings"], id_key="isrc", name_key="name" ) elif "record_store" in metafunc.fixturenames: _fetch_and_parametrize( metafunc, graphql, fixture="record_store", query="TopGlobalSoundRecordings", data_path=["topGlobalSoundRecordings"], id_key="isrc", name_key="name", offset=50, limit=30 )