Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsmith committed Oct 26, 2024
1 parent dabd1a4 commit 30ccb62
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
1 change: 0 additions & 1 deletion core/src/toga/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import importlib
import importlib.util
import warnings
from pathlib import Path

Expand Down
38 changes: 38 additions & 0 deletions core/tests/test_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import sys

import pytest


def test_lazy_succeed(monkeypatch):
"""Submodules are imported on demand."""
for mod_name in ["toga", "toga.documents", "toga.widgets.button"]:
monkeypatch.delitem(sys.modules, mod_name, raising=False)

# A clean import of the top-level toga module should not import any submodules.
import toga

assert "toga.documents" not in sys.modules
assert "toga.widgets.button" not in sys.modules

# Accessing a name should import only the necessary submodules.
Button = toga.Button
assert "toga.widgets.button" in sys.modules
assert "toga.documents" not in sys.modules

# Accessing a name multiple times should return the same object.
assert Button is toga.Button
assert Button is sys.modules["toga.widgets.button"].Button

# Same again with a different module.
Document = toga.Document
assert Document is sys.modules["toga.documents"].Document


def test_lazy_fail():
"""Nonexistent names should raise a normal AttributeError."""
import toga

with pytest.raises(
AttributeError, match="module 'toga' has no attribute 'nonexistent'"
):
toga.nonexistent

0 comments on commit 30ccb62

Please sign in to comment.