"""Unit tests for create_view.""" from unittest.mock import patch import pytest import snowflake_views.tasks as tasks from tests.tasks import conftest CREATE_SQL = 'SELECT "create_sql"' GRANT_SQL = 'SELECT "grant_sql"' CLUSTER_SQL = 'SELECT "cluster_sql"' @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: mock_view = load_view_from_config_mock.return_value mock_view.create_sql = CREATE_SQL mock_view.grant_sql = GRANT_SQL mock_view.cluster_sql = CLUSTER_SQL yield load_view_from_config_mock @pytest.fixture def create_view( mock_sf_executor_class, mock_config_sf_params, mock_activity, mock_sf_executor_context_validator, mock_load_view_from_config): """Create view.""" mock_sf_executor_context_validator.format_identifiers.side_effect = [ (CREATE_SQL, None), (GRANT_SQL, None), (CLUSTER_SQL, None)] tasks.create_view(mock_activity, conftest.VIEW_NAME) def test_view_is_loaded(mock_load_view_from_config, create_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, create_view): """Test params are passed to SF executor.""" mock_sf_executor_class.assert_called_with(mock_sf_config) def test_create_sql_is_executed(mock_sf_executor_context, create_view): """Test create SQL is executed.""" mock_sf_executor_context.execute.assert_any_call(CREATE_SQL) def test_grant_sql_is_executed(mock_sf_executor_context, create_view): """Test grant SQL is executed.""" mock_sf_executor_context.execute.assert_any_call(GRANT_SQL) def test_cluster_sql_is_executed(mock_sf_executor_context, create_view): """Test cluster SQL is executed.""" mock_sf_executor_context.execute.assert_any_call(CLUSTER_SQL) def test_identifiers_for_create_sql_are_validated( mock_sf_executor_context_validator, mock_sf_config, create_view): """Test identifiers for create SQL are validated.""" mock_sf_executor_context_validator.format_identifiers.assert_any_call( CREATE_SQL, mock_sf_config) def test_identifiers_for_grant_sql_are_validated( mock_sf_executor_context_validator, mock_sf_config, create_view): """Test identifiers for grant SQL are validated.""" mock_sf_executor_context_validator.format_identifiers.assert_any_call( GRANT_SQL, mock_sf_config) def test_identifiers_for_cluster_sql_are_validated( mock_sf_executor_context_validator, mock_sf_config, create_view): """Test identifiers for cluster SQL are validated.""" mock_sf_executor_context_validator.format_identifiers.assert_any_call( CLUSTER_SQL, mock_sf_config)