diff options
author | Gauvain Pocentek <gauvain@pocentek.net> | 2018-06-17 16:21:55 +0200 |
---|---|---|
committer | Gauvain Pocentek <gauvain@pocentek.net> | 2018-06-17 16:23:31 +0200 |
commit | 1a04634ae37888c3cd80c4676904664b0c8dbeab (patch) | |
tree | cadcc15bbe389c2b9d5af140d9c5213c6f4ffbcb | |
parent | ba90e305bc2d54eb42aa0f8251a9e45b0d1736e4 (diff) | |
download | gitlab-1a04634ae37888c3cd80c4676904664b0c8dbeab.tar.gz |
Revert "make as_list work for all queries"
This reverts commit 8e787612fa77dc945a4c1327e9faa6eee10c48f2.
This change broke the basic generator usage (Fixes #534)
-rw-r--r-- | gitlab/__init__.py | 20 | ||||
-rw-r--r-- | gitlab/tests/test_gitlab.py | 67 |
2 files changed, 20 insertions, 67 deletions
diff --git a/gitlab/__init__.py b/gitlab/__init__.py index 4a79551..8e62188 100644 --- a/gitlab/__init__.py +++ b/gitlab/__init__.py @@ -537,7 +537,8 @@ class Gitlab(object): Returns: list: A list of the objects returned by the server. If `as_list` is - False then a GitlabList object (generator) is returned + False and no pagination-related arguments (`page`, `per_page`, + `all`) are defined then a GitlabList object (generator) is returned instead. This object will make API calls when needed to fetch the next items from the server. @@ -547,16 +548,21 @@ class Gitlab(object): """ # In case we want to change the default behavior at some point - as_list = as_list is None or as_list + as_list = True if as_list is None else as_list get_all = kwargs.get('all', False) url = self._build_url(path) - glist = GitlabList(self, url, query_data, - get_next='page' not in kwargs and get_all, **kwargs) - if as_list: - glist = list(glist) - return glist + if get_all is True: + return list(GitlabList(self, url, query_data, **kwargs)) + + if 'page' in kwargs or as_list is True: + # pagination requested, we return a list + return list(GitlabList(self, url, query_data, get_next=False, + **kwargs)) + + # No pagination, generator requested + return GitlabList(self, url, query_data, **kwargs) def http_post(self, path, query_data={}, post_data={}, files=None, **kwargs): diff --git a/gitlab/tests/test_gitlab.py b/gitlab/tests/test_gitlab.py index 3220988..5174bd2 100644 --- a/gitlab/tests/test_gitlab.py +++ b/gitlab/tests/test_gitlab.py @@ -26,7 +26,7 @@ except ImportError: from httmock import HTTMock # noqa from httmock import response # noqa -from httmock import remember_called, urlmatch # noqa +from httmock import urlmatch # noqa import requests import gitlab @@ -57,7 +57,6 @@ class TestGitlabList(unittest.TestCase): def test_build_list(self): @urlmatch(scheme='http', netloc="localhost", path="/api/v4/tests", method="get") - @remember_called def resp_1(url, request): headers = {'content-type': 'application/json', 'X-Page': 1, @@ -73,7 +72,6 @@ class TestGitlabList(unittest.TestCase): @urlmatch(scheme='http', netloc="localhost", path="/api/v4/tests", method='get', query=r'.*page=2') - @remember_called def resp_2(url, request): headers = {'content-type': 'application/json', 'X-Page': 2, @@ -84,7 +82,7 @@ class TestGitlabList(unittest.TestCase): content = '[{"c": "d"}]' return response(200, content, headers, None, 5, request) - with HTTMock(resp_2, resp_1): + with HTTMock(resp_1): obj = self.gl.http_list('/tests', as_list=False) self.assertEqual(len(obj), 2) self.assertEqual(obj._next_url, @@ -96,62 +94,11 @@ class TestGitlabList(unittest.TestCase): self.assertEqual(obj.total_pages, 2) self.assertEqual(obj.total, 2) - l = list(obj) - self.assertListEqual(l, [{"a": "b"}]) - self.assertEqual(resp_1.call['count'], 1) - self.assertFalse(resp_2.call['called']) - - def test_build_list_all(self): - @urlmatch(scheme='http', netloc="localhost", path="/api/v4/tests", - method="get") - @remember_called - def resp_1(url, request): - headers = {'content-type': 'application/json', - 'X-Page': 1, - 'X-Next-Page': 2, - 'X-Per-Page': 1, - 'X-Total-Pages': 2, - 'X-Total': 2, - 'Link': ( - '<http://localhost/api/v4/tests?per_page=1&page=2>;' - ' rel="next"')} - content = '[{"a": "b"}]' - return response(200, content, headers, None, 5, request) - - @urlmatch(scheme='http', netloc="localhost", path="/api/v4/tests", - method='get', query=r'.*page=2') - @remember_called - def resp_2(url, request): - headers = {'content-type': 'application/json', - 'X-Page': 2, - 'X-Next-Page': 2, - 'X-Per-Page': 1, - 'X-Total-Pages': 2, - 'X-Total': 2} - content = '[{"c": "d"}]' - return response(200, content, headers, None, 5, request) - - with HTTMock(resp_2, resp_1): - obj = self.gl.http_list('/tests', as_list=False, all=True) - self.assertEqual(len(obj), 2) - self.assertEqual(obj._next_url, - 'http://localhost/api/v4/tests?per_page=1&page=2') - self.assertEqual(obj.current_page, 1) - self.assertEqual(obj.prev_page, None) - self.assertEqual(obj.next_page, 2) - self.assertEqual(obj.per_page, 1) - self.assertEqual(obj.total_pages, 2) - self.assertEqual(obj.total, 2) - self.assertEqual(resp_1.call['count'], 1) - self.assertFalse(resp_2.call['called']) - self.assertDictEqual(next(obj), {"a": "b"}) - self.assertEqual(resp_1.call['count'], 1) - self.assertFalse(resp_2.call['called']) - self.assertDictEqual(next(obj), {"c": "d"}) - self.assertEqual(resp_1.call['count'], 1) - self.assertEqual(resp_2.call['count'], 1) - with self.assertRaises(StopIteration): - next(obj) + with HTTMock(resp_2): + l = list(obj) + self.assertEqual(len(l), 2) + self.assertEqual(l[0]['a'], 'b') + self.assertEqual(l[1]['c'], 'd') class TestGitlabHttpMethods(unittest.TestCase): |