Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8e78761

Browse files
committed
make as_list work for all queries
1 parentbbefb99 commit8e78761

File tree

2 files changed

+67
-20
lines changed

2 files changed

+67
-20
lines changed

‎gitlab/__init__.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -506,8 +506,7 @@ def http_list(self, path, query_data={}, as_list=None, **kwargs):
506506
507507
Returns:
508508
list: A list of the objects returned by the server. If `as_list` is
509-
False and no pagination-related arguments (`page`, `per_page`,
510-
`all`) are defined then a GitlabList object (generator) is returned
509+
False then a GitlabList object (generator) is returned
511510
instead. This object will make API calls when needed to fetch the
512511
next items from the server.
513512
@@ -517,21 +516,16 @@ def http_list(self, path, query_data={}, as_list=None, **kwargs):
517516
"""
518517

519518
# In case we want to change the default behavior at some point
520-
as_list=Trueifas_listisNoneelseas_list
519+
as_list=as_listisNoneoras_list
521520

522521
get_all=kwargs.get('all',False)
523522
url=self._build_url(path)
524523

525-
ifget_allisTrue:
526-
returnlist(GitlabList(self,url,query_data,**kwargs))
527-
528-
if'page'inkwargsoras_listisTrue:
529-
# pagination requested, we return a list
530-
returnlist(GitlabList(self,url,query_data,get_next=False,
531-
**kwargs))
532-
533-
# No pagination, generator requested
534-
returnGitlabList(self,url,query_data,**kwargs)
524+
glist=GitlabList(self,url,query_data,
525+
get_next='page'notinkwargsandget_all,**kwargs)
526+
ifas_list:
527+
glist=list(glist)
528+
returnglist
535529

536530
defhttp_post(self,path,query_data={},post_data={},files=None,
537531
**kwargs):

‎gitlab/tests/test_gitlab.py

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
fromhttmockimportHTTMock# noqa
2828
fromhttmockimportresponse# noqa
29-
fromhttmockimporturlmatch# noqa
29+
fromhttmockimportremember_called,urlmatch# noqa
3030
importrequests
3131

3232
importgitlab
@@ -57,6 +57,7 @@ def setUp(self):
5757
deftest_build_list(self):
5858
@urlmatch(scheme='http',netloc="localhost",path="/api/v4/tests",
5959
method="get")
60+
@remember_called
6061
defresp_1(url,request):
6162
headers= {'content-type':'application/json',
6263
'X-Page':1,
@@ -72,6 +73,7 @@ def resp_1(url, request):
7273

7374
@urlmatch(scheme='http',netloc="localhost",path="/api/v4/tests",
7475
method='get',query=r'.*page=2')
76+
@remember_called
7577
defresp_2(url,request):
7678
headers= {'content-type':'application/json',
7779
'X-Page':2,
@@ -82,7 +84,7 @@ def resp_2(url, request):
8284
content='[{"c": "d"}]'
8385
returnresponse(200,content,headers,None,5,request)
8486

85-
withHTTMock(resp_1):
87+
withHTTMock(resp_2,resp_1):
8688
obj=self.gl.http_list('/tests',as_list=False)
8789
self.assertEqual(len(obj),2)
8890
self.assertEqual(obj._next_url,
@@ -94,11 +96,62 @@ def resp_2(url, request):
9496
self.assertEqual(obj.total_pages,2)
9597
self.assertEqual(obj.total,2)
9698

97-
withHTTMock(resp_2):
98-
l=list(obj)
99-
self.assertEqual(len(l),2)
100-
self.assertEqual(l[0]['a'],'b')
101-
self.assertEqual(l[1]['c'],'d')
99+
l=list(obj)
100+
self.assertListEqual(l, [{"a":"b"}])
101+
self.assertEqual(resp_1.call['count'],1)
102+
self.assertFalse(resp_2.call['called'])
103+
104+
deftest_build_list_all(self):
105+
@urlmatch(scheme='http',netloc="localhost",path="/api/v4/tests",
106+
method="get")
107+
@remember_called
108+
defresp_1(url,request):
109+
headers= {'content-type':'application/json',
110+
'X-Page':1,
111+
'X-Next-Page':2,
112+
'X-Per-Page':1,
113+
'X-Total-Pages':2,
114+
'X-Total':2,
115+
'Link': (
116+
'<http://localhost/api/v4/tests?per_page=1&page=2>;'
117+
' rel="next"')}
118+
content='[{"a": "b"}]'
119+
returnresponse(200,content,headers,None,5,request)
120+
121+
@urlmatch(scheme='http',netloc="localhost",path="/api/v4/tests",
122+
method='get',query=r'.*page=2')
123+
@remember_called
124+
defresp_2(url,request):
125+
headers= {'content-type':'application/json',
126+
'X-Page':2,
127+
'X-Next-Page':2,
128+
'X-Per-Page':1,
129+
'X-Total-Pages':2,
130+
'X-Total':2}
131+
content='[{"c": "d"}]'
132+
returnresponse(200,content,headers,None,5,request)
133+
134+
withHTTMock(resp_2,resp_1):
135+
obj=self.gl.http_list('/tests',as_list=False,all=True)
136+
self.assertEqual(len(obj),2)
137+
self.assertEqual(obj._next_url,
138+
'http://localhost/api/v4/tests?per_page=1&page=2')
139+
self.assertEqual(obj.current_page,1)
140+
self.assertEqual(obj.prev_page,None)
141+
self.assertEqual(obj.next_page,2)
142+
self.assertEqual(obj.per_page,1)
143+
self.assertEqual(obj.total_pages,2)
144+
self.assertEqual(obj.total,2)
145+
self.assertEqual(resp_1.call['count'],1)
146+
self.assertFalse(resp_2.call['called'])
147+
self.assertDictEqual(next(obj), {"a":"b"})
148+
self.assertEqual(resp_1.call['count'],1)
149+
self.assertFalse(resp_2.call['called'])
150+
self.assertDictEqual(next(obj), {"c":"d"})
151+
self.assertEqual(resp_1.call['count'],1)
152+
self.assertEqual(resp_2.call['count'],1)
153+
withself.assertRaises(StopIteration):
154+
next(obj)
102155

103156

104157
classTestGitlabHttpMethods(unittest.TestCase):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp