"""Unit tests for cache_view.""" from unittest.mock import patch import pytest import snowflake_views.tasks as tasks from tests.tasks import conftest CACHE_SQL = 'SELECT "cache_sql"' ATTEMPT_LIMIT = 5 @pytest.fixture def mock_load_view_from_config(): """Mock load_view_from_config.""" with patch('snowflake_views.tasks.' 'load_view_from_config') as load_view_from_config_mock: load_view_from_config_mock.return_value.cache_sql = CACHE_SQL yield load_view_from_config_mock @pytest.fixture def mock_warm_cache(mock_sf_metadata_conn_object): """Mock warm cache.""" mock_bytes = mock_sf_metadata_conn_object.get_query_scan_bytes_number mock_bytes.return_value = 0 @pytest.fixture def mock_cold_cache(mock_sf_metadata_conn_object): """Mock cold cache.""" mock_bytes = mock_sf_metadata_conn_object.get_query_scan_bytes_number mock_bytes.return_value = 1 @pytest.fixture def cache_view( mock_sf_executor_class, mock_config_sf_microservice_params, mock_sf_executor_context_validator, mock_load_view_from_config, mock_sf_metadata_conn_class, mock_warm_cache, mock_activity): """Cache view.""" mock_sf_executor_context_validator.format_identifiers.side_effect = [ (CACHE_SQL, None)] tasks.cache_view(mock_activity, conftest.VIEW_NAME, ATTEMPT_LIMIT) @pytest.fixture def cache_view_cold_cache( mock_sf_executor_class, mock_config_sf_microservice_params, mock_sf_executor_context_validator, mock_load_view_from_config, mock_sf_metadata_conn_class, mock_cold_cache, mock_activity): """Cache view with cold cache.""" mock_sf_executor_context_validator.format_identifiers.side_effect = [ (CACHE_SQL, None)] tasks.cache_view(mock_activity, conftest.VIEW_NAME, ATTEMPT_LIMIT) def test_view_is_loaded(mock_load_view_from_config, cache_view): """Test view is loaded.""" mock_load_view_from_config.assert_called_with(conftest.VIEW_NAME) def test_params_are_passed_to_sf_executor( mock_sf_executor_class, mock_sf_config, cache_view): """Test params are passed to SF executor.""" mock_sf_executor_class.assert_called_with(mock_sf_config) def test_params_are_passed_to_sf_metadata_connector( mock_sf_metadata_conn_class, mock_sf_config, cache_view): """Test params are passed to SF metadata connector.""" mock_sf_metadata_conn_class.assert_called_with(mock_sf_config) def test_sf_metadata_connector_is_authenticated( mock_sf_metadata_conn_object, cache_view): """Test SF metadata connector is authenticated.""" assert mock_sf_metadata_conn_object.authenticate.called def test_cache_sql_is_executed(mock_sf_executor_context, cache_view): """Test cache SQL is executed.""" mock_sf_executor_context.execute.assert_any_call(CACHE_SQL) def test_identifiers_for_cache_sql_are_validated( mock_sf_executor_context_validator, mock_sf_config, cache_view): """Test identifiers for cache SQL are validated.""" mock_sf_executor_context_validator.format_identifiers.assert_any_call( CACHE_SQL, mock_sf_config) def test_cache_sql_is_run_once_if_caching_successful( mock_sf_executor_context, cache_view): """Test cache SQL is run once if caching is successful.""" assert mock_sf_executor_context.execute.call_count == 1 def test_query_count_is_reached_if_caching_unsuccessful( mock_sf_executor_context, cache_view_cold_cache): """Test query count is reached if caching is unsuccessful.""" assert mock_sf_executor_context.execute.call_count == ATTEMPT_LIMIT def test_log_if_caching_successful(mock_activity, cache_view): """Test log if caching is successful.""" mock_activity.logger.info.assert_called_with( 'View was successfully cached') def test_log_if_caching_unsuccessful(mock_activity, cache_view_cold_cache): """Test log if caching is unsuccessful.""" mock_activity.logger.warning.assert_called_with( 'Cache warmup attempt limit exceeded')