Skip to content

Commit

Permalink
ENH: Test the CLI scripts
Browse files Browse the repository at this point in the history
Test the CLI scripts:
- Adapt and transfer the contents of the `test_scripts.py` test script
  to a more modern and compact approach that uses the `pytest`
  `pytest-console-script` plugin and that does not require fiddling
  with the location of the files.
- Split the tests into two different files following the scripts being
  tested: `tract_math` and `tract_querier`.
- Remove the `test_scripts.py` file.

Add the `pytest-console-script` dependency so that the scripts can be
called when executing `pytest`.

Convert the `output.values()` ordered dictionary values to a list prior
to slicing in `decorator.py`. Fixes:
```
"/src/tract_querier/scripts/cli_tract_math.py",
 line 109, in main\n    operations[args.operation]
(\n  File "/src/tract_querier/tract_querier/tract_math/decorator.py",
 line 148, in wrapper\n    process_output(func(*option_and_args))\n
  File "/src/tract_querier/tract_querier/tract_math/decorator.py",
 line 199, in process_output\n
    first_value = output.values()[0]\n
TypeError: \'odict_values\' object is not subscriptable\n')
```

raised locally when running the script tests.
  • Loading branch information
jhlegarreta committed Jan 17, 2025
1 parent afb437b commit 96883d3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 81 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ doc = [
test = [
"coverage",
"pytest",
"pytest-console-scripts",
]

[project.urls]
Expand Down
80 changes: 0 additions & 80 deletions tract_querier/tests/test_scripts.py

This file was deleted.

19 changes: 19 additions & 0 deletions tract_querier/tests/test_tract_math.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re

from tract_querier.tests import datasets


def test_help_option(script_runner):
ret = script_runner.run("tract_math", "--help")
assert ret.success


def test_tract_math_count(script_runner):
test_data = datasets.TestDataSet()
tract_file = test_data.files["tract_file"]
ret = script_runner.run("tract_math", tract_file, "count")
assert re.search('[^0-9]6783[^0-9]', ret.stdout) is not None
assert ret.success
29 changes: 29 additions & 0 deletions tract_querier/tests/test_tract_querier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
from os import path

from tract_querier.tests import datasets


def test_help_option(script_runner):
ret = script_runner.run("tract_querier", "--help")
assert ret.success


def test_tract_querier_query(script_runner):
test_data = datasets.TestDataSet()
atlas_file = test_data.files["atlas_file"]
query_uf_file = test_data.files["query_uf_file"]
tract_file = test_data.files["tract_file"]
output_prefix = test_data.dirname + "/test"

ret = script_runner.run("tract_querier", "-a", atlas_file,"-t", tract_file, "-q", query_uf_file, "-o", output_prefix)
tract_fname_end = "_uncinate.left.trk"
assert "uncinate.left: 000102" in ret.stdout
assert "uncinate.right: 000000" in ret.stdout
assert path.exists(output_prefix + tract_fname_end)
assert ret.success
if path.exists(output_prefix + tract_fname_end):
os.remove(output_prefix + tract_fname_end)
2 changes: 1 addition & 1 deletion tract_querier/tract_math/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def process_output(output, file_output=None):
header = dict(zip(writer.fieldnames, writer.fieldnames))
writer.writerow(header)

first_value = output.values()[0]
first_value = list(output.values())[0]
if (
not isinstance(first_value, str) and
isinstance(first_value, Iterable)
Expand Down

0 comments on commit 96883d3

Please sign in to comment.