diff --git a/moto/dynamodb2/models.py b/moto/dynamodb2/models.py index 0a994f783..a70d6347d 100644 --- a/moto/dynamodb2/models.py +++ b/moto/dynamodb2/models.py @@ -275,9 +275,14 @@ class Table(object): raise ValueError("The conditional request failed") elif key not in current_attr: raise ValueError("The conditional request failed") - elif DynamoType(val['Value']).value != current_attr[key].value: + elif 'Value' in val and DynamoType(val['Value']).value != current_attr[key].value: raise ValueError("The conditional request failed") - + elif 'ComparisonOperator' in val: + comparison_func = get_comparison_func(val['ComparisonOperator']) + dynamo_types = [DynamoType(ele) for ele in val["AttributeValueList"]] + for t in dynamo_types: + if not comparison_func(current_attr[key].value, t.value): + raise ValueError('The conditional request failed') if range_value: self.items[hash_value][range_value] = item else: diff --git a/tests/test_dynamodb2/test_dynamodb_table_without_range_key.py b/tests/test_dynamodb2/test_dynamodb_table_without_range_key.py index 5a32c6b20..691e14818 100644 --- a/tests/test_dynamodb2/test_dynamodb_table_without_range_key.py +++ b/tests/test_dynamodb2/test_dynamodb_table_without_range_key.py @@ -8,6 +8,7 @@ from freezegun import freeze_time from boto.exception import JSONResponseError from moto import mock_dynamodb2 from tests.helpers import requires_boto_gte +import botocore try: from boto.dynamodb2.fields import HashKey from boto.dynamodb2.table import Table @@ -469,6 +470,7 @@ def test_update_item_set(): }) + @mock_dynamodb2 def test_failed_overwrite(): table = Table.create('messages', schema=[ @@ -585,6 +587,37 @@ def test_boto3_conditions(): response['Items'][0].should.equal({"username": "johndoe"}) +@mock_dynamodb2 +def test_boto3_put_item_conditions_fails(): + table = _create_user_table() + table.put_item(Item={'username': 'johndoe', 'foo': 'bar'}) + table.put_item.when.called_with( + Item={'username': 'johndoe', 'foo': 'baz'}, + Expected={ + 'foo': { + 'ComparisonOperator': 'NE', + 'AttributeValueList': ['bar'] + } + }).should.throw(botocore.client.ClientError) + + +@mock_dynamodb2 +def test_boto3_put_item_conditions_pass(): + table = _create_user_table() + table.put_item(Item={'username': 'johndoe', 'foo': 'bar'}) + table.put_item( + Item={'username': 'johndoe', 'foo': 'baz'}, + Expected={ + 'foo': { + 'ComparisonOperator': 'EQ', + 'AttributeValueList': ['bar'] + } + }) + returned_item = table.get_item(Key={'username': 'johndoe'}) + assert dict(returned_item)['Item']['foo'].should.equal("baz") + + + @mock_dynamodb2 def test_scan_pagination(): table = _create_user_table()