-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_modules.py
More file actions
141 lines (117 loc) · 4.24 KB
/
Copy pathtest_modules.py
File metadata and controls
141 lines (117 loc) · 4.24 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import pytest
from ellar.common import Module
from ellar.common.constants import MODULE_METADATA, MODULE_WATERMARK
from ellar.common.exceptions import ImproperConfiguration
from ellar.di import has_binding, is_decorated_with_injectable
from ellar.reflect import reflect
@Module(
name="test",
static_folder="test",
controllers=["controllers"],
routers=["routers"],
providers=["providers"],
modules=["modules"],
)
class ModuleDecoratorTest:
pass
def test_module_decorator_keys_is_complete():
assert reflect.get_metadata(MODULE_METADATA.ROUTERS, ModuleDecoratorTest) == [
"routers"
]
assert reflect.get_metadata(MODULE_METADATA.NAME, ModuleDecoratorTest) == "test"
assert (
reflect.get_metadata(MODULE_METADATA.STATIC_FOLDER, ModuleDecoratorTest)
== "test"
)
assert reflect.get_metadata(MODULE_METADATA.PROVIDERS, ModuleDecoratorTest) == [
"providers"
]
assert reflect.get_metadata(MODULE_METADATA.CONTROLLERS, ModuleDecoratorTest) == [
"controllers"
]
assert reflect.get_metadata(MODULE_METADATA.MODULES, ModuleDecoratorTest) == [
"modules"
]
def test_decorated_class_has_module_watermark():
assert reflect.get_metadata(MODULE_WATERMARK, ModuleDecoratorTest)
def test_base_directory_is_dynamically_set_when_none():
base_path = reflect.get_metadata(
MODULE_METADATA.BASE_DIRECTORY, ModuleDecoratorTest
)
assert "/test_decorators" in str(base_path)
def test_cannot_decorate_module_class_twice():
with pytest.raises(
ImproperConfiguration, match="is already identified as a Module"
):
Module()(ModuleDecoratorTest)
def test_cannot_decorate_class_instance_as_a_module():
with pytest.raises(ImproperConfiguration, match="is a class decorator -"):
Module()(ModuleDecoratorTest())
def test_decorated_class_default_scope_is_singleton():
assert is_decorated_with_injectable(ModuleDecoratorTest)
assert not has_binding(ModuleDecoratorTest) # __init__ is not overridden
def test_module_decorator_does_not_override_module_metadata_if_existing():
class ExistingModule:
pass
reflect.define_metadata(
MODULE_METADATA.BASE_DIRECTORY, "/path/to/my/base/templates", ExistingModule
)
reflect.define_metadata(
MODULE_METADATA.TEMPLATE_FOLDER, "custom_template", ExistingModule
)
reflect.define_metadata(
MODULE_METADATA.STATIC_FOLDER, "static_custom", ExistingModule
)
Module(name="ExistingModule", controllers=["Controller1", "Controller2"])(
ExistingModule
)
data = reflect.get_all_metadata(ExistingModule)
assert data == {
"base_directory": "/path/to/my/base/templates",
"template_folder": "templates",
"static_folder": "static",
"MODULE_WATERMARK": True,
"name": "ExistingModule",
"controllers": ["Controller1", "Controller2"],
"routers": [],
"providers": [],
"modules": [],
"commands": [],
"exports": [],
"INJECTABLE_WATERMARK": True,
}
def test_module_decorator_override_module_metadata_if_provided_in_decorator():
class ExistingModule2:
pass
reflect.define_metadata(
MODULE_METADATA.BASE_DIRECTORY, "/path/to/my/base/templates", ExistingModule2
)
reflect.define_metadata(
MODULE_METADATA.TEMPLATE_FOLDER, "custom_template", ExistingModule2
)
reflect.define_metadata(
MODULE_METADATA.TEMPLATE_FOLDER, "custom_template", ExistingModule2
)
reflect.define_metadata(
MODULE_METADATA.STATIC_FOLDER, "ExistingModule2", ExistingModule2
)
Module(
name="ExistingModule",
controllers=["Controller1", "Controller2"],
base_directory="/path/to/another/base",
)(ExistingModule2)
data = reflect.get_all_metadata(ExistingModule2)
assert data == {
"base_directory": "/path/to/another/base",
"template_folder": "templates",
"static_folder": "static",
"MODULE_WATERMARK": True,
"name": "ExistingModule",
"controllers": ["Controller1", "Controller2"],
"routers": [],
"providers": [],
"modules": [],
"commands": [],
"exports": [],
"INJECTABLE_WATERMARK": True,
}