Merge "Unit test for asserting correct url in list_services"

This commit is contained in:
Jenkins 2017-06-07 14:38:01 +00:00 committed by Gerrit Code Review
commit 827493abe3
2 changed files with 41 additions and 4 deletions

View File

@ -31,12 +31,42 @@ class BaseServiceTest(base.TestCase):
def check_service_client_function(self, function, function2mock,
body, to_utf=False, status=200,
headers=None, **kwargs):
headers=None, mock_args=None,
**kwargs):
"""Mock a service client function for unit testing.
:param function: The service client function to call.
:param function2mock: The REST call to mock inside the service client
function.
:param body: Expected response body returned by the service client
function.
:param to_utf: Whether to use UTF-8 encoding for request.
:param status: Expected response status returned by the service client
function.
:param headers: Expected headers returned by the service client
function.
:param mock_args: List/dict/value of expected args/kwargs called by
function2mock. For example:
* If mock_args=['foo'] then ``assert_called_once_with('foo')``
is called.
* If mock_args={'foo': 'bar'} then
``assert_called_once_with(foo='bar')`` is called.
* If mock_args='foo' then ``assert_called_once_with('foo')``
is called.
:param kwargs: kwargs that are passed to function.
"""
mocked_response = self.create_response(body, to_utf, status, headers)
self.useFixture(fixtures.MockPatch(
fixture = self.useFixture(fixtures.MockPatch(
function2mock, return_value=mocked_response))
if kwargs:
resp = function(**kwargs)
else:
resp = function()
self.assertEqual(body, resp)
if isinstance(mock_args, list):
fixture.mock.assert_called_once_with(*mock_args)
elif isinstance(mock_args, dict):
fixture.mock.assert_called_once_with(**mock_args)
elif mock_args is not None:
fixture.mock.assert_called_once_with(mock_args)

View File

@ -101,12 +101,15 @@ class TestServicesClient(base.BaseServiceTest):
bytes_body,
service_id="686766")
def _test_list_services(self, bytes_body=False):
def _test_list_services(self, bytes_body=False, mock_args='services',
**params):
self.check_service_client_function(
self.client.list_services,
'tempest.lib.common.rest_client.RestClient.get',
self.FAKE_LIST_SERVICES,
bytes_body)
bytes_body,
mock_args=[mock_args],
**params)
def _test_update_service(self, bytes_body=False):
self.check_service_client_function(
@ -134,6 +137,10 @@ class TestServicesClient(base.BaseServiceTest):
def test_list_services_with_bytes_body(self):
self._test_list_services(bytes_body=True)
def test_list_services_with_params(self):
self._test_list_services(
type='fake-type', mock_args='services?type=fake-type')
def test_update_service_with_str_body(self):
self._test_update_service()