Skip to content

Commit

Permalink
Fix #77, broken select() result indexind
Browse files Browse the repository at this point in the history
  • Loading branch information
rudyryk committed Nov 17, 2017
1 parent dfe62eb commit b94a662
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

## 0.5.9 (latest)
## 0.5.10 (latest)

- #77, get object by index from `select()` result should work again

## 0.5.9

- Get rid of `limit(1)` in `get()`
- Tests for aggregated queries
Expand Down
10 changes: 9 additions & 1 deletion peewee_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
except ImportError:
aiomysql = None

__version__ = '0.5.9'
__version__ = '0.5.10'

__all__ = [
### High level API ###
Expand Down Expand Up @@ -792,6 +792,7 @@ def __init__(self, *, cursor=None, query=None):
self._initialized = False
self._cursor = cursor
self._rows = []
self._result_cache = None
self._result_wrapper = self._get_result_wrapper(query)

def __iter__(self):
Expand All @@ -801,6 +802,13 @@ def __iter__(self):
def __len__(self):
return len(self._rows)

def __getitem__(self, idx):
# NOTE: side effects will appear when both
# iterating and accessing by index!
if self._result_cache is None:
self._result_cache = list(self)
return self._result_cache[idx]

def _get_result_wrapper(self, query):
"""Get result wrapper class.
"""
Expand Down
11 changes: 11 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,17 @@ def test(objects):

self.run_with_managers(test)

def test_indexing_result(self):
@asyncio.coroutine
def test(objects):
yield from objects.create(TestModel, text="Test 1")
obj = yield from objects.create(TestModel, text="Test 2")
result = yield from objects.execute(
TestModel.select().order_by(TestModel.text))
self.assertEqual(obj, result[1])

self.run_with_managers(test)

def test_insert_many_rows_query(self):
@asyncio.coroutine
def test(objects):
Expand Down

0 comments on commit b94a662

Please sign in to comment.