From 467f669c1e6e48d8158a6b35474ccabe56aab3da Mon Sep 17 00:00:00 2001 From: Garrett Heel Date: Wed, 26 Jun 2019 23:13:01 +0100 Subject: [PATCH] add test for attr doesn't exist --- moto/dynamodb2/models.py | 1 - tests/test_dynamodb2/test_dynamodb.py | 54 +++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/moto/dynamodb2/models.py b/moto/dynamodb2/models.py index 1f2c6deb1..6d3a4b950 100644 --- a/moto/dynamodb2/models.py +++ b/moto/dynamodb2/models.py @@ -13,7 +13,6 @@ from moto.compat import OrderedDict from moto.core import BaseBackend, BaseModel from moto.core.utils import unix_time from moto.core.exceptions import JsonRESTError -from .comparisons import get_comparison_func, get_filter_expression, Op from .comparisons import get_comparison_func from .comparisons import get_filter_expression from .comparisons import get_expected diff --git a/tests/test_dynamodb2/test_dynamodb.py b/tests/test_dynamodb2/test_dynamodb.py index dc41e367e..a4d79f4d1 100644 --- a/tests/test_dynamodb2/test_dynamodb.py +++ b/tests/test_dynamodb2/test_dynamodb.py @@ -1563,7 +1563,6 @@ def test_dynamodb_streams_2(): @mock_dynamodb2 def test_condition_expressions(): client = boto3.client('dynamodb', region_name='us-east-1') - dynamodb = boto3.resource('dynamodb', region_name='us-east-1') # Create the DynamoDB table. client.create_table( @@ -1720,7 +1719,7 @@ def test_condition_expressions(): ) # Make sure update_item honors ConditionExpression as well - dynamodb.update_item( + client.update_item( TableName='test1', Key={ 'client': {'S': 'client1'}, @@ -1737,8 +1736,8 @@ def test_condition_expressions(): } ) - with assert_raises(dynamodb.exceptions.ConditionalCheckFailedException): - dynamodb.update_item( + with assert_raises(client.exceptions.ConditionalCheckFailedException): + client.update_item( TableName='test1', Key={ 'client': { 'S': 'client1'}, @@ -1756,6 +1755,53 @@ def test_condition_expressions(): ) +@mock_dynamodb2 +def test_condition_expression__attr_doesnt_exist(): + client = boto3.client('dynamodb', region_name='us-east-1') + + client.create_table( + TableName='test', + KeySchema=[{'AttributeName': 'forum_name', 'KeyType': 'HASH'}], + AttributeDefinitions=[ + {'AttributeName': 'forum_name', 'AttributeType': 'S'}, + ], + ProvisionedThroughput={'ReadCapacityUnits': 1, 'WriteCapacityUnits': 1}, + ) + + client.put_item( + TableName='test', + Item={ + 'forum_name': {'S': 'foo'}, + 'ttl': {'N': 'bar'}, + } + ) + + + def update_if_attr_doesnt_exist(): + # Test nonexistent top-level attribute. + client.update_item( + TableName='test', + Key={ + 'forum_name': {'S': 'the-key'}, + 'subject': {'S': 'the-subject'}, + }, + UpdateExpression='set #new_state=:new_state, #ttl=:ttl', + ConditionExpression='attribute_not_exists(#new_state)', + ExpressionAttributeNames={'#new_state': 'foobar', '#ttl': 'ttl'}, + ExpressionAttributeValues={ + ':new_state': {'S': 'some-value'}, + ':ttl': {'N': '12345.67'}, + }, + ReturnValues='ALL_NEW', + ) + + update_if_attr_doesnt_exist() + + # Second time should fail + with assert_raises(client.exceptions.ConditionalCheckFailedException): + update_if_attr_doesnt_exist() + + @mock_dynamodb2 def test_query_gsi_with_range_key(): dynamodb = boto3.client('dynamodb', region_name='us-east-1')