Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the --order-by/--order-direction options to verdi group list #3858

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the DB not reset in between tests?
anyhow, it doesn't hurt...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only between test class instances

"""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