Skip to content

Commit

Permalink
TEST: Test load(..., require=...) keyword arg and have_module() func
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Mar 14, 2023
1 parent 0644862 commit 87a1989
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lazy_loader/tests/test_lazy_loader.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import sys
import types
from unittest import mock

import pytest

import lazy_loader as lazy

try:
import importlib_metadata # noqa

have_importlib_metadata = True
except ImportError:
have_importlib_metadata = False


def test_lazy_import_basics():
math = lazy.load("math")
Expand Down Expand Up @@ -138,3 +146,32 @@ def test_stub_loading_errors(tmp_path):

with pytest.raises(ValueError, match="Cannot load imports from non-existent stub"):
lazy.attach_stub("name", "not a file")


def test_require_kwarg():
dot = "_" if have_importlib_metadata else "."
# Test with a module that definitely exists, behavior hinges on requirement
with mock.patch(f"importlib{dot}metadata.version") as version:
version.return_value = "1.0.0"
math = lazy.load("math", require="somepkg >= 2.0")
assert isinstance(math, lazy.DelayedImportErrorModule)

math = lazy.load("math", require="somepkg >= 1.0")
assert math.sin(math.pi) == pytest.approx(0, 1e-6)

# We can fail even after a successful import
math = lazy.load("math", require="somepkg >= 2.0")
assert isinstance(math, lazy.DelayedImportErrorModule)

# When a module can be loaded but the version can't be checked,
# raise a ValueError
with pytest.raises(ValueError):
lazy.load("math", require="somepkg >= 1.0")


def test_have_module():
math = lazy.load("math")
anything_not_real = lazy.load("anything_not_real")

assert lazy.have_module(math)
assert not lazy.have_module(anything_not_real)

0 comments on commit 87a1989

Please sign in to comment.