Skip to content

Commit e92c6ea

Browse files
authored
Merge pull request #55 from cloudblue/fix_client_mockers
Fix client mockers when multiple mockers are used
2 parents 3a92714 + e7dbade commit e92c6ea

5 files changed

Lines changed: 253 additions & 207 deletions

File tree

connect/client/testing/fluent.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ def match(request):
3535
return match
3636

3737

38+
_mocker = responses.RequestsMock()
39+
40+
3841
class ConnectClientMocker(_ConnectClientBase):
3942
def __init__(self, base_url):
4043
super().__init__('api_key', endpoint=base_url)
41-
self._mocker = responses.RequestsMock()
4244

4345
def get(
4446
self,
@@ -142,14 +144,14 @@ def mock(
142144
body_matcher(match_body),
143145
]
144146

145-
self._mocker.add(**kwargs)
147+
_mocker.add(**kwargs)
146148

147149
def start(self):
148-
self._mocker.start()
150+
_mocker.start()
149151

150152
def reset(self, success=True):
151-
self._mocker.stop(allow_assert=success)
152-
self._mocker.reset()
153+
_mocker.stop(allow_assert=success)
154+
_mocker.reset()
153155

154156
def __enter__(self):
155157
self.start()
@@ -165,29 +167,24 @@ def _get_namespace_class(self):
165167
return NSMock
166168

167169

170+
_monkeypatch = MonkeyPatch()
171+
_async_mocker = HTTPXMock()
172+
173+
168174
class AsyncConnectClientMocker(ConnectClientMocker):
169175
def __init__(self, base_url):
170176
super().__init__(base_url)
171-
self._monkeypatch = None
172-
self._mocker = None
173177

174178
def start(self):
175-
self._monkeypatch = MonkeyPatch()
176-
self._mocker = HTTPXMock()
177-
178-
mocker = self._mocker
179-
180-
self._monkeypatch.setattr(
179+
_monkeypatch.setattr(
181180
httpx.AsyncClient,
182181
'_transport_for_url',
183-
lambda self, url: _PytestAsyncTransport(mocker),
182+
lambda self, url: _PytestAsyncTransport(_async_mocker),
184183
)
185184

186185
def reset(self, success=True):
187-
self._mocker.reset(success)
188-
self._monkeypatch.undo()
189-
self._mocker = None
190-
self._monkeypatch = None
186+
_async_mocker.reset(success)
187+
_monkeypatch.undo()
191188

192189
def mock(
193190
self,
@@ -218,4 +215,4 @@ def mock(
218215
else:
219216
kwargs['match_content'] = match_body
220217

221-
self._mocker.add_response(**kwargs)
218+
_async_mocker.add_response(**kwargs)

0 commit comments

Comments
 (0)