-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_injection_types.py
More file actions
124 lines (90 loc) · 3.78 KB
/
test_injection_types.py
File metadata and controls
124 lines (90 loc) · 3.78 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
122
123
124
#!/usr/bin/env python3
"""Quick test script for injection_types module."""
from bevy.injection_types import (
Inject, Options, InjectionStrategy, TypeMatchingStrategy,
extract_injection_info, is_optional_type, get_non_none_type
)
class UserService:
pass
class Database:
pass
def test_inject_type_alias():
"""Test the Inject type alias works correctly."""
print("Testing Inject type alias...")
# Test basic Inject usage
def func1(service: Inject[UserService]): pass
def func2(service: Inject[UserService, Options(qualifier="primary")]): pass
# Extract info from annotations
_, sig1 = func1.__annotations__.popitem()
_, sig2 = func2.__annotations__.popitem()
type1, opts1 = extract_injection_info(sig1)
type2, opts2 = extract_injection_info(sig2)
print(f" func1: type={type1}, options={opts1}")
print(f" func2: type={type2}, options={opts2}")
assert type1 == UserService
assert opts1 is None
assert type2 == UserService
assert opts2.qualifier == "primary"
print(" ✓ Inject type alias works correctly")
def test_optional_types():
"""Test optional type detection."""
print("\nTesting optional type detection...")
def func1(service: UserService): pass
def func2(service: UserService | None): pass
def func3(service: Inject[UserService]): pass
def func4(service: Inject[UserService | None]): pass
# Test regular types
_, sig1 = func1.__annotations__.popitem()
_, sig2 = func2.__annotations__.popitem()
_, sig3 = func3.__annotations__.popitem()
_, sig4 = func4.__annotations__.popitem()
print(f" UserService optional: {is_optional_type(sig1)}")
print(f" UserService | None optional: {is_optional_type(sig2)}")
# Extract from Inject types
type3, _ = extract_injection_info(sig3)
type4, _ = extract_injection_info(sig4)
print(f" Inject[UserService] optional: {is_optional_type(type3)}")
print(f" Inject[UserService | None] optional: {is_optional_type(type4)}")
# Test non-None type extraction
non_none2 = get_non_none_type(sig2)
non_none4 = get_non_none_type(type4)
print(f" Non-None from UserService | None: {non_none2}")
print(f" Non-None from Inject[UserService | None]: {non_none4}")
assert not is_optional_type(sig1)
assert is_optional_type(sig2)
assert not is_optional_type(type3)
assert is_optional_type(type4)
assert non_none2 == UserService
assert non_none4 == UserService
print(" ✓ Optional type detection works correctly")
def test_options():
"""Test Options class."""
print("\nTesting Options class...")
opts1 = Options()
opts2 = Options(qualifier="primary")
opts3 = Options(qualifier="cache")
print(f" Empty options: {opts1}")
print(f" With qualifier: {opts2}")
print(f" With qualifier: {opts3}")
assert opts1.qualifier is None
assert opts2.qualifier == "primary"
assert opts3.qualifier == "cache"
print(" ✓ Options class works correctly")
def test_enums():
"""Test enum definitions."""
print("\nTesting enums...")
# Test injection strategies
assert InjectionStrategy.DEFAULT.value == "default"
assert InjectionStrategy.REQUESTED_ONLY.value == "requested_only"
print(f" InjectionStrategy.DEFAULT: {InjectionStrategy.DEFAULT}")
# Test type matching strategies
assert TypeMatchingStrategy.DEFAULT.value == "default"
assert TypeMatchingStrategy.SUBCLASS.value == "subclass"
print(f" TypeMatchingStrategy.DEFAULT: {TypeMatchingStrategy.DEFAULT}")
print(" ✓ Enums work correctly")
if __name__ == "__main__":
test_inject_type_alias()
test_optional_types()
test_options()
test_enums()
print("\n🎉 All tests passed!")