Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DynamoDB: transact_write_items does not return Item in CancellationReason (with example) #8516

Closed
cameron-toy opened this issue Jan 23, 2025 · 0 comments · Fixed by #8522
Closed

Comments

@cameron-toy
Copy link
Contributor

cameron-toy commented Jan 23, 2025

#7523

Original issue was closed due to no reproducible test case

import pytest
import boto3
from moto import mock_aws
from botocore.exceptions import ClientError

@mock_aws
def test_transact_write_condition_check_failure():
    dynamodb = boto3.resource("dynamodb")
    client = boto3.client("dynamodb")
    
    table_name = "test-table"

    table = dynamodb.create_table(
        TableName=table_name,
        KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}],
        AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}],
        BillingMode="PAY_PER_REQUEST"
    )
   
    table.put_item(Item={"pk": "key1", "foo": "bar"})

    try:
        client.transact_write_items(
            TransactItems=[
                {
                    "ConditionCheck": {
                        "TableName": table_name,
                        "Key": {"pk": {"S": "key1"}},
                        "ConditionExpression": "foo = :expected",
                        "ExpressionAttributeValues": {":expected": {"S": "nonexistent"}},
                        "ReturnValuesOnConditionCheckFailure": "ALL_OLD"
                    }
                }
            ]
        )
        pytest.fail("Expected TransactionCanceledException was not raised")
    except ClientError as e:
        error_code = e.response["Error"]["Code"]
        assert error_code == "TransactionCanceledException", f"Unexpected error code: {error_code}"
        
        cancellation_reasons = e.response.get("CancellationReasons", [])
        assert any(
            reason.get("Item") is not None for reason in cancellation_reasons
        ), "No cancellation reasons contain the 'Item' key"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants