Skip to content

Commit

Permalink
working mocks easy to use !
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVCaron committed Dec 14, 2023
1 parent ae25080 commit ee602cc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
17 changes: 2 additions & 15 deletions scilpy/denoise/tests/fixtures/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,5 @@ def _mock_side_effect(*args, **kwargs):
return nib.load(expected_results).get_fdata(dtype=np.float32)

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)
"angle_aware_bilateral_filtering",
side_effect=_mock_side_effect)
17 changes: 11 additions & 6 deletions scilpy/tests/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,24 @@ def pytest_configure(config):

@pytest.fixture
def mock_collector(request):
def _collector(mock_name):
def _collector(mock_names, patch_path):
try:
return request.getfixturevalue(mock_name)
return {_name: request.getfixturevalue(_name)(patch_path)
for _name in mock_names}
except pytest.FixtureLookupError:
warnings.warn(f"Fixture {mock_name} not found.")
warnings.warn(f"Some fixtures in {mock_names} cannot be found.")
return None
return _collector


@pytest.fixture
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)
def _mocker(base_module, object_name, side_effect=None):
def _patcher(module_name=None):
_base = base_module if module_name is None else module_name
return mocker.patch("{}.{}".format(_base, object_name),
side_effect=side_effect, create=True)

return _patcher

return _mocker
21 changes: 12 additions & 9 deletions scripts/tests/test_execute_angle_aware_bilateral_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def test_asym_basis_output(
script_runner, in_fodf, expected_results, mock_collector, request):

os.chdir(os.path.expanduser(tmp_dir.name))
_mock = mock_collector("bilateral_filtering_script")
_mocks = mock_collector(["bilateral_filtering"],
"scripts.scil_execute_angle_aware_bilateral_filtering")

print(request.fixturenames)
ret = script_runner.run('scil_execute_angle_aware_bilateral_filtering.py',
Expand All @@ -47,8 +48,8 @@ def test_asym_basis_output(

assert ret.success

if _mock:
_mock.assert_called_once()
if _mocks["bilateral_filtering"]:
_mocks["bilateral_filtering"].assert_called_once()

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

Expand All @@ -62,7 +63,8 @@ def test_sym_basis_output(
script_runner, in_fodf, expected_results, sym_fodf, mock_collector):

os.chdir(os.path.expanduser(tmp_dir.name))
_mock = mock_collector("bilateral_filtering_script")
_mocks = mock_collector(["bilateral_filtering"],
"scripts.scil_execute_angle_aware_bilateral_filtering")

ret = script_runner.run('scil_execute_angle_aware_bilateral_filtering.py',
in_fodf,
Expand All @@ -78,8 +80,8 @@ def test_sym_basis_output(

assert ret.success

if _mock:
_mock.assert_called_once()
if _mocks["bilateral_filtering"]:
_mocks["bilateral_filtering"].assert_called_once()

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

Expand All @@ -91,7 +93,8 @@ def test_sym_basis_output(
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")
_mocks = mock_collector(["bilateral_filtering"],
"scripts.scil_execute_angle_aware_bilateral_filtering")

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

assert ret.success

if _mock:
_mock.assert_called_once()
if _mocks["bilateral_filtering"]:
_mocks["bilateral_filtering"].assert_called_once()

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

0 comments on commit ee602cc

Please sign in to comment.