3
0
Fork 0

feat: add support for running via uvicorn; wutta webapp command

This commit is contained in:
Lance Edgar 2024-12-18 11:28:08 -06:00
parent 3fabc0a141
commit b6d5ffa8ce
13 changed files with 408 additions and 28 deletions

0
tests/cli/__init__.py Normal file
View file

104
tests/cli/test_webapp.py Normal file
View file

@ -0,0 +1,104 @@
# -*- coding: utf-8; -*-
from unittest.mock import MagicMock, patch
from wuttjamaican.testing import ConfigTestCase
from wuttaweb.cli import webapp as mod
class TestWebapp(ConfigTestCase):
def make_context(self, **kwargs):
params = {'auto_reload': False}
params.update(kwargs.get('params', {}))
ctx = MagicMock(params=params)
ctx.parent.wutta_config = self.config
return ctx
def test_missing_config_file(self):
# nb. our default config has no files, so can test w/ that
ctx = self.make_context()
with patch.object(mod, 'sys') as sys:
sys.exit.side_effect = RuntimeError
self.assertRaises(RuntimeError, mod.webapp, ctx)
sys.stderr.write.assert_called_once_with("no config files found!\n")
sys.exit.assert_called_once_with(1)
def test_invalid_runner(self):
# make new config from file, with bad setting
path = self.write_file('my.conf', """
[wutta.web]
app.runner = bogus
""")
self.config = self.make_config(files=[path])
ctx = self.make_context()
with patch.object(mod, 'sys') as sys:
sys.exit.side_effect = RuntimeError
self.assertRaises(RuntimeError, mod.webapp, ctx)
sys.stderr.write.assert_called_once_with("unknown web app runner: bogus\n")
sys.exit.assert_called_once_with(2)
def test_pserve(self):
path = self.write_file('my.conf', """
[wutta.web]
app.runner = pserve
""")
self.config = self.make_config(files=[path])
# normal
with patch.object(mod, 'pserve') as pserve:
ctx = self.make_context()
mod.webapp(ctx)
pserve.main.assert_called_once_with(argv=['pserve', f'file+ini:{path}'])
# with reload
with patch.object(mod, 'pserve') as pserve:
ctx = self.make_context(params={'auto_reload': True})
mod.webapp(ctx)
pserve.main.assert_called_once_with(argv=['pserve', f'file+ini:{path}', '--reload'])
def test_uvicorn(self):
path = self.write_file('my.conf', """
[wutta.web]
app.runner = uvicorn
app.spec = wuttaweb.app:make_wsgi_app
""")
self.config = self.make_config(files=[path])
orig_import = __import__
uvicorn = MagicMock()
def mock_import(name, *args, **kwargs):
if name == 'uvicorn':
return uvicorn
return orig_import(name, *args, **kwargs)
# normal
with patch('builtins.__import__', side_effect=mock_import):
ctx = self.make_context()
mod.webapp(ctx)
uvicorn.run.assert_called_once_with('wuttaweb.app:make_wsgi_app',
host='127.0.0.1',
port=8000,
reload=False,
reload_dirs=None,
factory=False,
interface='auto')
# with reload
uvicorn.run.reset_mock()
with patch('builtins.__import__', side_effect=mock_import):
ctx = self.make_context(params={'auto_reload': True})
mod.webapp(ctx)
uvicorn.run.assert_called_once_with('wuttaweb.app:make_wsgi_app',
host='127.0.0.1',
port=8000,
reload=True,
reload_dirs=None,
factory=False,
interface='auto')

View file

@ -1,8 +1,9 @@
# -*- coding: utf-8; -*-
from unittest import TestCase
from unittest.mock import patch
from wuttjamaican.testing import FileConfigTestCase
from wuttjamaican.testing import FileTestCase, ConfigTestCase
from pyramid.config import Configurator
from pyramid.router import Router
@ -21,7 +22,7 @@ class TestWebAppProvider(TestCase):
handler = app.get_web_handler()
class TestMakeWuttaConfig(FileConfigTestCase):
class TestMakeWuttaConfig(FileTestCase):
def test_config_path_required(self):
@ -51,7 +52,7 @@ class TestMakePyramidConfig(TestCase):
self.assertIsInstance(config, Configurator)
class TestMain(FileConfigTestCase):
class TestMain(FileTestCase):
def test_basic(self):
global_config = None
@ -59,3 +60,43 @@ class TestMain(FileConfigTestCase):
settings = {'wutta.config': myconf}
app = mod.main(global_config, **settings)
self.assertIsInstance(app, Router)
def mock_main(global_config, **settings):
wutta_config = mod.make_wutta_config(settings)
pyramid_config = mod.make_pyramid_config(settings)
pyramid_config.include('wuttaweb.static')
pyramid_config.include('wuttaweb.subscribers')
pyramid_config.include('wuttaweb.views')
return pyramid_config.make_wsgi_app()
class TestMakeWsgiApp(ConfigTestCase):
def test_with_callable(self):
# specify config
wsgi = mod.make_wsgi_app(mock_main, config=self.config)
self.assertIsInstance(wsgi, Router)
# auto config
with patch.object(mod, 'make_config', return_value=self.config):
wsgi = mod.make_wsgi_app(mock_main)
self.assertIsInstance(wsgi, Router)
def test_with_spec(self):
# specify config
wsgi = mod.make_wsgi_app('tests.test_app:mock_main', config=self.config)
self.assertIsInstance(wsgi, Router)
# auto config
with patch.object(mod, 'make_config', return_value=self.config):
wsgi = mod.make_wsgi_app('tests.test_app:mock_main')
self.assertIsInstance(wsgi, Router)
def test_invalid(self):
self.assertRaises(ValueError, mod.make_wsgi_app, 42, config=self.config)