|
| 1 | +from abc import abstractmethod |
| 2 | + |
| 3 | +from starlette.routing import Host, Mount |
| 4 | + |
| 5 | +from ellar.constants import MODULE_METADATA |
| 6 | +from ellar.core import TestClientFactory |
| 7 | +from ellar.di import ProviderConfig |
| 8 | +from ellar.reflect import reflect |
| 9 | + |
| 10 | +from .sample import ( |
| 11 | + ApplicationModule, |
| 12 | + ClassBaseController, |
| 13 | + create_tmp_template_and_static_dir, |
| 14 | + router, |
| 15 | + sub_domain, |
| 16 | + users, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +class IFoo: |
| 21 | + @abstractmethod |
| 22 | + def get_name(self): |
| 23 | + pass |
| 24 | + |
| 25 | + @abstractmethod |
| 26 | + def get_full_name(self): |
| 27 | + pass |
| 28 | + |
| 29 | + |
| 30 | +class Foo(IFoo): |
| 31 | + def get_name(self): |
| 32 | + return "Ellar" |
| 33 | + |
| 34 | + def get_full_name(self): |
| 35 | + return "Ellar Python Framework" |
| 36 | + |
| 37 | + |
| 38 | +class MockFoo(IFoo): |
| 39 | + def get_name(self): |
| 40 | + return "whatever name" |
| 41 | + |
| 42 | + def get_full_name(self): |
| 43 | + return "some full name" |
| 44 | + |
| 45 | + |
| 46 | +def test_test_client_factory_create_test_module(tmpdir): |
| 47 | + create_tmp_template_and_static_dir(tmpdir) |
| 48 | + tm = TestClientFactory.create_test_module( |
| 49 | + controllers=(ClassBaseController,), |
| 50 | + routers=[ |
| 51 | + Host("{subdomain}.example.org", app=sub_domain), |
| 52 | + Mount("/users", app=users), |
| 53 | + router, |
| 54 | + ], |
| 55 | + template_folder="templates", |
| 56 | + static_folder="statics", |
| 57 | + base_directory=tmpdir, |
| 58 | + ) |
| 59 | + |
| 60 | + client = tm.get_client() |
| 61 | + res = client.get("/static/example.txt") |
| 62 | + assert res.status_code == 200 |
| 63 | + assert res.text == "<file content>" |
| 64 | + |
| 65 | + template = tm.app.jinja_environment.get_template("example.html") |
| 66 | + result = template.render() |
| 67 | + assert result == "<html>Hello World<html/>" |
| 68 | + |
| 69 | + client = tm.get_client(base_url="https://foo.example.org/") |
| 70 | + |
| 71 | + response = client.get("/") |
| 72 | + assert response.status_code == 200 |
| 73 | + assert response.text == "Subdomain: foo" |
| 74 | + |
| 75 | + |
| 76 | +def test_client_factory_create_test_module_from_module(): |
| 77 | + reflect.metadata( |
| 78 | + metadata_key=MODULE_METADATA.PROVIDERS, |
| 79 | + metadata_value=ProviderConfig(base_type=IFoo, use_class=Foo), |
| 80 | + )( |
| 81 | + ApplicationModule |
| 82 | + ) # dynamically add IFoo to ApplicationModule Providers |
| 83 | + |
| 84 | + tm = TestClientFactory.create_test_module_from_module( |
| 85 | + module=ApplicationModule, |
| 86 | + mock_providers=( |
| 87 | + ProviderConfig( |
| 88 | + base_type=IFoo, use_value=MockFoo() |
| 89 | + ), # force provider override |
| 90 | + ), |
| 91 | + ) |
| 92 | + |
| 93 | + client = tm.get_client(base_url="https://foo.example.org/") |
| 94 | + |
| 95 | + response = client.get("/") |
| 96 | + assert response.status_code == 200 |
| 97 | + assert response.text == "Subdomain: foo" |
| 98 | + |
| 99 | + ifoo: IFoo = tm.app.injector.get(IFoo) |
| 100 | + assert isinstance(ifoo, MockFoo) |
0 commit comments