Moto: Dynamo UpdateExpression using ADD from zero broken on master

Created on 4 May 2020  路  4Comments  路  Source: spulec/moto

It appears #2924 broke some dynamo updates using ADD.

The issue is that dynamo allows you to use ADD in an UpdateExpression with non-existent attributes and zero will be assumed. The new AST-parsing scheme doesn't seem to honor that.

The following test case demonstrates that syntax. I've verified this worked on master right before the merge #2924, and fails right after:

@mock_dynamodb2
def test_update_item_atomic_counter_from_zero():
    table = "table_t"
    ddb_mock = boto3.client("dynamodb", region_name="eu-west-3")
    ddb_mock.create_table(
        TableName=table,
        KeySchema=[{"AttributeName": "t_id", "KeyType": "HASH"}],
        AttributeDefinitions=[{"AttributeName": "t_id", "AttributeType": "S"}],
        BillingMode="PAY_PER_REQUEST",
    )

    key = {"t_id": {"S": "item1"}}

    ddb_mock.put_item(
        TableName=table,
        Item={"t_id": {"S": "item1"}},
    )

    ddb_mock.update_item(
        TableName=table,
        Key=key,
        UpdateExpression="add n_i :inc1, n_f :inc2",
        ExpressionAttributeValues={":inc1": {"N": "1.2"}, ":inc2": {"N": "-0.5"}},
    )
    updated_item = ddb_mock.get_item(TableName=table, Key=key)["Item"]
    updated_item["n_i"]["N"].should.equal("1.2")
    updated_item["n_f"]["N"].should.equal("-0.5")
bug

All 4 comments

@pvbouwel FYI

Interesting. Thanks for providing the test case @mfogel that really helps to understand the situation. I wasn't aware of this behavior. Will check it with the new approach these type of bugs should be pretty local so hopefully it will be an easy fix. Will look into it.

I'll close this, now that #2975 has been merged.
This should be fixed in moto >= 1.3.15.dev839

Was this page helpful?
0 / 5 - 0 ratings