-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_testing.py
More file actions
95 lines (72 loc) · 2.41 KB
/
Copy pathtest_testing.py
File metadata and controls
95 lines (72 loc) · 2.41 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
from abc import abstractmethod
from ellar.common.constants import MODULE_METADATA
from ellar.di import ProviderConfig
from ellar.reflect import reflect
from ellar.testing import Test
from jinja2 import Environment
from starlette.routing import Host, Mount
from .test_application.sample import (
ApplicationModule,
ClassBaseController,
create_tmp_template_and_static_dir,
router,
sub_domain,
users,
)
class IFoo:
@abstractmethod
def get_name(self):
pass
@abstractmethod
def get_full_name(self):
pass
class Foo(IFoo):
def get_name(self):
return "Ellar"
def get_full_name(self):
return "Ellar Python Framework"
class MockFoo(IFoo):
def get_name(self):
return "whatever name"
def get_full_name(self):
return "some full name"
def test_test_client_factory_create_test_module(tmpdir):
create_tmp_template_and_static_dir(tmpdir)
tm = Test.create_test_module(
controllers=(ClassBaseController,),
routers=[
Host("{subdomain}.example.org", app=sub_domain),
Mount("/users", app=users),
router,
],
template_folder="templates",
static_folder="statics",
base_directory=tmpdir,
)
client = tm.get_test_client()
res = client.get("/static/example.txt")
assert res.status_code == 200
assert res.text == "<file content>"
template = (
tm.create_application().injector.get(Environment).get_template("example.html")
)
result = template.render()
assert result == "<html>Hello World<html/>"
client = tm.get_test_client(base_url="https://foo.example.org/")
response = client.get("/")
assert response.status_code == 200
assert response.text == "Subdomain: foo"
def test_client_factory_create_test_module_from_module():
reflect.metadata(
metadata_key=MODULE_METADATA.PROVIDERS,
metadata_value=[ProviderConfig(base_type=IFoo, use_class=Foo)],
)(ApplicationModule) # dynamically add IFoo to ApplicationModule Providers
tm = Test.create_test_module(
modules=[ApplicationModule],
).override_provider(IFoo, use_value=MockFoo())
client = tm.get_test_client(base_url="https://foo.example.org/")
response = client.get("/")
assert response.status_code == 200
assert response.text == "Subdomain: foo"
ifoo: IFoo = tm.get(IFoo)
assert isinstance(ifoo, MockFoo)