prefix stringlengths 512 512 | suffix stringlengths 256 256 | middle stringlengths 14 229 | meta dict |
|---|---|---|---|
pytest
from fastapi.testclient import TestClient
from inline_snapshot import snapshot
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial010_py310"),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.custom_response.{request.param}")
... | assert response.json() == snapshot(
{
"openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/items/": {
"get": {
"respons | assert response.text == snapshot("<h1>Items</h1><p>This is a list of items.</p>")
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
| {
"filepath": "tests/test_tutorial/test_custom_response/test_tutorial010.py",
"language": "python",
"file_size": 1537,
"cut_index": 537,
"middle_length": 229
} |
pytest.param("tutorial002_py310", marks=[needs_py310]),
pytest.param("tutorial002_an_py310", marks=[needs_py310]),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.header_param_models.{request.param}")
client = TestClient(mod.app)
client.headers.c... |
"host": "testserver",
"save_data": True,
"if_modified_since": "yesterday",
"traceparent": "123",
"x_tag": ["one", "two"],
}
def test_header_param_model_defaults(client: TestClient):
response = client.get(" | modified-since", "yesterday"),
("traceparent", "123"),
("x-tag", "one"),
("x-tag", "two"),
],
)
assert response.status_code == 200, response.text
assert response.json() == { | {
"filepath": "tests/test_tutorial/test_header_param_models/test_tutorial002.py",
"language": "python",
"file_size": 7440,
"cut_index": 716,
"middle_length": 229
} |
import importlib
import json
import pytest
from fastapi import Request
from fastapi.testclient import TestClient
from tests.utils import needs_py310
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial001_py310", marks=needs_py310),
pytest.param("tutorial001_an_py310", marks=needs... | est(client: TestClient, compress):
n = 1000
headers = {}
body = [1] * n
data = json.dumps(body).encode()
if compress:
data = gzip.compress(data)
headers["Content-Encoding"] = "gzip"
headers["Content-Type"] = "applica | async def check_gzip_request(request: Request):
return {"request_class": type(request).__name__}
client = TestClient(mod.app)
return client
@pytest.mark.parametrize("compress", [True, False])
def test_gzip_requ | {
"filepath": "tests/test_tutorial/test_custom_request_and_route/test_tutorial001.py",
"language": "python",
"file_size": 1278,
"cut_index": 524,
"middle_length": 229
} |
import pytest
from fastapi.testclient import TestClient
from tests.utils import needs_py310
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial003_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.custom_requ... | sponse.headers
def test_get_timed(client: TestClient):
response = client.get("/timed")
assert response.json() == {"message": "It's the time of my life"}
assert "X-Response-Time" in response.headers
assert float(response.headers["X-Respons | "Not timed"}
assert "X-Response-Time" not in re | {
"filepath": "tests/test_tutorial/test_custom_request_and_route/test_tutorial003.py",
"language": "python",
"file_size": 852,
"cut_index": 529,
"middle_length": 52
} |
pytest
from fastapi.testclient import TestClient
from inline_snapshot import snapshot
@pytest.fixture(
name="client",
params=[
"tutorial001_an_py310",
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(
f"docs_src.authentication_error_status_code.{reques... | ("/me")
assert response.status_code == 403
assert response.json() == {"detail": "Not authenticated"}
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
| assert response.status_code == 200
assert response.json() == {
"message": "You are authenticated",
"token": "secrettoken",
}
def test_get_me_no_credentials(client: TestClient):
response = client.get | {
"filepath": "tests/test_tutorial/test_authentication_error_status_code/test_tutorial001.py",
"language": "python",
"file_size": 1931,
"cut_index": 537,
"middle_length": 229
} |
ient import TestClient
from inline_snapshot import snapshot
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial001_py310"),
pytest.param("tutorial001_an_py310"),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.security.{request.... | testtoken"})
assert response.status_code == 200, response.text
assert response.json() == {"token": "testtoken"}
def test_incorrect_token(client: TestClient):
response = client.get("/items", headers={"Authorization": "Notexistent testtoken"}) | assert response.json() == {"detail": "Not authenticated"}
assert response.headers["WWW-Authenticate"] == "Bearer"
def test_token(client: TestClient):
response = client.get("/items", headers={"Authorization": "Bearer | {
"filepath": "tests/test_tutorial/test_security/test_tutorial001.py",
"language": "python",
"file_size": 2422,
"cut_index": 563,
"middle_length": 229
} |
: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.security.{request.param}")
return mod
def get_access_token(*, username="johndoe", password="secret", client: TestClient):
data = {"username": username, "password": password}
response = client.post("/token", data=data)
content = re... | gin_incorrect_password(mod: ModuleType):
client = TestClient(mod.app)
response = client.post(
"/token", data={"username": "johndoe", "password": "incorrect"}
)
assert response.status_code == 401, response.text
assert response.js | ta={"username": "johndoe", "password": "secret"})
assert response.status_code == 200, response.text
content = response.json()
assert "access_token" in content
assert content["token_type"] == "bearer"
def test_lo | {
"filepath": "tests/test_tutorial/test_security/test_tutorial004.py",
"language": "python",
"file_size": 14636,
"cut_index": 921,
"middle_length": 229
} |
def get_mod(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.security.{request.param}")
return mod
@pytest.fixture(scope="module", autouse=True)
def cache_verify_password(mod: ModuleType):
assert hasattr(mod, "verify_password"), (
f"Module {mod.__name__} does not have at... | nse = client.post("/token", data=data)
content = response.json()
access_token = content.get("access_token")
return access_token
def test_login(mod: ModuleType):
client = TestClient(mod.app)
response = client.post("/token", data={"user | = original_func
def get_access_token(
*, username="johndoe", password="secret", scope=None, client: TestClient
):
data = {"username": username, "password": password}
if scope:
data["scope"] = scope
respo | {
"filepath": "tests/test_tutorial/test_security/test_tutorial005.py",
"language": "python",
"file_size": 16716,
"cut_index": 921,
"middle_length": 229
} |
{"type": "array", "items": {}}
LIST_OF_STR_SCHEMA = {"type": "array", "items": {"type": "string"}}
SET_OF_STR_SCHEMA = {"type": "array", "items": {"type": "string"}, "uniqueItems": True}
@pytest.fixture(
name="mod_name",
params=[
pytest.param("tutorial001_py310", marks=needs_py310),
pytest.p... | t, mod_name: str):
if mod_name.startswith("tutorial003"):
tags_expected = IsList("foo", "bar", check_order=False)
else:
tags_expected = ["foo", "bar", "foo"]
response = client.put(
"/items/123",
json={
|
@pytest.fixture(name="client")
def get_client(mod_name: str):
mod = importlib.import_module(f"docs_src.body_nested_models.{mod_name}")
client = TestClient(mod.app)
return client
def test_put_all(client: TestClien | {
"filepath": "tests/test_tutorial/test_body_nested_models/test_tutorial001_tutorial002_tutorial003.py",
"language": "python",
"file_size": 8697,
"cut_index": 716,
"middle_length": 229
} |
s.{request.param}")
client = TestClient(mod.app)
return client
def test_put_all(client: TestClient):
response = client.put(
"/items/123",
json={
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
"tags":... | check_order=False),
"image": {"url": "http://example.com/image.png", "name": "example image"},
},
}
def test_put_only_required(client: TestClient):
response = client.put(
"/items/5",
json={"name": "Foo", "pric | response.json() == {
"item_id": 123,
"item": {
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
"tags": IsList("foo", "bar", | {
"filepath": "tests/test_tutorial/test_body_nested_models/test_tutorial005.py",
"language": "python",
"file_size": 10561,
"cut_index": 921,
"middle_length": 229
} |
astapi.testclient import TestClient
from inline_snapshot import snapshot
from docs_src.custom_response.tutorial006_py310 import app
client = TestClient(app)
def test_get():
response = client.get("/typer", follow_redirects=False)
assert response.status_code == 307, response.text
assert response.headers["... | mary": "Redirect Typer",
"operationId": "redirect_typer_typer_get",
"responses": {
"200": {
"description": "Successful Response",
| n() == snapshot(
{
"openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/typer": {
"get": {
"sum | {
"filepath": "tests/test_tutorial/test_custom_response/test_tutorial006.py",
"language": "python",
"file_size": 1194,
"cut_index": 518,
"middle_length": 229
} |
name="client",
params=[
pytest.param("tutorial001_py310", marks=needs_py310),
pytest.param("tutorial001_an_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.header_param_models.{request.param}")
client = TestClient(... | t": "testserver",
"save_data": True,
"if_modified_since": "yesterday",
"traceparent": "123",
"x_tag": ["one", "two"],
}
def test_header_param_model_defaults(client: TestClient):
response = client.get("/items/", hea | f-modified-since", "yesterday"),
("traceparent", "123"),
("x-tag", "one"),
("x-tag", "two"),
],
)
assert response.status_code == 200
assert response.json() == {
"hos | {
"filepath": "tests/test_tutorial/test_header_param_models/test_tutorial001.py",
"language": "python",
"file_size": 7682,
"cut_index": 716,
"middle_length": 229
} |
tlib
import pytest
from dirty_equals import IsOneOf
from fastapi.testclient import TestClient
from tests.utils import needs_py310
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial002_py310", marks=needs_py310),
pytest.param("tutorial002_an_py310", marks=needs_py310),
],
)
d... | s": [1, 2, 3]})
assert response.json() == {
"detail": {
"errors": [
{
"type": "list_type",
"loc": ["body"],
"msg": "Input should be a valid list",
| test_endpoint_works(client: TestClient):
response = client.post("/", json=[1, 2, 3])
assert response.json() == 6
def test_exception_handler_body_access(client: TestClient):
response = client.post("/", json={"number | {
"filepath": "tests/test_tutorial/test_custom_request_and_route/test_tutorial002.py",
"language": "python",
"file_size": 1276,
"cut_index": 524,
"middle_length": 229
} |
ient import TestClient
from inline_snapshot import snapshot
from ...utils import needs_py310
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial002_py310", marks=needs_py310),
pytest.param("tutorial002_an_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureR... | nt):
response = client.get("/users/me", headers={"Authorization": "Bearer testtoken"})
assert response.status_code == 200, response.text
assert response.json() == {
"username": "testtokenfakedecoded",
"email": "john@example.com" | nt.get("/users/me")
assert response.status_code == 401, response.text
assert response.json() == {"detail": "Not authenticated"}
assert response.headers["WWW-Authenticate"] == "Bearer"
def test_token(client: TestClie | {
"filepath": "tests/test_tutorial/test_security/test_tutorial002.py",
"language": "python",
"file_size": 2323,
"cut_index": 563,
"middle_length": 229
} |
rt pytest
from fastapi.testclient import TestClient
from inline_snapshot import snapshot
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial006_py310"),
pytest.param("tutorial006_an_py310"),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(... | .get("/users/me")
assert response.json() == {"detail": "Not authenticated"}
assert response.status_code == 401, response.text
assert response.headers["WWW-Authenticate"] == "Basic"
def test_security_http_basic_invalid_credentials(client: Test | ", "secret"))
assert response.status_code == 200, response.text
assert response.json() == {"username": "john", "password": "secret"}
def test_security_http_basic_no_credentials(client: TestClient):
response = client | {
"filepath": "tests/test_tutorial/test_security/test_tutorial006.py",
"language": "python",
"file_size": 2798,
"cut_index": 563,
"middle_length": 229
} |
s.{request.param}")
client = TestClient(mod.app)
return client
def test_put_all(client: TestClient):
response = client.put(
"/items/123",
json={
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
"tags":... | check_order=False),
"image": {"url": "http://example.com/image.png", "name": "example image"},
},
}
def test_put_only_required(client: TestClient):
response = client.put(
"/items/5",
json={"name": "Foo", "pric | response.json() == {
"item_id": 123,
"item": {
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
"tags": IsList("foo", "bar", | {
"filepath": "tests/test_tutorial/test_body_nested_models/test_tutorial004.py",
"language": "python",
"file_size": 9729,
"cut_index": 921,
"middle_length": 229
} |
name="client",
params=[
pytest.param("tutorial003_py310", marks=needs_py310),
pytest.param("tutorial003_an_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.header_param_models.{request.param}")
client = TestClient(... | t": "testserver",
"save_data": True,
"if_modified_since": "yesterday",
"traceparent": "123",
"x_tag": ["one", "two"],
}
def test_header_param_model_no_underscore(client: TestClient):
response = client.get(
| f_modified_since", "yesterday"),
("traceparent", "123"),
("x_tag", "one"),
("x_tag", "two"),
],
)
assert response.status_code == 200
assert response.json() == {
"hos | {
"filepath": "tests/test_tutorial/test_header_param_models/test_tutorial003.py",
"language": "python",
"file_size": 8948,
"cut_index": 716,
"middle_length": 229
} |
rams.tutorial005_py310 import app
client = TestClient(app)
def test_foo_needy_very():
response = client.get("/items/foo?needy=very")
assert response.status_code == 200
assert response.json() == {"item_id": "foo", "needy": "very"}
def test_foo_no_needy():
response = client.get("/items/foo")
asse... | pshot(
{
"openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/items/{item_id}": {
"get": {
"responses": {
| msg": "Field required",
"input": None,
}
]
}
def test_openapi_schema():
response = client.get("/openapi.json")
assert response.status_code == 200
assert response.json() == sna | {
"filepath": "tests/test_tutorial/test_query_params/test_tutorial005.py",
"language": "python",
"file_size": 4234,
"cut_index": 614,
"middle_length": 229
} |
pytest.param("tutorial002_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.response_model.{request.param}")
client = TestClient(mod.app)
return client
def test_post_user(client: TestClient):
user_data = {
"username":... | nse.text
assert response.json() == snapshot(
{
"openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/user/": {
"post": {
| assert response.status_code == 200, response.text
assert response.json() == user_data
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, respo | {
"filepath": "tests/test_tutorial/test_response_model/test_tutorial002.py",
"language": "python",
"file_size": 5211,
"cut_index": 716,
"middle_length": 229
} |
ne_snapshot import snapshot
from docs_src.sub_applications.tutorial001_py310 import app
client = TestClient(app)
def test_main():
response = client.get("/app")
assert response.status_code == 200, response.text
assert response.json() == {"message": "Hello World from main app"}
def test_sub():
respo... | "0.1.0"},
"paths": {
"/app": {
"get": {
"responses": {
"200": {
"description": "Successful Response",
| response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == snapshot(
{
"openapi": "3.1.0",
"info": {"title": "FastAPI", "version": | {
"filepath": "tests/test_tutorial/test_sub_applications/test_tutorial001.py",
"language": "python",
"file_size": 2208,
"cut_index": 563,
"middle_length": 229
} |
from inline_snapshot import snapshot
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial007_py310"),
pytest.param("tutorial007_an_py310"),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.security.{request.param}")
return Te... | ated"}
assert response.status_code == 401, response.text
assert response.headers["WWW-Authenticate"] == "Basic"
def test_security_http_basic_invalid_credentials(client: TestClient):
response = client.get(
"/users/me", headers={"Author | se.text
assert response.json() == {"username": "stanleyjobson"}
def test_security_http_basic_no_credentials(client: TestClient):
response = client.get("/users/me")
assert response.json() == {"detail": "Not authentic | {
"filepath": "tests/test_tutorial/test_security/test_tutorial007.py",
"language": "python",
"file_size": 3438,
"cut_index": 614,
"middle_length": 229
} |
from fastapi.testclient import TestClient
from inline_snapshot import snapshot
from docs_src.custom_response.tutorial006c_py310 import app
client = TestClient(app)
def test_redirect_status_code():
response = client.get("/pydantic", follow_redirects=False)
assert response.status_code == 302
assert respon... | "summary": "Redirect Pydantic",
"operationId": "redirect_pydantic_pydantic_get",
"responses": {"302": {"description": "Successful Response"}},
}
}
}, | response.json() == snapshot(
{
"openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/pydantic": {
"get": {
| {
"filepath": "tests/test_tutorial/test_custom_response/test_tutorial006c.py",
"language": "python",
"file_size": 1014,
"cut_index": 512,
"middle_length": 229
} |
snapshot
@pytest.fixture(
name="client",
params=[
"tutorial009_py310",
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.body_nested_models.{request.param}")
client = TestClient(mod.app)
return client
def test_post_body(client: TestClient)... | l": [
{
"type": "int_parsing",
"loc": ["body", "foo", "[key]"],
"msg": "Input should be a valid integer, unable to parse string as an integer",
"input": "foo",
}
| ost_invalid_body(client: TestClient):
data = {"foo": 2.2, "3": 3.3}
response = client.post("/index-weights/", json=data)
assert response.status_code == 422, response.text
assert response.json() == {
"detai | {
"filepath": "tests/test_tutorial/test_body_nested_models/test_tutorial009.py",
"language": "python",
"file_size": 4607,
"cut_index": 614,
"middle_length": 229
} |
pytest.param("tutorial003_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.response_model.{request.param}")
client = TestClient(mod.app)
return client
def test_post_user(client: TestClient):
response = client.post(
"... | :
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == snapshot(
{
"openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
" | sert response.status_code == 200, response.text
assert response.json() == {
"username": "foo",
"email": "foo@example.com",
"full_name": "Grave Dohl",
}
def test_openapi_schema(client: TestClient) | {
"filepath": "tests/test_tutorial/test_response_model/test_tutorial003.py",
"language": "python",
"file_size": 6081,
"cut_index": 716,
"middle_length": 229
} |
utorial003_py310 import (
app,
fake_answer_to_everything_ml_model,
ml_models,
)
def test_events():
assert not ml_models, "ml_models should be empty"
with TestClient(app) as client:
assert ml_models["answer_to_everything"] == fake_answer_to_everything_ml_model
response = client.get(... | "openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/predict": {
"get": {
"summary": "Predict",
|
def test_openapi_schema():
with TestClient(app) as client:
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == snapshot(
{
| {
"filepath": "tests/test_tutorial/test_events/test_tutorial003.py",
"language": "python",
"file_size": 4302,
"cut_index": 614,
"middle_length": 229
} |
pytest.param("tutorial001_py310", marks=needs_py310),
pytest.param("tutorial001_01_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.response_model.{request.param}")
client = TestClient(mod.app)
return client
def test_rea... | "tags": [],
"tax": None,
},
]
def test_create_item(client: TestClient):
item_data = {
"name": "Test Item",
"description": "A test item",
"price": 10.5,
"tax": 1.5,
"tags": ["test | n",
"description": None,
"price": 42.0,
"tags": [],
"tax": None,
},
{
"name": "Plumbus",
"description": None,
"price": 32.0,
| {
"filepath": "tests/test_tutorial/test_response_model/test_tutorial001_tutorial001_01.py",
"language": "python",
"file_size": 7368,
"cut_index": 716,
"middle_length": 229
} |
astapi.testclient import TestClient
from inline_snapshot import snapshot
from docs_src.response_model.tutorial003_03_py310 import app
client = TestClient(app)
def test_get_portal():
response = client.get("/teleport", follow_redirects=False)
assert response.status_code == 307, response.text
assert respon... | : {
"summary": "Get Teleport",
"operationId": "get_teleport_teleport_get",
"responses": {
"200": {
"description": "Successfu | .text
assert response.json() == snapshot(
{
"openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/teleport": {
"get" | {
"filepath": "tests/test_tutorial/test_response_model/test_tutorial003_03.py",
"language": "python",
"file_size": 1225,
"cut_index": 518,
"middle_length": 229
} |
pytest.param("tutorial004_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.response_model.{request.param}")
client = TestClient(mod.app)
return client
@pytest.mark.parametrize(
"url,data",
[
("/items/foo", {"name... | data, client: TestClient):
response = client.get(url)
assert response.status_code == 200, response.text
assert response.json() == data
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response | baz",
{
"name": "Baz",
"description": None,
"price": 50.2,
"tax": 10.5,
"tags": [],
},
),
],
)
def test_get(url, | {
"filepath": "tests/test_tutorial/test_response_model/test_tutorial004.py",
"language": "python",
"file_size": 5527,
"cut_index": 716,
"middle_length": 229
} |
pytest.param("tutorial006_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.response_model.{request.param}")
client = TestClient(mod.app)
return client
def test_read_item_name(client: TestClient):
response = client.get("/item... |
}
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == snapshot(
{
"openapi": "3.1.0",
"info": {"t | tClient):
response = client.get("/items/bar/public")
assert response.status_code == 200, response.text
assert response.json() == {
"name": "Bar",
"description": "The Bar fighters",
"price": 62, | {
"filepath": "tests/test_tutorial/test_response_model/test_tutorial006.py",
"language": "python",
"file_size": 6723,
"cut_index": 716,
"middle_length": 229
} |
issing",
"loc": ["query", "token"],
"msg": "Field required",
"input": None,
}
]
}
def test_users_foo_token_jessica(client: TestClient):
response = client.get("/users/foo?token=jessica")
assert response.status_code == 200
assert respon... | est_users_me_token_jessica(client: TestClient):
response = client.get("/users/me?token=jessica")
assert response.status_code == 200
assert response.json() == {"username": "fakecurrentuser"}
def test_users_me_with_no_token(client: TestClient): | () == {
"detail": [
{
"type": "missing",
"loc": ["query", "token"],
"msg": "Field required",
"input": None,
}
]
}
def t | {
"filepath": "tests/test_tutorial/test_bigger_applications/test_main.py",
"language": "python",
"file_size": 23329,
"cut_index": 1331,
"middle_length": 229
} |
001_py310",
"tutorial001_an_py310",
],
)
def get_app(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.request_forms_and_files.{request.param}")
return mod.app
@pytest.fixture(name="client")
def get_client(app: FastAPI):
client = TestClient(app)
return client
def... | "loc": ["body", "fileb"],
"msg": "Field required",
"input": None,
},
{
"type": "missing",
"loc": ["body", "token"],
"msg": "Field required",
| {
"type": "missing",
"loc": ["body", "file"],
"msg": "Field required",
"input": None,
},
{
"type": "missing",
| {
"filepath": "tests/test_tutorial/test_request_forms_and_files/test_tutorial001.py",
"language": "python",
"file_size": 8279,
"cut_index": 716,
"middle_length": 229
} |
pytest.param("tutorial001_py310", marks=needs_py310),
pytest.param("tutorial001_an_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.body_multiple_params.{request.param}")
client = TestClient(mod.app)
return client
def te... | ient: TestClient):
response = client.put("/items/5?q=bar", json=None)
assert response.status_code == 200
assert response.json() == {"item_id": 5, "q": "bar"}
def test_post_no_body(client: TestClient):
response = client.put("/items/5", jso | () == {
"item_id": 5,
"item": {
"name": "Foo",
"price": 50.5,
"description": None,
"tax": None,
},
"q": "bar",
}
def test_post_no_body_q_bar(cl | {
"filepath": "tests/test_tutorial/test_body_multiple_params/test_tutorial001.py",
"language": "python",
"file_size": 7085,
"cut_index": 716,
"middle_length": 229
} |
pytest.param("tutorial003_py310", marks=needs_py310),
pytest.param("tutorial003_an_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.security.{request.param}")
client = TestClient(mod.app)
return client
def test_login(cli... | onse.status_code == 400, response.text
assert response.json() == {"detail": "Incorrect username or password"}
def test_login_incorrect_username(client: TestClient):
response = client.post("/token", data={"username": "foo", "password": "secret"})
| "access_token": "johndoe", "token_type": "bearer"}
def test_login_incorrect_password(client: TestClient):
response = client.post(
"/token", data={"username": "johndoe", "password": "incorrect"}
)
assert resp | {
"filepath": "tests/test_tutorial/test_security/test_tutorial003.py",
"language": "python",
"file_size": 8698,
"cut_index": 716,
"middle_length": 229
} |
s.{request.param}")
client = TestClient(mod.app)
return client
def test_put_all(client: TestClient):
response = client.put(
"/items/123",
json={
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
"tags":... | "tags": IsList("foo", "bar", check_order=False),
"images": [
{"url": "http://example.com/image.png", "name": "example image"}
],
},
}
def test_put_only_required(client: TestClient):
response = | == 200, response.text
assert response.json() == {
"item_id": 123,
"item": {
"name": "Foo",
"description": "A very nice Item",
"price": 35.4,
"tax": 3.2,
| {
"filepath": "tests/test_tutorial/test_body_nested_models/test_tutorial006.py",
"language": "python",
"file_size": 9715,
"cut_index": 921,
"middle_length": 229
} |
i import FastAPI
from fastapi.testclient import TestClient
from inline_snapshot import snapshot
from tests.utils import workdir_lock
@pytest.fixture(name="app", scope="module")
def get_app():
with pytest.warns(DeprecationWarning):
from docs_src.events.tutorial002_py310 import app
yield app
@workdir... | ient.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == snapshot(
{
"openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
| nse.json() == [{"name": "Foo"}]
with open("log.txt") as log:
assert "Application shutdown" in log.read()
@workdir_lock
def test_openapi_schema(app: FastAPI):
with TestClient(app) as client:
response = cl | {
"filepath": "tests/test_tutorial/test_events/test_tutorial002.py",
"language": "python",
"file_size": 1623,
"cut_index": 537,
"middle_length": 229
} |
_model.tutorial003_02_py310 import app
client = TestClient(app)
def test_get_portal():
response = client.get("/portal")
assert response.status_code == 200, response.text
assert response.json() == {"message": "Here's your interdimensional portal."}
def test_get_redirect():
response = client.get("/po... | "info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/portal": {
"get": {
"summary": "Get Portal",
"operationId": "get_portal_portal_get",
| w9WgXcQ"
def test_openapi_schema():
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == snapshot(
{
"openapi": "3.1.0",
| {
"filepath": "tests/test_tutorial/test_response_model/test_tutorial003_02.py",
"language": "python",
"file_size": 4025,
"cut_index": 614,
"middle_length": 229
} |
pytest.param("tutorial005_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.response_model.{request.param}")
client = TestClient(mod.app)
return client
def test_read_item_name(client: TestClient):
response = client.get("/item... |
}
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == snapshot(
{
"openapi": "3.1.0",
"info": {"t | tClient):
response = client.get("/items/bar/public")
assert response.status_code == 200, response.text
assert response.json() == {
"name": "Bar",
"description": "The Bar fighters",
"price": 62, | {
"filepath": "tests/test_tutorial/test_response_model/test_tutorial005.py",
"language": "python",
"file_size": 6723,
"cut_index": 716,
"middle_length": 229
} |
001_py310"),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.stream_json_lines.{request.param}")
client = TestClient(mod.app)
return client
expected_items = [
{"name": "Plumbus", "description": "A multi-purpose household device."},
{"name": "Porta... | assert response.status_code == 200, response.text
assert response.headers["content-type"] == "application/jsonl"
lines = [json.loads(line) for line in response.text.strip().splitlines()]
assert lines == expected_items
def test_openapi_sche | ms/stream",
"/items/stream-no-async",
"/items/stream-no-annotation",
"/items/stream-no-async-no-annotation",
],
)
def test_stream_items(client: TestClient, path: str):
response = client.get(path)
| {
"filepath": "tests/test_tutorial/test_stream_json_lines/test_tutorial001.py",
"language": "python",
"file_size": 5356,
"cut_index": 716,
"middle_length": 229
} |
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.body_nested_models.{request.param}")
client = TestClient(mod.app)
return client
def test_post_body(client: TestClient):
data = [
{"url": "http://example.com/", "name": "Example"},
{"url"... | sponse.text
assert response.json() == {
"detail": [
{
"loc": ["body", 0, "url"],
"input": "not a valid url",
"msg": "Input should be a valid URL, relative URL without a base",
| json() == data
def test_post_invalid_list_item(client: TestClient):
data = [{"url": "not a valid url", "name": "Example"}]
response = client.post("/images/multiple", json=data)
assert response.status_code == 422, re | {
"filepath": "tests/test_tutorial/test_body_nested_models/test_tutorial008.py",
"language": "python",
"file_size": 6154,
"cut_index": 716,
"middle_length": 229
} |
pytest.param("tutorial003_01_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.response_model.{request.param}")
client = TestClient(mod.app)
return client
def test_post_user(client: TestClient):
response = client.post(
... | nt):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == snapshot(
{
"openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
| assert response.status_code == 200, response.text
assert response.json() == {
"username": "foo",
"email": "foo@example.com",
"full_name": "Grave Dohl",
}
def test_openapi_schema(client: TestClie | {
"filepath": "tests/test_tutorial/test_response_model/test_tutorial003_01.py",
"language": "python",
"file_size": 6087,
"cut_index": 716,
"middle_length": 229
} |
snapshot
from ...utils import needs_py310
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial003_05_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.response_model.{request.param}")
client = TestClient(m... | se.text
assert response.headers["location"] == "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert resp | message": "Here's your interdimensional portal."}
def test_get_redirect(client: TestClient):
response = client.get("/portal", params={"teleport": True}, follow_redirects=False)
assert response.status_code == 307, respon | {
"filepath": "tests/test_tutorial/test_response_model/test_tutorial003_05.py",
"language": "python",
"file_size": 4360,
"cut_index": 614,
"middle_length": 229
} |
ot import snapshot
@pytest.fixture(name="app", scope="module")
def get_app():
with pytest.warns(DeprecationWarning):
from docs_src.events.tutorial001_py310 import app
yield app
def test_events(app: FastAPI):
with TestClient(app) as client:
response = client.get("/items/foo")
asse... | "version": "0.1.0"},
"paths": {
"/items/{item_id}": {
"get": {
"responses": {
"200": {
"description | se = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == snapshot(
{
"openapi": "3.1.0",
"info": {"title": "FastAPI", | {
"filepath": "tests/test_tutorial/test_events/test_tutorial001.py",
"language": "python",
"file_size": 4256,
"cut_index": 614,
"middle_length": 229
} |
est.param}")
return mod
@pytest.fixture(name="client")
def get_client(mod: ModuleType):
client = TestClient(mod.app)
client.headers.clear()
return client
def test_get(client: TestClient):
response = client.post(
"/invoices/", json={"id": "fooinvoice", "customer": "John", "total": 5.3}
... | "openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/invoices/": {
"post": {
"summary": "Create Invoice",
"descripti | invoice_notification({})
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == snapshot(
{
| {
"filepath": "tests/test_tutorial/test_openapi_callbacks/test_tutorial001.py",
"language": "python",
"file_size": 9656,
"cut_index": 921,
"middle_length": 229
} |
ty_equals import IsAnyStr
from fastapi.testclient import TestClient
from inline_snapshot import snapshot
from pydantic import ValidationError
from pytest import MonkeyPatch
@pytest.fixture(
name="mod_name",
params=[
pytest.param("app01_py310"),
],
)
def get_mod_name(request: pytest.FixtureRequest)... | ion_error(mod_name: str, monkeypatch: MonkeyPatch):
monkeypatch.delenv("ADMIN_EMAIL", raising=False)
if mod_name in sys.modules:
del sys.modules[mod_name] # pragma: no cover
with pytest.raises(ValidationError) as exc_info:
imp | es:
del sys.modules[mod_name] # pragma: no cover
monkeypatch.setenv("ADMIN_EMAIL", "admin@example.com")
main_mod = importlib.import_module(mod_name)
return TestClient(main_mod.app)
def test_settings_validat | {
"filepath": "tests/test_tutorial/test_settings/test_app01.py",
"language": "python",
"file_size": 2359,
"cut_index": 563,
"middle_length": 229
} |
tlib
from types import ModuleType
import pytest
from fastapi.testclient import TestClient
from pytest import MonkeyPatch
@pytest.fixture(
name="mod_path",
params=[
pytest.param("app03_py310"),
pytest.param("app03_an_py310"),
],
)
def get_mod_path(request: pytest.FixtureRequest):
mod_p... | = "Awesome API"
assert settings.admin_email == "admin@example.com"
assert settings.items_per_user == 50
def test_endpoint(main_mod: ModuleType, monkeypatch: MonkeyPatch):
monkeypatch.setenv("ADMIN_EMAIL", "admin@example.com")
client = Tes | ath}.main")
return main_mod
def test_settings(main_mod: ModuleType, monkeypatch: MonkeyPatch):
monkeypatch.setenv("ADMIN_EMAIL", "admin@example.com")
settings = main_mod.get_settings()
assert settings.app_name = | {
"filepath": "tests/test_tutorial/test_settings/test_app03.py",
"language": "python",
"file_size": 1252,
"cut_index": 524,
"middle_length": 229
} |
SQLModel.metadata.clear()
# Clear the Models associated with the registry, to avoid warnings
default_registry.dispose()
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial001_py310", marks=needs_py310),
pytest.param("tutorial001_an_py310", marks=needs_py310),
],
s... | ngine = create_engine(
mod.sqlite_url, connect_args={"check_same_thread": False}, poolclass=StaticPool
)
with TestClient(mod.app) as c:
yield c
# Clean up connection explicitly to avoid resource warning
mod.engine.dispose() | ord=True):
warnings.simplefilter("always")
mod = importlib.import_module(f"docs_src.sql_databases.{request.param}")
clear_sqlmodel()
importlib.reload(mod)
mod.sqlite_url = "sqlite://"
mod.e | {
"filepath": "tests/test_tutorial/test_sql_databases/test_tutorial001.py",
"language": "python",
"file_size": 14397,
"cut_index": 921,
"middle_length": 229
} |
="client",
params=[
pytest.param("tutorial001_py310", marks=needs_py310),
pytest.param("tutorial002_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.extra_models.{request.param}")
client = TestClient(mod.app)
r... | example.com",
"full_name": "John Doe",
}
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == snapshot(
{
| "email": "johndoe@example.com",
"full_name": "John Doe",
},
)
assert response.status_code == 200, response.text
assert response.json() == {
"username": "johndoe",
"email": "johndoe@ | {
"filepath": "tests/test_tutorial/test_extra_models/test_tutorial001_tutorial002.py",
"language": "python",
"file_size": 6259,
"cut_index": 716,
"middle_length": 229
} |
ient import TestClient
from inline_snapshot import snapshot
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial004_py310"),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.extra_models.{request.param}")
client = TestClient(mod.app)... | se.status_code == 200, response.text
assert response.json() == snapshot(
{
"openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/items/": {
"ge | {"name": "Foo", "description": "There comes my hero"},
{"name": "Red", "description": "It's my aeroplane"},
]
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert respon | {
"filepath": "tests/test_tutorial/test_extra_models/test_tutorial004.py",
"language": "python",
"file_size": 2558,
"cut_index": 563,
"middle_length": 229
} |
import snapshot
from starlette.testclient import TestClient
warnings.filterwarnings(
"ignore",
message=r"The 'lia' package has been renamed to 'cross_web'\..*",
category=DeprecationWarning,
)
from docs_src.graphql_.tutorial001_py310 import app # noqa: E402
@pytest.fixture(name="client")
def get_client... | ponse.json() == snapshot(
{
"info": {
"title": "FastAPI",
"version": "0.1.0",
},
"openapi": "3.1.0",
"paths": {
"/graphql": {
"get": | ode == 200
assert response.json() == {"data": {"user": {"name": "Patrick", "age": 100}}}
def test_openapi(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200
assert res | {
"filepath": "tests/test_tutorial/test_graphql/test_tutorial001.py",
"language": "python",
"file_size": 2501,
"cut_index": 563,
"middle_length": 229
} |
om pathlib import Path
import pytest
from fastapi.testclient import TestClient
from inline_snapshot import snapshot
from tests.utils import workdir_lock
@pytest.fixture(scope="module")
def client():
static_dir: Path = Path(os.getcwd()) / "static"
static_dir.mkdir(exist_ok=True)
sample_file = static_dir ... | .text == "This is a sample static file."
@workdir_lock
def test_static_files_not_found(client: TestClient):
response = client.get("/static/non_existent_file.txt")
assert response.status_code == 404, response.text
@workdir_lock
def test_openapi_ |
sample_file.unlink()
static_dir.rmdir()
@workdir_lock
def test_static_files(client: TestClient):
response = client.get("/static/sample.txt")
assert response.status_code == 200, response.text
assert response | {
"filepath": "tests/test_tutorial/test_static_files/test_tutorial001.py",
"language": "python",
"file_size": 1319,
"cut_index": 524,
"middle_length": 229
} |
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.stream_data.{request.param}")
client = TestClient(mod.app)
return client
expected_text = (
""
"Rick: (stumbles in drunkenly, and turns on the lights)"
" Morty! You gotta come on. You got--... yo... | too hard!"
"Rick: We gotta go, gotta get outta here, come on."
" Got a surprise for you Morty."
)
@pytest.mark.parametrize(
"path",
[
"/story/stream",
"/story/stream-no-async",
"/story/stream-no-annotation",
| ut?"
"Rick: (spills alcohol on Morty's bed) Come on, I got a surprise for you."
" (drags Morty by the ankle) Come on, hurry up."
" (pulls Morty out of his bed and into the hall)"
"Morty: Ow! Ow! You're tugging me | {
"filepath": "tests/test_tutorial/test_stream_data/test_tutorial001.py",
"language": "python",
"file_size": 5858,
"cut_index": 716,
"middle_length": 229
} |
snapshot
@pytest.fixture(
name="mod",
params=[
pytest.param("tutorial002_py310"),
],
)
def get_mod(request: pytest.FixtureRequest):
return importlib.import_module(f"docs_src.stream_data.{request.param}")
@pytest.fixture(name="client")
def get_client(mod):
client = TestClient(mod.app)
... | ontent-type"] == "image/png"
assert response.content == mod.binary_image
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == snaps | nnotation",
"/image/stream-no-async-no-annotation",
],
)
def test_stream_image(mod, client: TestClient, path: str):
response = client.get(path)
assert response.status_code == 200
assert response.headers["c | {
"filepath": "tests/test_tutorial/test_stream_data/test_tutorial002.py",
"language": "python",
"file_size": 4481,
"cut_index": 614,
"middle_length": 229
} |
dule(f"docs_src.body_multiple_params.{request.param}")
client = TestClient(mod.app)
return client
def test_put_all(client: TestClient):
response = client.put(
"/items/5",
json={
"importance": 2,
"item": {"name": "Foo", "price": 50.5},
"user": {"username... | t_put_only_required(client: TestClient):
response = client.put(
"/items/5",
json={
"importance": 2,
"item": {"name": "Foo", "price": 50.5},
"user": {"username": "Dave"},
},
)
assert re | tem": {
"name": "Foo",
"price": 50.5,
"description": None,
"tax": None,
},
"user": {"username": "Dave", "full_name": None},
"q": "somequery",
}
def tes | {
"filepath": "tests/test_tutorial/test_body_multiple_params/test_tutorial004.py",
"language": "python",
"file_size": 10206,
"cut_index": 921,
"middle_length": 229
} |
pytest.param("tutorial002_py310", marks=[needs_py310]),
pytest.param("tutorial002_an_py310", marks=[needs_py310]),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.cookie_param_models.{request.param}")
client = TestClient(mod.app)
return client
... |
def test_cookie_param_model_defaults(client: TestClient):
with client as c:
c.cookies.set("session_id", "123")
response = c.get("/items/")
assert response.status_code == 200
assert response.json() == {
"session_id": " | l_tracker", "789")
response = c.get("/items/")
assert response.status_code == 200
assert response.json() == {
"session_id": "123",
"fatebook_tracker": "456",
"googall_tracker": "789",
} | {
"filepath": "tests/test_tutorial/test_cookie_param_models/test_tutorial002.py",
"language": "python",
"file_size": 6538,
"cut_index": 716,
"middle_length": 229
} |
.param("tutorial001_py310", marks=needs_py310)],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.json_base64_bytes.{request.param}")
client = TestClient(mod.app)
return client
def test_post_data(client: TestClient):
response = client.post(
"/data",
... | on": "A plumbus", "data": "aGVsbG8="}
def test_post_data_in_out(client: TestClient):
response = client.post(
"/data-in-out",
json={
"description": "A plumbus",
"data": "SGVsbG8sIFdvcmxkIQ==",
},
)
| == {"description": "A file", "content": "Hello, World!"}
def test_get_data(client: TestClient):
response = client.get("/data")
assert response.status_code == 200, response.text
assert response.json() == {"descripti | {
"filepath": "tests/test_tutorial/test_json_base64_bytes/test_tutorial001.py",
"language": "python",
"file_size": 9117,
"cut_index": 716,
"middle_length": 229
} |
snapshot
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial001_py310"),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.query_params.{request.param}")
client = TestClient(mod.app)
return client
@pytest.mark.parametrize(
... | xpected_json):
response = client.get(path)
assert response.status_code == 200
assert response.json() == expected_json
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 2 | =1",
[{"item_name": "Bar"}, {"item_name": "Baz"}],
),
(
"/items/?skip=1&limit=1",
[{"item_name": "Bar"}],
),
],
)
def test_read_user_item(client: TestClient, path, e | {
"filepath": "tests/test_tutorial/test_query_params/test_tutorial001.py",
"language": "python",
"file_size": 4815,
"cut_index": 614,
"middle_length": 229
} |
pytest.param("tutorial003_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.query_params.{request.param}")
client = TestClient(mod.app)
return client
@pytest.mark.parametrize(
("path", "expected_json"),
[
(
... |
(
"/items/baz?short=true",
{"item_id": "baz"},
),
],
)
def test_read_user_item(client: TestClient, path, expected_json):
response = client.get(path)
assert response.status_code == 200
assert response | "/items/bar?q=somequery",
{
"item_id": "bar",
"q": "somequery",
"description": "This is an amazing item that has a long description",
},
), | {
"filepath": "tests/test_tutorial/test_query_params/test_tutorial003.py",
"language": "python",
"file_size": 5646,
"cut_index": 716,
"middle_length": 229
} |
pytest.param("tutorial004_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.query_params.{request.param}")
client = TestClient(mod.app)
return client
@pytest.mark.parametrize(
("path", "expected_json"),
[
(
... | : "This is an amazing item that has a long description",
},
),
(
"/users/42/items/baz?short=true",
{"item_id": "baz", "owner_id": 42},
),
],
)
def test_read_user_item(client: TestClient, path, | on",
},
),
(
"/users/1/items/bar?q=somequery",
{
"item_id": "bar",
"owner_id": 1,
"q": "somequery",
"description" | {
"filepath": "tests/test_tutorial/test_query_params/test_tutorial004.py",
"language": "python",
"file_size": 6086,
"cut_index": 716,
"middle_length": 229
} |
# TODO: remove when updating SQL tutorial to use new lifespan API
with warnings.catch_warnings(record=True):
warnings.simplefilter("always")
mod = importlib.import_module(f"docs_src.sql_databases.{request.param}")
clear_sqlmodel()
importlib.reload(mod)
mod.sqlite_url = "sqlit... | .model_validate becomes independent of Pydantic v2
with warnings.catch_warnings(record=True):
warnings.simplefilter("always")
# No heroes before creating
response = client.get("heroes/")
assert response.status_code == 20 | ean up connection explicitly to avoid resource warning
mod.engine.dispose()
def test_crud_app(client: TestClient):
# TODO: this warns that SQLModel.from_orm is deprecated in Pydantic v1, refactor
# this if using obj | {
"filepath": "tests/test_tutorial/test_sql_databases/test_tutorial002.py",
"language": "python",
"file_size": 18386,
"cut_index": 1331,
"middle_length": 229
} |
pytest
from fastapi.testclient import TestClient
from inline_snapshot import snapshot
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial005_py310"),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.extra_models.{request.param}")
cl... | shot(
{
"openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/keyword-weights/": {
"get": {
"responses": {
| assert response.json() == {"foo": 2.3, "bar": 3.4}
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == snap | {
"filepath": "tests/test_tutorial/test_extra_models/test_tutorial005.py",
"language": "python",
"file_size": 1911,
"cut_index": 537,
"middle_length": 229
} |
rial001_an_py310",
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.request_forms.{request.param}")
client = TestClient(mod.app)
return client
def test_post_body_form(client: TestClient):
response = client.post("/login/", data={"username": "Foo", "pass... | "msg": "Field required",
"input": None,
}
]
}
def test_post_body_form_no_username(client: TestClient):
response = client.post("/login/", data={"password": "secret"})
assert response.status_code == | ("/login/", data={"username": "Foo"})
assert response.status_code == 422
assert response.json() == {
"detail": [
{
"type": "missing",
"loc": ["body", "password"],
| {
"filepath": "tests/test_tutorial/test_request_forms/test_tutorial001.py",
"language": "python",
"file_size": 6457,
"cut_index": 716,
"middle_length": 229
} |
pytest.param("tutorial003_py310", marks=needs_py310),
pytest.param("tutorial003_an_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.body_multiple_params.{request.param}")
client = TestClient(mod.app)
return client
def te... | ce": 50.5,
"description": None,
"tax": None,
},
"user": {"username": "Dave", "full_name": None},
}
def test_post_body_no_data(client: TestClient):
response = client.put("/items/5", json=None)
assert res | "user": {"username": "Dave"},
},
)
assert response.status_code == 200
assert response.json() == {
"item_id": 5,
"importance": 2,
"item": {
"name": "Foo",
"pri | {
"filepath": "tests/test_tutorial/test_body_multiple_params/test_tutorial003.py",
"language": "python",
"file_size": 8248,
"cut_index": 716,
"middle_length": 229
} |
om tests.utils import workdir_lock
@workdir_lock
def test_main():
if os.path.isdir("./static"): # pragma: nocover
shutil.rmtree("./static")
if os.path.isdir("./templates"): # pragma: nocover
shutil.rmtree("./templates")
shutil.copytree("./docs_src/templates/templates/", "./templates")
... | assert (
b'<h1><a href="http://testserver/items/foo">Item ID: foo</a></h1>'
in response.content
)
response = client.get("/static/styles.css")
assert response.status_code == 200, response.text
assert b"color: green;" in re | assert response.status_code == 200, response.text
| {
"filepath": "tests/test_tutorial/test_templates/test_tutorial001.py",
"language": "python",
"file_size": 968,
"cut_index": 582,
"middle_length": 52
} |
snapshot
from ...utils import needs_py310
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial002_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.query_params.{request.param}")
client = TestClient(mod.ap... | assert response.json() == expected_json
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200
assert response.json() == snapshot(
{
"openapi": "3.1.0",
| ms/bar?q=somequery",
{"item_id": "bar", "q": "somequery"},
),
],
)
def test_read_user_item(client: TestClient, path, expected_json):
response = client.get(path)
assert response.status_code == 200
| {
"filepath": "tests/test_tutorial/test_query_params/test_tutorial002.py",
"language": "python",
"file_size": 4856,
"cut_index": 614,
"middle_length": 229
} |
mport importlib
from types import ModuleType
import pytest
from pytest import MonkeyPatch
@pytest.fixture(
name="mod_path",
params=[
pytest.param("app02_py310"),
pytest.param("app02_an_py310"),
],
)
def get_mod_path(request: pytest.FixtureRequest):
mod_path = f"docs_src.settings.{requ... | monkeypatch: MonkeyPatch):
monkeypatch.setenv("ADMIN_EMAIL", "admin@example.com")
settings = main_mod.get_settings()
assert settings.app_name == "Awesome API"
assert settings.items_per_user == 50
def test_override_settings(test_main_mod: |
@pytest.fixture(name="test_main_mod")
def get_test_main_mod(mod_path: str) -> ModuleType:
test_main_mod = importlib.import_module(f"{mod_path}.test_main")
return test_main_mod
def test_settings(main_mod: ModuleType, | {
"filepath": "tests/test_tutorial/test_settings/test_app02.py",
"language": "python",
"file_size": 1040,
"cut_index": 513,
"middle_length": 229
} |
importlib
import pytest
from fastapi.testclient import TestClient
@pytest.fixture(
name="client",
params=[
"tutorial001_py310",
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.strict_content_type.{request.param}")
client = TestClient(mod.app)... | ems/",
json={"name": "Foo", "price": 50.5},
)
assert response.status_code == 200, response.text
assert response.json() == {"name": "Foo", "price": 50.5}
def test_lax_post_with_text_plain_is_still_rejected(client: TestClient):
resp | }',
)
assert response.status_code == 200, response.text
assert response.json() == {"name": "Foo", "price": 50.5}
def test_lax_post_with_json_content_type(client: TestClient):
response = client.post(
"/it | {
"filepath": "tests/test_tutorial/test_strict_content_type/test_tutorial001.py",
"language": "python",
"file_size": 1200,
"cut_index": 518,
"middle_length": 229
} |
pytest.param("tutorial005_py310", marks=needs_py310),
pytest.param("tutorial005_an_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.body_multiple_params.{request.param}")
client = TestClient(mod.app)
return client
def te... | "price": 50.5,
"description": "Some Foo",
"tax": 0.1,
},
}
def test_post_required(client: TestClient):
response = client.put(
"/items/5",
json={
"item": {"name": "Foo", "price | "description": "Some Foo",
"tax": 0.1,
},
},
)
assert response.status_code == 200
assert response.json() == {
"item_id": 5,
"item": {
"name": "Foo",
| {
"filepath": "tests/test_tutorial/test_body_multiple_params/test_tutorial005.py",
"language": "python",
"file_size": 9106,
"cut_index": 716,
"middle_length": 229
} |
pytest.param("tutorial003_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.extra_models.{request.param}")
client = TestClient(mod.app)
return client
def test_get_car(client: TestClient):
response = client.get("/items/item1")... | type": "plane",
"size": 5,
}
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == snapshot(
{
"openapi" | ane(client: TestClient):
response = client.get("/items/item2")
assert response.status_code == 200, response.text
assert response.json() == {
"description": "Music is my aeroplane, it's my aeroplane",
" | {
"filepath": "tests/test_tutorial/test_extra_models/test_tutorial003.py",
"language": "python",
"file_size": 6250,
"cut_index": 716,
"middle_length": 229
} |
from inline_snapshot import snapshot
from ...utils import needs_py310
@pytest.fixture(
name="mod",
params=[
pytest.param("tutorial001_py310", marks=needs_py310),
pytest.param("tutorial001_an_py310", marks=needs_py310),
],
)
def get_mod(request: pytest.FixtureRequest):
mod = importlib... | : "ads_track"},
),
("/items", {"session": "cookiesession"}, 200, {"ads_id": None}),
],
)
def test(path, cookies, expected_status, expected_response, mod: ModuleType):
client = TestClient(mod.app, cookies=cookies)
response = clie | {"ads_id": None}),
("/items", {"ads_id": "ads_track"}, 200, {"ads_id": "ads_track"}),
(
"/items",
{"ads_id": "ads_track", "session": "cookiesession"},
200,
{"ads_id" | {
"filepath": "tests/test_tutorial/test_cookie_params/test_tutorial001.py",
"language": "python",
"file_size": 4619,
"cut_index": 614,
"middle_length": 229
} |
pytest.param("tutorial001_py310", marks=needs_py310),
pytest.param("tutorial001_an_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.cookie_param_models.{request.param}")
client = TestClient(mod.app)
return client
def ... | ef test_cookie_param_model_defaults(client: TestClient):
with client as c:
c.cookies.set("session_id", "123")
response = c.get("/items/")
assert response.status_code == 200
assert response.json() == {
"session_id": "123" | acker", "789")
response = c.get("/items/")
assert response.status_code == 200
assert response.json() == {
"session_id": "123",
"fatebook_tracker": "456",
"googall_tracker": "789",
}
d | {
"filepath": "tests/test_tutorial/test_cookie_param_models/test_tutorial001.py",
"language": "python",
"file_size": 6256,
"cut_index": 716,
"middle_length": 229
} |
unpy
import sys
import unittest
import pytest
from fastapi.testclient import TestClient
from inline_snapshot import snapshot
MOD_NAME = "docs_src.debugging.tutorial001_py310"
@pytest.fixture(name="client")
def get_client():
mod = importlib.import_module(MOD_NAME)
client = TestClient(mod.app)
return clie... | son() == {"hello world": "ba"}
def test_uvicorn_run_called_when_run_as_main(): # Just for coverage
if sys.modules.get(MOD_NAME):
del sys.modules[MOD_NAME]
with unittest.mock.patch("uvicorn.run") as uvicorn_run_mock:
runpy.run_mod | orn_run_mock:
importlib.import_module(MOD_NAME)
uvicorn_run_mock.assert_not_called()
def test_get_root(client: TestClient):
response = client.get("/")
assert response.status_code == 200
assert response.j | {
"filepath": "tests/test_tutorial/test_debugging/test_tutorial001.py",
"language": "python",
"file_size": 1962,
"cut_index": 537,
"middle_length": 229
} |
rial001_an_py310",
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.request_files.{request.param}")
client = TestClient(mod.app)
return client
def test_post_form_no_body(client: TestClient):
response = client.post("/files/")
assert response.status_... | ponse.text
assert response.json() == {
"detail": [
{
"type": "missing",
"loc": ["body", "file"],
"msg": "Field required",
"input": None,
}
]
}
| "Field required",
"input": None,
}
]
}
def test_post_body_json(client: TestClient):
response = client.post("/files/", json={"file": "Foo"})
assert response.status_code == 422, res | {
"filepath": "tests/test_tutorial/test_request_files/test_tutorial001.py",
"language": "python",
"file_size": 8267,
"cut_index": 716,
"middle_length": 229
} |
utorial001_03_an_py310",
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.request_files.{request.param}")
client = TestClient(mod.app)
return client
def test_post_file(tmp_path, client: TestClient):
path = tmp_path / "test.txt"
path.write_bytes(b"<... | adfile/", files={"file": file})
assert response.status_code == 200, response.text
assert response.json() == {"filename": "test.txt"}
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.statu | n() == {"file_size": 14}
def test_post_upload_file(tmp_path, client: TestClient):
path = tmp_path / "test.txt"
path.write_bytes(b"<file content>")
with path.open("rb") as file:
response = client.post("/uplo | {
"filepath": "tests/test_tutorial/test_request_files/test_tutorial001_03.py",
"language": "python",
"file_size": 7211,
"cut_index": 716,
"middle_length": 229
} |
003_py310",
"tutorial003_an_py310",
],
)
def get_app(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.request_files.{request.param}")
return mod.app
@pytest.fixture(name="client")
def get_client(app: FastAPI):
client = TestClient(app)
return client
def test_post... | ("test2.txt", file2)),
),
)
assert response.status_code == 200, response.text
assert response.json() == {"file_sizes": [14, 15]}
def test_post_upload_file(tmp_path, app: FastAPI):
path = tmp_path / "test.txt"
path.writ | t = TestClient(app)
with path.open("rb") as file, path2.open("rb") as file2:
response = client.post(
"/files/",
files=(
("files", ("test.txt", file)),
("files", | {
"filepath": "tests/test_tutorial/test_request_files/test_tutorial003.py",
"language": "python",
"file_size": 8806,
"cut_index": 716,
"middle_length": 229
} |
rial002_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_post_body_form(client: TestClient):
response = client.post("/login/", data={"username": "Foo",... | "type": "extra_forbidden",
"loc": ["body", "extra"],
"msg": "Extra inputs are not permitted",
"input": "extra",
}
]
}
def test_post_body_form_no_password(client: TestClient):
| response = client.post(
"/login/", data={"username": "Foo", "password": "secret", "extra": "extra"}
)
assert response.status_code == 422
assert response.json() == {
"detail": [
{
| {
"filepath": "tests/test_tutorial/test_request_form_models/test_tutorial002.py",
"language": "python",
"file_size": 6927,
"cut_index": 716,
"middle_length": 229
} |
astapi.testclient import TestClient
from inline_snapshot import snapshot
from docs_src.path_operation_advanced_configuration.tutorial002_py310 import app
client = TestClient(app)
def test_get():
response = client.get("/items/")
assert response.status_code == 200, response.text
assert response.json() == ... | "200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
}
},
| {
"openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/items/": {
"get": {
"responses": {
| {
"filepath": "tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial002.py",
"language": "python",
"file_size": 1155,
"cut_index": 518,
"middle_length": 229
} |
pytest.param("tutorial006_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.query_params.{request.param}")
c = TestClient(mod.app)
return c
def test_foo_needy_very(client: TestClient):
response = client.get("/items/foo?needy=... | "loc": ["query", "needy"],
"msg": "Field required",
"input": None,
},
{
"type": "int_parsing",
"loc": ["query", "skip"],
"msg": "Input shoul | no_needy(client: TestClient):
response = client.get("/items/foo?skip=a&limit=b")
assert response.status_code == 422
assert response.json() == {
"detail": [
{
"type": "missing",
| {
"filepath": "tests/test_tutorial/test_query_params/test_tutorial006.py",
"language": "python",
"file_size": 5984,
"cut_index": 716,
"middle_length": 229
} |
fixture(name="client")
def get_client(app: FastAPI):
client = TestClient(app)
return client
def test_post_form_no_body(client: TestClient):
response = client.post("/files/")
assert response.status_code == 422, response.text
assert response.json() == {
"detail": [
{
... | ssing",
"loc": ["body", "files"],
"msg": "Field required",
"input": None,
}
]
}
def test_post_files(tmp_path, app: FastAPI):
path = tmp_path / "test.txt"
path.write_bytes(b"< | (client: TestClient):
response = client.post("/files/", json={"file": "Foo"})
assert response.status_code == 422, response.text
assert response.json() == {
"detail": [
{
"type": "mi | {
"filepath": "tests/test_tutorial/test_request_files/test_tutorial002.py",
"language": "python",
"file_size": 9446,
"cut_index": 921,
"middle_length": 229
} |
astapi.testclient import TestClient
from inline_snapshot import snapshot
from docs_src.path_operation_advanced_configuration.tutorial001_py310 import app
client = TestClient(app)
def test_get():
response = client.get("/items/")
assert response.status_code == 200, response.text
assert response.json() == ... | "200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
}
},
| {
"openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/items/": {
"get": {
"responses": {
| {
"filepath": "tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial001.py",
"language": "python",
"file_size": 1172,
"cut_index": 518,
"middle_length": 229
} |
rial001_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_post_body_form(client: TestClient):
response = client.post("/login/", data={"username": "Foo",... | ["body", "password"],
"msg": "Field required",
"input": {"username": "Foo"},
}
]
}
def test_post_body_form_no_username(client: TestClient):
response = client.post("/login/", data={"password": "s | :
response = client.post("/login/", data={"username": "Foo"})
assert response.status_code == 422
assert response.json() == {
"detail": [
{
"type": "missing",
"loc": | {
"filepath": "tests/test_tutorial/test_request_form_models/test_tutorial001.py",
"language": "python",
"file_size": 6390,
"cut_index": 716,
"middle_length": 229
} |
stapi.testclient import TestClient
from inline_snapshot import snapshot
def get_client() -> TestClient:
from docs_src.conditional_openapi import tutorial001_py310
importlib.reload(tutorial001_py310)
client = TestClient(tutorial001_py310.app)
return client
def test_disable_openapi(monkeypatch):
... | get_client()
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Hello World"}
def test_default_openapi():
client = get_client()
response = client.get("/docs")
assert response.statu | 04, response.text
response = client.get("/docs")
assert response.status_code == 404, response.text
response = client.get("/redoc")
assert response.status_code == 404, response.text
def test_root():
client = | {
"filepath": "tests/test_tutorial/test_conditional_openapi/test_tutorial001.py",
"language": "python",
"file_size": 1853,
"cut_index": 537,
"middle_length": 229
} |
pytest.param("tutorial004_py310", marks=needs_py310),
pytest.param("tutorial004_an_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(
f"docs_src.query_params_str_validations.{request.param}"
)
client = TestClient(mod.app)
... | assert response.json() == {
"items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
"q": "fixedquery",
}
def test_query_params_str_validations_q_nonregexquery(client: TestClient):
response = client.get("/items/", params={"q": "nonre | items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
def test_query_params_str_validations_q_fixedquery(client: TestClient):
response = client.get("/items/", params={"q": "fixedquery"})
assert response.status_code == 200
| {
"filepath": "tests/test_tutorial/test_query_params_str_validations/test_tutorial004.py",
"language": "python",
"file_size": 5381,
"cut_index": 716,
"middle_length": 229
} |
pytest.param("tutorial006c_py310", marks=needs_py310),
pytest.param("tutorial006c_an_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(
f"docs_src.query_params_str_validations.{request.param}"
)
client = TestClient(mod.app)
... | son="Code example is not valid. See https://github.com/fastapi/fastapi/issues/12419"
)
def test_query_params_str_validations_empty_str(client: TestClient):
response = client.get("/items/?q=")
assert response.status_code == 200
assert response.j | ient):
response = client.get("/items/")
assert response.status_code == 200
assert response.json() == { # pragma: no cover
"items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
}
@pytest.mark.xfail(
rea | {
"filepath": "tests/test_tutorial/test_query_params_str_validations/test_tutorial006c.py",
"language": "python",
"file_size": 5584,
"cut_index": 716,
"middle_length": 229
} |
pytest.param("tutorial008_py310", marks=needs_py310),
pytest.param("tutorial008_an_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(
f"docs_src.query_params_str_validations.{request.param}"
)
client = TestClient(mod.app)
... | assert response.json() == {
"items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
"q": "fixedquery",
}
def test_query_params_str_validations_q_fixedquery_too_short(client: TestClient):
response = client.get("/items/", params={"q": | items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
def test_query_params_str_validations_q_fixedquery(client: TestClient):
response = client.get("/items/", params={"q": "fixedquery"})
assert response.status_code == 200
| {
"filepath": "tests/test_tutorial/test_query_params_str_validations/test_tutorial008.py",
"language": "python",
"file_size": 5482,
"cut_index": 716,
"middle_length": 229
} |
snapshot
from ...utils import needs_py310
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial009_py310", marks=needs_py310),
pytest.param("tutorial009_an_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(
... | ent):
response = client.get("/items/", params={"item-query": "fixedquery"})
assert response.status_code == 200
assert response.json() == {
"items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
"q": "fixedquery",
}
def test_qu | sponse = client.get("/items/")
assert response.status_code == 200
assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
def test_query_params_str_validations_item_query_fixedquery(client: TestCli | {
"filepath": "tests/test_tutorial/test_query_params_str_validations/test_tutorial009.py",
"language": "python",
"file_size": 4865,
"cut_index": 614,
"middle_length": 229
} |
snapshot
from ...utils import needs_py310
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial011_py310", marks=needs_py310),
pytest.param("tutorial011_an_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(
... | ient.get(url)
assert response.status_code == 200, response.text
assert response.json() == {"q": None}
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
| q=bar"
response = client.get(url)
assert response.status_code == 200, response.text
assert response.json() == {"q": ["foo", "bar"]}
def test_query_no_values(client: TestClient):
url = "/items/"
response = cl | {
"filepath": "tests/test_tutorial/test_query_params_str_validations/test_tutorial011.py",
"language": "python",
"file_size": 4482,
"cut_index": 614,
"middle_length": 229
} |
snapshot
@pytest.fixture(
name="client",
params=[
"tutorial013_py310",
"tutorial013_an_py310",
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(
f"docs_src.query_params_str_validations.{request.param}"
)
client = TestClient(mod.app)
... | q": []}
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == snapshot(
{
"openapi": "3.1.0",
"info": {" | rt response.json() == {"q": ["foo", "bar"]}
def test_query_no_values(client: TestClient):
url = "/items/"
response = client.get(url)
assert response.status_code == 200, response.text
assert response.json() == {" | {
"filepath": "tests/test_tutorial/test_query_params_str_validations/test_tutorial013.py",
"language": "python",
"file_size": 4301,
"cut_index": 614,
"middle_length": 229
} |
"client",
params=[
pytest.param("tutorial015_an_py310", marks=[needs_py310]),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(
f"docs_src.query_params_str_validations.{request.param}"
)
client = TestClient(mod.app)
return client
def test_get_... | : "The Hitchhiker's Guide to the Galaxy",
}
def test_get_item_does_not_exist(client: TestClient):
response = client.get("/items?id=isbn-nope")
assert response.status_code == 200, response.text
assert response.json() == {"id": "isbn-nope", | test_get_item(client: TestClient):
response = client.get("/items?id=isbn-9781529046137")
assert response.status_code == 200, response.text
assert response.json() == {
"id": "isbn-9781529046137",
"name" | {
"filepath": "tests/test_tutorial/test_query_params_str_validations/test_tutorial015.py",
"language": "python",
"file_size": 5159,
"cut_index": 716,
"middle_length": 229
} |
al001_py310 import client, test_read_main
def test_main():
test_read_main()
def test_openapi_schema():
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == snapshot(
{
"openapi": "3.1.0",
"info": {"title": ... | sponse",
"content": {"application/json": {"schema": {}}},
}
},
"summary": "Read Main",
"operationId": "read_main__get",
| "description": "Successful Re | {
"filepath": "tests/test_tutorial/test_testing/test_tutorial001.py",
"language": "python",
"file_size": 957,
"cut_index": 582,
"middle_length": 52
} |
am}")
client = TestClient(mod.app)
return client
def test_body_float(client: TestClient):
response = client.post("/items/", json={"name": "Foo", "price": 50.5})
assert response.status_code == 200
assert response.json() == {
"name": "Foo",
"price": 50.5,
"description": None... | (
"/items/", json={"name": "Foo", "price": "50.5", "description": "Some Foo"}
)
assert response.status_code == 200
assert response.json() == {
"name": "Foo",
"price": 50.5,
"description": "Some Foo",
"tax | 200
assert response.json() == {
"name": "Foo",
"price": 50.5,
"description": None,
"tax": None,
}
def test_post_with_str_float_description(client: TestClient):
response = client.post | {
"filepath": "tests/test_tutorial/test_body/test_tutorial001.py",
"language": "python",
"file_size": 11331,
"cut_index": 921,
"middle_length": 229
} |
pytest.param("tutorial003_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.body.{request.param}")
client = TestClient(mod.app)
return client
def test_put_all(client: TestClient):
response = client.put(
"/items/123",
... | price": 50.1},
)
assert response.status_code == 200
assert response.json() == {
"item_id": 123,
"name": "Foo",
"price": 50.1,
"description": None,
"tax": None,
}
def test_put_with_no_data(client: Te | "name": "Foo",
"price": 50.1,
"description": "Some Foo",
"tax": 0.3,
}
def test_put_only_required(client: TestClient):
response = client.put(
"/items/123",
json={"name": "Foo", " | {
"filepath": "tests/test_tutorial/test_body/test_tutorial003.py",
"language": "python",
"file_size": 6276,
"cut_index": 716,
"middle_length": 229
} |
t",
params=[
pytest.param("tutorial001_02_py310", marks=needs_py310),
pytest.param("tutorial001_02_an_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.request_files.{request.param}")
client = TestClient(mod.app)
... | file sent"}
def test_post_file(tmp_path: Path, client: TestClient):
path = tmp_path / "test.txt"
path.write_bytes(b"<file content>")
with path.open("rb") as file:
response = client.post("/files/", files={"file": file})
assert re | ssage": "No file sent"}
def test_post_uploadfile_no_body(client: TestClient):
response = client.post("/uploadfile/")
assert response.status_code == 200, response.text
assert response.json() == {"message": "No upload | {
"filepath": "tests/test_tutorial/test_request_files/test_tutorial001_02.py",
"language": "python",
"file_size": 7883,
"cut_index": 716,
"middle_length": 229
} |
ne_snapshot import snapshot
from docs_src.path_operation_advanced_configuration.tutorial006_py310 import app
client = TestClient(app)
def test_post():
response = client.post("/items/", content=b"this is actually not validated")
assert response.status_code == 200, response.text
assert response.json() == ... | API", "version": "0.1.0"},
"paths": {
"/items/": {
"post": {
"summary": "Create Item",
"operationId": "create_item_items__post",
"reques | napi_schema():
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == snapshot(
{
"openapi": "3.1.0",
"info": {"title": "Fast | {
"filepath": "tests/test_tutorial/test_path_operation_advanced_configurations/test_tutorial006.py",
"language": "python",
"file_size": 2186,
"cut_index": 563,
"middle_length": 229
} |
snapshot
from ...utils import needs_py310
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial001_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(
f"docs_src.query_params_str_validations.{request.param}"
)
... | ponse.status_code == 200
assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
def test_query_params_str_validations_q_query(client: TestClient):
response = client.get("/items/", params={"q": "query"})
assert response. | 200
assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
def test_query_params_str_validations_q_empty_str(client: TestClient):
response = client.get("/items/", params={"q": ""})
assert res | {
"filepath": "tests/test_tutorial/test_query_params_str_validations/test_tutorial001.py",
"language": "python",
"file_size": 4738,
"cut_index": 614,
"middle_length": 229
} |
pytest.param("tutorial003_py310", marks=needs_py310),
pytest.param("tutorial003_an_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(
f"docs_src.query_params_str_validations.{request.param}"
)
client = TestClient(mod.app)
... | response.json() == {
"items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
"q": "query",
}
def test_query_params_str_validations_q_too_short(client: TestClient):
response = client.get("/items/", params={"q": "qu"})
assert respons | items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
def test_query_params_str_validations_q_query(client: TestClient):
response = client.get("/items/", params={"q": "query"})
assert response.status_code == 200
assert | {
"filepath": "tests/test_tutorial/test_query_params_str_validations/test_tutorial003.py",
"language": "python",
"file_size": 5745,
"cut_index": 716,
"middle_length": 229
} |
snapshot
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial006_py310"),
pytest.param("tutorial006_an_py310"),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(
f"docs_src.query_params_str_validations.{request.param}"
)
cli... | ]
}
def test_query_params_str_validations_q_fixedquery(client: TestClient):
response = client.get("/items/", params={"q": "fixedquery"})
assert response.status_code == 200
assert response.json() == {
"items": [{"item_id": "Foo" | assert response.json() == {
"detail": [
{
"type": "missing",
"loc": ["query", "q"],
"msg": "Field required",
"input": None,
}
| {
"filepath": "tests/test_tutorial/test_query_params_str_validations/test_tutorial006.py",
"language": "python",
"file_size": 5057,
"cut_index": 614,
"middle_length": 229
} |
pytest.param("tutorial007_py310", marks=needs_py310),
pytest.param("tutorial007_an_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(
f"docs_src.query_params_str_validations.{request.param}"
)
client = TestClient(mod.app)
... | assert response.json() == {
"items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
"q": "fixedquery",
}
def test_query_params_str_validations_q_fixedquery_too_short(client: TestClient):
response = client.get("/items/", params={"q": | items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
def test_query_params_str_validations_q_fixedquery(client: TestClient):
response = client.get("/items/", params={"q": "fixedquery"})
assert response.status_code == 200
| {
"filepath": "tests/test_tutorial/test_query_params_str_validations/test_tutorial007.py",
"language": "python",
"file_size": 5226,
"cut_index": 716,
"middle_length": 229
} |
snapshot
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial012_py310"),
pytest.param("tutorial012_an_py310"),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(
f"docs_src.query_params_str_validations.{request.param}"
)
cli... | ext
assert response.json() == {"q": ["baz", "foobar"]}
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == snapshot(
{
| onse.text
assert response.json() == {"q": ["foo", "bar"]}
def test_multi_query_values(client: TestClient):
url = "/items/?q=baz&q=foobar"
response = client.get(url)
assert response.status_code == 200, response.t | {
"filepath": "tests/test_tutorial/test_query_params_str_validations/test_tutorial012.py",
"language": "python",
"file_size": 4380,
"cut_index": 614,
"middle_length": 229
} |
snapshot
from ...utils import needs_py310
@pytest.fixture(
name="client",
params=[
pytest.param("tutorial014_py310", marks=needs_py310),
pytest.param("tutorial014_an_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(
... | assert response.status_code == 200, response.text
assert response.json() == {"hidden_query": "Not found"}
def test_openapi_schema(client: TestClient):
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
| ms?hidden_query=somevalue")
assert response.status_code == 200, response.text
assert response.json() == {"hidden_query": "somevalue"}
def test_no_hidden_query(client: TestClient):
response = client.get("/items")
| {
"filepath": "tests/test_tutorial/test_query_params_str_validations/test_tutorial014.py",
"language": "python",
"file_size": 3852,
"cut_index": 614,
"middle_length": 229
} |
pytest.param("tutorial002_py310", marks=needs_py310),
],
)
def get_client(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.body.{request.param}")
client = TestClient(mod.app)
return client
@pytest.mark.parametrize("price", ["50.5", 50.5])
def test_post_with_tax(client: TestCl... | 0.5])
def test_post_without_tax(client: TestClient, price: str | float):
response = client.post(
"/items/", json={"name": "Foo", "price": price, "description": "Some Foo"}
)
assert response.status_code == 200
assert response.json() | code == 200
assert response.json() == {
"name": "Foo",
"price": 50.5,
"description": "Some Foo",
"tax": 0.3,
"price_with_tax": 50.8,
}
@pytest.mark.parametrize("price", ["50.5", 5 | {
"filepath": "tests/test_tutorial/test_body/test_tutorial002.py",
"language": "python",
"file_size": 5945,
"cut_index": 716,
"middle_length": 229
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.