Skip to content

Commit 64890cc

Browse files
committed
test: drop httmock dependency in test_gitlab.py
1 parent d65ce36 commit 64890cc

File tree

1 file changed

+62
-45
lines changed

1 file changed

+62
-45
lines changed

tests/unit/test_gitlab.py

+62-45
Original file line numberDiff line numberDiff line change
@@ -20,61 +20,74 @@
2020
import warnings
2121

2222
import pytest
23-
from httmock import HTTMock, response, urlmatch, with_httmock # noqa
23+
import responses
2424

2525
import gitlab
2626

2727
localhost = "http://localhost"
28-
username = "username"
29-
user_id = 1
3028
token = "abc123"
3129

3230

33-
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/user", method="get")
34-
def resp_get_user(url, request):
35-
headers = {"content-type": "application/json"}
36-
content = f'{{"id": {user_id:d}, "username": "{username:s}"}}'.encode("utf-8")
37-
return response(200, content, headers, None, 5, request)
31+
@pytest.fixture
32+
def resp_get_user():
33+
return {
34+
"method": responses.GET,
35+
"url": "http://localhost/api/v4/user",
36+
"json": {"id": 1, "username": "username"},
37+
"content_type": "application/json",
38+
"status": 200,
39+
}
3840

3941

40-
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/tests", method="get")
41-
def resp_page_1(url, request):
42+
@pytest.fixture
43+
def resp_page_1():
4244
headers = {
43-
"content-type": "application/json",
44-
"X-Page": 1,
45-
"X-Next-Page": 2,
46-
"X-Per-Page": 1,
47-
"X-Total-Pages": 2,
48-
"X-Total": 2,
45+
"X-Page": "1",
46+
"X-Next-Page": "2",
47+
"X-Per-Page": "1",
48+
"X-Total-Pages": "2",
49+
"X-Total": "2",
4950
"Link": ("<http://localhost/api/v4/tests?per_page=1&page=2>;" ' rel="next"'),
5051
}
51-
content = '[{"a": "b"}]'
52-
return response(200, content, headers, None, 5, request)
5352

53+
return {
54+
"method": responses.GET,
55+
"url": "http://localhost/api/v4/tests",
56+
"json": [{"a": "b"}],
57+
"headers": headers,
58+
"content_type": "application/json",
59+
"status": 200,
60+
"match_querystring": True,
61+
}
5462

55-
@urlmatch(
56-
scheme="http",
57-
netloc="localhost",
58-
path="/api/v4/tests",
59-
method="get",
60-
query=r".*page=2",
61-
)
62-
def resp_page_2(url, request):
63+
64+
@pytest.fixture
65+
def resp_page_2():
6366
headers = {
64-
"content-type": "application/json",
65-
"X-Page": 2,
66-
"X-Next-Page": 2,
67-
"X-Per-Page": 1,
68-
"X-Total-Pages": 2,
69-
"X-Total": 2,
67+
"X-Page": "2",
68+
"X-Next-Page": "2",
69+
"X-Per-Page": "1",
70+
"X-Total-Pages": "2",
71+
"X-Total": "2",
72+
}
73+
params = {"per_page": "1", "page": "2"}
74+
75+
return {
76+
"method": responses.GET,
77+
"url": "http://localhost/api/v4/tests",
78+
"json": [{"c": "d"}],
79+
"headers": headers,
80+
"content_type": "application/json",
81+
"status": 200,
82+
"match": [responses.matchers.query_param_matcher(params)],
83+
"match_querystring": False,
7084
}
71-
content = '[{"c": "d"}]'
72-
return response(200, content, headers, None, 5, request)
7385

7486

75-
def test_gitlab_build_list(gl):
76-
with HTTMock(resp_page_1):
77-
obj = gl.http_list("/tests", as_list=False)
87+
@responses.activate
88+
def test_gitlab_build_list(gl, resp_page_1, resp_page_2):
89+
responses.add(**resp_page_1)
90+
obj = gl.http_list("/tests", as_list=False)
7891
assert len(obj) == 2
7992
assert obj._next_url == "http://localhost/api/v4/tests?per_page=1&page=2"
8093
assert obj.current_page == 1
@@ -84,15 +97,18 @@ def test_gitlab_build_list(gl):
8497
assert obj.total_pages == 2
8598
assert obj.total == 2
8699

87-
with HTTMock(resp_page_2):
88-
test_list = list(obj)
100+
responses.add(**resp_page_2)
101+
gl.enable_debug()
102+
test_list = list(obj)
89103
assert len(test_list) == 2
90104
assert test_list[0]["a"] == "b"
91105
assert test_list[1]["c"] == "d"
92106

93107

94-
@with_httmock(resp_page_1, resp_page_2)
95-
def test_gitlab_all_omitted_when_as_list(gl):
108+
@responses.activate
109+
def test_gitlab_all_omitted_when_as_list(gl, resp_page_1, resp_page_2):
110+
responses.add(**resp_page_1)
111+
responses.add(**resp_page_2)
96112
result = gl.http_list("/tests", as_list=False, all=True)
97113
assert isinstance(result, gitlab.GitlabList)
98114

@@ -119,11 +135,12 @@ def test_gitlab_pickability(gl):
119135
assert unpickled._objects == original_gl_objects
120136

121137

122-
@with_httmock(resp_get_user)
123-
def test_gitlab_token_auth(gl, callback=None):
138+
@responses.activate
139+
def test_gitlab_token_auth(gl, resp_get_user):
140+
responses.add(**resp_get_user)
124141
gl.auth()
125-
assert gl.user.username == username
126-
assert gl.user.id == user_id
142+
assert gl.user.username == "username"
143+
assert gl.user.id == 1
127144
assert isinstance(gl.user, gitlab.v4.objects.CurrentUser)
128145

129146

0 commit comments

Comments
 (0)