Skip to content

Commit

Permalink
Merge pull request getmoto#350 from zkourouma/add_lookup_to_dynamo
Browse files Browse the repository at this point in the history
[dynamodb2] adds lookup method to Table class
  • Loading branch information
spulec committed May 20, 2015
2 parents be5f041 + f03ded7 commit 7c75381
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions moto/dynamodb2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,16 @@ def scan(self, filters):
results.append(result)
return results, scanned_count, last_page

def lookup(self, *args, **kwargs):
if not self.schema:
self.describe()
for x, arg in enumerate(args):
kwargs[self.schema[x].name] = arg
ret = self.get_item(**kwargs)
if not ret.keys():
return None
return ret


class DynamoDBBackend(BaseBackend):

Expand Down
20 changes: 20 additions & 0 deletions tests/test_dynamodb2/test_dynamodb_table_with_range_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,23 @@ def test_query_with_global_indexes():

results = table.query(status__eq='active')
list(results).should.have.length_of(0)


@mock_dynamodb2
def test_lookup():
from decimal import Decimal
table = Table.create('messages', schema=[
HashKey('test_hash'),
RangeKey('test_range'),
], throughput={
'read': 10,
'write': 10,
})

hash_key = 3241526475
range_key = 1234567890987
data = {'test_hash': hash_key, 'test_range': range_key}
table.put_item(data=data)
message = table.lookup(hash_key, range_key)
message.get('test_hash').should.equal(Decimal(hash_key))
message.get('test_range').should.equal(Decimal(range_key))

0 comments on commit 7c75381

Please sign in to comment.