Skip to content

Commit

Permalink
Improve a bit the test
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex committed Sep 26, 2024
1 parent 01a96a8 commit 8188ab8
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions tests/test_lifespan.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from typing import Literal

import pytest
from quart import Quart
Expand All @@ -7,6 +8,7 @@

from mangum import Mangum
from mangum.exceptions import LifespanFailure
from mangum.types import Receive, Scope, Send


@pytest.mark.parametrize(
Expand Down Expand Up @@ -210,25 +212,22 @@ async def app(scope, receive, send):


@pytest.mark.parametrize(
"mock_aws_api_gateway_event,lifespan_state,lifespan",
[
(["GET", None, None], {"test_key": "test_value"}, "auto"),
(["GET", None, None], {"test_key": "test_value"}, "on"),
],
"mock_aws_api_gateway_event,lifespan",
[(["GET", None, None], "auto"), (["GET", None, None], "on")],
indirect=["mock_aws_api_gateway_event"],
)
def test_lifespan_state(mock_aws_api_gateway_event, lifespan_state, lifespan) -> None:
def test_lifespan_state(mock_aws_api_gateway_event, lifespan: Literal["on", "auto"]) -> None:
startup_complete = False
shutdown_complete = False

async def app(scope, receive, send):
async def app(scope: Scope, receive: Receive, send: Send):
nonlocal startup_complete, shutdown_complete

if scope["type"] == "lifespan":
while True:
message = await receive()
if message["type"] == "lifespan.startup":
scope["state"].update(lifespan_state)
scope["state"].update({"test_key": b"Hello, world!"})
await send({"type": "lifespan.startup.complete"})
startup_complete = True
elif message["type"] == "lifespan.shutdown":
Expand All @@ -237,15 +236,14 @@ async def app(scope, receive, send):
return

if scope["type"] == "http":
assert lifespan_state.items() <= scope["state"].items()
await send(
{
"type": "http.response.start",
"status": 200,
"headers": [[b"content-type", b"text/plain; charset=utf-8"]],
}
)
await send({"type": "http.response.body", "body": b"Hello, world!"})
await send({"type": "http.response.body", "body": scope["state"]["test_key"]})

handler = Mangum(app, lifespan=lifespan)
response = handler(mock_aws_api_gateway_event, {})
Expand Down

0 comments on commit 8188ab8

Please sign in to comment.