""" This module contains fixtures and helper functions for the PyTest tests. """ import inspect from pathlib import Path from typing import Callable, Generator import pytest # noqa: F401, F403 import pytest_asyncio import sqlglot from src.backend.connectors import db from src.backend.connectors import snowflake_db as sf from src.backend.environment_vars import AUTH0_SETTINGS from src.backend.logic.look_data.io import snowflake as sf_io from src.backend.security import auth0 from src.backend.typings import LabelID # Import all fixtures from tests.fixtures_test_db for autodiscovery from tests_backend.fixtures_test_db import * # noqa: F401, F403 PROJECT_ROOT = Path(__file__).parent.parent # Use a real label ID for real tests involving label data TEST_LABEL_ID: LabelID = 33960 # For real YouTube Content ID tests, use real asset ID, ISRC, and video ID TEST_YT_ASSET_ID: str = "A640902090016112" TEST_YT_ISRC: str = "QM4TX2107030" TEST_YT_VIDEO_ID: str = "83ytyLgxiuI" # For real YouTube Data API tests, use real channel ID TEST_YT_CHANNEL_ID: str = "UCXpXXSCBSZTtaTr51bTxVfw" # Real Auth0 client for testing purposes. auth0_client: auth0.Auth0 = auth0.Auth0(AUTH0_SETTINGS) @pytest.fixture(scope="module") def snowflake_client(): """Instantiate a Snowflake client for real execution of the queries.""" credentials = sf_io.SnowflakeRequest.credentials return sf.Client(**credentials) @pytest.fixture def db_client(): return db.Client() @pytest.fixture async def sample_audit_group_id(db_client) -> int: """Fixture to get a sample audit group ID from the database for testing.""" db_resp = await db_client.AuditGroups.list(limit=1) sample_audit_group = db_resp[0] return sample_audit_group[0]["group"] @pytest_asyncio.fixture(scope="session") async def get_random_auth0_subject() -> auth0.types.Auth0Subject: """Get a random real Auth0 subject. Returns: Random Auth0 subject. """ users = await auth0_client.get_users() return users[0]["user_id"] @pytest_asyncio.fixture(scope="session") async def get_auth0_user(get_random_auth0_subject) -> dict: """Get the data of an Auth0 subject.""" return await auth0_client.get_user(get_random_auth0_subject) @pytest_asyncio.fixture async def new_user(db_client): user_id = await db_client.Users.create( subject="mock_subject", fields={ "nickname": "mock_nickname", "name": "mock_name", "email": "test@test.com", }, ) return user_id def get_all_functions_in_module(module) -> Generator[Callable, None, None]: """Get all functions in a module. Args: module: Module to get functions from. Returns: Generator of functions. """ for _, func in inspect.getmembers(module, inspect.isfunction): yield func def is_valid_sql(query: str) -> bool: """Check if a SQL query is syntactically valid by attempting to parse it. Args: query: SQL query to check, as a string. Returns: True if the query is valid, False otherwise. """ try: # Attempt to parse the SQL query. # No error means the query is valid. sqlglot.parse_one(query) except sqlglot.errors.ParseError as ex: print(f"Invalid SQL: {ex}") return False return True