Skip to content

Commit 6b51074

Browse files
committed
🧪 Fix flaky test_show_limit PK matching (#7326)
When PKs are small numbers (e.g. 2), `str(pk) in output` would falsely match strings like "2s ago". Use a regex that excludes matches followed by time-unit suffixes.
1 parent 517e8b3 commit 6b51074

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

tests/cmdline/commands/test_group.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,15 +340,21 @@ def test_show(self, run_cli_command):
340340
@pytest.mark.usefixtures('aiida_profile_clean')
341341
def test_show_limit(self, run_cli_command):
342342
"""Test `--limit` option of the `verdi group show` command."""
343+
import re
344+
343345
label = 'test_group_limit'
344346
nodes = [orm.Data().store(), orm.Data().store()]
345347
group = orm.Group(label=label).store()
346348
group.add_nodes(nodes)
347349

350+
def _pk_in_output(pk, output):
351+
"""Check if a PK is preceded by whitespace or string start and followed by whitespace."""
352+
return bool(re.search(rf'(^|\s){pk}\s', output))
353+
348354
# Default should include all nodes in the output
349355
result = run_cli_command(cmd_group.group_show, [label], use_subprocess=True)
350356
for node in nodes:
351-
assert str(node.pk) in result.output
357+
assert _pk_in_output(node.pk, result.output)
352358

353359
# Repeat test with `limit=1`, use also the `--raw` option to only display nodes
354360
result = run_cli_command(
@@ -358,14 +364,20 @@ def test_show_limit(self, run_cli_command):
358364
# The current `verdi group show` does not support ordering so we cannot rely on that for now to test if only
359365
# one of the nodes is shown
360366
assert len(result.output.strip().split('\n')) == 1
361-
assert str(nodes[0].pk) in result.output or str(nodes[1].pk) in result.output
367+
assert _pk_in_output(nodes[0].pk, result.output) or _pk_in_output(
368+
nodes[1].pk, result.output
369+
), f'Neither found PK {nodes[0].pk} nor {nodes[1].pk} in expression {result.output!r}'
362370

363371
# Repeat test with `limit=1` but without the `--raw` flag as it has a different code path that is affected
364372
result = run_cli_command(cmd_group.group_show, [label, '--limit', '1'], use_subprocess=True)
365373

366374
# Check that one, and only one pk appears in the output
367-
assert str(nodes[0].pk) in result.output or str(nodes[1].pk) in result.output
368-
assert not (str(nodes[0].pk) in result.output and str(nodes[1].pk) in result.output)
375+
assert _pk_in_output(nodes[0].pk, result.output) or _pk_in_output(
376+
nodes[1].pk, result.output
377+
), f'Neither found PK {nodes[0].pk} nor PK {nodes[1].pk} in expression {result.output!r}'
378+
assert not (
379+
_pk_in_output(nodes[0].pk, result.output) and _pk_in_output(nodes[1].pk, result.output)
380+
), f'Found both PKs {nodes[0].pk} and {nodes[1].pk} (but only one is allowed) in expression {result.output!r}'
369381

370382
def test_description(self, run_cli_command):
371383
"""Test `verdi group description` command."""

0 commit comments

Comments
 (0)