1
+ # pylint: disable=redefined-outer-name
2
+ # pylint: disable=unused-argument
3
+ # pylint: disable=unused-variable
4
+
1
5
import re
2
6
from pathlib import Path
3
- from typing import Dict , List
7
+ from typing import Any , Dict , List
4
8
5
9
import pytest
6
- from aioresponses import aioresponses
10
+ from aioresponses import aioresponses as AioResponsesMock
7
11
from aioresponses .core import CallbackResult
8
12
from models_library .projects_state import RunningState
9
13
from yarl import URL
10
14
15
+ # WARNING: any request done through the client will go through aioresponses. It is
16
+ # unfortunate but that means any valid request (like calling the test server) prefix must be set as passthrough.
17
+ # Other than that it seems to behave nicely
18
+ PASSTHROUGH_REQUESTS_PREFIXES = ["http://127.0.0.1" , "ws://" ]
19
+
20
+
11
21
# The adjacency list is defined as a dictionary with the key to the node and its list of successors
12
22
FULL_PROJECT_PIPELINE_ADJACENCY : Dict [str , List [str ]] = {
13
23
"62bca361-8594-48c8-875e-b8577e868aec" : [
21
31
"e83a359a-1efe-41d3-83aa-a285afbfaf12" : [],
22
32
}
23
33
24
- FULL_PROJECT_NODE_STATES : Dict [str , Dict [str , str ]] = {
34
+ FULL_PROJECT_NODE_STATES : Dict [str , Dict [str , Any ]] = {
25
35
"62bca361-8594-48c8-875e-b8577e868aec" : {"modified" : True , "dependencies" : []},
26
36
"e0d7a1a5-0700-42c7-b033-97f72ac4a5cd" : {
27
37
"modified" : True ,
46
56
}
47
57
48
58
59
+ @pytest .fixture
60
+ def aioresponses_mocker () -> AioResponsesMock :
61
+ """Generick aioresponses mock
62
+
63
+ SEE https://github.com/pnuckowski/aioresponses
64
+
65
+ Usage
66
+
67
+ async def test_this(aioresponses_mocker):
68
+ aioresponses_mocker.get("https://foo.io")
69
+
70
+ async with aiohttp.ClientSession() as session:
71
+ async with session.get("https://foo.aio") as response:
72
+ assert response.status == 200
73
+ """
74
+ with AioResponsesMock (passthrough = PASSTHROUGH_REQUESTS_PREFIXES ) as mock :
75
+ yield mock
76
+
77
+
49
78
def creation_cb (url , ** kwargs ) -> CallbackResult :
50
79
51
80
assert "json" in kwargs , f"missing body in call to { url } "
@@ -113,14 +142,10 @@ def get_computation_cb(url, **kwargs) -> CallbackResult:
113
142
114
143
115
144
@pytest .fixture
116
- async def director_v2_service_mock () -> aioresponses :
117
-
118
- """uses aioresponses to mock all calls of an aiohttpclient
119
- WARNING: any request done through the client will go through aioresponses. It is
120
- unfortunate but that means any valid request (like calling the test server) prefix must be set as passthrough.
121
- Other than that it seems to behave nicely
122
- """
123
- PASSTHROUGH_REQUESTS_PREFIXES = ["http://127.0.0.1" , "ws://" ]
145
+ async def director_v2_service_mock (
146
+ aioresponses_mocker : AioResponsesMock ,
147
+ ) -> AioResponsesMock :
148
+ """mocks responses of director-v2"""
124
149
create_computation_pattern = re .compile (
125
150
r"^http://[a-z\-_]*director-v2:[0-9]+/v2/computations$"
126
151
)
@@ -132,37 +157,33 @@ async def director_v2_service_mock() -> aioresponses:
132
157
r"^http://[a-z\-_]*director-v2:[0-9]+/v2/computations/.*:stop$"
133
158
)
134
159
delete_computation_pattern = get_computation_pattern
135
- with aioresponses (passthrough = PASSTHROUGH_REQUESTS_PREFIXES ) as mock :
136
- mock .post (
137
- create_computation_pattern ,
138
- callback = creation_cb ,
139
- repeat = True ,
140
- )
141
- mock .post (
142
- stop_computation_pattern ,
143
- status = 204 ,
144
- repeat = True ,
145
- )
146
- mock .get (
147
- get_computation_pattern ,
148
- status = 202 ,
149
- callback = get_computation_cb ,
150
- repeat = True ,
151
- )
152
- mock .delete (delete_computation_pattern , status = 204 , repeat = True )
153
160
154
- yield mock
161
+ aioresponses_mocker .post (
162
+ create_computation_pattern ,
163
+ callback = creation_cb ,
164
+ repeat = True ,
165
+ )
166
+ aioresponses_mocker .post (
167
+ stop_computation_pattern ,
168
+ status = 204 ,
169
+ repeat = True ,
170
+ )
171
+ aioresponses_mocker .get (
172
+ get_computation_pattern ,
173
+ status = 202 ,
174
+ callback = get_computation_cb ,
175
+ repeat = True ,
176
+ )
177
+ aioresponses_mocker .delete (delete_computation_pattern , status = 204 , repeat = True )
155
178
179
+ return aioresponses_mocker
156
180
157
- @pytest .fixture
158
- async def storage_v0_service_mock () -> aioresponses :
159
181
160
- """uses aioresponses to mock all calls of an aiohttpclient
161
- WARNING: any request done through the client will go through aioresponses. It is
162
- unfortunate but that means any valid request (like calling the test server) prefix must be set as passthrough.
163
- Other than that it seems to behave nicely
164
- """
165
- PASSTHROUGH_REQUESTS_PREFIXES = ["http://127.0.0.1" , "ws://" ]
182
+ @pytest .fixture
183
+ async def storage_v0_service_mock (
184
+ aioresponses_mocker : AioResponsesMock ,
185
+ ) -> AioResponsesMock :
186
+ """mocks responses of storage API"""
166
187
167
188
def get_download_link_cb (url : URL , ** kwargs ) -> CallbackResult :
168
189
file_id = url .path .rsplit ("/files/" )[1 ]
@@ -179,11 +200,12 @@ def get_download_link_cb(url: URL, **kwargs) -> CallbackResult:
179
200
r"^http://[a-z\-_]*storage:[0-9]+/v0/locations.*$"
180
201
)
181
202
182
- with aioresponses (passthrough = PASSTHROUGH_REQUESTS_PREFIXES ) as mock :
183
- mock .get (get_download_link_pattern , callback = get_download_link_cb , repeat = True )
184
- mock .get (
185
- get_locations_link_pattern ,
186
- status = 200 ,
187
- payload = {"data" : [{"name" : "simcore.s3" , "id" : "0" }]},
188
- )
189
- yield mock
203
+ aioresponses_mocker .get (
204
+ get_download_link_pattern , callback = get_download_link_cb , repeat = True
205
+ )
206
+ aioresponses_mocker .get (
207
+ get_locations_link_pattern ,
208
+ status = 200 ,
209
+ payload = {"data" : [{"name" : "simcore.s3" , "id" : "0" }]},
210
+ )
211
+ return aioresponses_mocker
0 commit comments