Merge pull request #2381 from bblommers/bugfix/2380
2380 - Validate parameter-list for duplicates in dynamodb.batch_get_item
This commit is contained in:
commit
7fa46e9659
@ -318,6 +318,9 @@ class DynamoHandler(BaseResponse):
|
|||||||
|
|
||||||
for table_name, table_request in table_batches.items():
|
for table_name, table_request in table_batches.items():
|
||||||
keys = table_request['Keys']
|
keys = table_request['Keys']
|
||||||
|
if self._contains_duplicates(keys):
|
||||||
|
er = 'com.amazon.coral.validate#ValidationException'
|
||||||
|
return self.error(er, 'Provided list of item keys contains duplicates')
|
||||||
attributes_to_get = table_request.get('AttributesToGet')
|
attributes_to_get = table_request.get('AttributesToGet')
|
||||||
results["Responses"][table_name] = []
|
results["Responses"][table_name] = []
|
||||||
for key in keys:
|
for key in keys:
|
||||||
@ -333,6 +336,15 @@ class DynamoHandler(BaseResponse):
|
|||||||
})
|
})
|
||||||
return dynamo_json_dump(results)
|
return dynamo_json_dump(results)
|
||||||
|
|
||||||
|
def _contains_duplicates(self, keys):
|
||||||
|
unique_keys = []
|
||||||
|
for k in keys:
|
||||||
|
if k in unique_keys:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
unique_keys.append(k)
|
||||||
|
return False
|
||||||
|
|
||||||
def query(self):
|
def query(self):
|
||||||
name = self.body['TableName']
|
name = self.body['TableName']
|
||||||
# {u'KeyConditionExpression': u'#n0 = :v0', u'ExpressionAttributeValues': {u':v0': {u'S': u'johndoe'}}, u'ExpressionAttributeNames': {u'#n0': u'username'}}
|
# {u'KeyConditionExpression': u'#n0 = :v0', u'ExpressionAttributeValues': {u':v0': {u'S': u'johndoe'}}, u'ExpressionAttributeNames': {u'#n0': u'username'}}
|
||||||
|
@ -2141,3 +2141,55 @@ def test_scan_by_non_exists_index():
|
|||||||
ex.exception.response['Error']['Message'].should.equal(
|
ex.exception.response['Error']['Message'].should.equal(
|
||||||
'The table does not have the specified index: non_exists_index'
|
'The table does not have the specified index: non_exists_index'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@mock_dynamodb2
|
||||||
|
def test_batch_items_returns_all():
|
||||||
|
dynamodb = _create_user_table()
|
||||||
|
returned_items = dynamodb.batch_get_item(RequestItems={
|
||||||
|
'users': {
|
||||||
|
'Keys': [{
|
||||||
|
'username': {'S': 'user0'}
|
||||||
|
}, {
|
||||||
|
'username': {'S': 'user1'}
|
||||||
|
}, {
|
||||||
|
'username': {'S': 'user2'}
|
||||||
|
}, {
|
||||||
|
'username': {'S': 'user3'}
|
||||||
|
}],
|
||||||
|
'ConsistentRead': True
|
||||||
|
}
|
||||||
|
})['Responses']['users']
|
||||||
|
assert len(returned_items) == 3
|
||||||
|
assert [item['username']['S'] for item in returned_items] == ['user1', 'user2', 'user3']
|
||||||
|
|
||||||
|
|
||||||
|
@mock_dynamodb2
|
||||||
|
def test_batch_items_should_throw_exception_for_duplicate_request():
|
||||||
|
client = _create_user_table()
|
||||||
|
with assert_raises(ClientError) as ex:
|
||||||
|
client.batch_get_item(RequestItems={
|
||||||
|
'users': {
|
||||||
|
'Keys': [{
|
||||||
|
'username': {'S': 'user0'}
|
||||||
|
}, {
|
||||||
|
'username': {'S': 'user0'}
|
||||||
|
}],
|
||||||
|
'ConsistentRead': True
|
||||||
|
}})
|
||||||
|
ex.exception.response['Error']['Code'].should.equal('ValidationException')
|
||||||
|
ex.exception.response['Error']['Message'].should.equal('Provided list of item keys contains duplicates')
|
||||||
|
|
||||||
|
|
||||||
|
def _create_user_table():
|
||||||
|
client = boto3.client('dynamodb', region_name='us-east-1')
|
||||||
|
client.create_table(
|
||||||
|
TableName='users',
|
||||||
|
KeySchema=[{'AttributeName': 'username', 'KeyType': 'HASH'}],
|
||||||
|
AttributeDefinitions=[{'AttributeName': 'username', 'AttributeType': 'S'}],
|
||||||
|
ProvisionedThroughput={'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5}
|
||||||
|
)
|
||||||
|
client.put_item(TableName='users', Item={'username': {'S': 'user1'}, 'foo': {'S': 'bar'}})
|
||||||
|
client.put_item(TableName='users', Item={'username': {'S': 'user2'}, 'foo': {'S': 'bar'}})
|
||||||
|
client.put_item(TableName='users', Item={'username': {'S': 'user3'}, 'foo': {'S': 'bar'}})
|
||||||
|
return client
|
||||||
|
Loading…
Reference in New Issue
Block a user