"""Tests for utils.error_log.""" import inspect import pytest from src.constants import common_errors from src.utils import error_log from src.utils import partial def get_abc(x, y, zz, *args, **kwargs): """Works as test data.""" pass @pytest.fixture def arguments(): """Return list of get_abc arguments.""" return list(inspect.signature(get_abc).parameters.values()) @pytest.mark.parametrize( 'func, expected_result', ( (get_abc, ((), {})), (partial.wrapped_partial(get_abc, 'x', zz='z', y='y'), (('x',), {'zz': 'z', 'y': 'y'})), (partial.wrapped_partial(get_abc, 'x', 'y', zz='z'), (('x', 'y'), {'zz': 'z'})), ) ) def test_get_partial_args(func, expected_result): """Test _get_partial_args function.""" result = error_log._get_partial_args(func) assert result == expected_result @pytest.mark.parametrize( 'args, kwargs, expected_result', ( ((), {}, ['x=None', 'y=None', 'zz=None']), (('x', 'y'), {'zz': 'z'}, ['x=x', 'y=y', 'zz=z']), (('x'), {'zz': 'z', 'y': 'y'}, ['x=x', 'y=y', 'zz=z']), (('x', 'y', 'z', 'a1', 'a2'), {'k1': 1, 'k2': '2'}, ['x=x', 'y=y', 'zz=z', 'args=(a1,a2)', 'k1=1', 'k2=2']), ) ) def test_process_arguments(args, kwargs, expected_result, arguments): """Test _process_arguments function.""" result = error_log._process_arguments(arguments, *args, **kwargs) assert result == expected_result @pytest.mark.parametrize( 'func, args, kwargs, partial_args, partial_kwargs, filters', ( (get_abc, ('x', 'y'), {'zz': 'z', 'k': 1}, (), {}, ['f1=1', 'f2=2']), (partial.wrapped_partial(get_abc, 'x', 'y', k1=1), ('z', 'a'), {'k2': '2'}, ('x', 'y'), {'k1': 1}, ['f1=1', 'f2=2']), ) ) def test_get_function_call_description( func, args, kwargs, partial_args, partial_kwargs, filters, arguments, mocker): """Test _get_function_call_description function.""" mocked_get_partial_args = mocker.patch('src.utils.error_log._get_partial_args') mocked_get_partial_args.return_value = (partial_args, partial_kwargs) mocked_process_arguments = mocker.patch( 'src.utils.error_log._process_arguments') mocked_process_arguments.return_value = filters result = error_log._get_function_call_description(func, *args, **kwargs) args = partial_args + args kwargs.update(partial_kwargs) mocked_get_partial_args.assert_called_once_with(func) mocked_process_arguments.assert_called_once_with( arguments, *args, **kwargs) assert result == ('get_abc', ','.join(filters)) def test_log_message(mocker): """Test _log_message function.""" message = 'test msg' target_arn = 'test arn' mocked_logging = mocker.patch('src.utils.error_log.logging') mocked_logger = mocked_logging.logger mocker_error = mocked_logger.error mocked_sns = mocker.patch('src.utils.error_log.sns') mocked_client = mocked_sns.client mocked_publish = mocked_client.publish mocked_config = mocker.patch('src.utils.error_log.sns_config') mocked_config.SNS_TARGET_ARN = target_arn error_log._log_message(message) assert mocker_error.call_count == 1 assert mocker_error.call_args[0] == (message,) assert mocked_publish.call_count == 1 assert mocked_publish.call_args[1] == { 'TargetArn': target_arn, 'Message': message} def test_log_missing_records(mocker): """Test logging of missing records.""" def get_something(): pass def something(): pass records = ['a', 'b', {'k': 'v'}] mocked_log_message = mocker.patch('src.utils.error_log._log_message') message = common_errors.RECORDS_NOT_FOUND.format('something', records) for func in get_something, something: error_log.log_missing_records(func, records) mocked_log_message.assert_has_calls( [mocker.call(message), mocker.call(message)]) def test_log_error_by_func(mocker): """Test log_error_by_func function.""" message = 'mess' mocked_func = mocker.Mock() func_name = 'f1' args = (1,) kwargs = {'a': 2} arguments = 'a=1' error = common_errors.COMMON_FUNCTION_ERROR.format( message, func_name, arguments) mocked_call_description = mocker.patch( 'src.utils.error_log._get_function_call_description') mocked_call_description.return_value = (func_name, arguments) mocked_log_message = mocker.patch('src.utils.error_log._log_message') error_log.log_error_by_func(message, mocked_func, *args, **kwargs) mocked_call_description.assert_called_once_with( mocked_func, *args, **kwargs) mocked_log_message.assert_called_once_with(error)