"""Test SFN Connector.""" from unittest.mock import MagicMock, patch import boto3 import pytest from product_review.connectors import sfn @pytest.fixture def reset_sfn_client_cache(): """Reset SFN Client cache.""" sfn.get_sfn_client.cache_clear() @patch("product_review.connectors.sfn.boto3") @patch("product_review.connectors.sfn.config") def test_trigger_sfn(mock_config, mock_boto3, reset_sfn_client_cache): """Test trigger_sfn.""" sfn.get_sfn_client.cache_clear() mock_config.AWS_REGION = "us-east-1" mock_client = MagicMock() mock_client.start_execution.return_value = "baz" mock_boto3.client.return_value = mock_client result = sfn.trigger_sfn("arn:foo", "ex-name-1", {"foo": "bar"}) mock_client.start_execution.assert_called_once_with( **{"stateMachineArn": "arn:foo", "input": '{"foo": "bar"}', "name": "ex-name-1"} ) assert result == (True, "baz") @patch("product_review.connectors.sfn.boto3") @patch("product_review.connectors.sfn.config") def test_trigger_sfn_execution_already_exists_handling( mock_config, mock_boto3, reset_sfn_client_cache ): """Test handling an exception when an execution already exists.""" mock_config.AWS_REGION = "us-east-1" fake_client = boto3.client("stepfunctions", "us-east-1") mock_client = MagicMock() mock_client.exceptions = fake_client.exceptions mock_exception = {"Error": {"Code": "foo", "Message": "test"}} mock_client.start_execution.side_effect = ( fake_client.exceptions.ExecutionAlreadyExists(mock_exception, "op") ) mock_boto3.client.return_value = mock_client assert sfn.trigger_sfn("arn:foo", "ex-name-1", {"foo": "bar"}) == ( False, mock_client.start_execution.side_effect, )