Skip to content

Commit

Permalink
Add the --order-by/--order-direction options to verdi group list
Browse files Browse the repository at this point in the history
  • Loading branch information
sphuber committed Mar 23, 2020
1 parent 9ce2183 commit 8c73510
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
6 changes: 4 additions & 2 deletions aiida/cmdline/commands/cmd_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand Down
36 changes: 30 additions & 6 deletions tests/cmdline/commands/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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'])
Expand Down

0 comments on commit 8c73510

Please sign in to comment.