Re-factor test cases to reduce code

This patch re-factors test_segment and test_host modules.
The common code is moved to a private method (_test_take_action)
and called from respective methods to reduce the code footprint.

Change-Id: I1577c66f8a59eb86fb488d8201a0b2da42be5d97
This commit is contained in:
nehaalhat 2017-08-21 18:39:05 +05:30
parent 32eb12e676
commit d4725667db
2 changed files with 61 additions and 109 deletions

View File

@ -112,11 +112,20 @@ class TestV1ShowHost(BaseV1Host):
self.show_host = ShowHost(self.app, self.app_args, self.show_host = ShowHost(self.app, self.app_args,
cmd_name='host show') cmd_name='host show')
@mock.patch.object(utils, 'get_dict_properties') def test_take_action_by_uuid(self):
def test_take_action_by_uuid(self, mock_get_dict_properties):
# command param # command param
parsed_args = FakeNamespace(segment_id=SEGMENT_ID, host=HOST_ID) parsed_args = FakeNamespace(segment_id=SEGMENT_ID, host=HOST_ID)
self._test_take_action(parsed_args)
def test_take_action_by_name(self):
# command param
parsed_args = FakeNamespace(segment_id=SEGMENT_ID, host=HOST_NAME)
self._test_take_action(parsed_args)
@mock.patch.object(utils, 'get_dict_properties')
def _test_take_action(self, parsed_args, mock_get_dict_properties):
# return value segment list # return value segment list
self.app.client_manager.ha.segments.return_value = self.dummy_segments self.app.client_manager.ha.segments.return_value = self.dummy_segments
# return value host list # return value host list
@ -129,22 +138,6 @@ class TestV1ShowHost(BaseV1Host):
mock_get_dict_properties.assert_called_once_with( mock_get_dict_properties.assert_called_once_with(
self.dummy_host.to_dict(), self.columns, formatters={}) self.dummy_host.to_dict(), self.columns, formatters={})
@mock.patch.object(utils, 'get_dict_properties')
def test_take_action_by_name(self, mock_get_dict_properties):
# command param
parsed_args = FakeNamespace(segment_id=SEGMENT_ID, host=HOST_NAME)
# return value segment list
self.app.client_manager.ha.segments.return_value = self.dummy_segments
# return value host list
self.app.client_manager.ha.hosts.return_value = self.dummy_hosts
# return value host show
self.app.client_manager.ha.get_host.return_value = self.dummy_host
# show the host specified by name
self.show_host.take_action(parsed_args)
mock_get_dict_properties.assert_called_once_with(
self.dummy_host.to_dict(), self.columns, formatters={})
class TestV1UpdateHost(BaseV1Host): class TestV1UpdateHost(BaseV1Host):
def setUp(self): def setUp(self):
@ -152,12 +145,22 @@ class TestV1UpdateHost(BaseV1Host):
self.update_host = UpdateHost(self.app, self.app_args, self.update_host = UpdateHost(self.app, self.app_args,
cmd_name='host update') cmd_name='host update')
@mock.patch.object(utils, 'get_dict_properties') def test_take_action_by_uuid(self):
def test_take_action_by_uuid(self, mock_get_dict_properties):
# command param # command param
parsed_args = FakeNamespace( parsed_args = FakeNamespace(
segment_id=SEGMENT_ID, host=HOST_ID, reserved=True) segment_id=SEGMENT_ID, host=HOST_ID, reserved=True)
self._test_take_action(parsed_args)
def test_take_action_by_name(self):
# command param
parsed_args = FakeNamespace(
segment_id=SEGMENT_ID, host=HOST_NAME, reserved=True)
self._test_take_action(parsed_args)
@mock.patch.object(utils, 'get_dict_properties')
def _test_take_action(self, parsed_args, mock_get_dict_properties):
# return value segment list # return value segment list
self.app.client_manager.ha.segments.return_value = self.dummy_segments self.app.client_manager.ha.segments.return_value = self.dummy_segments
# return value host list # return value host list
@ -169,23 +172,6 @@ class TestV1UpdateHost(BaseV1Host):
mock_get_dict_properties.assert_called_once_with( mock_get_dict_properties.assert_called_once_with(
self.dummy_host.to_dict(), self.columns, formatters={}) self.dummy_host.to_dict(), self.columns, formatters={})
@mock.patch.object(utils, 'get_dict_properties')
def test_take_action_by_name(self, mock_get_dict_properties):
# command param
parsed_args = FakeNamespace(
segment_id=SEGMENT_ID, host=HOST_NAME, reserved=True)
# return value segment list
self.app.client_manager.ha.segments.return_value = self.dummy_segments
# return value host list
self.app.client_manager.ha.hosts.return_value = self.dummy_hosts
# return value host show
self.app.client_manager.ha.get_host.return_value = self.dummy_host
# show the host specified by name
self.update_host.take_action(parsed_args)
mock_get_dict_properties.assert_called_once_with(
self.dummy_host.to_dict(), self.columns, formatters={})
class TestV1DeleteHost(BaseV1Host): class TestV1DeleteHost(BaseV1Host):
def setUp(self): def setUp(self):
@ -197,6 +183,15 @@ class TestV1DeleteHost(BaseV1Host):
# command param # command param
parsed_args = FakeNamespace(segment_id=SEGMENT_ID, host=HOST_ID) parsed_args = FakeNamespace(segment_id=SEGMENT_ID, host=HOST_ID)
self._test_take_action(parsed_args)
def test_take_action_by_name(self):
# command param
parsed_args = FakeNamespace(segment_id=SEGMENT_ID, host=HOST_NAME)
self._test_take_action(parsed_args)
def _test_take_action(self, parsed_args):
# return value segment list # return value segment list
self.app.client_manager.ha.segments.return_value = self.dummy_segments self.app.client_manager.ha.segments.return_value = self.dummy_segments
# return value host list # return value host list
@ -208,19 +203,3 @@ class TestV1DeleteHost(BaseV1Host):
self.app.client_manager.ha.delete_host.assert_called_once_with( self.app.client_manager.ha.delete_host.assert_called_once_with(
SEGMENT_ID, HOST_ID, False) SEGMENT_ID, HOST_ID, False)
def test_take_action_by_name(self):
# command param
parsed_args = FakeNamespace(segment_id=SEGMENT_ID, host=HOST_NAME)
# return value segment list
self.app.client_manager.ha.segments.return_value = self.dummy_segments
# return value host list
self.app.client_manager.ha.hosts.return_value = self.dummy_hosts
# return value host show
self.app.client_manager.ha.delete_host.return_value = None
# show the host specified by name
self.delete_host.take_action(parsed_args)
self.app.client_manager.ha.delete_host.assert_called_once_with(
SEGMENT_ID, HOST_ID, False)

