3
0
Fork 0

feat: add basic support for running in ASGI context

This commit is contained in:
Lance Edgar 2024-12-18 15:09:16 -06:00
parent b6d5ffa8ce
commit a132253cb1
3 changed files with 44 additions and 0 deletions

View file

@ -30,6 +30,7 @@ classifiers = [
]
requires-python = ">= 3.8"
dependencies = [
"asgiref",
"ColanderAlchemy",
"humanize",
"markdown",

View file

@ -30,6 +30,7 @@ import os
from wuttjamaican.app import AppProvider
from wuttjamaican.conf import make_config
from asgiref.wsgi import WsgiToAsgi
from pyramid.config import Configurator
import wuttaweb.db
@ -182,6 +183,8 @@ def make_wsgi_app(main_app=None, config=None):
"""
Make and return a WSGI app, using the given Paste app factory.
See also :func:`make_asgi_app()` for the ASGI equivalent.
This function could be used directly for general WSGI servers
(e.g. uvicorn), ***if*** you just want the built-in :func:`main()`
app factory.
@ -231,3 +234,14 @@ def make_wsgi_app(main_app=None, config=None):
# construct a pyramid app "per usual"
return make_wsgi_app({}, **settings)
def make_asgi_app(main_app=None, config=None):
"""
Make and return a ASGI app, using the given Paste app factory.
This works the same as :func:`make_wsgi_app()` and should be
called in the same way etc.
"""
wsgi_app = make_wsgi_app(main_app, config=config)
return WsgiToAsgi(wsgi_app)

View file

@ -5,6 +5,7 @@ from unittest.mock import patch
from wuttjamaican.testing import FileTestCase, ConfigTestCase
from asgiref.wsgi import WsgiToAsgi
from pyramid.config import Configurator
from pyramid.router import Router
@ -100,3 +101,31 @@ class TestMakeWsgiApp(ConfigTestCase):
def test_invalid(self):
self.assertRaises(ValueError, mod.make_wsgi_app, 42, config=self.config)
class TestMakeAsgiApp(ConfigTestCase):
def test_with_callable(self):
# specify config
asgi = mod.make_asgi_app(mock_main, config=self.config)
self.assertIsInstance(asgi, WsgiToAsgi)
# auto config
with patch.object(mod, 'make_config', return_value=self.config):
asgi = mod.make_asgi_app(mock_main)
self.assertIsInstance(asgi, WsgiToAsgi)
def test_with_spec(self):
# specify config
asgi = mod.make_asgi_app('tests.test_app:mock_main', config=self.config)
self.assertIsInstance(asgi, WsgiToAsgi)
# auto config
with patch.object(mod, 'make_config', return_value=self.config):
asgi = mod.make_asgi_app('tests.test_app:mock_main')
self.assertIsInstance(asgi, WsgiToAsgi)
def test_invalid(self):
self.assertRaises(ValueError, mod.make_asgi_app, 42, config=self.config)