forked from sponsfreixes/jinja2-fragments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_fastapi.py
73 lines (62 loc) · 2.38 KB
/
test_fastapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import re
import pytest
from jinja2_fragments import BlockNotFoundError
class TestFastAPIRenderBlock:
"""Tests each of the methods to make sure the html generated is
as expected. Removed whitespace and newline characters from html compare
to make it easier to compare output. I believe either encoding
or OS makes for odd variances otherwise.
"""
def test_simple_page(
self,
fastapi_client,
get_html,
):
response = fastapi_client.get("/simple_page")
html = get_html("simple_page.html")
assert html == response.text
def test_simple_page_content(
self,
fastapi_client,
get_html,
):
response = fastapi_client.get("/simple_page_content")
response_text = response.text.replace('"', "").strip("\n")
html = get_html("simple_page_content.html").strip("\n")
assert html == response_text
def test_nested_content(
self,
fastapi_client,
get_html,
):
response = fastapi_client.get("/nested_content")
response_text = re.sub(r"[\s\"]*", "", response.text).replace("\\n", "")
html = get_html("nested_blocks_and_variables_content.html")
html = re.sub(r"[\s\"]*", "", html)
assert html == response_text
def test_nested_inner(
self,
fastapi_client,
get_html,
):
response = fastapi_client.get("/nested_inner")
response_text = re.sub(r"[\s\"]*", "", response.text).replace("\\n", "")
html = get_html("nested_blocks_and_variables_inner.html")
html = re.sub(r"[\s\"]*", "", html)
assert html == response_text
def test_nested_inner_html_response_class(
self,
fastapi_client,
get_html,
):
"""using `response_class=HTMLResponse` should still work"""
response = fastapi_client.get("/nested_inner_html_response_class")
response_text = re.sub(r"[\s\"]*", "", response.text).replace("\\n", "")
html = get_html("nested_blocks_and_variables_inner.html")
html = re.sub(r"[\s\"]*", "", html)
assert html == response_text
def test_exception(self, fastapi_client):
with pytest.raises(BlockNotFoundError) as exc:
fastapi_client.get("/invalid_block")
assert exc.value.block_name == "invalid_block"
assert exc.value.template_name == "simple_page.html.jinja2"