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:
parent
0ec761cb96
commit
ab49589345
@ -26,8 +26,7 @@ from cratonclient.v1 import cells
|
|||||||
def do_cell_show(cc, args):
|
def do_cell_show(cc, args):
|
||||||
"""Show detailed information about a cell."""
|
"""Show detailed information about a cell."""
|
||||||
cell = cc.cells.get(args.id)
|
cell = cc.cells.get(args.id)
|
||||||
data = {f: getattr(cell, f, '') for f in cells.CELL_FIELDS}
|
args.formatter.configure(wrap=72).handle(cell)
|
||||||
cliutils.print_dict(data, wrap=72)
|
|
||||||
|
|
||||||
|
|
||||||
@cliutils.arg('-r', '--region',
|
@cliutils.arg('-r', '--region',
|
||||||
@ -117,7 +116,7 @@ def do_cell_list(cc, args):
|
|||||||
params['autopaginate'] = args.all
|
params['autopaginate'] = args.all
|
||||||
|
|
||||||
listed_cells = cc.cells.list(**params)
|
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',
|
@cliutils.arg('-n', '--name',
|
||||||
@ -143,8 +142,7 @@ def do_cell_create(cc, args):
|
|||||||
fields = {k: v for (k, v) in vars(args).items()
|
fields = {k: v for (k, v) in vars(args).items()
|
||||||
if k in cells.CELL_FIELDS and not (v is None)}
|
if k in cells.CELL_FIELDS and not (v is None)}
|
||||||
cell = cc.cells.create(**fields)
|
cell = cc.cells.create(**fields)
|
||||||
data = {f: getattr(cell, f, '') for f in cells.CELL_FIELDS}
|
args.formatter.configure(wrap=72).handle(cell)
|
||||||
cliutils.print_dict(data, wrap=72)
|
|
||||||
|
|
||||||
|
|
||||||
@cliutils.arg('id',
|
@cliutils.arg('id',
|
||||||
@ -177,8 +175,7 @@ def do_cell_update(cc, args):
|
|||||||
'--cloud, or --note'
|
'--cloud, or --note'
|
||||||
)
|
)
|
||||||
cell = cc.cells.update(cell_id, **fields)
|
cell = cc.cells.update(cell_id, **fields)
|
||||||
data = {f: getattr(cell, f, '') for f in cells.CELL_FIELDS}
|
args.formatter.configure(wrap=72).handle(cell)
|
||||||
cliutils.print_dict(data, wrap=72)
|
|
||||||
|
|
||||||
|
|
||||||
@cliutils.arg('id',
|
@cliutils.arg('id',
|
||||||
|
@ -29,18 +29,26 @@ class TestCellsShell(base.ShellTestCase):
|
|||||||
|
|
||||||
re_options = re.DOTALL | re.MULTILINE
|
re_options = re.DOTALL | re.MULTILINE
|
||||||
cell_valid_fields = None
|
cell_valid_fields = None
|
||||||
cell_invalid_field = None
|
cell_invalid_fields = None
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
"""Setup required test fixtures."""
|
"""Setup required test fixtures."""
|
||||||
super(TestCellsShell, self).setUp()
|
super(TestCellsShell, self).setUp()
|
||||||
self.cell_valid_fields = Namespace(project_id=1,
|
self.cell_valid_kwargs = {
|
||||||
region_id=1,
|
'project_id': 1,
|
||||||
name='mock_cell')
|
'region_id': 1,
|
||||||
self.cell_invalid_field = Namespace(project_id=1,
|
'name': 'mock_cell',
|
||||||
region_id=1,
|
}
|
||||||
name='mock_cell',
|
self.cell_valid_fields = Namespace(**self.cell_valid_kwargs)
|
||||||
invalid_foo='ignored')
|
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')
|
@mock.patch('cratonclient.v1.cells.CellManager.list')
|
||||||
def test_cell_list_success(self, mock_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.v1.cells.CellManager.list')
|
||||||
@mock.patch('cratonclient.common.cliutils.print_list')
|
def test_cell_list_fields_success(self, mock_list):
|
||||||
def test_cell_list_fields_success(self, mock_printlist, mock_list):
|
|
||||||
"""Verify --fields argument successfully passed to Client."""
|
"""Verify --fields argument successfully passed to Client."""
|
||||||
self.shell('cell-list -r 1 --fields id name')
|
self.shell('cell-list -r 1 --fields id name')
|
||||||
mock_list.assert_called_once_with(
|
mock_list.assert_called_once_with(
|
||||||
@ -113,9 +120,6 @@ class TestCellsShell(base.ShellTestCase):
|
|||||||
autopaginate=False,
|
autopaginate=False,
|
||||||
marker=None,
|
marker=None,
|
||||||
)
|
)
|
||||||
mock_printlist.assert_called_once_with(mock.ANY,
|
|
||||||
list({'id': 'ID',
|
|
||||||
'name': 'Name'}))
|
|
||||||
|
|
||||||
@mock.patch('cratonclient.v1.cells.CellManager.list')
|
@mock.patch('cratonclient.v1.cells.CellManager.list')
|
||||||
def test_cell_list_sort_key_field_key_success(self, mock_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,
|
region_id=mock.ANY,
|
||||||
)
|
)
|
||||||
cells_shell.do_cell_create(client, self.cell_valid_fields)
|
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')
|
@mock.patch('cratonclient.v1.cells.CellManager.create')
|
||||||
def test_do_cell_create_ignores_unknown_fields(self, mock_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/',
|
mock.ANY, 'http://127.0.0.1/',
|
||||||
region_id=mock.ANY,
|
region_id=mock.ANY,
|
||||||
)
|
)
|
||||||
cells_shell.do_cell_create(client, self.cell_invalid_field)
|
cells_shell.do_cell_create(client, self.cell_invalid_fields)
|
||||||
mock_create.assert_called_once_with(**vars(self.cell_valid_fields))
|
mock_create.assert_called_once_with(**self.cell_valid_kwargs)
|
||||||
|
|
||||||
def test_cell_update_missing_required_args(self):
|
def test_cell_update_missing_required_args(self):
|
||||||
"""Verify that missing required args results in error message."""
|
"""Verify that missing required args results in error message."""
|
||||||
@ -222,7 +226,8 @@ class TestCellsShell(base.ShellTestCase):
|
|||||||
)
|
)
|
||||||
valid_input = Namespace(region=1,
|
valid_input = Namespace(region=1,
|
||||||
id=1,
|
id=1,
|
||||||
name='mock_cell')
|
name='mock_cell',
|
||||||
|
formatter=mock.Mock())
|
||||||
cells_shell.do_cell_update(client, valid_input)
|
cells_shell.do_cell_update(client, valid_input)
|
||||||
mock_update.assert_called_once_with(1, name='mock_cell')
|
mock_update.assert_called_once_with(1, name='mock_cell')
|
||||||
|
|
||||||
@ -237,7 +242,8 @@ class TestCellsShell(base.ShellTestCase):
|
|||||||
invalid_input = Namespace(region=1,
|
invalid_input = Namespace(region=1,
|
||||||
id=1,
|
id=1,
|
||||||
name='mock_cell',
|
name='mock_cell',
|
||||||
invalid=True)
|
invalid=True,
|
||||||
|
formatter=mock.Mock())
|
||||||
cells_shell.do_cell_update(client, invalid_input)
|
cells_shell.do_cell_update(client, invalid_input)
|
||||||
mock_update.assert_called_once_with(1, name='mock_cell')
|
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/',
|
mock.ANY, 'http://127.0.0.1/',
|
||||||
region_id=mock.ANY,
|
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)
|
cells_shell.do_cell_show(client, test_args)
|
||||||
mock_get.assert_called_once_with(vars(test_args)['id'])
|
mock_get.assert_called_once_with(vars(test_args)['id'])
|
||||||
|
|
||||||
|
@ -93,3 +93,9 @@ class TestShellCommandUsingPrintList(TestShellCommand):
|
|||||||
kwargs = self.formatter.configure.call_args[1]
|
kwargs = self.formatter.configure.call_args[1]
|
||||||
self.assertListEqual(expected_fields,
|
self.assertListEqual(expected_fields,
|
||||||
sorted(kwargs['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']))
|
||||||
|
@ -33,10 +33,8 @@ class TestDoShellShow(base.TestShellCommandUsingPrintDict):
|
|||||||
cells_shell.do_cell_show(self.craton_client, args)
|
cells_shell.do_cell_show(self.craton_client, args)
|
||||||
|
|
||||||
self.craton_client.cells.get.assert_called_once_with(456)
|
self.craton_client.cells.get.assert_called_once_with(456)
|
||||||
self.print_dict.assert_called_once_with(
|
self.formatter.configure.assert_called_once_with(wrap=72)
|
||||||
{f: mock.ANY for f in cells.CELL_FIELDS},
|
self.assertEqual(1, self.formatter.handle.call_count)
|
||||||
wrap=72,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TestDoCellList(base.TestShellCommandUsingPrintList):
|
class TestDoCellList(base.TestShellCommandUsingPrintList):
|
||||||
@ -45,7 +43,8 @@ class TestDoCellList(base.TestShellCommandUsingPrintList):
|
|||||||
def assertNothingWasCalled(self):
|
def assertNothingWasCalled(self):
|
||||||
"""Assert inventory, list, and print_list were not called."""
|
"""Assert inventory, list, and print_list were not called."""
|
||||||
super(TestDoCellList, self).assertNothingWasCalled()
|
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):
|
def args_for(self, **kwargs):
|
||||||
"""Generate the default argument list for cell-list."""
|
"""Generate the default argument list for cell-list."""
|
||||||
@ -72,9 +71,7 @@ class TestDoCellList(base.TestShellCommandUsingPrintList):
|
|||||||
autopaginate=False,
|
autopaginate=False,
|
||||||
marker=None,
|
marker=None,
|
||||||
)
|
)
|
||||||
self.assertTrue(self.print_list.called)
|
self.assertSortedFieldsEqualTo(['id', 'name'])
|
||||||
self.assertEqual(['id', 'name'],
|
|
||||||
sorted(self.print_list.call_args[0][-1]))
|
|
||||||
|
|
||||||
def test_with_cloud_id(self):
|
def test_with_cloud_id(self):
|
||||||
"""Verify the behaviour of do_cell_list with mostly default values."""
|
"""Verify the behaviour of do_cell_list with mostly default values."""
|
||||||
@ -89,9 +86,7 @@ class TestDoCellList(base.TestShellCommandUsingPrintList):
|
|||||||
autopaginate=False,
|
autopaginate=False,
|
||||||
marker=None,
|
marker=None,
|
||||||
)
|
)
|
||||||
self.assertTrue(self.print_list.called)
|
self.assertSortedFieldsEqualTo(['id', 'name'])
|
||||||
self.assertEqual(['id', 'name'],
|
|
||||||
sorted(self.print_list.call_args[0][-1]))
|
|
||||||
|
|
||||||
def test_negative_limit(self):
|
def test_negative_limit(self):
|
||||||
"""Ensure we raise an exception for negative limits."""
|
"""Ensure we raise an exception for negative limits."""
|
||||||
@ -113,9 +108,7 @@ class TestDoCellList(base.TestShellCommandUsingPrintList):
|
|||||||
autopaginate=False,
|
autopaginate=False,
|
||||||
marker=None,
|
marker=None,
|
||||||
)
|
)
|
||||||
self.assertTrue(self.print_list.called)
|
self.assertSortedFieldsEqualTo(['id', 'name'])
|
||||||
self.assertEqual(['id', 'name'],
|
|
||||||
sorted(self.print_list.call_args[0][-1]))
|
|
||||||
|
|
||||||
def test_valid_sort_key(self):
|
def test_valid_sort_key(self):
|
||||||
"""Verify that we pass on our sort key."""
|
"""Verify that we pass on our sort key."""
|
||||||
@ -130,9 +123,7 @@ class TestDoCellList(base.TestShellCommandUsingPrintList):
|
|||||||
autopaginate=False,
|
autopaginate=False,
|
||||||
marker=None,
|
marker=None,
|
||||||
)
|
)
|
||||||
self.assertTrue(self.print_list.called)
|
self.assertSortedFieldsEqualTo(['id', 'name'])
|
||||||
self.assertEqual(['id', 'name'],
|
|
||||||
sorted(self.print_list.call_args[0][-1]))
|
|
||||||
|
|
||||||
def test_invalid_sort_key(self):
|
def test_invalid_sort_key(self):
|
||||||
"""Verify that do not we pass on our sort key."""
|
"""Verify that do not we pass on our sort key."""
|
||||||
@ -154,8 +145,7 @@ class TestDoCellList(base.TestShellCommandUsingPrintList):
|
|||||||
autopaginate=False,
|
autopaginate=False,
|
||||||
marker=None,
|
marker=None,
|
||||||
)
|
)
|
||||||
self.assertEqual(sorted(list(cells.CELL_FIELDS)),
|
self.assertSortedFieldsEqualTo(sorted(list(cells.CELL_FIELDS)))
|
||||||
sorted(self.print_list.call_args[0][-1]))
|
|
||||||
|
|
||||||
def test_raises_exception_with_detail_and_fields(self):
|
def test_raises_exception_with_detail_and_fields(self):
|
||||||
"""Verify that we fail when users specify --detail and --fields."""
|
"""Verify that we fail when users specify --detail and --fields."""
|
||||||
@ -179,8 +169,7 @@ class TestDoCellList(base.TestShellCommandUsingPrintList):
|
|||||||
autopaginate=False,
|
autopaginate=False,
|
||||||
marker=None,
|
marker=None,
|
||||||
)
|
)
|
||||||
self.assertEqual(['id', 'name', 'note'],
|
self.assertSortedFieldsEqualTo(['id', 'name', 'note'])
|
||||||
sorted(self.print_list.call_args[0][-1]))
|
|
||||||
|
|
||||||
def test_invalid_fields(self):
|
def test_invalid_fields(self):
|
||||||
"""Verify that we error out with invalid fields."""
|
"""Verify that we error out with invalid fields."""
|
||||||
@ -238,7 +227,8 @@ class TestDoCellCreate(base.TestShellCommandUsingPrintDict):
|
|||||||
name='New Cell',
|
name='New Cell',
|
||||||
region_id=123,
|
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):
|
def test_create_with_note(self):
|
||||||
"""Verify that we include the note argument when present."""
|
"""Verify that we include the note argument when present."""
|
||||||
@ -251,7 +241,8 @@ class TestDoCellCreate(base.TestShellCommandUsingPrintDict):
|
|||||||
region_id=123,
|
region_id=123,
|
||||||
note='This is a note',
|
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):
|
class TestDoCellUpdate(base.TestShellCommandUsingPrintDict):
|
||||||
@ -282,7 +273,8 @@ class TestDoCellUpdate(base.TestShellCommandUsingPrintDict):
|
|||||||
123,
|
123,
|
||||||
name='New name',
|
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):
|
def test_update_with_new_region(self):
|
||||||
"""Verify we update with only the new region id."""
|
"""Verify we update with only the new region id."""
|
||||||
@ -294,7 +286,8 @@ class TestDoCellUpdate(base.TestShellCommandUsingPrintDict):
|
|||||||
123,
|
123,
|
||||||
region_id=678,
|
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):
|
def test_update_with_new_note(self):
|
||||||
"""Verify we update with only the new note text."""
|
"""Verify we update with only the new note text."""
|
||||||
@ -306,7 +299,8 @@ class TestDoCellUpdate(base.TestShellCommandUsingPrintDict):
|
|||||||
123,
|
123,
|
||||||
note='A new note',
|
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):
|
def test_update_with_everything(self):
|
||||||
"""Verify we update with everything."""
|
"""Verify we update with everything."""
|
||||||
@ -324,7 +318,8 @@ class TestDoCellUpdate(base.TestShellCommandUsingPrintDict):
|
|||||||
region_id=678,
|
region_id=678,
|
||||||
note='A new note',
|
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):
|
class TestDoCellDelete(base.TestShellCommand):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user