From 5d5701870702a554dcea61213999670ee15f4ea8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Suchomel?= Date: Thu, 23 Oct 2014 10:48:33 +0200 Subject: Print info message about incorrect --totals usage when neither -l nor --lh is provided. Added test coverage for --totals. Change-Id: I3245e715c26ec28457a21dec07311a58c475c066 Closes-Bug: 1258392 --- swiftclient/shell.py | 5 +++++ tests/unit/test_shell.py | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/swiftclient/shell.py b/swiftclient/shell.py index d58de60..c3f4628 100755 --- a/swiftclient/shell.py +++ b/swiftclient/shell.py @@ -378,6 +378,11 @@ def st_list(parser, args, output_manager): _opts.pop('human') _opts['long'] = True + if options.totals and not options.long and not options.human: + output_manager.error( + "Listing totals only works with -l or --lh.") + return + with SwiftService(options=_opts) as swift: try: if not args: diff --git a/tests/unit/test_shell.py b/tests/unit/test_shell.py index e6434dc..65530bc 100644 --- a/tests/unit/test_shell.py +++ b/tests/unit/test_shell.py @@ -225,6 +225,33 @@ class TestShell(unittest.TestCase): ' 0 0 ????-??-?? ??:??:?? container\n' ' 0 0\n') + @mock.patch('swiftclient.shell.OutputManager._print') + @mock.patch('swiftclient.service.Connection') + def test_list_account_totals_error(self, connection, error): + # No --lh provided: expect info message about incorrect --totals use + argv = ["", "list", "--totals"] + + self.assertRaises(SystemExit, swiftclient.shell.main, argv) + self.assertEqual(error.call_args[0][0], + "Listing totals only works with -l or --lh.") + + @mock.patch('swiftclient.shell.OutputManager._print') + @mock.patch('swiftclient.service.Connection') + def test_list_account_totals(self, connection, mock_print): + + # Test account listing, only total count and size + connection.return_value.get_account.side_effect = [ + [None, [{'name': 'container1', 'bytes': 1, 'count': 2}, + {'name': 'container2', 'bytes': 2, 'count': 4}]], + [None, []], + ] + + argv = ["", "list", "--lh", "--totals"] + swiftclient.shell.main(argv) + calls = [mock.call(marker='', prefix=None)] + connection.return_value.get_account.assert_has_calls(calls) + mock_print.assert_called_once_with(' 6 3') + @mock.patch('swiftclient.service.Connection') def test_list_container(self, connection): connection.return_value.get_container.side_effect = [ -- cgit v1.2.1