Add getitem support for QueryChain
This commit is contained in:
@@ -44,5 +44,16 @@ class QueryChain(object):
|
|||||||
else:
|
else:
|
||||||
skipped += obj_count
|
skipped += obj_count
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
if isinstance(key, slice):
|
||||||
|
return self.__class__(
|
||||||
|
queries=self.queries,
|
||||||
|
limit=key.stop,
|
||||||
|
offset=key.start
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
for obj in self[key:1]:
|
||||||
|
return obj
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<QueryChain at 0x%x>' % id(self)
|
return '<QueryChain at 0x%x>' % id(self)
|
||||||
|
@@ -80,3 +80,12 @@ class TestQueryChain(TestCase):
|
|||||||
|
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
assert repr(self.chain) == '<QueryChain at 0x%x>' % id(self.chain)
|
assert repr(self.chain) == '<QueryChain at 0x%x>' % id(self.chain)
|
||||||
|
|
||||||
|
def test_getitem_with_slice(self):
|
||||||
|
chain = self.chain[1:]
|
||||||
|
assert chain.offset == 1
|
||||||
|
assert chain.limit is None
|
||||||
|
|
||||||
|
def test_getitem_with_single_key(self):
|
||||||
|
article = self.chain[2]
|
||||||
|
assert article == self.articles[0]
|
||||||
|
Reference in New Issue
Block a user