"""Unit tests for staging_raw tables related generic Snowflake SQL executor.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from snowflake import connector from feed_ingestion.common.youtube_sf.base_executor \ import YouTubeExecutor from tests.conftest import SubstringMatcher @pytest.fixture def mock_connector_context(): """Yield connector context.""" connector_class_path = ( 'feed_ingestion.common.youtube_sf.base_executor.connector') with patch(connector_class_path) as connector: mock_connector_context = connector.\ connect.return_value.__enter__.return_value yield mock_connector_context def staging_raw_table(self): """Return value to mock the property.""" return 'staging_raw_table' def test_notimplemented_properties(monkeypatch, sf_config_mock): """Test properties to be implemented in feed specific executors.""" monkeypatch.setattr(connector, 'connect', MagicMock()) with pytest.raises(NotImplementedError): YouTubeExecutor(sf_config_mock).staging_raw_table def test_update_channel_names_table(monkeypatch, sf_config_mock): """Test clean_staging_raw_table method.""" connect_mock = MagicMock() monkeypatch.setattr(connector, 'connect', connect_mock) setattr(YouTubeExecutor, 'staging_raw_table', property(staging_raw_table)) YouTubeExecutor(sf_config_mock).update_channel_names_table('2021-09-01') assert connect_mock.return_value.cursor.return_value.execute.\ call_count == 1 def test_cleanup_duplicated_channel_names( monkeypatch, mock_connector_context, sf_config_mock): """Test cleanup_duplicated_channel_names method.""" connect_mock = MagicMock() monkeypatch.setattr(connector, 'connect', connect_mock) YouTubeExecutor(sf_config_mock).cleanup_duplicated_channel_names() assert mock_connector_context.cursor.return_value.execute.\ call_count == 5 mock_connector_context.cursor.return_value.execute.assert_any_call( SubstringMatcher( containing=[ 'CREATE OR REPLACE TRANSIENT', 'test_db.test_schema.dim_youtube_channel_names_duplicates_']), params={}) mock_connector_context.cursor.return_value.execute.assert_any_call( SubstringMatcher( containing=[ 'DELETE FROM', 'test_db.test_schema.dim_youtube_channel_names', 'USING', 'test_db.test_schema.dim_youtube_channel_names_duplicates']), params={}) mock_connector_context.cursor.return_value.execute.assert_any_call( SubstringMatcher( containing=[ 'INSERT INTO', 'test_db.test_schema.dim_youtube_channel_names', 'SELECT * FROM ' 'test_db.test_schema.dim_youtube_channel_names_duplicates']), params={}) mock_connector_context.cursor.return_value.execute.assert_any_call( SubstringMatcher( containing=[ 'DROP TABLE IF EXISTS', 'test_db.test_schema.dim_youtube_channel_names_duplicates']), params={'channel_names_table': 'dim_youtube_channel_names'})