Skip to content

Commit fb90a89

Browse files
committed
test: create TestGitlabList for async way
1 parent 791de22 commit fb90a89

File tree

3 files changed

+96
-6
lines changed

3 files changed

+96
-6
lines changed

gitlab/__init__.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
import importlib
2323
import warnings
2424

25-
import gitlab.config
2625
import httpx
26+
27+
import gitlab.config
2728
from gitlab import utils # noqa
2829
from gitlab.const import * # noqa
2930
from gitlab.exceptions import * # noqa
@@ -622,13 +623,15 @@ async def http_list(self, path, query_data=None, as_list=None, **kwargs):
622623
url = self._build_url(path)
623624

624625
if get_all is True and as_list is True:
625-
return list(await GitlabList.create(self, url, query_data, **kwargs))
626+
gitlab_list = await GitlabList.create(self, url, query_data, **kwargs)
627+
return await gitlab_list.as_list()
626628

627629
if "page" in kwargs or as_list is True:
628630
# pagination requested, we return a list
629-
return list(
630-
await GitlabList.create(self, url, query_data, get_next=False, **kwargs)
631+
gitlab_list = await GitlabList.create(
632+
self, url, query_data, get_next=False, **kwargs
631633
)
634+
return await gitlab_list.as_list()
632635

633636
# No pagination, generator requested
634637
return await GitlabList.create(self, url, query_data, **kwargs)
@@ -825,10 +828,13 @@ def total(self):
825828
"""The total number of items."""
826829
return int(self._total)
827830

828-
async def __aiter__(self):
829-
return await self
831+
def __aiter__(self):
832+
return self
830833

831834
async def __anext__(self):
835+
return await self.next()
836+
837+
async def next(self):
832838
try:
833839
item = self._data[self._current]
834840
self._current += 1
@@ -844,3 +850,7 @@ async def __anext__(self):
844850

845851
def __len__(self):
846852
return int(self._total)
853+
854+
async def as_list(self):
855+
# since list() does not support async way
856+
return [o async for o in self]

gitlab/tests/test_async_gitlab.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import pytest
2+
import respx
3+
from httpx import status_codes
4+
from httpx.status_codes import StatusCode
5+
6+
from gitlab import Gitlab, GitlabList
7+
8+
9+
class TestGitlabList:
10+
@pytest.fixture
11+
def gl(self):
12+
return Gitlab("http://localhost", private_token="private_token", api_version=4)
13+
14+
@respx.mock
15+
@pytest.mark.asyncio
16+
async def test_build_list(self, gl):
17+
request_1 = respx.get(
18+
"http://localhost/api/v4/tests",
19+
headers={
20+
"content-type": "application/json",
21+
"X-Page": "1",
22+
"X-Next-Page": "2",
23+
"X-Per-Page": "1",
24+
"X-Total-Pages": "2",
25+
"X-Total": "2",
26+
"Link": (
27+
"<http://localhost/api/v4/tests?per_page=1&page=2>;" ' rel="next"'
28+
),
29+
},
30+
content=[{"a": "b"}],
31+
status_code=StatusCode.OK,
32+
)
33+
request_2 = respx.get(
34+
"http://localhost/api/v4/tests?per_page=1&page=2",
35+
headers={
36+
"content-type": "application/json",
37+
"X-Page": "2",
38+
"X-Next-Page": "2",
39+
"X-Per-Page": "1",
40+
"X-Total-Pages": "2",
41+
"X-Total": "2",
42+
},
43+
content=[{"c": "d"}],
44+
status_code=StatusCode.OK,
45+
)
46+
47+
obj = await gl.http_list("/tests", as_list=False)
48+
assert len(obj) == 2
49+
assert obj._next_url == "http://localhost/api/v4/tests?per_page=1&page=2"
50+
assert obj.current_page == 1
51+
assert obj.prev_page == None
52+
assert obj.next_page == 2
53+
assert obj.per_page == 1
54+
assert obj.total_pages == 2
55+
assert obj.total == 2
56+
57+
l = await obj.as_list()
58+
assert len(l) == 2
59+
assert l[0]["a"] == "b"
60+
assert l[1]["c"] == "d"
61+
62+
@respx.mock
63+
@pytest.mark.asyncio
64+
async def test_all_ommited_when_as_list(self, gl):
65+
request = respx.get(
66+
"http://localhost/api/v4/tests",
67+
headers={
68+
"content-type": "application/json",
69+
"X-Page": "2",
70+
"X-Next-Page": "2",
71+
"X-Per-Page": "1",
72+
"X-Total-Pages": "2",
73+
"X-Total": "2",
74+
},
75+
content=[{"c": "d"}],
76+
status_code=StatusCode.OK,
77+
)
78+
result = await gl.http_list("/tests", as_list=False, all=True)
79+
assert isinstance(result, GitlabList)

test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ mock
88
sphinx>=1.3
99
sphinx_rtd_theme
1010
requests>=2.22.0
11+
respx>=0.10.0,<0.11

0 commit comments

Comments
 (0)