-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for missing libraries (#127)
* add tests for missing libraries * also add the test file * fix some typo's * actually go for logging instead of raising a warning * try using importlib reload instead * make sure mocks are working * try overriding namespace as well * see if reloading the full module works * also do reload on to see if the tests fail * remove extra_filter and mock matplotlib.pyplot instead * move mock definitions to inside the test * restructure plotting import to be testable * update minimum coverage to 78 to reflect updated score (79) * Update src/pytom_tm/__init__.py Co-authored-by: Marten <[email protected]> * remove unnesesary base matplotlib import --------- Co-authored-by: Marten <[email protected]>
- Loading branch information
Showing
5 changed files
with
93 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import logging | ||
from importlib import metadata | ||
__version__ = metadata.version('pytom-match-pick') | ||
|
||
|
||
try: | ||
import cupy | ||
except (ModuleNotFoundError, ImportError): | ||
print('Error for template matching: cupy installation not found or not functional.') | ||
logging.warning('Error for template matching: cupy installation not found or not functional.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# No imports of pytom_tm outside of the methods | ||
import unittest | ||
from importlib import reload | ||
# Mock out installed dependencies | ||
orig_import = __import__ | ||
|
||
def module_not_found_mock(missing_name): | ||
def import_mock(name, *args): | ||
if name == missing_name: | ||
raise ModuleNotFoundError(f"No module named '{name}'") | ||
return orig_import(name, *args) | ||
return import_mock | ||
|
||
def cupy_import_error_mock(name, *args): | ||
if name == 'cupy': | ||
raise ImportError("Failed to import cupy") | ||
return orig_import(name, *args) | ||
|
||
|
||
class TestMissingDependencies(unittest.TestCase): | ||
|
||
def test_missing_cupy(self): | ||
# assert working import | ||
with self.assertNoLogs(level='WARNING'): | ||
import pytom_tm | ||
cupy_not_found = module_not_found_mock('cupy') | ||
# Test missing cupy | ||
with unittest.mock.patch('builtins.__import__', side_effect=cupy_not_found): | ||
with self.assertLogs(level='WARNING') as cm: | ||
reload(pytom_tm) | ||
self.assertEqual(len(cm.output), 1) | ||
self.assertIn("cupy installation not found or not functional", cm.output[0]) | ||
|
||
def test_broken_cupy(self): | ||
# assert working import | ||
with self.assertNoLogs(level='WARNING'): | ||
import pytom_tm | ||
# Test cupy ImportError | ||
with unittest.mock.patch('builtins.__import__', side_effect=cupy_import_error_mock): | ||
with self.assertLogs(level='WARNING') as cm: | ||
reload(pytom_tm) | ||
self.assertEqual(len(cm.output), 1) | ||
self.assertIn("cupy installation not found or not functional", cm.output[0]) | ||
|
||
def test_missing_matplotlib(self): | ||
# assert working import | ||
import pytom_tm | ||
|
||
matplotlib_not_found = module_not_found_mock('matplotlib.pyplot') | ||
with unittest.mock.patch('builtins.__import__', side_effect=matplotlib_not_found): | ||
with self.assertRaisesRegex(ModuleNotFoundError, 'matplotlib'): | ||
# only pyplot is directly imported so this should be tested | ||
import matplotlib.pyplot as plt | ||
# force reload | ||
# check if we can still import pytom_tm | ||
reload(pytom_tm) | ||
|
||
# check if plotting is indeed disabled after reload | ||
# (reload is needed to prevent python import caching) | ||
self.assertFalse(reload(pytom_tm.template).plotting_available) | ||
self.assertFalse(reload(pytom_tm.extract).plotting_available) | ||
# assert that importing the plotting module fails completely | ||
with self.assertRaisesRegex(RuntimeError, "matplotlib and seaborn"): | ||
reload(pytom_tm.plotting) | ||
|
||
def test_missing_seaborn(self): | ||
# assert working import | ||
import pytom_tm | ||
|
||
seaborn_not_found = module_not_found_mock('seaborn') | ||
with unittest.mock.patch('builtins.__import__', side_effect=seaborn_not_found): | ||
with self.assertRaisesRegex(ModuleNotFoundError, 'seaborn'): | ||
import seaborn | ||
# check if we can still import pytom_tm | ||
reload(pytom_tm) | ||
# check if plotting is indeed disabled | ||
# (reload is needed to prevent python import caching) | ||
self.assertFalse(reload(pytom_tm.template).plotting_available) | ||
self.assertFalse(reload(pytom_tm.extract).plotting_available) | ||
# assert that importing the plotting module fails completely | ||
with self.assertRaisesRegex(RuntimeError, "matplotlib and seaborn"): | ||
reload(pytom_tm.plotting) | ||
|
||
|
||
|