Skip to content

Commit

Permalink
Add unit tests for base ASGI app
Browse files Browse the repository at this point in the history
92e6b68
d2220ba
735618a
62d2e32

- Add unit tests for base ASGI app endpoints using Starlette TestClient
- Update base ASGI app with `PROCESS_MANAGER` environment variable
- Type-annotate base ASGI app instance as `Callable`

Test coverage of inboard/app/base/main.py: 0% -> 100%
  • Loading branch information
br3ndonland committed Sep 13, 2020
1 parent 62d2e32 commit ed03219
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
8 changes: 6 additions & 2 deletions inboard/app/base/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ async def __call__(
}
)
version = f"{sys.version_info.major}.{sys.version_info.minor}"
server = "Uvicorn" if bool(os.getenv("WITH_RELOAD")) else "Uvicorn, Gunicorn,"
server = (
"Uvicorn"
if os.getenv("PROCESS_MANAGER") == "uvicorn"
else "Uvicorn, Gunicorn,"
)
message = f"Hello World, from {server} and Python {version}!"
response: Dict = {"type": "http.response.body", "body": message.encode("utf-8")}
await send(response)
return response


app = App
app: Callable = App
26 changes: 26 additions & 0 deletions tests/app/test_main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import re
from typing import Dict, List

import pytest
from _pytest.monkeypatch import MonkeyPatch
from fastapi import FastAPI
from fastapi.testclient import TestClient
from starlette.applications import Starlette
Expand Down Expand Up @@ -76,6 +78,30 @@ class TestEndpoints:
- https://docs.pytest.org/en/latest/parametrize.html
"""

def test_get_asgi_uvicorn(
self, client_asgi: TestClient, monkeypatch: MonkeyPatch
) -> None:
"""Test `GET` request to base ASGI app set for Uvicorn without Gunicorn."""
monkeypatch.setenv("PROCESS_MANAGER", "uvicorn")
monkeypatch.setenv("WITH_RELOAD", "false")
assert os.getenv("PROCESS_MANAGER") == "uvicorn"
assert os.getenv("WITH_RELOAD") == "false"
response = client_asgi.get("/")
assert response.status_code == 200
assert response.text == "Hello World, from Uvicorn and Python 3.8!"

def test_get_asgi_uvicorn_gunicorn(
self, client_asgi: TestClient, monkeypatch: MonkeyPatch
) -> None:
"""Test `GET` request to base ASGI app set for Uvicorn with Gunicorn."""
monkeypatch.setenv("PROCESS_MANAGER", "gunicorn")
monkeypatch.setenv("WITH_RELOAD", "false")
assert os.getenv("PROCESS_MANAGER") == "gunicorn"
assert os.getenv("WITH_RELOAD") == "false"
response = client_asgi.get("/")
assert response.status_code == 200
assert response.text == "Hello World, from Uvicorn, Gunicorn, and Python 3.8!"

def test_get_root(self, clients: List[TestClient]) -> None:
"""Test a `GET` request to the root endpoint."""
for client in clients:
Expand Down

0 comments on commit ed03219

Please sign in to comment.