2 changed files with 87 additions and 0 deletions
@ -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} |
||||
@ -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…
Reference in new issue