Skip to content

Commit

Permalink
Fix mock collection so they are accessible anywhere. Still need to tr…
Browse files Browse the repository at this point in the history
…y merging lib and scripts mocks together
  • Loading branch information
AlexVCaron committed Dec 14, 2023
1 parent 99ff760 commit 7452db5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
20 changes: 16 additions & 4 deletions scilpy/denoise/tests/fixtures/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@


@pytest.fixture(scope='function')
def bilateral_filtering(mock_creator, expected_results=None):
def bilateral_filtering(mock_creator, expected_results):
def _mock_side_effect(*args, **kwargs):
if expected_results is None or len(expected_results) == 0:
return None

_out_odf_fname = expected_results[0]
return nib.load(_out_odf_fname).get_fdata(dtype=np.float32)
return nib.load(expected_results).get_fdata(dtype=np.float32)

return mock_creator("scripts.scil_execute_angle_aware_bilateral_filtering",
return mock_creator("scilpy.denoise.bilateral_filtering",
"angle_aware_bilateral_filtering",
side_effect=_mock_side_effect)


@pytest.fixture(scope='function')
def bilateral_filtering_script(mock_creator, expected_results):
def _mock_side_effect(*args, **kwargs):
if expected_results is None or len(expected_results) == 0:
return None

return nib.load(expected_results).get_fdata(dtype=np.float32)

return mock_creator("scripts.scil_execute_angle_aware_bilateral_filtering",
"angle_aware_bilateral_filtering",
side_effect=_mock_side_effect)
18 changes: 11 additions & 7 deletions scilpy/tests/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from glob import glob
from os.path import realpath
import pytest
import warnings


# Load mock modules from all library tests
Expand Down Expand Up @@ -76,15 +77,18 @@ def pytest_configure(config):
@pytest.fixture
def mock_collector(request):
def _collector(mock_name):
_fixture = request(mock_name)
print(_fixture)
return _fixture.getfixturevalue()
try:
return request.getfixturevalue(mock_name)
except pytest.FixtureLookupError:
warnings.warn(f"Fixture {mock_name} not found.")
return None
return _collector


@pytest.fixture
def mock_creator(module_name, object_name, mocker,
side_effect=None):
def mock_creator(mocker):
def _mocker(module_name, object_name, side_effect=None):
return mocker.patch("{}.{}".format(module_name, object_name),
side_effect=side_effect, create=True)

return mocker.patch("{}.{}".format(module_name, object_name),
side_effect=side_effect, create=True)
return _mocker
26 changes: 15 additions & 11 deletions scripts/tests/test_execute_angle_aware_bilateral_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ def test_help_option(script_runner):
assert ret.success


@pytest.mark.parametrize("in_fodf,out_fodf",
@pytest.mark.parametrize("in_fodf,expected_results",
[[os.path.join(data_path, 'fodf_descoteaux07_sub.nii.gz'),
os.path.join(data_path, 'fodf_descoteaux07_sub_full.nii.gz')]],
scope='function')
def test_asym_basis_output(
script_runner, in_fodf, out_fodf, mock_collector):
script_runner, in_fodf, expected_results, mock_collector, request):

os.chdir(os.path.expanduser(tmp_dir.name))
_mock = mock_collector("bilateral_filtering_script")

print(request.fixturenames)
ret = script_runner.run('scil_execute_angle_aware_bilateral_filtering.py',
in_fodf,
'out_fodf1.nii.gz',
Expand All @@ -44,21 +47,22 @@ def test_asym_basis_output(

assert ret.success

_mock = mock_collector("bilateral_filtering")
if _mock:
_mock.assert_called_once()

assert_images_close(nib.load(out_fodf), nib.load("out_fodf1.nii.gz"))
assert_images_close(nib.load(expected_results), nib.load("out_fodf1.nii.gz"))


@pytest.mark.parametrize("in_fodf,out_fodf,sym_fodf",
@pytest.mark.parametrize("in_fodf,expected_results,sym_fodf",
[[os.path.join(data_path, "fodf_descoteaux07_sub.nii.gz"),
os.path.join(data_path, "fodf_descoteaux07_sub_full.nii.gz"),
os.path.join(data_path, "fodf_descoteaux07_sub_sym.nii.gz")]],
scope='function')
def test_sym_basis_output(
script_runner, in_fodf, out_fodf, sym_fodf, mock_collector):
script_runner, in_fodf, expected_results, sym_fodf, mock_collector):

os.chdir(os.path.expanduser(tmp_dir.name))
_mock = mock_collector("bilateral_filtering_script")

ret = script_runner.run('scil_execute_angle_aware_bilateral_filtering.py',
in_fodf,
Expand All @@ -74,19 +78,20 @@ def test_sym_basis_output(

assert ret.success

_mock = mock_collector("bilateral_filtering")
if _mock:
_mock.assert_called_once()

assert_images_close(nib.load(sym_fodf), nib.load("out_sym.nii.gz"))


@pytest.mark.parametrize("in_fodf,out_fodf",
@pytest.mark.parametrize("in_fodf,expected_results",
[[os.path.join(data_path, "fodf_descoteaux07_sub_full.nii.gz"),
os.path.join(data_path, "fodf_descoteaux07_sub_twice.nii.gz")]],
scope='function')
def test_asym_input(script_runner, in_fodf, out_fodf, mock_collector):
def test_asym_input(script_runner, in_fodf, expected_results, mock_collector):

os.chdir(os.path.expanduser(tmp_dir.name))
_mock = mock_collector("bilateral_filtering_script")

ret = script_runner.run('scil_execute_angle_aware_bilateral_filtering.py',
in_fodf,
Expand All @@ -101,8 +106,7 @@ def test_asym_input(script_runner, in_fodf, out_fodf, mock_collector):

assert ret.success

_mock = mock_collector("bilateral_filtering")
if _mock:
_mock.assert_called_once()

assert_images_close(nib.load(out_fodf), nib.load("out_fodf3.nii.gz"))
assert_images_close(nib.load(expected_results), nib.load("out_fodf3.nii.gz"))

0 comments on commit 7452db5

Please sign in to comment.