Skip to content

Commit

Permalink
Merge branch 'ddb2-port-lookup' into develop
Browse files Browse the repository at this point in the history
Fixes #1780.
  • Loading branch information
toastdriven committed Oct 25, 2013
2 parents dc8bbea + 1bb7758 commit 2cecaca
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
41 changes: 40 additions & 1 deletion boto/dynamodb2/table.py
Expand Up @@ -418,6 +418,45 @@ def get_item(self, consistent=False, **kwargs):
item.load(item_data)
return item

def lookup(self, *args, **kwargs):
"""
Look up an entry in DynamoDB. This is mostly backwards compatible
with boto.dynamodb. Unlike get_item, it takes hash_key and range_key first,
although you may still specify keyword arguments instead.
Also unlike the get_item command, if the returned item has no keys
(i.e., it does not exist in DynamoDB), a None result is returned, instead
of an empty key object.
Example::
>>> user = users.lookup(username)
>>> user = users.lookup(username, consistent=True)
>>> app = apps.lookup('my_customer_id', 'my_app_id')
"""
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

def new_item(self, *args):
"""
Returns a new, blank item
This is mostly for consistency with boto.dynamodb
"""
if not self.schema:
self.describe()
data = {}
for x, arg in enumerate(args):
data[self.schema[x].name] = arg
return Item(self, data=data)


def put_item(self, data, overwrite=False):
"""
Saves an entire item to DynamoDB.
Expand Down Expand Up @@ -1164,4 +1203,4 @@ def resend_unprocessed(self):
self.handle_unprocessed(resp)
boto.log.info(
"%s unprocessed items left" % len(self._unprocessed)
)
)
58 changes: 58 additions & 0 deletions tests/unit/dynamodb2/test_table.py
Expand Up @@ -1286,6 +1286,64 @@ def test_get_item(self):
'username': {'S': 'johndoe'}
}, consistent_read=False)

def test_lookup_hash(self):
"""Tests the "lookup" function with just a hash key"""
expected = {
'Item': {
'username': {'S': 'johndoe'},
'first_name': {'S': 'John'},
'last_name': {'S': 'Doe'},
'date_joined': {'N': '1366056668'},
'friend_count': {'N': '3'},
'friends': {'SS': ['alice', 'bob', 'jane']},
}
}

# Set the Schema
self.users.schema = [
HashKey('username'),
RangeKey('date_joined', data_type=NUMBER),
]

with mock.patch.object(
self.users,
'get_item',
return_value=expected) as mock_get_item:
self.users.lookup('johndoe')

mock_get_item.assert_called_once_with(
username= 'johndoe')

def test_lookup_hash_and_range(self):
"""Test the "lookup" function with a hash and range key"""
expected = {
'Item': {
'username': {'S': 'johndoe'},
'first_name': {'S': 'John'},
'last_name': {'S': 'Doe'},
'date_joined': {'N': '1366056668'},
'friend_count': {'N': '3'},
'friends': {'SS': ['alice', 'bob', 'jane']},
}
}

# Set the Schema
self.users.schema = [
HashKey('username'),
RangeKey('date_joined', data_type=NUMBER),
]

with mock.patch.object(
self.users,
'get_item',
return_value=expected) as mock_get_item:
self.users.lookup('johndoe', 1366056668)

mock_get_item.assert_called_once_with(
username= 'johndoe',
date_joined= 1366056668)


def test_put_item(self):
with mock.patch.object(
self.users.connection,
Expand Down

0 comments on commit 2cecaca

Please sign in to comment.