Skip to content

Commit

Permalink
Wrap items in a set call for Model.batch_get (#946)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmphilli committed Jun 14, 2021
1 parent b90d2b5 commit dc68e72
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pynamodb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def batch_get(
:param items: Should be a list of hash keys to retrieve, or a list of
tuples if range keys are used.
"""
items = list(items)
items = set(items)
hash_key_attribute = cls._hash_key_attribute()
range_key_attribute = cls._range_key_attribute()
keys_to_get: List[Any] = []
Expand Down
39 changes: 21 additions & 18 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1855,6 +1855,7 @@ def test_batch_get(self):
Model.batch_get
"""
self.init_table_meta(SimpleUserModel, SIMPLE_MODEL_TABLE_DATA)
self.maxDiff = None

with patch(PATCH_METHOD) as req:
req.return_value = SIMPLE_BATCH_GET_ITEMS
Expand Down Expand Up @@ -1888,21 +1889,22 @@ def test_batch_get(self):
item_keys = ['hash-{}'.format(x) for x in range(10)]
for item in SimpleUserModel.batch_get(item_keys, attributes_to_get=['numbers']):
self.assertIsNotNone(item)
req.call_args[0][1]['RequestItems']['SimpleModel']['Keys'].sort(key=json.dumps)
params = {
'ReturnConsumedCapacity': 'TOTAL',
'RequestItems': {
'SimpleModel': {
'Keys': [
{'user_name': {'S': 'hash-9'}},
{'user_name': {'S': 'hash-8'}},
{'user_name': {'S': 'hash-7'}},
{'user_name': {'S': 'hash-6'}},
{'user_name': {'S': 'hash-5'}},
{'user_name': {'S': 'hash-4'}},
{'user_name': {'S': 'hash-3'}},
{'user_name': {'S': 'hash-2'}},
{'user_name': {'S': 'hash-0'}},
{'user_name': {'S': 'hash-1'}},
{'user_name': {'S': 'hash-0'}}
{'user_name': {'S': 'hash-2'}},
{'user_name': {'S': 'hash-3'}},
{'user_name': {'S': 'hash-4'}},
{'user_name': {'S': 'hash-5'}},
{'user_name': {'S': 'hash-6'}},
{'user_name': {'S': 'hash-7'}},
{'user_name': {'S': 'hash-8'}},
{'user_name': {'S': 'hash-9'}}
],
'ProjectionExpression': '#0',
'ExpressionAttributeNames': {
Expand All @@ -1918,21 +1920,22 @@ def test_batch_get(self):
item_keys = ['hash-{}'.format(x) for x in range(10)]
for item in SimpleUserModel.batch_get(item_keys, consistent_read=True):
self.assertIsNotNone(item)
req.call_args[0][1]['RequestItems']['SimpleModel']['Keys'].sort(key=json.dumps)
params = {
'ReturnConsumedCapacity': 'TOTAL',
'RequestItems': {
'SimpleModel': {
'Keys': [
{'user_name': {'S': 'hash-9'}},
{'user_name': {'S': 'hash-8'}},
{'user_name': {'S': 'hash-7'}},
{'user_name': {'S': 'hash-6'}},
{'user_name': {'S': 'hash-5'}},
{'user_name': {'S': 'hash-4'}},
{'user_name': {'S': 'hash-3'}},
{'user_name': {'S': 'hash-2'}},
{'user_name': {'S': 'hash-0'}},
{'user_name': {'S': 'hash-1'}},
{'user_name': {'S': 'hash-0'}}
{'user_name': {'S': 'hash-2'}},
{'user_name': {'S': 'hash-3'}},
{'user_name': {'S': 'hash-4'}},
{'user_name': {'S': 'hash-5'}},
{'user_name': {'S': 'hash-6'}},
{'user_name': {'S': 'hash-7'}},
{'user_name': {'S': 'hash-8'}},
{'user_name': {'S': 'hash-9'}}
],
'ConsistentRead': True
}
Expand Down

0 comments on commit dc68e72

Please sign in to comment.