feat(FastAPI): add get_(errors|successes) testing HTML helpers

These functions will allow us to more easily check errors or success
messages when testing routes.

Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-10-09 22:00:18 -07:00
parent 34c96ed81b
commit 27fbda5e7b
No known key found for this signature in database
GPG key ID: F7E46DED420788F3
2 changed files with 32 additions and 1 deletions

View file

@ -1,4 +1,5 @@
from io import StringIO from io import StringIO
from typing import List
from lxml import etree from lxml import etree
@ -12,3 +13,13 @@ def parse_root(html: str) -> etree.Element:
:return: etree.Element :return: etree.Element
""" """
return etree.parse(StringIO(html), parser) return etree.parse(StringIO(html), parser)
def get_errors(content: str) -> List[etree._Element]:
root = parse_root(content)
return root.xpath('//ul[@class="errorlist"]/li')
def get_successes(content: str) -> List[etree._Element]:
root = parse_root(content)
return root.xpath('//ul[@class="success"]/li')

View file

@ -9,7 +9,7 @@ from aurweb import asgi, db
from aurweb.models.account_type import TRUSTED_USER_ID, USER_ID, AccountType from aurweb.models.account_type import TRUSTED_USER_ID, USER_ID, AccountType
from aurweb.models.user import User from aurweb.models.user import User
from aurweb.testing import setup_test_db from aurweb.testing import setup_test_db
from aurweb.testing.html import parse_root from aurweb.testing.html import get_errors, get_successes, parse_root
from aurweb.testing.requests import Request from aurweb.testing.requests import Request
@ -97,3 +97,23 @@ def test_archdev_navbar_authenticated_tu(client: TestClient,
items = root.xpath('//div[@id="archdev-navbar"]/ul/li/a') items = root.xpath('//div[@id="archdev-navbar"]/ul/li/a')
for i, item in enumerate(items): for i, item in enumerate(items):
assert item.text.strip() == expected[i] assert item.text.strip() == expected[i]
def test_get_errors():
html = """
<ul class="errorlist">
<li>Test</li>
</ul>
"""
errors = get_errors(html)
assert errors[0].text.strip() == "Test"
def test_get_successes():
html = """
<ul class="success">
<li>Test</li>
</ul>
"""
successes = get_successes(html)
assert successes[0].text.strip() == "Test"