Skip to content

Commit

Permalink
Merge pull request #2160 from toastdriven/fix-ddbv2-query-order
Browse files Browse the repository at this point in the history
Fixed how ``reverse`` works in DDBv2.

Fixes #2070 & #2115.
  • Loading branch information
toastdriven committed Mar 14, 2014
2 parents 22c6751 + 90672c4 commit afdd805
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 7 deletions.
8 changes: 5 additions & 3 deletions boto/dynamodb2/table.py
Expand Up @@ -824,7 +824,7 @@ def query(self, limit=None, index=None, reverse=False, consistent=False,
(Default: ``None``)
Optionally accepts a ``reverse`` parameter, which will present the
results in reverse order. (Default: ``None`` - normal order)
results in reverse order. (Default: ``False`` - normal order)
Optionally accepts a ``consistent`` parameter, which should be a
boolean. If you provide ``True``, it will force a consistent read of
Expand Down Expand Up @@ -975,12 +975,14 @@ def _query(self, limit=None, index=None, reverse=False, consistent=False,
kwargs = {
'limit': limit,
'index_name': index,
'scan_index_forward': reverse,
'consistent_read': consistent,
'select': select,
'attributes_to_get': attributes_to_get
'attributes_to_get': attributes_to_get,
}

if reverse:
kwargs['scan_index_forward'] = False

if exclusive_start_key:
kwargs['exclusive_start_key'] = {}

Expand Down
82 changes: 81 additions & 1 deletion tests/integration/dynamodb2/test_highlevel.py
Expand Up @@ -515,4 +515,84 @@ def test_query_with_limits(self):
[post['posted_by'] for post in all_posts],
['joe', 'jane', 'joe', 'joe', 'jane', 'joe']
)
self.assertEqual(results._fetches, 3)
self.assertTrue(results._fetches >= 3)

def test_query_with_reverse(self):
posts = Table.create('more-posts', schema=[
HashKey('thread'),
RangeKey('posted_on')
], throughput={
'read': 5,
'write': 5,
})
self.addCleanup(posts.delete)

# Wait for it.
time.sleep(60)

# Add some data.
test_data_path = os.path.join(
os.path.dirname(__file__),
'forum_test_data.json'
)
with open(test_data_path, 'r') as test_data:
data = json.load(test_data)

with posts.batch_write() as batch:
for post in data:
batch.put_item(post)

time.sleep(5)

# Test the default order (ascending).
results = posts.query(
thread__eq='Favorite chiptune band?',
posted_on__gte='2013-12-24T00:00:00'
)
self.assertEqual(
[post['posted_on'] for post in results],
[
'2013-12-24T12:30:54',
'2013-12-24T12:35:40',
'2013-12-24T13:45:30',
'2013-12-24T14:15:14',
'2013-12-24T14:25:33',
'2013-12-24T15:22:22',
]
)

# Test the explicit ascending order.
results = posts.query(
thread__eq='Favorite chiptune band?',
posted_on__gte='2013-12-24T00:00:00',
reverse=False
)
self.assertEqual(
[post['posted_on'] for post in results],
[
'2013-12-24T12:30:54',
'2013-12-24T12:35:40',
'2013-12-24T13:45:30',
'2013-12-24T14:15:14',
'2013-12-24T14:25:33',
'2013-12-24T15:22:22',
]
)

# Test the explicit descending order.
results = posts.query(
thread__eq='Favorite chiptune band?',
posted_on__gte='2013-12-24T00:00:00',
reverse=True
)
self.assertEqual(
[post['posted_on'] for post in results],
[
'2013-12-24T15:22:22',
'2013-12-24T14:25:33',
'2013-12-24T14:15:14',
'2013-12-24T13:45:30',
'2013-12-24T12:35:40',
'2013-12-24T12:30:54',
]
)
6 changes: 3 additions & 3 deletions tests/unit/dynamodb2/test_table.py
Expand Up @@ -883,7 +883,7 @@ def test_max_page_size_and_bigger_limit_fetch_more(self):
self.results.fetch_more()
self.result_function.assert_called_with('john', greeting='Hello', limit=10)
self.result_function.reset_mock()

def test_fetch_more(self):
# First "page".
self.results.fetch_more()
Expand Down Expand Up @@ -2149,7 +2149,7 @@ def test_private_query(self):

mock_query.assert_called_once_with('users',
consistent_read=False,
scan_index_forward=True,
scan_index_forward=False,
index_name=None,
attributes_to_get=None,
limit=4,
Expand Down Expand Up @@ -2196,7 +2196,7 @@ def test_private_query(self):
},
index_name=None,
attributes_to_get=None,
scan_index_forward=True,
scan_index_forward=False,
limit=4,
exclusive_start_key={
'username': {
Expand Down

0 comments on commit afdd805

Please sign in to comment.