From a468c8ad6016eecf7261b857422f50b87aed9426 Mon Sep 17 00:00:00 2001 From: ducklet Date: Tue, 7 Dec 2021 23:54:06 +0100 Subject: [PATCH] add a few first tests for `/movies` web route --- poetry.lock | 20 +++++++++++++++++++- pyproject.toml | 1 + scripts/tests | 7 ++++++- tests/test_web.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 tests/test_web.py diff --git a/poetry.lock b/poetry.lock index 5d7df28..c0f6562 100644 --- a/poetry.lock +++ b/poetry.lock @@ -337,6 +337,20 @@ toml = "*" [package.extras] testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "requests", "xmlschema"] +[[package]] +name = "pytest-asyncio" +version = "0.16.0" +description = "Pytest support for asyncio." +category = "dev" +optional = false +python-versions = ">= 3.6" + +[package.dependencies] +pytest = ">=5.4.0" + +[package.extras] +testing = ["coverage", "hypothesis (>=5.7.1)"] + [[package]] name = "requests" version = "2.26.0" @@ -497,7 +511,7 @@ python-versions = "*" [metadata] lock-version = "1.1" python-versions = "^3.10" -content-hash = "d5c421072660832e6e0786b7bf183d1a0919221a87f91a82e55d060aac1af69f" +content-hash = "dfd40d7d9a6e61c9845c3f80365b95dfe9638c44f9261af21907cd6b070c1170" [metadata.files] aiosqlite = [ @@ -660,6 +674,10 @@ pytest = [ {file = "pytest-6.2.5-py3-none-any.whl", hash = "sha256:7310f8d27bc79ced999e760ca304d69f6ba6c6649c0b60fb0e04a4a77cacc134"}, {file = "pytest-6.2.5.tar.gz", hash = "sha256:131b36680866a76e6781d13f101efb86cf674ebb9762eb70d3082b6f29889e89"}, ] +pytest-asyncio = [ + {file = "pytest-asyncio-0.16.0.tar.gz", hash = "sha256:7496c5977ce88c34379df64a66459fe395cd05543f0a2f837016e7144391fcfb"}, + {file = "pytest_asyncio-0.16.0-py3-none-any.whl", hash = "sha256:5f2a21273c47b331ae6aa5b36087047b4899e40f03f18397c0e65fa5cca54e9b"}, +] requests = [ {file = "requests-2.26.0-py2.py3-none-any.whl", hash = "sha256:6c1246513ecd5ecd4528a0906f910e8f0f9c6b8ec72030dc9fd154dc1a6efd24"}, {file = "requests-2.26.0.tar.gz", hash = "sha256:b8aa58f8cf793ffd8782d3d8cb19e66ef36f7aba4353eec859e74678b01b07a7"}, diff --git a/pyproject.toml b/pyproject.toml index 088d48c..6889076 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,6 +21,7 @@ pytest = "^6.2.4" pyright = "*" black = "*" isort = "*" +pytest-asyncio = "^0.16.0" [build-system] requires = ["poetry-core>=1.0.0"] diff --git a/scripts/tests b/scripts/tests index b4bb899..56ff576 100755 --- a/scripts/tests +++ b/scripts/tests @@ -2,6 +2,11 @@ cd "$RUN_DIR" +dbfile="$RUN_DIR/tests.sqlite.local" + +trap 'rm "$dbfile"' EXIT TERM INT QUIT + [ -z "${DEBUG:-}" ] || set -x -exec python -m pytest "$@" +UNWIND_STORAGE="$dbfile" \ + python -m pytest "$@" diff --git a/tests/test_web.py b/tests/test_web.py new file mode 100644 index 0000000..1a6773c --- /dev/null +++ b/tests/test_web.py @@ -0,0 +1,45 @@ +from starlette.testclient import TestClient +import pytest + +from unwind import create_app +from unwind import db, models + +pytestmark = pytest.mark.asyncio + +app = create_app() + + +async def test_app(): + await db.open_connection_pool() + conn = db.shared_connection() + + async with conn.transaction(force_rollback=True): + + client = TestClient(app) + response = client.get("/api/v1/movies") + assert response.status_code == 403 + + client.auth = "user1", "secret1" + + response = client.get("/api/v1/movies") + assert response.status_code == 200 + assert response.json() == [] + + m = models.Movie( + title="test movie", + release_year=2013, + media_type="Movie", + imdb_id="tt12345678", + genres={"genre-1"}, + ) + await db.add(m) + + response = client.get("/api/v1/movies", params={"include_unrated": 1}) + assert response.status_code == 200 + assert response.json() == [{**db.asplain(m), "user_scores": []}] + + response = client.get("/api/v1/movies", params={"imdb_id": m.imdb_id}) + assert response.status_code == 200 + assert response.json() == [db.asplain(m)] + + await db.close_connection_pool()