53 lines
1.5 KiB
Python
53 lines
1.5 KiB
Python
|
import sys
|
||
|
import types
|
||
|
|
||
|
import pytest
|
||
|
|
||
|
from pandas.compat._optional import VERSIONS, import_optional_dependency
|
||
|
|
||
|
import pandas._testing as tm
|
||
|
|
||
|
|
||
|
def test_import_optional():
|
||
|
match = "Missing .*notapackage.* pip .* conda .* notapackage"
|
||
|
with pytest.raises(ImportError, match=match):
|
||
|
import_optional_dependency("notapackage")
|
||
|
|
||
|
result = import_optional_dependency("notapackage", raise_on_missing=False)
|
||
|
assert result is None
|
||
|
|
||
|
|
||
|
def test_xlrd_version_fallback():
|
||
|
pytest.importorskip("xlrd")
|
||
|
import_optional_dependency("xlrd")
|
||
|
|
||
|
|
||
|
def test_bad_version(monkeypatch):
|
||
|
name = "fakemodule"
|
||
|
module = types.ModuleType(name)
|
||
|
module.__version__ = "0.9.0"
|
||
|
sys.modules[name] = module
|
||
|
monkeypatch.setitem(VERSIONS, name, "1.0.0")
|
||
|
|
||
|
match = "Pandas requires .*1.0.0.* of .fakemodule.*'0.9.0'"
|
||
|
with pytest.raises(ImportError, match=match):
|
||
|
import_optional_dependency("fakemodule")
|
||
|
|
||
|
with tm.assert_produces_warning(UserWarning):
|
||
|
result = import_optional_dependency("fakemodule", on_version="warn")
|
||
|
assert result is None
|
||
|
|
||
|
module.__version__ = "1.0.0" # exact match is OK
|
||
|
result = import_optional_dependency("fakemodule")
|
||
|
assert result is module
|
||
|
|
||
|
|
||
|
def test_no_version_raises(monkeypatch):
|
||
|
name = "fakemodule"
|
||
|
module = types.ModuleType(name)
|
||
|
sys.modules[name] = module
|
||
|
monkeypatch.setitem(VERSIONS, name, "1.0.0")
|
||
|
|
||
|
with pytest.raises(ImportError, match="Can't determine .* fakemodule"):
|
||
|
import_optional_dependency(name)
|