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

verdi group remove-nodes: Add warning when nodes are not in Group #4728

Merged
merged 4 commits into from
Feb 11, 2021
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
37 changes: 33 additions & 4 deletions aiida/cmdline/commands/cmd_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,41 @@ def group_add_nodes(group, force, nodes):
@with_dbenv()
def group_remove_nodes(group, nodes, clear, force):
"""Remove nodes from a group."""
if clear:
message = f'Do you really want to remove ALL the nodes from Group<{group.label}>?'
else:
message = f'Do you really want to remove {len(nodes)} nodes from Group<{group.label}>?'
from aiida.orm import QueryBuilder, Group, Node

label = group.label
klass = group.__class__.__name__

if nodes and clear:
echo.echo_critical(
'Specify either the `--clear` flag to remove all nodes or the identifiers of the nodes you want to remove.'
)

if not force:

if nodes:
node_pks = [node.pk for node in nodes]

query = QueryBuilder()
query.append(Group, filters={'id': group.pk}, tag='group')
query.append(Node, with_group='group', filters={'id': {'in': node_pks}}, project='id')

group_node_pks = query.all(flat=True)

if not group_node_pks:
echo.echo_critical(f'None of the specified nodes are in {klass}<{label}>.')

if len(node_pks) > len(group_node_pks):
node_pks = set(node_pks).difference(set(group_node_pks))
echo.echo_warning(f'{len(node_pks)} nodes with PK {node_pks} are not in {klass}<{label}>.')

message = f'Are you sure you want to remove {len(group_node_pks)} nodes from {klass}<{label}>?'

elif clear:
message = f'Are you sure you want to remove ALL the nodes from {klass}<{label}>?'
else:
echo.echo_critical(f'No nodes were provided for removal from {klass}<{label}>.')

click.confirm(message, abort=True)

if clear:
Expand Down
33 changes: 31 additions & 2 deletions tests/cmdline/commands/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from aiida.backends.testbase import AiidaTestCase
from aiida.common import exceptions
from aiida.cmdline.commands import cmd_group
from aiida.cmdline.utils.echo import ExitCode


class TestVerdiGroup(AiidaTestCase):
Expand Down Expand Up @@ -156,7 +157,6 @@ def test_delete(self):
self.assertEqual(group.count(), 2)

result = self.cli_runner.invoke(cmd_group.group_delete, ['--force', 'group_test_delete_02'])
self.assertClickResultNoException(result)

with self.assertRaises(exceptions.NotExistent):
orm.load_group(label='group_test_delete_02')
Expand Down Expand Up @@ -265,7 +265,7 @@ def test_add_remove_nodes(self):
result = self.cli_runner.invoke(cmd_group.group_remove_nodes, ['--force', '--group=dummygroup1', node_01.uuid])
self.assertIsNone(result.exception, result.output)

# Check if node is added in group using group show command
# Check that the node is no longer in the group
result = self.cli_runner.invoke(cmd_group.group_show, ['-r', 'dummygroup1'])
self.assertClickResultNoException(result)
self.assertNotIn('CalculationNode', result.output)
Expand All @@ -280,6 +280,35 @@ def test_add_remove_nodes(self):
self.assertClickResultNoException(result)
self.assertEqual(group.count(), 0)

# Try to remove node that isn't in the group
result = self.cli_runner.invoke(cmd_group.group_remove_nodes, ['--group=dummygroup1', node_01.uuid])
self.assertEqual(result.exit_code, ExitCode.CRITICAL)

# Try to remove no nodes nor clear the group
result = self.cli_runner.invoke(cmd_group.group_remove_nodes, ['--group=dummygroup1'])
self.assertEqual(result.exit_code, ExitCode.CRITICAL)

# Try to remove both nodes and clear the group
result = self.cli_runner.invoke(cmd_group.group_remove_nodes, ['--group=dummygroup1', '--clear', node_01.uuid])
self.assertEqual(result.exit_code, ExitCode.CRITICAL)

# Add a node with confirmation
result = self.cli_runner.invoke(cmd_group.group_add_nodes, ['--group=dummygroup1', node_01.uuid], input='y')
self.assertEqual(group.count(), 1)

# Try to remove two nodes, one that isn't in the group, but abort
result = self.cli_runner.invoke(
cmd_group.group_remove_nodes, ['--group=dummygroup1', node_01.uuid, node_02.uuid], input='N'
)
self.assertIn('Warning', result.output)
self.assertEqual(group.count(), 1)

# Try to clear all nodes from the group, but abort
result = self.cli_runner.invoke(cmd_group.group_remove_nodes, ['--group=dummygroup1', '--clear'], input='N')
self.assertIn('Are you sure you want to remove ALL', result.output)
self.assertIn('Aborted', result.output)
self.assertEqual(group.count(), 1)

def test_copy_existing_group(self):
"""Test user is prompted to continue if destination group exists and is not empty"""
source_label = 'source_copy_existing_group'
Expand Down