"""Unit tests for the set_product_to_complete Lambda function.""" from unittest.mock import patch import pytest from src import index from src.constants.queries import APPROVE_PRODUCT from src.exceptions import SetProductToCompleteException @patch('src.index.graphql_gateway') def test_handler_approves_product( mock_gql_gateway, context_event, approve_product_response, ): """Test handler calls approveProduct with product_id from context.""" mock_gql_gateway.execute.return_value = approve_product_response result = index.handler(context_event, None) mock_gql_gateway.execute.assert_called_once() call_args = mock_gql_gateway.execute.call_args assert call_args[0][0] == APPROVE_PRODUCT assert call_args[0][1] == {'productId': 3141281} assert result['product']['upc'] == '231232563123' @patch('src.index.graphql_gateway') def test_handler_graphql_error_raises( mock_gql_gateway, context_event, ): """Test handler wraps GraphQLError in SetProductToCompleteException.""" from lambdacommon.graphql import graphql mock_gql_gateway.execute.side_effect = graphql.GraphQLError( [{'message': 'Test GraphQL error'}] ) with pytest.raises(SetProductToCompleteException) as exc_info: index.handler(context_event, None) assert 'Test GraphQL error' in str(exc_info.value) @patch('src.index.graphql_gateway') def test_handler_returns_serialized_context( mock_gql_gateway, context_event, approve_product_response, ): """Test handler returns properly serialized state machine context.""" mock_gql_gateway.execute.return_value = approve_product_response result = index.handler(context_event, None) assert 'product' in result assert 'project' in result assert 'tracks' in result assert 'label_participants' in result assert 'correlation_id' in result assert 'grps_ingestion_id' in result assert result['correlation_id'] == 'test-correlation-id' assert result['grps_ingestion_id'] == 12345