"""Unit tests for Kuwo Snowflake SQL executor.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from snowflake import connector from feed_ingestion.flows.kuwo.snowflake_executor import KuwoSMEFA from feed_ingestion.flows.kuwo.snowflake_executor import KuwoTheOrchardFA from tests.conftest import SubstringMatcher @pytest.fixture def mock_sql_loader(): """Return sql_loader mock.""" sql_loader_path = ( 'feed_ingestion.flows.kuwo.sql_loader') with patch(sql_loader_path) as sql_loader: yield sql_loader @pytest.fixture def mock_executor(sf_config_mock, monkeypatch): """Yield executor context.""" connect_mock = MagicMock() monkeypatch.setattr(connector, 'connect', connect_mock) executor = KuwoTheOrchardFA(sf_config_mock) with patch.object(executor, 'execute', wraps=executor.execute) as \ executor.ex_mock: executor.fetchall = MagicMock() yield executor @pytest.fixture def mock_executor_sme(sf_config_mock, monkeypatch): """Yield executor context.""" connect_mock = MagicMock() monkeypatch.setattr(connector, 'connect', connect_mock) executor = KuwoSMEFA(sf_config_mock) with patch.object(executor, 'execute', wraps=executor.execute) as \ executor.ex_mock: executor.fetchall = MagicMock() yield executor def test_load_staging_fact_table_theorchard(mock_executor): """Test load_staging_fact_table method.""" mock_executor.load_staging_fact_table('2017-01-01') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'test_db.test_schema.' 'staging_fact_analytics_kuwo_theorchard_20170101', 'test_db.test_schema.staging_raw_kuwo' ]), params={ 'reportdate': '2017-01-01', 'storeid': 1601, 'feedid': 49, 'licensor': 'theorchard'}) def test_load_staging_fact_table_sme(mock_executor_sme): """Test load_staging_fact_table method.""" mock_executor_sme.load_staging_fact_table('2017-01-01') assert mock_executor_sme.ex_mock.call_count == 1 mock_executor_sme.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'test_db.test_schema.' 'staging_fact_analytics_kuwo_sme_20170101', 'test_db.test_schema.staging_raw_kuwo' ]), params={ 'reportdate': '2017-01-01', 'storeid': 1601, 'feedid': 49, 'licensor': 'sme'}) def test_load_fact_error_data_theorchard(mock_executor): """Test load_fact_error_data method.""" mock_executor.load_fact_error_data('2017-01-01') assert mock_executor.ex_mock.call_count == 1 print(mock_executor.ex_mock.mock_calls[0][1][0]) mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'test_db.test_schema.' 'staging_fact_analytics_kuwo_theorchard_20170101', 'test_db.test_schema.staging_raw_kuwo', 'test_db.test_schema.fact_analytics_error' ]), params={ 'reportdate': '2017-01-01', 'feedid': 49}) def test_load_fact_error_data_sme(mock_executor_sme): """Test load_fact_error_data method.""" mock_executor_sme.load_fact_error_data('2017-01-01') assert mock_executor_sme.ex_mock.call_count == 1 mock_executor_sme.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'test_db.test_schema.' 'staging_fact_analytics_kuwo_sme_20170101', 'test_db.test_schema.staging_raw_kuwo', 'test_db.test_schema.fact_analytics_error' ]), params={ 'reportdate': '2017-01-01', 'feedid': 49})