"""Lambda test module.""" from unittest.mock import call from unittest.mock import patch from marshmallow import ValidationError from pymysql import OperationalError import pytest import constants.general as constants from src import app as index from src.app import RetryException @pytest.fixture def input_data_fixture(): """Return input_data. Returns: dict: header dict """ return { 'vendor_id': 123, } @pytest.fixture def input_data_fixture_incorrect(): """Return incorrect input_data. Returns: dict: header dict """ return { 'vendor_id': None } @patch('src.app.execute_query') def test_get_user_id_if_user_exist(mock_execute_query, input_data_fixture): """Test get_user_id if there is a contact for such vendor.""" mock_execute_query.return_value = [{'id': 100}] assert index.get_user_id(input_data_fixture) == {'user_id': 100} mock_execute_query.assert_called_once_with( constants.GET_VENDOR_CONTACT_SQL, input_data_fixture) @patch('src.app.execute_query') def test_get_user_id_if_user_not_exist(mock_execute_query, input_data_fixture): """Test get_user_id if there is no contact for such vendor.""" mock_execute_query.return_value = () assert index.get_user_id(input_data_fixture) == {} mock_execute_query.assert_called_once_with( constants.GET_VENDOR_CONTACT_SQL, input_data_fixture) @patch('src.app.get_user_id') @patch('src.app.execute_query') def test_handler_existing_vendor_contact_and_token( mock_execute_query, mock_get_user_id, input_data_fixture): """Test handler if vendor_contact exists.""" # return user_id when get_user_id is called with input_data_fixture mock_get_user_id.return_value = [{'id': 100}] # return access token when execute_query is called with user_id mock_execute_query.return_value = [{ 'oauth_token': 'token', 'client_id': 100, 'user_id': 100, 'user_type': 'alw', 'expires_REMOVE': 0}] index.handler(input_data_fixture, {}) mock_execute_query.assert_called_once_with( constants.GET_ACCESS_TOKEN_SQL, [{'id': 100}]) mock_get_user_id.assert_called_once_with(input_data_fixture) @patch('src.app.get_user_id') @patch('src.app.execute_query') def test_handler_if_there_is_no_vendor_contact_and_no_token( mock_execute_query, mock_get_user_id, input_data_fixture): """Test handler if vendor_contact does not exist.""" # return fist time when get_user_id is called empty list # and second time when it is called with input_data_fixture mock_get_user_id.side_effect = [(), [{'id': 100}]] # return empty list when execute_query is called with mock_execute_query.return_value = () index.handler(input_data_fixture, {}) mock_execute_query.assert_has_calls([ call(constants.CREATE_VENDOR_CONTACT_SQL, input_data_fixture), call(constants.GET_ACCESS_TOKEN_SQL, [{'id': 100}]), call(constants.CREATE_ACCESS_TOKEN_SQL, [{'id': 100}]) ]) mock_get_user_id.assert_has_calls([ call(input_data_fixture), call(input_data_fixture) ]) @patch('src.app.execute_query') def test_handler_invalid_input( mock_execute_query, input_data_fixture_incorrect): """Test handler with invalid input.""" with pytest.raises(ValidationError): index.handler(input_data_fixture_incorrect, {}) @patch('src.app.execute_query') def test_handler_with_connectivity_issue( mock_execute_query, input_data_fixture): """Test handler with invalid input.""" mock_execute_query.side_effect = OperationalError('MySQL connection issue.') with pytest.raises(RetryException): index.handler(input_data_fixture, {})