fix: dynamodb2 conditions

This commit is contained in:
Georges Chaudy 2016-09-23 16:14:17 +01:00
parent 97513590c8
commit 6b74487b31
2 changed files with 40 additions and 2 deletions

View File

@ -275,9 +275,14 @@ class Table(object):
raise ValueError("The conditional request failed") raise ValueError("The conditional request failed")
elif key not in current_attr: elif key not in current_attr:
raise ValueError("The conditional request failed") 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") 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: if range_value:
self.items[hash_value][range_value] = item self.items[hash_value][range_value] = item
else: else:

View File

@ -8,6 +8,7 @@ from freezegun import freeze_time
from boto.exception import JSONResponseError from boto.exception import JSONResponseError
from moto import mock_dynamodb2 from moto import mock_dynamodb2
from tests.helpers import requires_boto_gte from tests.helpers import requires_boto_gte
import botocore
try: try:
from boto.dynamodb2.fields import HashKey from boto.dynamodb2.fields import HashKey
from boto.dynamodb2.table import Table from boto.dynamodb2.table import Table
@ -469,6 +470,7 @@ def test_update_item_set():
}) })
@mock_dynamodb2 @mock_dynamodb2
def test_failed_overwrite(): def test_failed_overwrite():
table = Table.create('messages', schema=[ table = Table.create('messages', schema=[
@ -585,6 +587,37 @@ def test_boto3_conditions():
response['Items'][0].should.equal({"username": "johndoe"}) 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 @mock_dynamodb2
def test_scan_pagination(): def test_scan_pagination():
table = _create_user_table() table = _create_user_table()