From 8c73510c4a9fe63187594705834dca9a1d78ad1b Mon Sep 17 00:00:00 2001 From: Sebastiaan Huber Date: Mon, 23 Mar 2020 23:30:37 +0100 Subject: [PATCH] Add the `--order-by/--order-direction` options to `verdi group list` --- aiida/cmdline/commands/cmd_group.py | 6 +++-- tests/cmdline/commands/test_group.py | 36 +++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/aiida/cmdline/commands/cmd_group.py b/aiida/cmdline/commands/cmd_group.py index 9e0eb26d07..d74e416bd5 100644 --- a/aiida/cmdline/commands/cmd_group.py +++ b/aiida/cmdline/commands/cmd_group.py @@ -235,11 +235,13 @@ def user_defined_group(): default=None, help='add a filter to show only groups for which the name contains STRING' ) +@options.ORDER_BY(type=click.Choice(['id', 'label', 'ctime']), default='id') +@options.ORDER_DIRECTION() @options.NODE(help='Show only the groups that contain the node') @with_dbenv() def group_list( all_users, user_email, all_types, group_type, with_description, count, past_days, startswith, endswith, contains, - node + order_by, order_dir, node ): """Show a list of existing groups.""" # pylint: disable=too-many-branches,too-many-arguments, too-many-locals @@ -290,7 +292,7 @@ def group_list( from aiida.orm import Node query.append(Node, filters={'id': {'==': node.id}}, with_group='group') - query.order_by({Group: {'id': 'asc'}}) + query.order_by({Group: {order_by: order_dir}}) result = query.all() projection_lambdas = { diff --git a/tests/cmdline/commands/test_group.py b/tests/cmdline/commands/test_group.py index dbba9bf3f1..4302420633 100644 --- a/tests/cmdline/commands/test_group.py +++ b/tests/cmdline/commands/test_group.py @@ -17,17 +17,19 @@ class TestVerdiGroup(AiidaTestCase): """Tests for the `verdi group` command.""" - @classmethod - def setUpClass(cls, *args, **kwargs): - super().setUpClass(*args, **kwargs) - for group in ['dummygroup1', 'dummygroup2', 'dummygroup3', 'dummygroup4']: - orm.Group(label=group).store() - def setUp(self): """Create runner object to run tests.""" from click.testing import CliRunner self.cli_runner = CliRunner() + for group in ['dummygroup1', 'dummygroup2', 'dummygroup3', 'dummygroup4']: + orm.Group(label=group).store() + + def tearDown(self): + """Delete all created group objects.""" + for group in orm.Group.objects.all(): + orm.Group.objects.delete(group.pk) + def test_help(self): """Tests help text for all group sub commands.""" options = ['--help'] @@ -96,6 +98,28 @@ def test_list(self): for grp in ['dummygroup1', 'dummygroup2']: self.assertIn(grp, result.output) + def test_list_order(self): + """Test `verdi group list` command with ordering options.""" + orm.Group(label='agroup').store() + + options = [] + result = self.cli_runner.invoke(cmd_group.group_list, options) + self.assertClickResultNoException(result) + group_ordering = [l.split()[1] for l in result.output.split('\n')[3:] if l] + self.assertEqual(['dummygroup1', 'dummygroup2', 'dummygroup3', 'dummygroup4', 'agroup'], group_ordering) + + options = ['--order-by', 'label'] + result = self.cli_runner.invoke(cmd_group.group_list, options) + self.assertClickResultNoException(result) + group_ordering = [l.split()[1] for l in result.output.split('\n')[3:] if l] + self.assertEqual(['agroup', 'dummygroup1', 'dummygroup2', 'dummygroup3', 'dummygroup4'], group_ordering) + + options = ['--order-by', 'id', '--order-direction', 'desc'] + result = self.cli_runner.invoke(cmd_group.group_list, options) + self.assertClickResultNoException(result) + group_ordering = [l.split()[1] for l in result.output.split('\n')[3:] if l] + self.assertEqual(['agroup', 'dummygroup4', 'dummygroup3', 'dummygroup2', 'dummygroup1'], group_ordering) + def test_copy(self): """Test `verdi group copy` command.""" result = self.cli_runner.invoke(cmd_group.group_copy, ['dummygroup1', 'dummygroup2'])