import pytest from ....src.config import SNOWFLAKE_CLIENT_SETTINGS from ....src.connectors import snowflake_db from ....src.auth import get_snowflake_private_key from ....src.enums import OutputFormat, SFClientSettings # noinspection SqlNoDataSourceInspection class TestClient: _class = snowflake_db.Client @pytest.fixture(scope="class") def instance(self): settings = { **SNOWFLAKE_CLIENT_SETTINGS, SFClientSettings.PRIVATE_KEY: get_snowflake_private_key(), } sf_client = self._class(**settings) return sf_client select_from: str = """ SELECT TABLE_NAME FROM FACTS.INFORMATION_SCHEMA.TABLES """ @pytest.mark.asyncio async def test_fetch_one(self, instance): table_name = "DIM_BRAND" # Use a table that exists in the database sql_query = f""" {self.select_from} WHERE TABLE_SCHEMA = 'PROD' AND TABLE_NAME = %s; """ result = await instance.afetch_one(sql_query, params=(table_name,)) assert result[0] == table_name, f"Expected {table_name}, got {result[0]}" @staticmethod def validate_df(result, limit): assert len(result) == limit, f"Expected {limit} rows, got {len(result)}" assert not result.columns.empty, "Expected result to be a non-empty DataFrame" @staticmethod def validate_json(result, limit): assert len(result) == limit, f"Expected {limit} rows, got {len(result)}" assert all(isinstance(r, dict) for r in result), "Expected list of dictionaries" @staticmethod def validate_csv(result, limit): assert isinstance(result, str), "Expected result to be a string" assert ( len(result.splitlines()) == limit + 1 ), "Expected header + one line per row" @pytest.mark.asyncio @pytest.mark.parametrize( "output_format, validate_fn", [ (OutputFormat.DF, validate_df), (None, validate_json), (OutputFormat.JSON, validate_json), (OutputFormat.CSV, validate_csv), ], ) async def test_afetch_all(self, instance, output_format, validate_fn): limit = 3 sql_query = f""" {self.select_from} LIMIT {limit}; """ result = await instance.afetch_all(sql_query, output_format=output_format) validate_fn(result, limit)