"""Lambda test module.""" from unittest.mock import call from unittest.mock import patch from marshmallow import ValidationError import pytest from snowflake.connector.errors import OperationalError from constants.general import CHECK_VENDOR_AND_SUBACCOUNT_SQL from constants.general import UPDATE_VENDOR_AND_SUBACCOUNT_SQL from src import app as index from src.app import MappingExist from src.app import RetryException @pytest.fixture def input_data_fixture(): """Return input_data. Returns: dict: header dict """ return { 'parent_rep_owner_key': 'N857', 'rep_owner_key': 'W241', 'vendor_id': 123, 'subaccount_id': 345 } @pytest.fixture def input_data_fixture_incorrect(): """Return incorrect input_data. Returns: dict: header dict """ return { 'parent_rep_owner_key': 'N857', 'rep_owner_key': 'W241', } @patch('src.app.execute_snowflake_query') def test_handler_existing_mapping( mock_execute_snowflake_query, input_data_fixture): """Test handler don't update mapping if it is already exist.""" mock_execute_snowflake_query.return_value = [('W241', 'N857', 234, 456)] with pytest.raises(MappingExist): index.handler(input_data_fixture, {}) mock_execute_snowflake_query.called_once() @patch('src.app.execute_snowflake_query') def test_handler_update_mapping( mock_execute_snowflake_query, input_data_fixture): """Test handler update mapping.""" mock_execute_snowflake_query.return_value = [] index.handler(input_data_fixture, {}) mock_execute_snowflake_query.assert_has_calls([ call(CHECK_VENDOR_AND_SUBACCOUNT_SQL, input_data_fixture), call(UPDATE_VENDOR_AND_SUBACCOUNT_SQL, input_data_fixture)]) @patch('src.app.execute_snowflake_query') def test_handler_snowflake_connectivity_issue( mock_execute_snowflake_query, input_data_fixture): """Test handler if there is connectivity issue.""" mock_execute_snowflake_query.side_effect = OperationalError with pytest.raises(RetryException): index.handler(input_data_fixture, {}) @patch('src.app.execute_snowflake_query') def test_handler_invalid_input( mock_execute_snowflake_query, input_data_fixture_incorrect): """Test handler with invalid input.""" mock_execute_snowflake_query.return_value = [] with pytest.raises(ValidationError): index.handler(input_data_fixture, {})