def test_decorator_register_config_test(self): try: @fastapi_plugins.registered_configuration_test class MyConfig(fastapi_plugins.PluginSettings): api_name: str = 'API name' # exp = d2json(MyConfig().dict()) res = d2json( fastapi_plugins.get_config( fastapi_plugins.CONFIG_NAME_TEST).dict()) # noqa E501 self.assertTrue(res == exp, 'get configuration failed') # os.environ[ fastapi_plugins. DEFAULT_CONFIG_ENVVAR] = fastapi_plugins.CONFIG_NAME_TEST # noqa E501 try: exp = d2json(MyConfig().dict()) res = d2json(fastapi_plugins.get_config().dict()) self.assertTrue(res == exp, 'get configuration failed') finally: os.environ.pop(fastapi_plugins.DEFAULT_CONFIG_ENVVAR) finally: fastapi_plugins.reset_config()
def test_wrap_register_config_local(self): try: class MyConfig(fastapi_plugins.PluginSettings): api_name: str = 'API name' # fastapi_plugins.register_config_local(MyConfig) exp = d2json(MyConfig().dict()) res = d2json( fastapi_plugins.get_config( fastapi_plugins.CONFIG_NAME_LOCAL).dict()) # noqa E501 self.assertTrue(res == exp, 'get configuration failed') # os.environ[ fastapi_plugins. DEFAULT_CONFIG_ENVVAR] = fastapi_plugins.CONFIG_NAME_LOCAL # noqa E501 try: exp = d2json(MyConfig().dict()) res = d2json(fastapi_plugins.get_config().dict()) self.assertTrue(res == exp, 'get configuration failed') finally: os.environ.pop(fastapi_plugins.DEFAULT_CONFIG_ENVVAR) finally: fastapi_plugins.reset_config()
async def _test(): os.environ[ fastapi_plugins. DEFAULT_CONFIG_ENVVAR] = fastapi_plugins.CONFIG_NAME_LOCAL # noqa E501 try: @fastapi_plugins.registered_configuration class MyConfigDocker(fastapi_plugins.PluginSettings): api_name: str = 'docker' @fastapi_plugins.registered_configuration_local class MyConfigLocal(fastapi_plugins.PluginSettings): api_name: str = 'local' app = fastapi.FastAPI() config = fastapi_plugins.get_config() await fastapi_plugins.config_plugin.init_app(app=app, config=config ) # noqa E501 await fastapi_plugins.config_plugin.init() try: c = await fastapi_plugins.config_plugin() exp = d2json(MyConfigLocal().dict()) res = d2json(c.dict()) self.assertTrue(res == exp, 'get configuration failed: %s != %s' % (exp, res)) # noqa E501 finally: await fastapi_plugins.config_plugin.terminate() finally: os.environ.pop(fastapi_plugins.DEFAULT_CONFIG_ENVVAR) fastapi_plugins.reset_config()
async def _test(): @fastapi_plugins.registered_configuration class MyConfigDocker(fastapi_plugins.PluginSettings): api_name: str = 'docker' @fastapi_plugins.registered_configuration_local class MyConfigLocal(fastapi_plugins.PluginSettings): api_name: str = 'local' app = fastapi.FastAPI() config = fastapi_plugins.get_config() await fastapi_plugins.config_plugin.init_app(app=app, config=config ) # noqa E501 await fastapi_plugins.config_plugin.init() try: c = await fastapi_plugins.config_plugin() exp = d2json(MyConfigDocker().dict()) res = d2json(c.dict()) self.assertTrue(res == exp, 'get configuration failed') finally: await fastapi_plugins.config_plugin.terminate() fastapi_plugins.reset_config()
def test_decorator_register_config_docker(self): try: @fastapi_plugins.registered_configuration_docker class MyConfig(fastapi_plugins.PluginSettings): api_name: str = 'API name' # docker is default exp = d2json(MyConfig().dict()) res = d2json(fastapi_plugins.get_config().dict()) self.assertTrue(res == exp, 'get configuration failed') # exp = d2json(MyConfig().dict()) res = d2json( fastapi_plugins.get_config( fastapi_plugins.CONFIG_NAME_DOCKER).dict()) # noqa E501 self.assertTrue(res == exp, 'get configuration failed') finally: fastapi_plugins.reset_config()
def test_wrap_get_config(self): try: class MyConfig(fastapi_plugins.PluginSettings): api_name: str = 'API name' fastapi_plugins.register_config(MyConfig) exp = d2json(MyConfig().dict()) res = d2json(fastapi_plugins.get_config().dict()) self.assertTrue(res == exp, 'get configuration failed') finally: fastapi_plugins.reset_config()
def test_decorator_register_config_by_name(self): try: name = 'myconfig' @fastapi_plugins.registered_configuration(name=name) class MyConfig(fastapi_plugins.PluginSettings): api_name: str = 'API name' exp = d2json(MyConfig().dict()) res = d2json(fastapi_plugins.get_config(name).dict()) self.assertTrue(res == exp, 'get configuration failed') finally: fastapi_plugins.reset_config()
def test_decorator_register_config(self): try: @fastapi_plugins.registered_configuration class MyConfig(fastapi_plugins.PluginSettings): api_name: str = 'API name' exp = d2json(MyConfig().dict()) res = d2json(fastapi_plugins.get_config().dict()) self.assertTrue(res == exp, 'get configuration failed: %s != %s' % (exp, res)) # noqa E501 finally: fastapi_plugins.reset_config()
api_name: str = str(__name__) @fastapi_plugins.registered_configuration(name='sentinel') class AppSettingsSentinel(AppSettings): redis_type = fastapi_plugins.RedisType.sentinel redis_sentinels = 'localhost:26379' @fastapi_plugins.registered_configuration_local class AppSettingsLocal(AppSettings): pass app = fastapi.FastAPI() config = fastapi_plugins.get_config() @app.get("/") async def root_get( cache: aioredis.Redis = fastapi.Depends(fastapi_plugins.depends_redis), conf: pydantic.BaseSettings = fastapi.Depends( fastapi_plugins.depends_config) # noqa E501 ) -> typing.Dict: return dict(ping=await cache.ping(), api_name=conf.api_name) @app.post("/jobs/schedule/<timeout>") async def job_post( timeout: int = fastapi.Query(..., title='the job sleep time'), cache: aioredis.Redis = fastapi.Depends(fastapi_plugins.depends_redis),