Browse Source

add tests for tutorials

pull/13464/head
sneakers-the-rat 2 months ago
parent
commit
f8c6df190c
No known key found for this signature in database GPG Key ID: 6DCB96EF1E4D232D
  1. 43
      tests/test_tutorial/test_request_form_models/test_tutorial003.py
  2. 44
      tests/test_tutorial/test_request_form_models/test_tutorial004.py

43
tests/test_tutorial/test_request_form_models/test_tutorial003.py

@ -0,0 +1,43 @@
import importlib
import pytest
from fastapi.testclient import TestClient
@pytest.fixture(
name="client",
params=[
"tutorial003_py310",
"tutorial003_an_py310",
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.request_form_models.{request.param}")
client = TestClient(mod.app)
return client
def test_get_form(client: TestClient):
"""The form has a checkbox that is by default checked"""
response = client.get("/form")
response.raise_for_status()
assert '<input type="checkbox" name="checkbox"' in response.text
assert 'checked="checked"' in response.text
def test_post_form_checked(client: TestClient):
"""When the checkbox is checked, the value is (correctly) True"""
response = client.post("/form", data={"checkbox": "on"})
response.raise_for_status()
assert response.json() == {"checkbox": True}
def test_post_form_unchecked(client: TestClient):
"""
When the checkbox is not checked,
the value is (maybe correctly but undesirably) still True
"""
response = client.post("/form", data={"checkbox": ""})
response.raise_for_status()
assert response.json() == {"checkbox": True}

44
tests/test_tutorial/test_request_form_models/test_tutorial004.py

@ -0,0 +1,44 @@
import importlib
import pytest
from fastapi.testclient import TestClient
@pytest.fixture(
name="client",
params=[
"tutorial004_py310",
"tutorial004_an_py310",
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.request_form_models.{request.param}")
client = TestClient(mod.app)
return client
def test_get_form(client: TestClient):
"""The form has a checkbox that is by default checked"""
response = client.get("/form")
response.raise_for_status()
assert '<input type="checkbox" name="checkbox"' in response.text
assert 'checked="checked"' in response.text
def test_post_form_checked(client: TestClient):
"""When the checkbox is checked, the value is (correctly) True"""
response = client.post("/form", data={"checkbox": "on"})
response.raise_for_status()
assert response.json() == {"checkbox": True}
def test_post_form_unchecked(client: TestClient):
"""
When the checkbox is not checked,
now that we have a model validator that can differentiate defaults vs. undefined,
the checkbox is correctly false.
"""
response = client.post("/form", data={"checkbox": ""})
response.raise_for_status()
assert response.json() == {"checkbox": False}
Loading…
Cancel
Save