Skip to content

Commit d9d9af8

Browse files
committed
test: refactor TestGitlabHttpMethods to async way
1 parent 3b90d09 commit d9d9af8

File tree

3 files changed

+167
-223
lines changed

3 files changed

+167
-223
lines changed

gitlab/tests/test_async_gitlab.py

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import httpx
12
import pytest
23
import respx
34
from httpx.status_codes import StatusCode
45

56
from gitlab import Gitlab, GitlabList
7+
from gitlab import exceptions as exc
68

79

810
class TestGitlabList:
@@ -74,5 +76,169 @@ async def test_all_ommited_when_as_list(self, gl):
7476
content=[{"c": "d"}],
7577
status_code=StatusCode.OK,
7678
)
79+
7780
result = await gl.http_list("/tests", as_list=False, all=True)
7881
assert isinstance(result, GitlabList)
82+
83+
84+
class TestGitlabHttpMethods:
85+
@pytest.fixture
86+
def gl(self):
87+
return Gitlab("http://localhost", private_token="private_token", api_version=4)
88+
89+
@respx.mock
90+
@pytest.mark.asyncio
91+
async def test_http_request(self, gl):
92+
request = respx.get(
93+
"http://localhost/api/v4/projects",
94+
headers={"content-type": "application/json"},
95+
content=[{"name": "project1"}],
96+
status_code=StatusCode.OK,
97+
)
98+
99+
http_r = await gl.http_request("get", "/projects")
100+
http_r.json()
101+
assert http_r.status_code == StatusCode.OK
102+
103+
@respx.mock
104+
@pytest.mark.asyncio
105+
async def test_get_request(self, gl):
106+
request = respx.get(
107+
"http://localhost/api/v4/projects",
108+
headers={"content-type": "application/json"},
109+
content={"name": "project1"},
110+
status_code=StatusCode.OK,
111+
)
112+
113+
result = await gl.http_get("/projects")
114+
assert isinstance(result, dict)
115+
assert result["name"] == "project1"
116+
117+
@respx.mock
118+
@pytest.mark.asyncio
119+
async def test_get_request_raw(self, gl):
120+
request = respx.get(
121+
"http://localhost/api/v4/projects",
122+
headers={"content-type": "application/octet-stream"},
123+
content="content",
124+
status_code=StatusCode.OK,
125+
)
126+
127+
result = await gl.http_get("/projects")
128+
assert result.content.decode("utf-8") == "content"
129+
130+
@respx.mock
131+
@pytest.mark.asyncio
132+
@pytest.mark.parametrize(
133+
"respx_params, gl_exc, path",
134+
[
135+
(
136+
{
137+
"url": "http://localhost/api/v4/not_there",
138+
"content": "Here is why it failed",
139+
"status_code": StatusCode.NOT_FOUND,
140+
},
141+
exc.GitlabHttpError,
142+
"/not_there",
143+
),
144+
(
145+
{
146+
"url": "http://localhost/api/v4/projects",
147+
"headers": {"content-type": "application/json"},
148+
"content": '["name": "project1"]',
149+
"status_code": StatusCode.OK,
150+
},
151+
exc.GitlabParsingError,
152+
"/projects",
153+
),
154+
],
155+
)
156+
@pytest.mark.parametrize(
157+
"http_method, gl_method",
158+
[
159+
("get", "http_get"),
160+
("get", "http_list"),
161+
("post", "http_post"),
162+
("put", "http_put"),
163+
],
164+
)
165+
async def test_errors(self, gl, http_method, gl_method, respx_params, gl_exc, path):
166+
request = getattr(respx, http_method)(**respx_params)
167+
168+
with pytest.raises(gl_exc):
169+
http_r = await getattr(gl, gl_method)(path)
170+
171+
@respx.mock
172+
@pytest.mark.asyncio
173+
async def test_list_request(self, gl):
174+
request = respx.get(
175+
"http://localhost/api/v4/projects",
176+
headers={"content-type": "application/json", "X-Total": "1"},
177+
content=[{"name": "project1"}],
178+
status_code=StatusCode.OK,
179+
)
180+
181+
result = await gl.http_list("/projects", as_list=True)
182+
assert isinstance(result, list)
183+
assert len(result) == 1
184+
185+
result = await gl.http_list("/projects", as_list=False)
186+
assert isinstance(result, GitlabList)
187+
assert len(result) == 1
188+
189+
result = await gl.http_list("/projects", all=True)
190+
assert isinstance(result, list)
191+
assert len(result) == 1
192+
193+
@respx.mock
194+
@pytest.mark.asyncio
195+
async def test_post_request(self, gl):
196+
request = respx.post(
197+
"http://localhost/api/v4/projects",
198+
headers={"content-type": "application/json"},
199+
content={"name": "project1"},
200+
status_code=StatusCode.OK,
201+
)
202+
203+
result = await gl.http_post("/projects")
204+
assert isinstance(result, dict)
205+
assert result["name"] == "project1"
206+
207+
@respx.mock
208+
@pytest.mark.asyncio
209+
async def test_put_request(self, gl):
210+
request = respx.put(
211+
"http://localhost/api/v4/projects",
212+
headers={"content-type": "application/json"},
213+
content='{"name": "project1"}',
214+
status_code=StatusCode.OK,
215+
)
216+
result = await gl.http_put("/projects")
217+
assert isinstance(result, dict)
218+
assert result["name"] == "project1"
219+
220+
@respx.mock
221+
@pytest.mark.asyncio
222+
async def test_delete_request(self, gl):
223+
request = respx.delete(
224+
"http://localhost/api/v4/projects",
225+
headers={"content-type": "application/json"},
226+
content="true",
227+
status_code=StatusCode.OK,
228+
)
229+
230+
result = await gl.http_delete("/projects")
231+
assert isinstance(result, httpx.Response)
232+
assert result.json() is True
233+
234+
@respx.mock
235+
@pytest.mark.asyncio
236+
async def test_delete_request_404(self, gl):
237+
result = respx.delete(
238+
"http://localhost/api/v4/not_there",
239+
content="Here is why it failed",
240+
status_code=StatusCode.NOT_FOUND,
241+
)
242+
243+
with pytest.raises(exc.GitlabHttpError):
244+
await gl.http_delete("/not_there")

0 commit comments

Comments
 (0)