def test_environment_can_be_changed_by_variable(self):
     import os
     os.environ["SERVICE_ENV"] = "apples"
     host = SrvHostBuilder("ut").build()
     del os.environ["SERVICE_ENV"]
     self.assertEqual(host.environment.environment_name, "apples")
 def test_environment_can_use_alternative_environment_variable(self):
     import os
     os.environ["MY_SRV_HOST_ENV_FOR_UT"] = "bananas"
     host = SrvHostBuilder("ut", "MY_SRV_HOST_ENV_FOR_UT").build()
     self.assertEqual(host.environment.environment_name, "bananas")
 def test_host_has_app_name(self):
     host = SrvHostBuilder("ut").build()
     self.assertEqual(host.environment.application_name, "ut")
 def test_environment_defaults_to_development(self):
     host = SrvHostBuilder("ut").build()
     self.assertEqual(host.environment.environment_name, "Development")
 def test_host_has_config(self):
     host = SrvHostBuilder("ut").build()
     self.assertIsInstance(host.configuration, IConfigSection)
 def test_host_has_environment(self):
     host = SrvHostBuilder("ut").build()
     self.assertIsInstance(host.environment, IHostingEnvironment)
 def test_host_has_service_provider(self):
     host = SrvHostBuilder("ut").build()
     self.assertIsInstance(host.service_provider, Container)
 def test_build_returns_builtin_host(self):
     host = SrvHostBuilder("ut").build()
     self.assertIsInstance(host, ISrvHost)
 def test_build_returns_my_host(self):
     host = SrvHostBuilder("ut") \
         .config_services(setup_services) \
         .build()
     self.assertIsInstance(host, HostMock)
        """ Scoped request Example:

        Note: this here is the service locator patter - This should be avoided.
        This is generally considered a bad practice but at the same time it is
        a very valid way to scope a request. Think of http requests where each
        request that comes in pulls its own dependency graph. If you do use
        this pattern, it should only happen at the top of your application.
        """
        print("Press CTRL+C to exit...")
        while True:
            name = input("Type your name: ")
            greeter: Service = self.service_provider.get(Service)
            greeter.greet(name)


def setup_config(ctx: ISrvHostContext, conf: IConfigFactory):
    conf.add_source(InMemorySource({"Greeting": "Hello"}))


def setup_services(ctx: ISrvHostContext, services: StaticContainerBuilder):
    services.bind(ISrvHost, Host)
    services.bind(Service, Service)


if __name__ == "__main__":
    SrvHostBuilder("Long running") \
        .config_configuration(setup_config) \
        .config_services(setup_services) \
        .build() \
        .run()
Exemple #11
0
from dnry.config import IConfigFactory
from dnry.config.in_memory import InMemorySource
from pyioc3 import StaticContainerBuilder

from dnry.srvhost.builder import SrvHostBase, ISrvHostContext, ISrvHost, SrvHostBuilder


class Host(SrvHostBase):
    def run(self):
        greeting = self.configuration.get("Greeting")
        name = self.configuration.get("Name")
        print(f"{greeting}, {name}!")


def setup_config(ctx: ISrvHostContext, conf: IConfigFactory):
    conf.add_source(InMemorySource({"Greeting": "Hello", "Name": "World"}))


def setup_services(ctx: ISrvHostContext, services: StaticContainerBuilder):
    services.bind(ISrvHost, Host)


if __name__ == "__main__":
    SrvHostBuilder("simple") \
        .config_configuration(setup_config) \
        .config_services(setup_services) \
        .build() \
        .run()