View File

@ -102,39 +102,30 @@ class TestV1ShowSegment(BaseV1Segment):
self.dummy_segments = [FakeSegments(name=SEGMENT_NAME, self.dummy_segments = [FakeSegments(name=SEGMENT_NAME,
uuid=SEGMENT_ID)] uuid=SEGMENT_ID)]
@mock.patch.object(utils, 'get_dict_properties') def test_take_action_by_uuid(self):
def test_take_action_by_uuid(self, mock_get_dict_properties):
# command param # command param
parsed_args = FakeNamespace(segment=SEGMENT_ID) parsed_args = FakeNamespace(segment=SEGMENT_ID)
self._test_take_action(parsed_args)
def test_take_action_by_name(self):
# command param
parsed_args = FakeNamespace(segment=SEGMENT_NAME)
self._test_take_action(parsed_args)
@mock.patch.object(utils, 'get_dict_properties')
def _test_take_action(self, parsed_args, mock_get_dict_properties):
# return value segment list # return value segment list
self.app.client_manager.ha.segments.return_value =\ self.app.client_manager.ha.segments.return_value = self.dummy_segments
self.dummy_segments
# return value segment show # return value segment show
self.app.client_manager.ha.get_segment.return_value =\ self.app.client_manager.ha.get_segment.return_value = (
self.dummy_segment self.dummy_segment)
# show segment # show segment
self.show_seg.take_action(parsed_args) self.show_seg.take_action(parsed_args)
mock_get_dict_properties.assert_called_once_with( mock_get_dict_properties.assert_called_once_with(
self.dummy_segment.to_dict(), self.columns, formatters={}) self.dummy_segment.to_dict(), self.columns, formatters={})
@mock.patch.object(utils, 'get_dict_properties')
def test_take_action_by_name(self, mock_get_dict_properties):
# command param
parsed_args = FakeNamespace(segment=SEGMENT_NAME)
# return value segment list
self.app.client_manager.ha.segments.return_value =\
self.dummy_segments
# return value segment show
dummy_segment = FakeSegment()
self.app.client_manager.ha.get_segment.return_value =\
dummy_segment
# show segment
self.show_seg.take_action(parsed_args)
mock_get_dict_properties.assert_called_once_with(
dummy_segment.to_dict(), self.columns, formatters={})
class TestV1UpdateSegment(BaseV1Segment): class TestV1UpdateSegment(BaseV1Segment):
def setUp(self): def setUp(self):
@ -160,35 +151,27 @@ class TestV1UpdateSegment(BaseV1Segment):
recovery_method='Update_recovery_method', recovery_method='Update_recovery_method',
service_type='test_type')) service_type='test_type'))
@mock.patch.object(utils, 'get_dict_properties') def test_take_action_by_uuid(self):
def test_take_action_by_uuid(self, mock_get_dict_properties):
# command param # command param
parsed_args = FakeNamespace( parsed_args = FakeNamespace(
segment=SEGMENT_ID, segment=SEGMENT_ID,
description='FakeNamespace_description') description='FakeNamespace_description')
# return value segment list self._test_take_action(parsed_args)
self.app.client_manager.ha.segments.return_value =\
self.dummy_segments
# return value segment data setup
self.app.client_manager.ha.get_segment.return_value =\
self.dummy_segment
# segment update
self.update_seg.take_action(parsed_args)
mock_get_dict_properties.assert_called_once_with(
self.dummy_segment.to_dict(), self.columns, formatters={})
@mock.patch.object(utils, 'get_dict_properties') def test_take_action_by_name(self):
def test_take_action_by_name(self, mock_get_dict_properties):
# command param # command param
parsed_args = FakeNamespace(segment=SEGMENT_NAME) parsed_args = FakeNamespace(segment=SEGMENT_NAME)
self._test_take_action(parsed_args)
@mock.patch.object(utils, 'get_dict_properties')
def _test_take_action(self, parsed_args, mock_get_dict_properties):
# return value segment list # return value segment list
self.app.client_manager.ha.segments.return_value =\ self.app.client_manager.ha.segments.return_value = self.dummy_segments
self.dummy_segments
# return value segment data setup # return value segment data setup
self.app.client_manager.ha.get_segment.return_value =\ self.app.client_manager.ha.get_segment.return_value = (
self.dummy_segment self.dummy_segment)
# segment update # segment update
self.update_seg.take_action(parsed_args) self.update_seg.take_action(parsed_args)
mock_get_dict_properties.assert_called_once_with( mock_get_dict_properties.assert_called_once_with(
@ -214,28 +197,18 @@ class TestV1DeleteSegment(BaseV1Segment):
# command param # command param
parsed_args = FakeNamespace(segment=[SEGMENT_ID]) parsed_args = FakeNamespace(segment=[SEGMENT_ID])
self._test_take_action(parsed_args)
# return_value segment list
self.app.client_manager.ha.segments.return_value =\
self.dummy_segments
# return_value segment delete
self.app.client_manager.ha.delete_segment.return_value = None
# segment delete
self.delete_seg.take_action(parsed_args)
self.app.client_manager.ha.delete_segment.assert_called_once_with(
SEGMENT_ID, False)
def test_take_action_by_name(self): def test_take_action_by_name(self):
# command param # command param
parsed_args = FakeNamespace(segment=[SEGMENT_NAME]) parsed_args = FakeNamespace(segment=[SEGMENT_NAME])
self._test_take_action(parsed_args)
def _test_take_action(self, parsed_args):
# return_value segment list # return_value segment list
self.app.client_manager.ha.segments.return_value =\ self.app.client_manager.ha.segments.return_value = self.dummy_segments
self.dummy_segments
# return_value segment delete # return_value segment delete
self.app.client_manager.ha.delete_segment.return_value = None self.app.client_manager.ha.delete_segment.return_value = None