Update cells commands to use pluggable formatters

This now allows users of the cell-* command-line entry points to use the
formatter provided by --format.

Change-Id: I1e2356f4d38051c97af68e48f12d0ec95ac6167a
This commit is contained in:
Ian Cordasco 2017-02-28 11:24:25 -06:00
parent 0ec761cb96
commit ab49589345
4 changed files with 57 additions and 53 deletions

View File

@ -26,8 +26,7 @@ from cratonclient.v1 import cells
def do_cell_show(cc, args):
"""Show detailed information about a cell."""
cell = cc.cells.get(args.id)
data = {f: getattr(cell, f, '') for f in cells.CELL_FIELDS}
cliutils.print_dict(data, wrap=72)
args.formatter.configure(wrap=72).handle(cell)
@cliutils.arg('-r', '--region',
@ -117,7 +116,7 @@ def do_cell_list(cc, args):
params['autopaginate'] = args.all
listed_cells = cc.cells.list(**params)
cliutils.print_list(listed_cells, list(fields))
args.formatter.configure(fields=list(fields)).handle(listed_cells)
@cliutils.arg('-n', '--name',
@ -143,8 +142,7 @@ def do_cell_create(cc, args):
fields = {k: v for (k, v) in vars(args).items()
if k in cells.CELL_FIELDS and not (v is None)}
cell = cc.cells.create(**fields)
data = {f: getattr(cell, f, '') for f in cells.CELL_FIELDS}
cliutils.print_dict(data, wrap=72)
args.formatter.configure(wrap=72).handle(cell)
@cliutils.arg('id',
@ -177,8 +175,7 @@ def do_cell_update(cc, args):
'--cloud, or --note'
)
cell = cc.cells.update(cell_id, **fields)
data = {f: getattr(cell, f, '') for f in cells.CELL_FIELDS}
cliutils.print_dict(data, wrap=72)
args.formatter.configure(wrap=72).handle(cell)
@cliutils.arg('id',

View File

@ -29,18 +29,26 @@ class TestCellsShell(base.ShellTestCase):
re_options = re.DOTALL | re.MULTILINE
cell_valid_fields = None
cell_invalid_field = None
cell_invalid_fields = None
def setUp(self):
"""Setup required test fixtures."""
super(TestCellsShell, self).setUp()
self.cell_valid_fields = Namespace(project_id=1,
region_id=1,
name='mock_cell')
self.cell_invalid_field = Namespace(project_id=1,
region_id=1,
name='mock_cell',
invalid_foo='ignored')
self.cell_valid_kwargs = {
'project_id': 1,
'region_id': 1,
'name': 'mock_cell',
}
self.cell_valid_fields = Namespace(**self.cell_valid_kwargs)
self.cell_valid_fields.formatter = mock.Mock()
self.cell_invalid_kwargs = {
'project_id': 1,
'region_id': 1,
'name': 'mock_cell',
'invalid_foo': 'ignored',
}
self.cell_invalid_fields = Namespace(**self.cell_invalid_kwargs)
self.cell_invalid_fields.formatter = mock.Mock()
@mock.patch('cratonclient.v1.cells.CellManager.list')
def test_cell_list_success(self, mock_list):
@ -103,8 +111,7 @@ class TestCellsShell(base.ShellTestCase):
)
@mock.patch('cratonclient.v1.cells.CellManager.list')
@mock.patch('cratonclient.common.cliutils.print_list')
def test_cell_list_fields_success(self, mock_printlist, mock_list):
def test_cell_list_fields_success(self, mock_list):
"""Verify --fields argument successfully passed to Client."""
self.shell('cell-list -r 1 --fields id name')
mock_list.assert_called_once_with(
@ -113,9 +120,6 @@ class TestCellsShell(base.ShellTestCase):
autopaginate=False,
marker=None,
)
mock_printlist.assert_called_once_with(mock.ANY,
list({'id': 'ID',
'name': 'Name'}))
@mock.patch('cratonclient.v1.cells.CellManager.list')
def test_cell_list_sort_key_field_key_success(self, mock_list):
@ -187,7 +191,7 @@ class TestCellsShell(base.ShellTestCase):
region_id=mock.ANY,
)
cells_shell.do_cell_create(client, self.cell_valid_fields)
mock_create.assert_called_once_with(**vars(self.cell_valid_fields))
mock_create.assert_called_once_with(**self.cell_valid_kwargs)
@mock.patch('cratonclient.v1.cells.CellManager.create')
def test_do_cell_create_ignores_unknown_fields(self, mock_create):
@ -197,8 +201,8 @@ class TestCellsShell(base.ShellTestCase):
mock.ANY, 'http://127.0.0.1/',
region_id=mock.ANY,
)
cells_shell.do_cell_create(client, self.cell_invalid_field)
mock_create.assert_called_once_with(**vars(self.cell_valid_fields))
cells_shell.do_cell_create(client, self.cell_invalid_fields)
mock_create.assert_called_once_with(**self.cell_valid_kwargs)
def test_cell_update_missing_required_args(self):
"""Verify that missing required args results in error message."""
@ -222,7 +226,8 @@ class TestCellsShell(base.ShellTestCase):
)
valid_input = Namespace(region=1,
id=1,
name='mock_cell')
name='mock_cell',
formatter=mock.Mock())
cells_shell.do_cell_update(client, valid_input)
mock_update.assert_called_once_with(1, name='mock_cell')
@ -237,7 +242,8 @@ class TestCellsShell(base.ShellTestCase):
invalid_input = Namespace(region=1,
id=1,
name='mock_cell',
invalid=True)
invalid=True,
formatter=mock.Mock())
cells_shell.do_cell_update(client, invalid_input)
mock_update.assert_called_once_with(1, name='mock_cell')
@ -261,7 +267,7 @@ class TestCellsShell(base.ShellTestCase):
mock.ANY, 'http://127.0.0.1/',
region_id=mock.ANY,
)
test_args = Namespace(id=1)
test_args = Namespace(id=1, formatter=mock.Mock())
cells_shell.do_cell_show(client, test_args)
mock_get.assert_called_once_with(vars(test_args)['id'])

