Skip to content

Commit

Permalink
add basic tests to demo site (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin authored Dec 11, 2023
1 parent 1f8313f commit acc26f0
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 13 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ jobs:
- run: pip install -r src/python-fastui/requirements/pyproject.txt
- run: pip install src/python-fastui

- run: make test
- run: coverage run -m pytest src

# test demo on 3.11 and 3.12, these tests are intentionally omitted from coverage
- if: matrix.python-version == '3.11' || matrix.python-version == '3.12'
run: pytest demo/tests.py

- run: coverage xml

Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ install:
update-lockfiles:
@echo "Updating requirements files using pip-compile"
pip-compile -q --strip-extras -o $(path)/requirements/lint.txt $(path)/requirements/lint.in
pip-compile -q --strip-extras -o $(path)/requirements/test.txt $(path)/requirements/test.in
pip-compile -q --strip-extras -o $(path)/requirements/pyproject.txt $(path)/pyproject.toml --extra=fastapi
pip-compile -q --strip-extras -o $(path)/requirements/pyproject.txt -c $(path)/requirements/lint.txt $(path)/pyproject.toml --extra=fastapi
pip-compile -q --strip-extras -o $(path)/requirements/test.txt -c $(path)/requirements/lint.txt -c $(path)/requirements/pyproject.txt $(path)/requirements/test.in
pip install --dry-run -r $(path)/requirements/all.txt

.PHONY: format
Expand Down
66 changes: 66 additions & 0 deletions demo/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import re

import pytest
from dirty_equals import IsList, IsStr
from fastapi.testclient import TestClient

from . import app

client = TestClient(app)


def test_index():
r = client.get('/')
assert r.status_code == 200, r.text
assert r.text.startswith('<!doctype html>\n')
assert r.headers.get('content-type') == 'text/html; charset=utf-8'


def test_api_root():
r = client.get('/api/')
assert r.status_code == 200
data = r.json()
assert data == [
{
'text': 'FastUI Demo',
'type': 'PageTitle',
},
{
'title': 'FastUI Demo',
'titleEvent': {'url': '/', 'type': 'go-to'},
'links': IsList(length=3),
'type': 'Navbar',
},
{
'components': [
{
'text': IsStr(regex='This site provides a demo of.*', regex_flags=re.DOTALL),
'type': 'Markdown',
},
],
'type': 'Page',
},
]


def get_menu_links():
"""
This is pretty cursory, we just go through the menu and load each page.
"""
r = client.get('/api/')
assert r.status_code == 200
data = r.json()
for link in data[1]['links']:
url = link['onClick']['url']
yield pytest.param(f'/api{url}', id=url)


@pytest.mark.parametrize('url', get_menu_links())
def test_menu_links(url: str):
r = client.get(url)
assert r.status_code == 200
data = r.json()
assert isinstance(data, list)


# TODO tests for forms, including submission
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ target-version = "py38"
include = ["src/python-fastui/fastui"]

[tool.pytest.ini_options]
testpaths = "src/python-fastui/tests"
testpaths = [
"src/python-fastui/tests",
"demo/tests.py",
]
xfail_strict = true
filterwarnings = ["error"]
asyncio_mode = "auto"
Expand Down
2 changes: 1 addition & 1 deletion src/python-fastui/requirements/pyproject.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile --extra=fastapi --output-file=src/python-fastui/requirements/pyproject.txt --strip-extras src/python-fastui/pyproject.toml
# pip-compile --constraint=src/python-fastui/requirements/lint.txt --extra=fastapi --output-file=src/python-fastui/requirements/pyproject.txt --strip-extras src/python-fastui/pyproject.toml
#
annotated-types==0.6.0
# via pydantic
Expand Down
1 change: 1 addition & 0 deletions src/python-fastui/requirements/test.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pytest
pytest-pretty
dirty-equals
pytest-asyncio
httpx
34 changes: 26 additions & 8 deletions src/python-fastui/requirements/test.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
#
# This file is autogenerated by pip-compile with Python 3.10
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile --output-file=src/python-fastui/requirements/test.txt --strip-extras src/python-fastui/requirements/test.in
# pip-compile --constraint=src/python-fastui/requirements/lint.txt --constraint=src/python-fastui/requirements/pyproject.txt --output-file=src/python-fastui/requirements/test.txt --strip-extras src/python-fastui/requirements/test.in
#
colorama==0.4.6
# via pytest
anyio==3.7.1
# via
# -c src/python-fastui/requirements/pyproject.txt
# httpx
certifi==2023.11.17
# via
# httpcore
# httpx
coverage==7.3.2
# via -r src/python-fastui/requirements/test.in
dirty-equals==0.7.1.post0
# via -r src/python-fastui/requirements/test.in
exceptiongroup==1.2.0
# via pytest
h11==0.14.0
# via httpcore
httpcore==1.0.2
# via httpx
httpx==0.25.2
# via -r src/python-fastui/requirements/test.in
idna==3.6
# via
# -c src/python-fastui/requirements/pyproject.txt
# anyio
# httpx
iniconfig==2.0.0
# via pytest
markdown-it-py==3.0.0
Expand All @@ -37,5 +52,8 @@ pytz==2023.3.post1
# via dirty-equals
rich==13.7.0
# via pytest-pretty
tomli==2.0.1
# via pytest
sniffio==1.3.0
# via
# -c src/python-fastui/requirements/pyproject.txt
# anyio
# httpx

0 comments on commit acc26f0

Please sign in to comment.