Skip to content

Commit acc26f0

Browse files
authored
add basic tests to demo site (#87)
1 parent 1f8313f commit acc26f0

File tree

7 files changed

+105
-13
lines changed

7 files changed

+105
-13
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ jobs:
6060
- run: pip install -r src/python-fastui/requirements/pyproject.txt
6161
- run: pip install src/python-fastui
6262

63-
- run: make test
63+
- run: coverage run -m pytest src
64+
65+
# test demo on 3.11 and 3.12, these tests are intentionally omitted from coverage
66+
- if: matrix.python-version == '3.11' || matrix.python-version == '3.12'
67+
run: pytest demo/tests.py
6468

6569
- run: coverage xml
6670

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ install:
1212
update-lockfiles:
1313
@echo "Updating requirements files using pip-compile"
1414
pip-compile -q --strip-extras -o $(path)/requirements/lint.txt $(path)/requirements/lint.in
15-
pip-compile -q --strip-extras -o $(path)/requirements/test.txt $(path)/requirements/test.in
16-
pip-compile -q --strip-extras -o $(path)/requirements/pyproject.txt $(path)/pyproject.toml --extra=fastapi
15+
pip-compile -q --strip-extras -o $(path)/requirements/pyproject.txt -c $(path)/requirements/lint.txt $(path)/pyproject.toml --extra=fastapi
16+
pip-compile -q --strip-extras -o $(path)/requirements/test.txt -c $(path)/requirements/lint.txt -c $(path)/requirements/pyproject.txt $(path)/requirements/test.in
1717
pip install --dry-run -r $(path)/requirements/all.txt
1818

1919
.PHONY: format

demo/tests.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import re
2+
3+
import pytest
4+
from dirty_equals import IsList, IsStr
5+
from fastapi.testclient import TestClient
6+
7+
from . import app
8+
9+
client = TestClient(app)
10+
11+
12+
def test_index():
13+
r = client.get('/')
14+
assert r.status_code == 200, r.text
15+
assert r.text.startswith('<!doctype html>\n')
16+
assert r.headers.get('content-type') == 'text/html; charset=utf-8'
17+
18+
19+
def test_api_root():
20+
r = client.get('/api/')
21+
assert r.status_code == 200
22+
data = r.json()
23+
assert data == [
24+
{
25+
'text': 'FastUI Demo',
26+
'type': 'PageTitle',
27+
},
28+
{
29+
'title': 'FastUI Demo',
30+
'titleEvent': {'url': '/', 'type': 'go-to'},
31+
'links': IsList(length=3),
32+
'type': 'Navbar',
33+
},
34+
{
35+
'components': [
36+
{
37+
'text': IsStr(regex='This site provides a demo of.*', regex_flags=re.DOTALL),
38+
'type': 'Markdown',
39+
},
40+
],
41+
'type': 'Page',
42+
},
43+
]
44+
45+
46+
def get_menu_links():
47+
"""
48+
This is pretty cursory, we just go through the menu and load each page.
49+
"""
50+
r = client.get('/api/')
51+
assert r.status_code == 200
52+
data = r.json()
53+
for link in data[1]['links']:
54+
url = link['onClick']['url']
55+
yield pytest.param(f'/api{url}', id=url)
56+
57+
58+
@pytest.mark.parametrize('url', get_menu_links())
59+
def test_menu_links(url: str):
60+
r = client.get(url)
61+
assert r.status_code == 200
62+
data = r.json()
63+
assert isinstance(data, list)
64+
65+
66+
# TODO tests for forms, including submission

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ target-version = "py38"
1212
include = ["src/python-fastui/fastui"]
1313

1414
[tool.pytest.ini_options]
15-
testpaths = "src/python-fastui/tests"
15+
testpaths = [
16+
"src/python-fastui/tests",
17+
"demo/tests.py",
18+
]
1619
xfail_strict = true
1720
filterwarnings = ["error"]
1821
asyncio_mode = "auto"

src/python-fastui/requirements/pyproject.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# This file is autogenerated by pip-compile with Python 3.11
33
# by the following command:
44
#
5-
# pip-compile --extra=fastapi --output-file=src/python-fastui/requirements/pyproject.txt --strip-extras src/python-fastui/pyproject.toml
5+
# 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
66
#
77
annotated-types==0.6.0
88
# via pydantic

src/python-fastui/requirements/test.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ pytest
33
pytest-pretty
44
dirty-equals
55
pytest-asyncio
6+
httpx

src/python-fastui/requirements/test.txt

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
#
2-
# This file is autogenerated by pip-compile with Python 3.10
2+
# This file is autogenerated by pip-compile with Python 3.11
33
# by the following command:
44
#
5-
# pip-compile --output-file=src/python-fastui/requirements/test.txt --strip-extras src/python-fastui/requirements/test.in
5+
# 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
66
#
7-
colorama==0.4.6
8-
# via pytest
7+
anyio==3.7.1
8+
# via
9+
# -c src/python-fastui/requirements/pyproject.txt
10+
# httpx
11+
certifi==2023.11.17
12+
# via
13+
# httpcore
14+
# httpx
915
coverage==7.3.2
1016
# via -r src/python-fastui/requirements/test.in
1117
dirty-equals==0.7.1.post0
1218
# via -r src/python-fastui/requirements/test.in
13-
exceptiongroup==1.2.0
14-
# via pytest
19+
h11==0.14.0
20+
# via httpcore
21+
httpcore==1.0.2
22+
# via httpx
23+
httpx==0.25.2
24+
# via -r src/python-fastui/requirements/test.in
25+
idna==3.6
26+
# via
27+
# -c src/python-fastui/requirements/pyproject.txt
28+
# anyio
29+
# httpx
1530
iniconfig==2.0.0
1631
# via pytest
1732
markdown-it-py==3.0.0
@@ -37,5 +52,8 @@ pytz==2023.3.post1
3752
# via dirty-equals
3853
rich==13.7.0
3954
# via pytest-pretty
40-
tomli==2.0.1
41-
# via pytest
55+
sniffio==1.3.0
56+
# via
57+
# -c src/python-fastui/requirements/pyproject.txt
58+
# anyio
59+
# httpx

0 commit comments

Comments
 (0)