View File

@ -93,3 +93,9 @@ class TestShellCommandUsingPrintList(TestShellCommand):
kwargs = self.formatter.configure.call_args[1]
self.assertListEqual(expected_fields,
sorted(kwargs['fields']))
def assertSortedFieldsEqualTo(self, expected_fields):
"""Assert the sorted fields parameter is equal expected_fields."""
kwargs = self.formatter.configure.call_args[1]
self.assertListEqual(expected_fields,
sorted(kwargs['fields']))

View File

@ -33,10 +33,8 @@ class TestDoShellShow(base.TestShellCommandUsingPrintDict):
cells_shell.do_cell_show(self.craton_client, args)
self.craton_client.cells.get.assert_called_once_with(456)
self.print_dict.assert_called_once_with(
{f: mock.ANY for f in cells.CELL_FIELDS},
wrap=72,
)
self.formatter.configure.assert_called_once_with(wrap=72)
self.assertEqual(1, self.formatter.handle.call_count)
class TestDoCellList(base.TestShellCommandUsingPrintList):
@ -45,7 +43,8 @@ class TestDoCellList(base.TestShellCommandUsingPrintList):
def assertNothingWasCalled(self):
"""Assert inventory, list, and print_list were not called."""
super(TestDoCellList, self).assertNothingWasCalled()
self.assertFalse(self.print_list.called)
self.assertFalse(self.formatter.configure.called)
self.assertFalse(self.formatter.handle.called)
def args_for(self, **kwargs):
"""Generate the default argument list for cell-list."""
@ -72,9 +71,7 @@ class TestDoCellList(base.TestShellCommandUsingPrintList):
autopaginate=False,
marker=None,
)
self.assertTrue(self.print_list.called)
self.assertEqual(['id', 'name'],
sorted(self.print_list.call_args[0][-1]))
self.assertSortedFieldsEqualTo(['id', 'name'])
def test_with_cloud_id(self):
"""Verify the behaviour of do_cell_list with mostly default values."""
@ -89,9 +86,7 @@ class TestDoCellList(base.TestShellCommandUsingPrintList):
autopaginate=False,
marker=None,
)
self.assertTrue(self.print_list.called)
self.assertEqual(['id', 'name'],
sorted(self.print_list.call_args[0][-1]))
self.assertSortedFieldsEqualTo(['id', 'name'])
def test_negative_limit(self):
"""Ensure we raise an exception for negative limits."""
@ -113,9 +108,7 @@ class TestDoCellList(base.TestShellCommandUsingPrintList):
autopaginate=False,
marker=None,
)
self.assertTrue(self.print_list.called)
self.assertEqual(['id', 'name'],
sorted(self.print_list.call_args[0][-1]))
self.assertSortedFieldsEqualTo(['id', 'name'])
def test_valid_sort_key(self):
"""Verify that we pass on our sort key."""
@ -130,9 +123,7 @@ class TestDoCellList(base.TestShellCommandUsingPrintList):
autopaginate=False,
marker=None,
)
self.assertTrue(self.print_list.called)
self.assertEqual(['id', 'name'],
sorted(self.print_list.call_args[0][-1]))
self.assertSortedFieldsEqualTo(['id', 'name'])
def test_invalid_sort_key(self):
"""Verify that do not we pass on our sort key."""
@ -154,8 +145,7 @@ class TestDoCellList(base.TestShellCommandUsingPrintList):
autopaginate=False,
marker=None,
)
self.assertEqual(sorted(list(cells.CELL_FIELDS)),
sorted(self.print_list.call_args[0][-1]))
self.assertSortedFieldsEqualTo(sorted(list(cells.CELL_FIELDS)))
def test_raises_exception_with_detail_and_fields(self):
"""Verify that we fail when users specify --detail and --fields."""
@ -179,8 +169,7 @@ class TestDoCellList(base.TestShellCommandUsingPrintList):
autopaginate=False,
marker=None,
)
self.assertEqual(['id', 'name', 'note'],
sorted(self.print_list.call_args[0][-1]))
self.assertSortedFieldsEqualTo(['id', 'name', 'note'])
def test_invalid_fields(self):
"""Verify that we error out with invalid fields."""
@ -238,7 +227,8 @@ class TestDoCellCreate(base.TestShellCommandUsingPrintDict):
name='New Cell',
region_id=123,
)
self.print_dict.assert_called_once_with(mock.ANY, wrap=72)
self.formatter.configure.assert_called_once_with(wrap=72)
self.assertEqual(1, self.formatter.handle.call_count)
def test_create_with_note(self):
"""Verify that we include the note argument when present."""
@ -251,7 +241,8 @@ class TestDoCellCreate(base.TestShellCommandUsingPrintDict):
region_id=123,
note='This is a note',
)
self.print_dict.assert_called_once_with(mock.ANY, wrap=72)
self.formatter.configure.assert_called_once_with(wrap=72)
self.assertEqual(1, self.formatter.handle.call_count)
class TestDoCellUpdate(base.TestShellCommandUsingPrintDict):
@ -282,7 +273,8 @@ class TestDoCellUpdate(base.TestShellCommandUsingPrintDict):
123,
name='New name',
)
self.print_dict.assert_called_once_with(mock.ANY, wrap=72)
self.formatter.configure.assert_called_once_with(wrap=72)
self.assertEqual(1, self.formatter.handle.call_count)
def test_update_with_new_region(self):
"""Verify we update with only the new region id."""
@ -294,7 +286,8 @@ class TestDoCellUpdate(base.TestShellCommandUsingPrintDict):
123,
region_id=678,
)
self.print_dict.assert_called_once_with(mock.ANY, wrap=72)
self.formatter.configure.assert_called_once_with(wrap=72)
self.assertEqual(1, self.formatter.handle.call_count)
def test_update_with_new_note(self):
"""Verify we update with only the new note text."""
@ -306,7 +299,8 @@ class TestDoCellUpdate(base.TestShellCommandUsingPrintDict):
123,
note='A new note',
)
self.print_dict.assert_called_once_with(mock.ANY, wrap=72)
self.formatter.configure.assert_called_once_with(wrap=72)
self.assertEqual(1, self.formatter.handle.call_count)
def test_update_with_everything(self):
"""Verify we update with everything."""
@ -324,7 +318,8 @@ class TestDoCellUpdate(base.TestShellCommandUsingPrintDict):
region_id=678,
note='A new note',
)
self.print_dict.assert_called_once_with(mock.ANY, wrap=72)
self.formatter.configure.assert_called_once_with(wrap=72)
self.assertEqual(1, self.formatter.handle.call_count)
class TestDoCellDelete(base.TestShellCommand):