-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_profile_loader.py
More file actions
109 lines (87 loc) · 2.56 KB
/
test_profile_loader.py
File metadata and controls
109 lines (87 loc) · 2.56 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
from dataclasses import dataclass
from uuid import uuid4
import pytest
from injection import find_instance, injectable, mod
from injection.loaders import ProfileLoader
class TestProfileLoader:
def test_required_module_names_with_success_return_frozenset(self):
loader = ProfileLoader(
{
mod().name: ["a", "b", "c"],
"dev": ["m", "n", "o"],
"b": ["x", "y", "z"],
"c": ["i", "j"],
}
)
assert loader.required_module_names() == {
mod().name,
"a",
"b",
"c",
"x",
"y",
"z",
"i",
"j",
}
def test_required_module_names_with_name_return_frozenset(self):
loader = ProfileLoader(
{
mod().name: ["a"],
"dev": ["z"],
"test": ["i"],
}
)
assert loader.required_module_names("dev") == {
mod().name,
"dev",
"a",
"z",
}
def test_load_with_success(self):
profile_name = "test"
global_profile_name = uuid4().hex
@mod(global_profile_name).constant
class GlobalConfig: ...
@injectable
@dataclass
class A:
config: GlobalConfig
@mod(profile_name).injectable(on=A)
class B(A): ...
loader = ProfileLoader(
{
mod().name: [global_profile_name],
profile_name: [global_profile_name],
}
)
with pytest.raises(TypeError):
find_instance(A)
loader.init()
assert type(find_instance(A)) is A
loaded_profile = loader.load(profile_name)
assert type(find_instance(A)) is B
# Cleaning
loaded_profile.unload()
def test_load_with_context_manager(self):
profile_name = "test"
global_profile_name = uuid4().hex
@mod(global_profile_name).constant
class GlobalConfig: ...
@injectable
@dataclass
class A:
config: GlobalConfig
@mod(profile_name).injectable(on=A)
class B(A): ...
loader = ProfileLoader(
{
mod().name: [global_profile_name],
profile_name: [global_profile_name],
}
)
loader.init()
assert type(find_instance(A)) is A
with loader.load(profile_name):
assert type(find_instance(A)) is B
assert type(find_instance(A)) is A