Skip to content

Commit 0ff0555

Browse files
Add end-to-end test cases for Flask
1 parent 0560c11 commit 0ff0555

File tree

5 files changed

+274
-3
lines changed

5 files changed

+274
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,10 @@ Copyright (c) 2023 Miguel Guthridge, COMP1010 UNSW
235235

236236
Source code for the library is open source, using the
237237
[MIT license](https://choosealicense.com/licenses/mit/). A copy of the license
238-
text is available in [`LICENSE.md`](https://github.com/COMP1010UNSW/pyhtml-enhanced/blob/main/LICENSE.md)
238+
text is available in [LICENSE.md](https://github.com/COMP1010UNSW/pyhtml-enhanced/blob/main/LICENSE.md)
239239

240240
### Documentation
241241

242242
Documentation is copied from MDN Web Docs, and is license under
243243
[CC-BY-SA-2.5](https://creativecommons.org/licenses/by-sa/2.5/). A copy of the
244-
license text is available in [`LICENSE_DOCS.md`](https://github.com/COMP1010UNSW/pyhtml-enhanced/blob/main/LICENSE_DOCS.md)
244+
license text is available in [LICENSE_DOCS.md](https://github.com/COMP1010UNSW/pyhtml-enhanced/blob/main/LICENSE_DOCS.md)

poetry.lock

Lines changed: 197 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ types-requests = "^2.31.0.2"
4848
pyyaml = "^6.0.1"
4949
types-pyyaml = "^6.0.12.11"
5050
flake8-pyproject = "^1.2.3"
51+
flask = "^3.0.0"
5152

5253
[build-system]
5354
requires = ["poetry-core"]

tests/end_to_end/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""
2+
End-to-end tests to ensure that our library works nicely alongside other tools
3+
"""

tests/end_to_end/flask_test.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""
2+
Tests to ensure our code works nicely with Flask
3+
"""
4+
import pytest
5+
import pyhtml as p
6+
from flask import Flask
7+
from flask.testing import FlaskClient
8+
9+
10+
app = Flask(__name__)
11+
12+
13+
@app.get("/")
14+
def simple_route():
15+
return str(p.html(
16+
p.body("Hello, world!")
17+
))
18+
19+
20+
# This route intentionally returns the wrong type
21+
@app.get("/no_str") # type: ignore
22+
def no_stringify():
23+
"""
24+
This route intentionally returns an un-stringified PyHTML object. We use
25+
it to test that a reasonable error message is given
26+
"""
27+
return p.html(
28+
p.body("Hello, world!")
29+
)
30+
31+
32+
@pytest.fixture
33+
def get_app():
34+
app.config.update({"TESTING": True})
35+
36+
yield app
37+
38+
app.config.pop("TESTING")
39+
40+
41+
@pytest.fixture
42+
def client(get_app: Flask):
43+
return get_app.test_client()
44+
45+
46+
def test_simple_stringify(client: FlaskClient):
47+
"""Does requesting to the server give some nice HTML?"""
48+
response = client.get("/")
49+
assert response.status_code == 200
50+
51+
assert response.text == '\n'.join([
52+
"<html>",
53+
" <body>",
54+
" Hello, world!",
55+
" </body>",
56+
"</html>",
57+
])
58+
59+
60+
def test_failed_to_stringify(client: FlaskClient):
61+
"""
62+
Does requesting to a route that returns un-stringified HTML give an
63+
appropriate error message
64+
"""
65+
with pytest.raises(TypeError) as exception_info:
66+
client.get("/no_str")
67+
68+
expected_error = "HINT: try converting your PyHTML into a string using "\
69+
"the `str` function"
70+
71+
assert expected_error in str(exception_info.value)

0 commit comments

Comments
 (0)