diff --git a/moto/dynamodb/responses.py b/moto/dynamodb/responses.py index 593d40b4..1c046a3d 100644 --- a/moto/dynamodb/responses.py +++ b/moto/dynamodb/responses.py @@ -155,6 +155,29 @@ class DynamoHandler(object): er = 'com.amazonaws.dynamodb.v20111205#ResourceNotFoundException' return self.error(er) + def BatchGetItem(self, uri, body, headers): + table_batches = body['RequestItems'] + + results = { + "Responses": { + "UnprocessedKeys": {} + } + } + + for table_name, table_request in table_batches.iteritems(): + items = [] + keys = table_request['Keys'] + attributes_to_get = table_request.get('AttributesToGet') + for key in keys: + hash_key = value_from_dynamo_type(key["HashKeyElement"]) + range_key = value_from_dynamo_type(key.get("RangeKeyElement")) + item = dynamodb_backend.get_item(table_name, hash_key, range_key) + if item: + item_describe = item.describe_attrs(attributes_to_get) + items.append(item_describe) + results["Responses"][table_name] = {"Items": items, "ConsumedCapacityUnits": 1} + return json.dumps(results) + def Query(self, uri, body, headers): name = body['TableName'] hash_key = body['HashKeyValue'].values()[0] diff --git a/tests/test_dynamodb/test_dynamodb.py b/tests/test_dynamodb/test_dynamodb.py index 8ff9da7f..fec27bdd 100644 --- a/tests/test_dynamodb/test_dynamodb.py +++ b/tests/test_dynamodb/test_dynamodb.py @@ -338,5 +338,44 @@ def test_write_batch(): table.item_count.should.equal(1) -# Batch read -# Batch write +@mock_dynamodb +def test_batch_read(): + conn = boto.connect_dynamodb() + table = create_table(conn) + + item_data = { + 'Body': 'http://url_to_lolcat.gif', + 'SentBy': 'User A', + 'ReceivedTime': '12/9/2011 11:36:03 PM', + } + item = table.new_item( + hash_key='the-key', + range_key='456', + attrs=item_data, + ) + item.put() + + item = table.new_item( + hash_key='the-key', + range_key='123', + attrs=item_data, + ) + item.put() + + item_data = { + 'Body': 'http://url_to_lolcat.gif', + 'SentBy': 'User B', + 'ReceivedTime': '12/9/2011 11:36:03 PM', + 'Ids': {1, 2, 3}, + 'PK': 7, + } + item = table.new_item( + hash_key='another-key', + range_key='789', + attrs=item_data, + ) + item.put() + + items = table.batch_get_item([('the-key', '123'), ('another-key', '789')]) + count = len([item for item in items]) + count.should.equal(2)