App registry (#1979)

* Add app level registry

* Add documentation for app registry

* Remove unused import

* Add force_create keyword to Sanic.get_app

* Add force_commit to docs
This commit is contained in:
Adam Hopkins
2020-12-28 22:47:31 +02:00
committed by GitHub
parent 262f89f2b6
commit 449bc417a3
4 changed files with 90 additions and 20 deletions

View File

@@ -11,6 +11,7 @@ from sanic.router import RouteExists, Router
random.seed("Pack my box with five dozen liquor jugs.")
Sanic.test_mode = True
if sys.platform in ["win32", "cygwin"]:
collect_ignore = ["test_worker.py"]

View File

@@ -258,7 +258,7 @@ def test_handle_request_with_nested_sanic_exception(app, monkeypatch, caplog):
def test_app_name_required():
with pytest.deprecated_call():
with pytest.raises(SanicException):
Sanic()
@@ -274,14 +274,35 @@ def test_app_has_test_mode_sync():
assert response.status == 200
# @pytest.mark.asyncio
# async def test_app_has_test_mode_async():
# app = Sanic("test")
def test_app_registry():
instance = Sanic("test")
assert Sanic._app_registry["test"] is instance
# @app.get("/")
# async def handler(request):
# assert request.app.test_mode
# return text("test")
# _, response = await app.asgi_client.get("/")
# assert response.status == 200
def test_app_registry_wrong_type():
with pytest.raises(SanicException):
Sanic.register_app(1)
def test_app_registry_name_reuse():
Sanic("test")
Sanic.test_mode = False
with pytest.raises(SanicException):
Sanic("test")
Sanic.test_mode = True
def test_app_registry_retrieval():
instance = Sanic("test")
assert Sanic.get_app("test") is instance
def test_get_app_does_not_exist():
with pytest.raises(SanicException):
Sanic.get_app("does-not-exist")
def test_get_app_does_not_exist_force_create():
assert isinstance(
Sanic.get_app("does-not-exist", force_create=True), Sanic
)