add unit test for error handling in build_config()

This commit is contained in:
Stefan Allius
2025-05-07 23:42:55 +02:00
parent e1f0aac9bf
commit fc93930656
3 changed files with 17 additions and 3 deletions

View File

@@ -137,7 +137,7 @@ class Server():
if config_err is not None: if config_err is not None:
logging.info(f'config_err: {config_err}') logging.info(f'config_err: {config_err}')
return # fixme raise an exception return
logging.info('******') logging.info('******')
@@ -239,7 +239,9 @@ async def healthy():
return Response(status=200, response="I'm fine") return Response(status=200, response="I'm fine")
async def handle_client(reader: StreamReader, writer: StreamWriter, inv_class): async def handle_client(reader: StreamReader,
writer: StreamWriter,
inv_class): # pragma: no cover
'''Handles a new incoming connection and starts an async loop''' '''Handles a new incoming connection and starts an async loop'''
with inv_class(reader, writer) as inv: with inv_class(reader, writer) as inv:
@@ -247,7 +249,7 @@ async def handle_client(reader: StreamReader, writer: StreamWriter, inv_class):
@app.before_serving @app.before_serving
async def startup_app(): async def startup_app(): # pragma: no cover
HypercornLogHndl.save() HypercornLogHndl.save()
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
Proxy.class_init() Proxy.class_init()

View File

@@ -0,0 +1 @@
mqtt.port = ":1883"

View File

@@ -123,6 +123,17 @@ class TestServerClass:
assert logging.getLogger('hypercorn.access').level == logging.INFO assert logging.getLogger('hypercorn.access').level == logging.INFO
assert logging.getLogger('hypercorn.error').level == logging.INFO assert logging.getLogger('hypercorn.error').level == logging.INFO
def test_build_config_error(self, caplog):
s = self.FakeServer()
s.src_dir = 'app/src/'
s.toml_config = 'app/tests/cnf/invalid_config.toml'
with caplog.at_level(logging.ERROR):
s.build_config()
assert "Can't read from app/tests/cnf/invalid_config.toml" in caplog.text
assert "Key 'port' error:" in caplog.text
class TestHypercornLogHndl: class TestHypercornLogHndl:
class FakeServer(Server): class FakeServer(Server):
def __init__(self): def __init__(self):