From cfd5241b4ae7c8a8abf3507ce6e505986d63b500 Mon Sep 17 00:00:00 2001 From: RomanZhukov Date: Thu, 23 Jan 2025 00:39:35 +0500 Subject: [PATCH] tests revision --- aiohttp_devtools/runserver/watch.py | 2 +- main.py | 22 ---------------------- tests/test_runserver_config.py | 12 ++++++++---- tests/test_runserver_main.py | 19 ++++++++++++------- tests/test_runserver_watch.py | 2 ++ 5 files changed, 23 insertions(+), 34 deletions(-) delete mode 100755 main.py diff --git a/aiohttp_devtools/runserver/watch.py b/aiohttp_devtools/runserver/watch.py index 5f575c9f..84125721 100644 --- a/aiohttp_devtools/runserver/watch.py +++ b/aiohttp_devtools/runserver/watch.py @@ -107,7 +107,7 @@ async def _src_reload_when_live(self, checks: int) -> None: assert self._app is not None and self._session is not None if self._app[WS]: - url = "{0.protocol}}://{0.host}:{0.main_port}/?_checking_alive=1".format(self._config) + url = "{0.protocol}://{0.host}:{0.main_port}/?_checking_alive=1".format(self._config) logger.debug('checking app at "%s" is running before prompting reload...', url) for i in range(checks): await asyncio.sleep(0.1) diff --git a/main.py b/main.py deleted file mode 100755 index 438e5c88..00000000 --- a/main.py +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env python -from aiohttp import web -from aiohttp_devtools import cli -import ssl - -async def hello(request): - return web.Response(text="

hello world

", content_type="text/html") - -async def create_app(): - a = web.Application() - a.router.add_get("/", hello) - return a - -def get_ssl_context(): - ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) - ssl_context.load_cert_chain('/home/roman/Projects/SemanticStoreSuite/SSArchive/SS_PWA&UI_Resarch/certs/server.crt', - '/home/roman/Projects/SemanticStoreSuite/SSArchive/SS_PWA&UI_Resarch/certs/server.key') - return ssl_context - # return False - -if __name__ == '__main__': - cli.cli() \ No newline at end of file diff --git a/tests/test_runserver_config.py b/tests/test_runserver_config.py index ec9c2ce3..a719adf8 100644 --- a/tests/test_runserver_config.py +++ b/tests/test_runserver_config.py @@ -36,7 +36,8 @@ async def test_create_app_wrong_name(tmpworkdir): mktree(tmpworkdir, SIMPLE_APP) config = Config(app_path='app.py', app_factory_name='missing') with pytest.raises(AiohttpDevConfigError) as excinfo: - config.get_app_factory() + module = config.import_module + config.get_app_factory(module) assert excinfo.value.args[0] == "Module 'app.py' does not define a 'missing' attribute/class" @@ -56,7 +57,8 @@ async def app_factory(): """ }) config = Config(app_path='app.py') - app = await config.load_app(config.get_app_factory()) + module = config.import_module() + app = await config.load_app(config.get_app_factory(module)) assert isinstance(app, web.Application) @@ -69,9 +71,10 @@ def app_factory(): """ }) config = Config(app_path='app.py') + module = config.import_module() with pytest.raises(AiohttpDevConfigError, match=r"'app_factory' returned 'int' not an aiohttp\.web\.Application"): - await config.load_app(config.get_app_factory()) + await config.load_app(config.get_app_factory(module)) @forked @@ -83,6 +86,7 @@ def app_factory(foo): """ }) config = Config(app_path='app.py') + module = config.import_module() with pytest.raises(AiohttpDevConfigError, match=r"'app\.py\.app_factory' should not have required arguments"): - await config.load_app(config.get_app_factory()) + await config.load_app(config.get_app_factory(module)) diff --git a/tests/test_runserver_main.py b/tests/test_runserver_main.py index 6368352b..a2f4602f 100644 --- a/tests/test_runserver_main.py +++ b/tests/test_runserver_main.py @@ -145,7 +145,8 @@ async def create_app(): set_start_method("spawn") config = Config(app_path="app.py", root_path=tmpworkdir, main_port=0, app_factory_name="create_app") - config.get_app_factory() + module = config.import_module() + config.get_app_factory(module) app_task = AppTask(config) app_task._start_dev_server() @@ -162,7 +163,8 @@ async def create_app(): async def test_run_app_aiohttp_client(tmpworkdir, aiohttp_client): mktree(tmpworkdir, SIMPLE_APP) config = Config(app_path='app.py') - app_factory = config.get_app_factory() + module = config.import_module() + app_factory = config.get_app_factory(module) app = await config.load_app(app_factory) modify_main_app(app, config) assert isinstance(app, aiohttp.web.Application) @@ -178,7 +180,8 @@ async def test_run_app_aiohttp_client(tmpworkdir, aiohttp_client): async def test_run_app_browser_cache(tmpworkdir, aiohttp_client): mktree(tmpworkdir, SIMPLE_APP) config = Config(app_path="app.py", browser_cache=True) - app_factory = config.get_app_factory() + module = config.import_module() + app_factory = config.get_app_factory(module) app = await config.load_app(app_factory) modify_main_app(app, config) cli = await aiohttp_client(app) @@ -208,8 +211,9 @@ async def test_serve_main_app(tmpworkdir, mocker): loop.call_later(0.5, loop.stop) config = Config(app_path="app.py", main_port=0) - runner = await create_main_app(config, config.get_app_factory()) - await start_main_app(runner, config.bind_address, config.main_port) + module = config.import_module() + runner = await create_main_app(config, config.get_app_factory(module)) + await start_main_app(runner, config.bind_address, config.main_port, None) mock_modify_main_app.assert_called_with(mock.ANY, config) @@ -232,8 +236,9 @@ async def hello(request): mock_modify_main_app = mocker.patch('aiohttp_devtools.runserver.serve.modify_main_app') config = Config(app_path="app.py", main_port=0) - runner = await create_main_app(config, config.get_app_factory()) - await start_main_app(runner, config.bind_address, config.main_port) + module = config.import_module() + runner = await create_main_app(config, config.get_app_factory(module)) + await start_main_app(runner, config.bind_address, config.main_port, None) mock_modify_main_app.assert_called_with(mock.ANY, config) diff --git a/tests/test_runserver_watch.py b/tests/test_runserver_watch.py index b8373d68..d733b97e 100644 --- a/tests/test_runserver_watch.py +++ b/tests/test_runserver_watch.py @@ -77,6 +77,7 @@ async def test_python_no_server(mocker): config = MagicMock() config.main_port = 8000 + config.protocol = 'http' app_task = AppTask(config) start_mock = mocker.patch.object(app_task, "_start_dev_server", autospec=True) stop_mock = mocker.patch.object(app_task, "_stop_dev_server", autospec=True) @@ -109,6 +110,7 @@ async def test_reload_server_running(aiohttp_client, mocker): config = MagicMock() config.host = "localhost" config.main_port = cli.server.port + config.protocol = 'http' app_task = AppTask(config) app_task._app = app