-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathtest_lazy_imports.py
More file actions
121 lines (91 loc) · 4.18 KB
/
test_lazy_imports.py
File metadata and controls
121 lines (91 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
Tests for lazy import behavior.
These tests verify that heavy dependencies (networkx, matplotlib, click)
are not loaded until their associated features are accessed.
"""
import sys
def test_lazy_diagram_import():
"""Diagram module should not be loaded until dj.Diagram is accessed."""
# Remove datajoint from sys.modules to get fresh import
modules_to_remove = [key for key in sys.modules if key.startswith("datajoint")]
for mod in modules_to_remove:
del sys.modules[mod]
# Import datajoint
import datajoint as dj
# Diagram module should not be loaded yet
assert "datajoint.diagram" not in sys.modules, "diagram module loaded eagerly"
# Access Diagram - should trigger lazy load
Diagram = dj.Diagram
assert "datajoint.diagram" in sys.modules, "diagram module not loaded after access"
assert Diagram.__name__ == "Diagram"
def test_lazy_admin_import():
"""Admin module should not be loaded until dj.kill is accessed."""
# Remove datajoint from sys.modules to get fresh import
modules_to_remove = [key for key in sys.modules if key.startswith("datajoint")]
for mod in modules_to_remove:
del sys.modules[mod]
# Import datajoint
import datajoint as dj
# Admin module should not be loaded yet
assert "datajoint.admin" not in sys.modules, "admin module loaded eagerly"
# Access kill - should trigger lazy load
kill = dj.kill
assert "datajoint.admin" in sys.modules, "admin module not loaded after access"
assert callable(kill)
def test_lazy_cli_import():
"""CLI module should not be loaded until dj.cli is accessed."""
# Remove datajoint from sys.modules to get fresh import
modules_to_remove = [key for key in sys.modules if key.startswith("datajoint")]
for mod in modules_to_remove:
del sys.modules[mod]
# Import datajoint
import datajoint as dj
# CLI module should not be loaded yet
assert "datajoint.cli" not in sys.modules, "cli module loaded eagerly"
# Access cli - should trigger lazy load and return the function
cli_func = dj.cli
assert "datajoint.cli" in sys.modules, "cli module not loaded after access"
assert callable(cli_func), "dj.cli should be callable (the cli function)"
def test_diagram_module_access():
"""dj.diagram should return the diagram module for accessing module-level attrs."""
# Remove datajoint from sys.modules to get fresh import
modules_to_remove = [key for key in sys.modules if key.startswith("datajoint")]
for mod in modules_to_remove:
del sys.modules[mod]
import datajoint as dj
# Access dj.diagram should return the module
diagram_module = dj.diagram
assert hasattr(diagram_module, "diagram_active"), "diagram module should have diagram_active"
assert hasattr(diagram_module, "Diagram"), "diagram module should have Diagram class"
def test_diagram_aliases():
"""Di and ERD should be aliases for Diagram."""
# Remove datajoint from sys.modules to get fresh import
modules_to_remove = [key for key in sys.modules if key.startswith("datajoint")]
for mod in modules_to_remove:
del sys.modules[mod]
import datajoint as dj
# All aliases should resolve to the same class
assert dj.Diagram is dj.Di
assert dj.Diagram is dj.ERD
def test_core_imports_available():
"""Core functionality should be available immediately after import."""
# Remove datajoint from sys.modules to get fresh import
modules_to_remove = [key for key in sys.modules if key.startswith("datajoint")]
for mod in modules_to_remove:
del sys.modules[mod]
import datajoint as dj
# Core classes should be available without triggering lazy loads
assert hasattr(dj, "Schema")
assert hasattr(dj, "Table")
assert hasattr(dj, "Manual")
assert hasattr(dj, "Lookup")
assert hasattr(dj, "Computed")
assert hasattr(dj, "Imported")
assert hasattr(dj, "Part")
assert hasattr(dj, "Connection")
assert hasattr(dj, "config")
assert hasattr(dj, "errors")
# Heavy modules should still not be loaded
assert "datajoint.diagram" not in sys.modules
assert "datajoint.admin" not in sys.modules
assert "datajoint.cli" not in sys.modules