import asyncio from unittest.mock import PropertyMock, create_autospec import pytest from monday_com_orca_backend import config from monday_com_orca_backend.connectors.snowflake_db import client SnowflakeConnection = client.sf_connector.SnowflakeConnection CONCURRENCY_COUNT: int = 10 class TestSnowflake: _class = client.Client @pytest.fixture def instance(self): """Create a Snowflake instance with mock credentials.""" return self._class( user="mock_user", account="mock_account", warehouse="mock_warehouse", database="mock_database", schema="mock_schema", private_key="mock_private_key", ) @pytest.fixture def instance_real(self): """Create a Snowflake instance with real credentials, using the FACTS database and PROD schema for testing. """ return self._class( **config.SNOWFLAKE_CREDENTIALS, database="FACTS", schema="PROD", ) @pytest.fixture def instance_mocked_connection(self, instance, mock_connection): """Return an instance with a mocked connection.""" instance.connection = mock_connection return instance @pytest.fixture def mock_connection(self): """Return a mock connection.""" return create_autospec(SnowflakeConnection) @pytest.fixture def patcher_connect(self, mocker, mock_connection): return mocker.patch.object( client.sf_connector, "connect", return_value=mock_connection ) @pytest.fixture def patcher_private_key(self, mocker, instance): return mocker.patch.object( self._class, "private_key", new_callable=PropertyMock ) def test_init(self, instance): assert instance.connection is None @pytest.mark.parametrize( "database", ["mock_database", "", None], ) @pytest.mark.parametrize( "schema", ["mock_schema", "", None], ) @pytest.mark.asyncio async def test_connect_raises_value_error_if_database_or_schema_missing( self, instance, database, schema ): instance.database = database instance.schema = schema # Ensure connection is not set so that the connection flow # is triggered. instance.connection = None if database and schema: assert True # If both are provided, pass the test, as this is a valid case. with pytest.raises(ValueError): await instance.connect() @pytest.mark.asyncio async def test_cursor(self, instance_mocked_connection): await instance_mocked_connection.cursor() assert instance_mocked_connection.connection.cursor.call_count == 1 @pytest.mark.asyncio async def test_connect(self, instance, patcher_connect, patcher_private_key): connections = (instance.connect() for _ in range(CONCURRENCY_COUNT)) await asyncio.gather(*connections) assert patcher_private_key.call_count == 1 assert patcher_connect.call_count == 1 assert not patcher_connect.call_args.kwargs.get( "insecure_mode" ), "Insecure mode should not be used in production." assert isinstance( instance.connection, SnowflakeConnection, ), "Connection was not correctly persisted." @pytest.mark.asyncio async def test_disconnect(self, instance_mocked_connection): instance = instance_mocked_connection mocked_connection = instance.connection # Create multiple coroutines to test concurrency. async def disconnect_task(): await instance.disconnect() await asyncio.gather(*(disconnect_task() for _ in range(CONCURRENCY_COUNT))) assert ( mocked_connection.close.call_count == 1 ), "Connection close should be called only once." assert instance.connection is None, "Connection was not purged." def test_private_key_returns_if_already_set(self, instance): content = b"mock_private_key_content" instance._private_key_content = content assert ( instance.private_key is content ), "Private key content should be returned if already set."