Unit tests: Allow multiple responses in mocked client
Sometimes function under test makes multiple calls to the backend, and being able to provide list of different responses would improve test coverage. Change-Id: I27a8cefe28287d25ed5411d44c6e2cc6f0a0701e
This commit is contained in:
parent
fc115e4605
commit
635439325f
@ -223,7 +223,12 @@ class NsxClientTestCase(NsxLibTestCase):
|
||||
|
||||
def __init__(self, session_response=None):
|
||||
super(NsxClientTestCase.MockHTTPProvider, self).__init__()
|
||||
self._session_response = session_response
|
||||
if isinstance(session_response, list):
|
||||
self._session_responses = session_response
|
||||
elif session_response:
|
||||
self._session_responses = [session_response]
|
||||
else:
|
||||
self._session_responses = None
|
||||
|
||||
def new_connection(self, cluster_api, provider):
|
||||
# wrapper the session so we can intercept and record calls
|
||||
@ -246,12 +251,15 @@ class NsxClientTestCase(NsxLibTestCase):
|
||||
|
||||
def _session_send(request, **kwargs):
|
||||
# calls at the Session level
|
||||
if self._session_response:
|
||||
if self._session_responses:
|
||||
# pop first response
|
||||
current_response = self._session_responses[0]
|
||||
del self._session_responses[0]
|
||||
# consumer has setup a response for the session
|
||||
cluster_api.record_call(request, **kwargs)
|
||||
return (self._session_response()
|
||||
if hasattr(self._session_response, '__call__')
|
||||
else self._session_response)
|
||||
return (current_response()
|
||||
if hasattr(current_response, '__call__')
|
||||
else current_response)
|
||||
|
||||
# bypass requests redirect handling for mock
|
||||
kwargs['allow_redirects'] = False
|
||||
|
@ -197,7 +197,7 @@ class TestSwitchingProfileTestCase(nsxlib_testcase.NsxClientTestCase):
|
||||
session_response = mocks.MockRequestsResponse(
|
||||
200, jsonutils.dumps(resp_resources))
|
||||
mocked_resource = self._mocked_switching_profile(
|
||||
session_response=session_response)
|
||||
session_response=[session_response] * 3)
|
||||
|
||||
self.assertEqual([{'display_name': 'resource-1'}],
|
||||
mocked_resource.find_by_display_name('resource-1'))
|
||||
|
Loading…
x
Reference in New Issue
Block a user