mirror of
https://github.com/elastic/eland.git
synced 2025-07-11 00:02:14 +08:00
68 lines
2.1 KiB
Python
68 lines
2.1 KiB
Python
# Licensed to Elasticsearch B.V. under one or more contributor
|
|
# license agreements. See the NOTICE file distributed with
|
|
# this work for additional information regarding copyright
|
|
# ownership. Elasticsearch B.V. licenses this file to you under
|
|
# the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing,
|
|
# software distributed under the License is distributed on an
|
|
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
# KIND, either express or implied. See the License for the
|
|
# specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
import sys
|
|
import types
|
|
|
|
import pytest
|
|
|
|
from eland.ml._optional import VERSIONS, import_optional_dependency
|
|
|
|
|
|
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():
|
|
name = "fakemodule"
|
|
module = types.ModuleType(name)
|
|
module.__version__ = "0.9.0"
|
|
sys.modules[name] = module
|
|
VERSIONS[name] = "1.0.0"
|
|
|
|
match = "Eland requires .*1.0.0.* of .fakemodule.*'0.9.0'"
|
|
with pytest.raises(ImportError, match=match):
|
|
import_optional_dependency("fakemodule")
|
|
|
|
with pytest.warns(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():
|
|
name = "fakemodule"
|
|
module = types.ModuleType(name)
|
|
sys.modules[name] = module
|
|
VERSIONS[name] = "1.0.0"
|
|
|
|
with pytest.raises(ImportError, match="Can't determine .* fakemodule"):
|
|
import_optional_dependency(name)
|