|
| 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 |
0 commit comments