Skip to content

Commit 090af39

Browse files
committed
test: create fixtures for async and sync gitlab clients usage
1 parent 3554a04 commit 090af39

File tree

2 files changed

+62
-12
lines changed

2 files changed

+62
-12
lines changed

gitlab/tests/conftest.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import asyncio
2+
3+
import pytest
4+
5+
from gitlab import AsyncGitlab, Gitlab
6+
7+
gitlab_kwargs = {
8+
"url": "http://localhost",
9+
"private_token": "private_token",
10+
"api_version": 4,
11+
}
12+
13+
14+
@pytest.fixture(
15+
params=[Gitlab(**gitlab_kwargs), AsyncGitlab(**gitlab_kwargs)],
16+
ids=["sync", "async"],
17+
)
18+
def gl(request):
19+
return request.param
20+
21+
22+
async def awaiter(v):
23+
if asyncio.iscoroutine(v):
24+
return await v
25+
else:
26+
return v
27+
28+
29+
async def returner(v):
30+
return v
31+
32+
33+
@pytest.fixture
34+
def gl_get_value(gl):
35+
"""Fixture that returns async function that either return input value or awaits it
36+
37+
Usage::
38+
39+
result = gl.http_get()
40+
result = await gl_get_value(result)
41+
"""
42+
if isinstance(gl, Gitlab):
43+
return returner
44+
else:
45+
return awaiter
46+
47+
48+
@pytest.fixture
49+
def is_gl_sync(gl):
50+
"""If gitlab client sync or not
51+
"""
52+
if isinstance(gl, Gitlab):
53+
return True
54+
else:
55+
return False

gitlab/tests/test_gitlab.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,10 @@ def test_http_auth(self, gitlab_class):
156156
assert "Authorization" not in gl.headers
157157

158158

159-
@pytest.mark.parametrize(
160-
"gl, sync", [(sync_gitlab, True), (async_gitlab, False),],
161-
)
162159
class TestGitlabList:
163160
@respx.mock
164161
@pytest.mark.asyncio
165-
async def test_build_list(self, gl, sync):
162+
async def test_build_list(self, gl, gl_get_value, is_gl_sync):
166163
request_1 = respx.get(
167164
"http://localhost/api/v4/tests",
168165
headers={
@@ -193,8 +190,7 @@ async def test_build_list(self, gl, sync):
193190
status_code=StatusCode.OK,
194191
)
195192
obj = gl.http_list("/tests", as_list=False)
196-
if not sync:
197-
obj = await obj
193+
obj = await gl_get_value(obj)
198194

199195
assert len(obj) == 2
200196
assert obj._next_url == "http://localhost/api/v4/tests?per_page=1&page=2"
@@ -205,18 +201,18 @@ async def test_build_list(self, gl, sync):
205201
assert obj.total_pages == 2
206202
assert obj.total == 2
207203

208-
if not sync:
209-
l = await obj.as_list()
210-
else:
204+
if is_gl_sync:
211205
l = list(obj)
206+
else:
207+
l = await obj.as_list()
212208

213209
assert len(l) == 2
214210
assert l[0]["a"] == "b"
215211
assert l[1]["c"] == "d"
216212

217213
@respx.mock
218214
@pytest.mark.asyncio
219-
async def test_all_ommited_when_as_list(self, gl, sync):
215+
async def test_all_ommited_when_as_list(self, gl, gl_get_value):
220216
request = respx.get(
221217
"http://localhost/api/v4/tests",
222218
headers={
@@ -232,8 +228,7 @@ async def test_all_ommited_when_as_list(self, gl, sync):
232228
)
233229

234230
result = gl.http_list("/tests", as_list=False, all=True)
235-
if not sync:
236-
result = await result
231+
result = await gl_get_value(result)
237232

238233
assert isinstance(result, GitlabList)
239234

0 commit comments

Comments
 (0)