"""Unit tests for tasks module.""" from unittest.mock import Mock import pytest from dim_refresh_etl.flows.dynamo_sync import config from dim_refresh_etl.flows.dynamo_sync import tasks from dim_refresh_etl.util import sentry_utils @pytest.fixture def acceptable_sync_scope(): """Return acceptable scope type for synchronization.""" return 'analytics_metadata' @pytest.fixture def executor_mock(mocker): """Snowflake executor mock.""" sf_executor_class_path = ( 'dim_refresh_etl.flows.dynamo_sync.tasks.SnowflakeSyncExecutor') sf_executor = mocker.patch(sf_executor_class_path) return sf_executor.return_value.__enter__.return_value def test_bootstrap_fail_on_incorrect_sync_scope(): """Test failure with incorrect sync scope.""" with pytest.raises(TypeError): tasks.bootstrap(Mock(), 'incorrect_sync_scope', '') def test_correct_wcu(acceptable_sync_scope): """Check WCU value.""" result = tasks.bootstrap(Mock(), acceptable_sync_scope, 'True') assert result['table_throughput_settings'] == \ config.table_throughput_settings def test_unload_data_to_s3(sf_config_mock, aws_config, executor_mock): """Test unload data to S3 task.""" s3_path = 's3_path' model_type = 'isrcs' sync_from_date = '2018-01-01 10:00:00' full_refresh = False tasks.unload_data_to_s3( Mock(), model_type, s3_path, sync_from_date, full_refresh, aws_config) executor_mock.unload_to_s3.assert_called_once_with( model_type, s3_path, sync_from_date, full_refresh, aws_config) def test_upload_to_dynamodb_sentry_capture_on_error(mocker): """Test upload_to_dynamodb with error.""" start_upload = mocker.patch( 'dim_refresh_etl.flows.dynamo_sync.dynamo_upload.' 'dynamo_upload.start_upload') start_upload.side_effect = Exception('Random exception') capture_exception = mocker.patch.object(sentry_utils, 'capture_exception') tasks.upload_to_dynamodb(Mock(), Mock(), Mock()) assert capture_exception.call_count == 1 def test_health_check_with_error(): """Test health_check with error.""" with pytest.raises(Exception): tasks.health_check(Mock(), {'error': 'test_error'}) def test_health_check_without_error(): """Test health_check without error.""" activity = Mock() tasks.health_check(activity, error=None) assert activity.logger.info.call_count == 1 @pytest.mark.parametrize( 'param_value, expected_result', [ ('True', True), ('true', True), ('False', False), (None, False) ] ) def test__param_to_bool(param_value, expected_result): """Test _param_to_bool with assumed options.""" result = tasks._param_to_bool(param_value) assert result == expected_result