Skip to content

Commit

Permalink
regroup tests, start testing dunders
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidCEllis committed Nov 6, 2023
1 parent 26e4186 commit acc65ea
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 97 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ build/
_build/
dist/
htmlcov/
*.lcov
coverage.xml

.vscode/
__pycache__/
Expand Down
211 changes: 120 additions & 91 deletions tests/test_basic_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,94 +11,123 @@
)


def test_imports_lazy():
laz = LazyImporter([
ModuleImport("example_1"),
FromImport("example_2", "item", asname="i"),
])

assert "example_1" not in sys.modules
assert "example_2" not in sys.modules
laz.example_1
assert "example_1" in sys.modules
assert "example_2" not in sys.modules
laz.i
assert "example_2" in sys.modules

assert laz.i == "example"

# Check the imports are the correct objects
import example_1 # noqa
import example_2 # noqa

assert example_1 is laz.example_1
assert example_2.item is laz.i


def test_imports_submod():
laz_nosub = LazyImporter([
ModuleImport("ex_mod")
])

laz_sub = LazyImporter([
ModuleImport("ex_mod.ex_submod")
])

# Import ex_mod
assert laz_nosub.ex_mod.name == "ex_mod"

with pytest.raises(AttributeError):
laz_nosub.ex_mod.ex_submod

assert laz_sub.ex_mod.name == "ex_mod"
assert laz_sub.ex_mod.ex_submod.name == "ex_submod"


def test_submod_from():
laz = LazyImporter([
FromImport("ex_mod.ex_submod", "name"),
])

assert laz.name == "ex_submod"


def test_submod_multifrom():
laz = LazyImporter([
MultiFromImport("ex_mod.ex_submod", ["name", ("name2", "othername")]),
])

assert laz.name == "ex_submod"
assert laz.othername == "ex_submod2"


def test_try_except_import():
# When the first import fails
laz = LazyImporter([
TryExceptImport("module_does_not_exist", "ex_mod", "ex_mod"),
])

assert laz.ex_mod.name == "ex_mod"

# When the first import succeeds
laz2 = LazyImporter([
TryExceptImport("ex_mod", "ex_othermod", "ex_mod"),
])

assert laz2.ex_mod.name == "ex_mod"


def test_relative_import():
import example_modules.lazy_submod_ex as lse

laz = lse.lazy_submod_from_import()
assert laz.name == "ex_submod"

laz = lse.lazy_submod_multi_from_import()
assert laz.name == "ex_submod"
assert laz.othername == "ex_submod2"


def test_submod_relative_import():
from example_modules.ex_othermod import laz

assert laz.name == "ex_submod"
class TestDirectImports:
def test_module_import(self):
"""
Test Basic Module import occurs when expected
"""
laz = LazyImporter(
[
ModuleImport("example_1"),
]
)

assert "example_1" not in sys.modules
laz.example_1
assert "example_1" in sys.modules

# Check the import is the correct object
import example_1 # noqa # pyright: ignore

assert example_1 is laz.example_1

def test_from_import(self):
"""
Test a basic from import from a module
"""
laz = LazyImporter(
[
FromImport("example_2", "item", asname="i"),
]
)

assert "example_2" not in sys.modules
laz.i
assert "example_2" in sys.modules

assert laz.i == "example"

import example_2 # noqa # pyright: ignore

assert example_2.item is laz.i

def test_imports_submod(self):
"""
Test submodules are only imported when used
"""
laz_nosub = LazyImporter([ModuleImport("ex_mod")])

laz_sub = LazyImporter([ModuleImport("ex_mod.ex_submod")])

# Import ex_mod
assert laz_nosub.ex_mod.name == "ex_mod"

with pytest.raises(AttributeError):
laz_nosub.ex_mod.ex_submod

assert laz_sub.ex_mod.name == "ex_mod"
assert laz_sub.ex_mod.ex_submod.name == "ex_submod"

def test_submod_from(self):
"""
Test a from import from a submodule
"""
laz = LazyImporter(
[
FromImport("ex_mod.ex_submod", "name"),
]
)

assert laz.name == "ex_submod"

def test_submod_multifrom(self):
"""
Test a basic multi from import
"""
laz = LazyImporter(
[
MultiFromImport("ex_mod.ex_submod", ["name", ("name2", "othername")]),
]
)

assert laz.name == "ex_submod"
assert laz.othername == "ex_submod2"

def test_try_except_import(self):
"""
Test a basic try/except import
"""
# When the first import fails
laz = LazyImporter(
[
TryExceptImport("module_does_not_exist", "ex_mod", "ex_mod"),
]
)

assert laz.ex_mod.name == "ex_mod"

# When the first import succeeds
laz2 = LazyImporter(
[
TryExceptImport("ex_mod", "ex_othermod", "ex_mod"),
]
)

assert laz2.ex_mod.name == "ex_mod"


class TestImportsWithinExamples:
def test_relative_import(self):
import example_modules.lazy_submod_ex as lse

laz = lse.lazy_submod_from_import()
assert laz.name == "ex_submod"

laz = lse.lazy_submod_multi_from_import()
assert laz.name == "ex_submod"
assert laz.othername == "ex_submod2"

def test_submod_relative_import(self):
from example_modules.ex_othermod import laz

assert laz.name == "ex_submod"
57 changes: 51 additions & 6 deletions tests/test_internals.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,66 @@
import itertools

from ducktools.lazyimporter import (
ModuleImport,
FromImport,
MultiFromImport,
TryExceptImport,
LazyImporter,
_SubmoduleImports,
MultiFromImport,
)


def test_equal_module():
mod1 = ModuleImport("collections")
mod2 = ModuleImport("collections")
class TestImporterDunders:
def test_equal_module(self):
mod1 = ModuleImport("collections")
mod2 = ModuleImport("collections")

assert mod1 == mod2

mod2 = ModuleImport("collections", "c")

assert mod1 != mod2

def test_equal_from(self):
from1 = FromImport("collections", "namedtuple")
from2 = FromImport("collections", "namedtuple")

assert from1 == from2

from2 = FromImport("collections", "defaultdict")

assert from1 != from2

def test_equal_multifrom(self):
mf1 = MultiFromImport("collections", ["namedtuple", "defaultdict"])
mf2 = MultiFromImport("collections", ["namedtuple", "defaultdict"])

assert mf1 == mf2

mf2 = MultiFromImport("collections", ["namedtuple"])

assert mf1 != mf2

def test_equal_tryexcept(self):
te1 = TryExceptImport("tomllib", "tomli", "tomllib")
te2 = TryExceptImport("tomllib", "tomli", "tomllib")

assert te1 == te2

te2 = TryExceptImport("dataclasses", "attrs", "dataclasses")
assert te1 != te2

assert mod1 == mod2
def test_unequal_different_types(self):
mod1 = ModuleImport("collections")
from1 = FromImport("collections", "namedtuple")
mf1 = MultiFromImport("collections", ["namedtuple", "defaultdict"])
te1 = TryExceptImport("tomllib", "tomli", "tomllib")

mod2 = ModuleImport("collections", "c")
combs = itertools.combinations([mod1, from1, mf1, te1], 2)

assert mod1 != mod2
for i1, i2 in combs:
assert i1 != i2


def test_no_duplication():
Expand Down

0 comments on commit acc65ea

Please sign in to comment.