Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc764bee

Browse files
nejchJohnVillalovos
authored andcommitted
test: drop httmock dependency in test_gitlab.py
1 parentf26bf7d commitc764bee

File tree

1 file changed

+61
-45
lines changed

1 file changed

+61
-45
lines changed

‎tests/unit/test_gitlab.py‎

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,61 +20,74 @@
2020
importwarnings
2121

2222
importpytest
23-
fromhttmockimportHTTMock,response,urlmatch,with_httmock# noqa
23+
importresponses
2424

2525
importgitlab
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-
defresp_get_user(url,request):
35-
headers= {"content-type":"application/json"}
36-
content=f'{{"id":{user_id:d}, "username": "{username:s}"}}'.encode("utf-8")
37-
returnresponse(200,content,headers,None,5,request)
31+
@pytest.fixture
32+
defresp_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-
defresp_page_1(url,request):
42+
@pytest.fixture
43+
defresp_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-
returnresponse(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-
defresp_page_2(url,request):
63+
64+
@pytest.fixture
65+
defresp_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-
returnresponse(200,content,headers,None,5,request)
7385

7486

75-
deftest_gitlab_build_list(gl):
76-
withHTTMock(resp_page_1):
77-
obj=gl.http_list("/tests",as_list=False)
87+
@responses.activate
88+
deftest_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
assertlen(obj)==2
7992
assertobj._next_url=="http://localhost/api/v4/tests?per_page=1&page=2"
8093
assertobj.current_page==1
@@ -84,15 +97,17 @@ def test_gitlab_build_list(gl):
8497
assertobj.total_pages==2
8598
assertobj.total==2
8699

87-
withHTTMock(resp_page_2):
88-
test_list=list(obj)
100+
responses.add(**resp_page_2)
101+
test_list=list(obj)
89102
assertlen(test_list)==2
90103
asserttest_list[0]["a"]=="b"
91104
asserttest_list[1]["c"]=="d"
92105

93106

94-
@with_httmock(resp_page_1,resp_page_2)
95-
deftest_gitlab_all_omitted_when_as_list(gl):
107+
@responses.activate
108+
deftest_gitlab_all_omitted_when_as_list(gl,resp_page_1,resp_page_2):
109+
responses.add(**resp_page_1)
110+
responses.add(**resp_page_2)
96111
result=gl.http_list("/tests",as_list=False,all=True)
97112
assertisinstance(result,gitlab.GitlabList)
98113

@@ -119,11 +134,12 @@ def test_gitlab_pickability(gl):
119134
assertunpickled._objects==original_gl_objects
120135

121136

122-
@with_httmock(resp_get_user)
123-
deftest_gitlab_token_auth(gl,callback=None):
137+
@responses.activate
138+
deftest_gitlab_token_auth(gl,resp_get_user):
139+
responses.add(**resp_get_user)
124140
gl.auth()
125-
assertgl.user.username==username
126-
assertgl.user.id==user_id
141+
assertgl.user.username=="username"
142+
assertgl.user.id==1
127143
assertisinstance(gl.user,gitlab.v4.objects.CurrentUser)
128144

129145

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp