Skip to content

Commit 0d7cbb7

Browse files
aaronlelevierssbarnea
authored andcommitted
ResultList class to support the iterator interface
* [Fixes #581] ResultList class to support the iterator interfac
1 parent 3463f27 commit 0d7cbb7

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

jira/client.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def _field_worker(fields=None, **fieldargs):
147147

148148
class ResultList(list):
149149

150-
def __init__(self, iterable=None, _startAt=None, _maxResults=None, _total=None, _isLast=None):
150+
def __init__(self, iterable=None, _startAt=0, _maxResults=0, _total=0, _isLast=None):
151151
if iterable is not None:
152152
list.__init__(self, iterable)
153153
else:
@@ -159,6 +159,18 @@ def __init__(self, iterable=None, _startAt=None, _maxResults=None, _total=None,
159159
self.isLast = _isLast
160160
self.total = _total
161161

162+
self.iterable = iterable or []
163+
self.current = self.startAt
164+
165+
def __next__(self):
166+
self.current += 1
167+
if self.current > self.total:
168+
raise StopIteration
169+
else:
170+
return self.iterable[self.current - 1]
171+
# Python 2 and 3 compat
172+
next = __next__
173+
162174

163175
class QshGenerator(object):
164176

tests/test_client.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,31 @@ def test_template_list():
127127
assert [t['name'] for t in template_list] == ["Scrum software development", "Kanban software development", "Basic software development",
128128
"Basic Service Desk", "IT Service Desk", "Task management", "Project management",
129129
"Process management"]
130+
131+
132+
def test_result_list():
133+
iterable = [2, 3]
134+
startAt = 0
135+
maxResults = 50
136+
total = 2
137+
138+
results = jira.client.ResultList(iterable, startAt, maxResults, total)
139+
140+
for idx, result in enumerate(results):
141+
assert results[idx] == iterable[idx]
142+
143+
assert next(results) == iterable[0]
144+
assert next(results) == iterable[1]
145+
146+
with pytest.raises(StopIteration):
147+
next(results)
148+
149+
150+
def test_result_list_if_empty():
151+
results = jira.client.ResultList()
152+
153+
for r in results:
154+
raise AssertionError("`results` should be empty")
155+
156+
with pytest.raises(StopIteration):
157+
next(results)

0 commit comments

Comments
 (0)