def test_sklearn_import(): """ Tests scikit-learn import in the module header. Tests ``sklearn`` import in the :mod:`fatf.utils.data.feature_selection.sklearn` module. """ # When present assert 'fatf.utils.data.feature_selection.sklearn' in sys.modules with futi.module_import_tester('sklearn', when_missing=False): importlib.reload(fatf.utils.data.feature_selection.sklearn) assert 'fatf.utils.data.feature_selection.sklearn' in sys.modules # When missing assert 'fatf.utils.data.feature_selection.sklearn' in sys.modules warning_msg = ( 'scikit-learn (sklearn) Python module is not installed on your ' 'system. You must install it in order to use ' 'fatf.utils.data.feature_selection.sklearn functionality. ' 'One possibility is to install scikit-learn alongside this package ' 'via machine learning dependencies with: pip install fatf[ml].') with futi.module_import_tester('sklearn', when_missing=True): with pytest.warns(ImportWarning) as warning: importlib.reload(fatf.utils.data.feature_selection.sklearn) assert len(warning) == 1 assert str(warning[0].message) == warning_msg assert 'fatf.utils.data.feature_selection.sklearn' in sys.modules
def test_import_when_installed(): """ Tests importing :mod:`fatf.vis` module with matplotlib_ installed. .. _matplotlib: https://matplotlib.org/ """ assert 'fatf.vis' in sys.modules with futi.module_import_tester('matplotlib', when_missing=False): importlib.reload(fatf.vis) assert 'fatf.vis' in sys.modules
def test_import_when_installed(): """ Tests importing :mod:`fatf.transparency.sklearn` with sklearn_ installed. .. _sklearn: https://scikit-learn.org/ """ assert 'fatf.transparency.sklearn' in sys.modules with futi.module_import_tester('sklearn', when_missing=False): importlib.reload(fatf.transparency.sklearn) assert 'fatf.transparency.sklearn' in sys.modules
def test_import_when_missing(): """ Tests importing :mod:`fatf.utils.testing.vis` module sans matplotlib. """ assert 'fatf.utils.testing.vis' in sys.modules warning_msg = ('Visualisation testing helper functions require matplotlib ' 'Python module, which is not installed on your system.') with futi.module_import_tester('matplotlib', when_missing=True): with pytest.warns(ImportWarning) as w: importlib.reload(futv) assert len(w) == 1 assert str(w[0].message) == warning_msg assert 'fatf.utils.testing.vis' in sys.modules
def test_import_when_missing(): """ Tests importing :mod:`fatf.utils.testing.vis` module sans matplotlib. """ assert 'fatf.utils.testing.vis' in sys.modules exception_msg = ('Visualisation testing helper functions require ' 'matplotlib Python module, which is not installed ' 'on your system.') with futi.module_import_tester('matplotlib', when_missing=True): with pytest.raises(ImportError) as excinfo: importlib.reload(futv) assert str(excinfo.value) == exception_msg assert 'fatf.utils.testing.vis' in sys.modules
def test_import_when_missing(): """ Tests importing :mod:`fatf.transparency.lime` module when LIME is missing. """ assert 'fatf.transparency.lime' in sys.modules warning_msg = ( 'Lime package is not installed on your system. You must install it in ' 'order to use the fatf.transparency.lime module. One possibility is ' 'to install LIME alongside this package with: pip install fatf[lime].') with futi.module_import_tester('lime', when_missing=True): with pytest.warns(ImportWarning) as w: importlib.reload(ftl) assert len(w) == 1 assert str(w[0].message) == warning_msg assert 'fatf.transparency.lime' in sys.modules
def test_import_when_missing(): """ Tests importing :mod:`fatf.vis` module with matplotlib_ missing. .. _matplotlib: https://matplotlib.org/ """ assert 'fatf.vis' in sys.modules warning_msg = ( 'matplotlib Python module is not installed on your system. ' 'You must install it in order to use fatf.vis functionality. ' 'One possibility is to install matplotlib alongside this package via ' 'visualisation dependencies with: pip install fatf[vis].') with futi.module_import_tester('matplotlib', when_missing=True): with pytest.warns(ImportWarning) as w: importlib.reload(fatf.vis) assert str(w[0].message) == warning_msg assert 'fatf.vis' in sys.modules
def test_import_when_missing(): """ Tests importing :mod:`fatf.vis` module with matplotlib_ missing. .. _matplotlib: https://matplotlib.org/ """ assert 'fatf.vis' in sys.modules exception_msg = ( 'matplotlib Python module is not installed on your system. ' 'You must install it in order to use fatf.vis functionality. ' 'One possibility is to install matplotlib alongside this package via ' 'visualisation dependencies with: pip install fat-forensics[vis].') with futi.module_import_tester('matplotlib', when_missing=True): with pytest.raises(ImportError) as exin: importlib.reload(fatf.vis) assert str(exin.value) == exception_msg assert 'fatf.vis' in sys.modules
def test_import_when_missing(): """ Tests importing :mod:`fatf.transparency.sklearn` with sklearn_ missing. .. _sklearn: https://scikit-learn.org/ """ assert 'fatf.transparency.sklearn' in sys.modules warning_msg = ( 'scikit-learn (sklearn) Python module is not installed on your ' 'system. You must install it in order to use ' 'fatf.transparency.sklearn functionality. ' 'One possibility is to install scikit-learn alongside this package ' 'via machine learning dependencies with: pip install fatf[ml].') with futi.module_import_tester('sklearn', when_missing=True): with pytest.warns(ImportWarning) as warning: importlib.reload(fatf.transparency.sklearn) assert str(warning[0].message) == warning_msg assert 'fatf.transparency.sklearn' in sys.modules
def test_module_import_tester(): """ Tests importing modules in various contexts. """ # When the module is installed and we test assuming that it is installed. # Import should succeed. import pytest importlib.reload(pytest) with futi.module_import_tester('pytest', when_missing=True): # Import should fail. with pytest.raises(ImportError) as excinfo: importlib.reload(pytest) assert 'module pytest not in sys.modules' in str(excinfo.value) with pytest.raises(ImportError) as excinfo: import pytest assert 'No module named \'pytest\'' in str(excinfo.value) # import should succeed. import pytest importlib.reload(pytest) # When the module is installed and we pretend that it is NOT installed. # Import should succeed. import pytest importlib.reload(pytest) with futi.module_import_tester('existing_module', when_missing=False): # Import should succeed. importlib.reload(sys) import pytest # Import should succeed. import pytest importlib.reload(pytest) missing_mod = 'gibberish_module_42' missing_mod_exception = 'No module named \'{}\''.format(missing_mod) # When the module is NOT installed and we test assuming that it is there. # Import should fail. with pytest.raises(ImportError) as excinfo: import gibberish_module_42 assert missing_mod_exception in str(excinfo.value) with pytest.raises(ImportError) as excinfo: importlib.import_module(missing_mod) assert missing_mod_exception in str(excinfo.value) with futi.module_import_tester(missing_mod, when_missing=True): # Import should fail. with pytest.raises(ImportError) as excinfo: import gibberish_module_42 assert missing_mod_exception in str(excinfo.value) with pytest.raises(ImportError) as excinfo: importlib.import_module(missing_mod) assert missing_mod_exception in str(excinfo.value) # Import should fail. with pytest.raises(ImportError) as excinfo: import gibberish_module_42 assert missing_mod_exception in str(excinfo.value) with pytest.raises(ImportError) as excinfo: importlib.import_module(missing_mod) assert missing_mod_exception in str(excinfo.value) # When the module is NOT installed and we assume that it is NOT installed. # Import should fail. with pytest.raises(ImportError) as excinfo: import gibberish_module_42 assert missing_mod_exception in str(excinfo.value) with pytest.raises(ImportError) as excinfo: importlib.import_module(missing_mod) assert missing_mod_exception in str(excinfo.value) with futi.module_import_tester(missing_mod, when_missing=False): # Import should succeed. import gibberish_module_42 with futi.module_import_tester(missing_mod, when_missing=False): # Import should succeed. importlib.import_module(missing_mod) # Import should fail. with pytest.raises(ImportError) as excinfo: import gibberish_module_42 assert missing_mod_exception in str(excinfo.value) with pytest.raises(ImportError) as excinfo: importlib.import_module(missing_mod) assert missing_mod_exception in str(excinfo.value)