Refactor OpenAPI FastAPI tests to simplify updating them later, this moves things around without changes (#671)

This commit is contained in:
Sebastián Ramírez 2023-10-23 19:33:08 +04:00 committed by GitHub
parent 189059e07e
commit dee70033b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 1999 additions and 1874 deletions

View File

@ -2,7 +2,64 @@ from fastapi.testclient import TestClient
from sqlmodel import create_engine
from sqlmodel.pool import StaticPool
openapi_schema = {
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.delete import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
hero3_data = {
"name": "Rusty-Man",
"secret_name": "Tommy Sharp",
"age": 48,
}
response = client.post("/heroes/", json=hero1_data)
assert response.status_code == 200, response.text
response = client.post("/heroes/", json=hero2_data)
assert response.status_code == 200, response.text
hero2 = response.json()
hero2_id = hero2["id"]
response = client.post("/heroes/", json=hero3_data)
assert response.status_code == 200, response.text
response = client.get(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 3
response = client.patch(
f"/heroes/{hero2_id}", json={"secret_name": "Spider-Youngster"}
)
assert response.status_code == 200, response.text
response = client.patch("/heroes/9001", json={"name": "Dragon Cube X"})
assert response.status_code == 404, response.text
response = client.delete(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
response = client.delete("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
@ -13,7 +70,11 @@ openapi_schema = {
"parameters": [
{
"required": False,
"schema": {"title": "Offset", "type": "integer", "default": 0},
"schema": {
"title": "Offset",
"type": "integer",
"default": 0,
},
"name": "offset",
"in": "query",
},
@ -37,7 +98,9 @@ openapi_schema = {
"schema": {
"title": "Response Read Heroes Heroes Get",
"type": "array",
"items": {"$ref": "#/components/schemas/HeroRead"},
"items": {
"$ref": "#/components/schemas/HeroRead"
},
}
}
},
@ -60,7 +123,9 @@ openapi_schema = {
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroCreate"}
"schema": {
"$ref": "#/components/schemas/HeroCreate"
}
}
},
"required": True,
@ -70,7 +135,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
"schema": {
"$ref": "#/components/schemas/HeroRead"
}
}
},
},
@ -104,7 +171,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
"schema": {
"$ref": "#/components/schemas/HeroRead"
}
}
},
},
@ -162,7 +231,9 @@ openapi_schema = {
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroUpdate"}
"schema": {
"$ref": "#/components/schemas/HeroUpdate"
}
}
},
"required": True,
@ -172,7 +243,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
"schema": {
"$ref": "#/components/schemas/HeroRead"
}
}
},
},
@ -199,7 +272,9 @@ openapi_schema = {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": "#/components/schemas/ValidationError"},
"items": {
"$ref": "#/components/schemas/ValidationError"
},
}
},
},
@ -250,61 +325,3 @@ openapi_schema = {
}
},
}
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.delete import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
hero3_data = {
"name": "Rusty-Man",
"secret_name": "Tommy Sharp",
"age": 48,
}
response = client.post("/heroes/", json=hero1_data)
assert response.status_code == 200, response.text
response = client.post("/heroes/", json=hero2_data)
assert response.status_code == 200, response.text
hero2 = response.json()
hero2_id = hero2["id"]
response = client.post("/heroes/", json=hero3_data)
assert response.status_code == 200, response.text
response = client.get(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == openapi_schema
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 3
response = client.patch(
f"/heroes/{hero2_id}", json={"secret_name": "Spider-Youngster"}
)
assert response.status_code == 200, response.text
response = client.patch("/heroes/9001", json={"name": "Dragon Cube X"})
assert response.status_code == 404, response.text
response = client.delete(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
response = client.delete("/heroes/9000")
assert response.status_code == 404, response.text

View File

@ -2,7 +2,69 @@ from fastapi.testclient import TestClient
from sqlmodel import create_engine
from sqlmodel.pool import StaticPool
openapi_schema = {
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.limit_and_offset import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
hero3_data = {
"name": "Rusty-Man",
"secret_name": "Tommy Sharp",
"age": 48,
}
response = client.post("/heroes/", json=hero1_data)
assert response.status_code == 200, response.text
response = client.post("/heroes/", json=hero2_data)
assert response.status_code == 200, response.text
hero2 = response.json()
hero_id = hero2["id"]
response = client.post("/heroes/", json=hero3_data)
assert response.status_code == 200, response.text
response = client.get(f"/heroes/{hero_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 3
response = client.get("/heroes/", params={"limit": 2})
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
assert data[0]["name"] == hero1_data["name"]
assert data[1]["name"] == hero2_data["name"]
response = client.get("/heroes/", params={"offset": 1})
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
assert data[0]["name"] == hero2_data["name"]
assert data[1]["name"] == hero3_data["name"]
response = client.get("/heroes/", params={"offset": 1, "limit": 1})
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 1
assert data[0]["name"] == hero2_data["name"]
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
@ -13,7 +75,11 @@ openapi_schema = {
"parameters": [
{
"required": False,
"schema": {"title": "Offset", "type": "integer", "default": 0},
"schema": {
"title": "Offset",
"type": "integer",
"default": 0,
},
"name": "offset",
"in": "query",
},
@ -37,7 +103,9 @@ openapi_schema = {
"schema": {
"title": "Response Read Heroes Heroes Get",
"type": "array",
"items": {"$ref": "#/components/schemas/HeroRead"},
"items": {
"$ref": "#/components/schemas/HeroRead"
},
}
}
},
@ -60,7 +128,9 @@ openapi_schema = {
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroCreate"}
"schema": {
"$ref": "#/components/schemas/HeroCreate"
}
}
},
"required": True,
@ -70,7 +140,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
"schema": {
"$ref": "#/components/schemas/HeroRead"
}
}
},
},
@ -104,7 +176,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
"schema": {
"$ref": "#/components/schemas/HeroRead"
}
}
},
},
@ -131,7 +205,9 @@ openapi_schema = {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": "#/components/schemas/ValidationError"},
"items": {
"$ref": "#/components/schemas/ValidationError"
},
}
},
},
@ -173,66 +249,3 @@ openapi_schema = {
}
},
}
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.limit_and_offset import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
hero3_data = {
"name": "Rusty-Man",
"secret_name": "Tommy Sharp",
"age": 48,
}
response = client.post("/heroes/", json=hero1_data)
assert response.status_code == 200, response.text
response = client.post("/heroes/", json=hero2_data)
assert response.status_code == 200, response.text
hero2 = response.json()
hero_id = hero2["id"]
response = client.post("/heroes/", json=hero3_data)
assert response.status_code == 200, response.text
response = client.get(f"/heroes/{hero_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == openapi_schema
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 3
response = client.get("/heroes/", params={"limit": 2})
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
assert data[0]["name"] == hero1_data["name"]
assert data[1]["name"] == hero2_data["name"]
response = client.get("/heroes/", params={"offset": 1})
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
assert data[0]["name"] == hero2_data["name"]
assert data[1]["name"] == hero3_data["name"]
response = client.get("/heroes/", params={"offset": 1, "limit": 1})
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 1
assert data[0]["name"] == hero2_data["name"]

View File

@ -2,7 +2,112 @@ from fastapi.testclient import TestClient
from sqlmodel import create_engine
from sqlmodel.pool import StaticPool
openapi_schema = {
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.relationships import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
team_preventers = {"name": "Preventers", "headquarters": "Sharp Tower"}
team_z_force = {"name": "Z-Force", "headquarters": "Sister Margarets Bar"}
response = client.post("/teams/", json=team_preventers)
assert response.status_code == 200, response.text
team_preventers_data = response.json()
team_preventers_id = team_preventers_data["id"]
response = client.post("/teams/", json=team_z_force)
assert response.status_code == 200, response.text
team_z_force_data = response.json()
team_z_force_id = team_z_force_data["id"]
response = client.get("/teams/")
data = response.json()
assert len(data) == 2
response = client.get("/teams/9000")
assert response.status_code == 404, response.text
response = client.patch(
f"/teams/{team_preventers_id}", json={"headquarters": "Preventers Tower"}
)
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == team_preventers["name"]
assert data["headquarters"] == "Preventers Tower"
response = client.patch("/teams/9000", json={"name": "Freedom League"})
assert response.status_code == 404, response.text
hero1_data = {
"name": "Deadpond",
"secret_name": "Dive Wilson",
"team_id": team_z_force_id,
}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
hero3_data = {
"name": "Rusty-Man",
"secret_name": "Tommy Sharp",
"age": 48,
"team_id": team_preventers_id,
}
response = client.post("/heroes/", json=hero1_data)
assert response.status_code == 200, response.text
hero1 = response.json()
hero1_id = hero1["id"]
response = client.post("/heroes/", json=hero2_data)
assert response.status_code == 200, response.text
hero2 = response.json()
hero2_id = hero2["id"]
response = client.post("/heroes/", json=hero3_data)
assert response.status_code == 200, response.text
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 3
response = client.get(f"/heroes/{hero1_id}")
assert response.status_code == 200, response.text
data = response.json()
assert data["name"] == hero1_data["name"]
assert data["team"]["name"] == team_z_force["name"]
response = client.patch(
f"/heroes/{hero2_id}", json={"secret_name": "Spider-Youngster"}
)
assert response.status_code == 200, response.text
response = client.patch("/heroes/9001", json={"name": "Dragon Cube X"})
assert response.status_code == 404, response.text
response = client.delete(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
response = client.delete("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get(f"/teams/{team_preventers_id}")
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == team_preventers_data["name"]
assert data["heroes"][0]["name"] == hero3_data["name"]
response = client.delete(f"/teams/{team_preventers_id}")
assert response.status_code == 200, response.text
response = client.delete("/teams/9000")
assert response.status_code == 404, response.text
response = client.get("/teams/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 1
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
@ -13,7 +118,11 @@ openapi_schema = {
"parameters": [
{
"required": False,
"schema": {"title": "Offset", "type": "integer", "default": 0},
"schema": {
"title": "Offset",
"type": "integer",
"default": 0,
},
"name": "offset",
"in": "query",
},
@ -37,7 +146,9 @@ openapi_schema = {
"schema": {
"title": "Response Read Heroes Heroes Get",
"type": "array",
"items": {"$ref": "#/components/schemas/HeroRead"},
"items": {
"$ref": "#/components/schemas/HeroRead"
},
}
}
},
@ -60,7 +171,9 @@ openapi_schema = {
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroCreate"}
"schema": {
"$ref": "#/components/schemas/HeroCreate"
}
}
},
"required": True,
@ -70,7 +183,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
"schema": {
"$ref": "#/components/schemas/HeroRead"
}
}
},
},
@ -164,7 +279,9 @@ openapi_schema = {
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroUpdate"}
"schema": {
"$ref": "#/components/schemas/HeroUpdate"
}
}
},
"required": True,
@ -174,7 +291,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
"schema": {
"$ref": "#/components/schemas/HeroRead"
}
}
},
},
@ -198,7 +317,11 @@ openapi_schema = {
"parameters": [
{
"required": False,
"schema": {"title": "Offset", "type": "integer", "default": 0},
"schema": {
"title": "Offset",
"type": "integer",
"default": 0,
},
"name": "offset",
"in": "query",
},
@ -222,7 +345,9 @@ openapi_schema = {
"schema": {
"title": "Response Read Teams Teams Get",
"type": "array",
"items": {"$ref": "#/components/schemas/TeamRead"},
"items": {
"$ref": "#/components/schemas/TeamRead"
},
}
}
},
@ -245,7 +370,9 @@ openapi_schema = {
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/TeamCreate"}
"schema": {
"$ref": "#/components/schemas/TeamCreate"
}
}
},
"required": True,
@ -255,7 +382,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/TeamRead"}
"schema": {
"$ref": "#/components/schemas/TeamRead"
}
}
},
},
@ -349,7 +478,9 @@ openapi_schema = {
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/TeamUpdate"}
"schema": {
"$ref": "#/components/schemas/TeamUpdate"
}
}
},
"required": True,
@ -359,7 +490,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/TeamRead"}
"schema": {
"$ref": "#/components/schemas/TeamRead"
}
}
},
},
@ -386,7 +519,9 @@ openapi_schema = {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": "#/components/schemas/ValidationError"},
"items": {
"$ref": "#/components/schemas/ValidationError"
},
}
},
},
@ -497,110 +632,3 @@ openapi_schema = {
}
},
}
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.relationships import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == openapi_schema
team_preventers = {"name": "Preventers", "headquarters": "Sharp Tower"}
team_z_force = {"name": "Z-Force", "headquarters": "Sister Margarets Bar"}
response = client.post("/teams/", json=team_preventers)
assert response.status_code == 200, response.text
team_preventers_data = response.json()
team_preventers_id = team_preventers_data["id"]
response = client.post("/teams/", json=team_z_force)
assert response.status_code == 200, response.text
team_z_force_data = response.json()
team_z_force_id = team_z_force_data["id"]
response = client.get("/teams/")
data = response.json()
assert len(data) == 2
response = client.get("/teams/9000")
assert response.status_code == 404, response.text
response = client.patch(
f"/teams/{team_preventers_id}", json={"headquarters": "Preventers Tower"}
)
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == team_preventers["name"]
assert data["headquarters"] == "Preventers Tower"
response = client.patch("/teams/9000", json={"name": "Freedom League"})
assert response.status_code == 404, response.text
hero1_data = {
"name": "Deadpond",
"secret_name": "Dive Wilson",
"team_id": team_z_force_id,
}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
hero3_data = {
"name": "Rusty-Man",
"secret_name": "Tommy Sharp",
"age": 48,
"team_id": team_preventers_id,
}
response = client.post("/heroes/", json=hero1_data)
assert response.status_code == 200, response.text
hero1 = response.json()
hero1_id = hero1["id"]
response = client.post("/heroes/", json=hero2_data)
assert response.status_code == 200, response.text
hero2 = response.json()
hero2_id = hero2["id"]
response = client.post("/heroes/", json=hero3_data)
assert response.status_code == 200, response.text
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 3
response = client.get(f"/heroes/{hero1_id}")
assert response.status_code == 200, response.text
data = response.json()
assert data["name"] == hero1_data["name"]
assert data["team"]["name"] == team_z_force["name"]
response = client.patch(
f"/heroes/{hero2_id}", json={"secret_name": "Spider-Youngster"}
)
assert response.status_code == 200, response.text
response = client.patch("/heroes/9001", json={"name": "Dragon Cube X"})
assert response.status_code == 404, response.text
response = client.delete(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
response = client.delete("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get(f"/teams/{team_preventers_id}")
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == team_preventers_data["name"]
assert data["heroes"][0]["name"] == hero3_data["name"]
response = client.delete(f"/teams/{team_preventers_id}")
assert response.status_code == 200, response.text
response = client.delete("/teams/9000")
assert response.status_code == 404, response.text
response = client.get("/teams/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 1

View File

@ -2,7 +2,64 @@ from fastapi.testclient import TestClient
from sqlmodel import create_engine
from sqlmodel.pool import StaticPool
openapi_schema = {
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.session_with_dependency import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
hero3_data = {
"name": "Rusty-Man",
"secret_name": "Tommy Sharp",
"age": 48,
}
response = client.post("/heroes/", json=hero1_data)
assert response.status_code == 200, response.text
response = client.post("/heroes/", json=hero2_data)
assert response.status_code == 200, response.text
hero2 = response.json()
hero2_id = hero2["id"]
response = client.post("/heroes/", json=hero3_data)
assert response.status_code == 200, response.text
response = client.get(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 3
response = client.patch(
f"/heroes/{hero2_id}", json={"secret_name": "Spider-Youngster"}
)
assert response.status_code == 200, response.text
response = client.patch("/heroes/9001", json={"name": "Dragon Cube X"})
assert response.status_code == 404, response.text
response = client.delete(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
response = client.delete("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
@ -13,7 +70,11 @@ openapi_schema = {
"parameters": [
{
"required": False,
"schema": {"title": "Offset", "type": "integer", "default": 0},
"schema": {
"title": "Offset",
"type": "integer",
"default": 0,
},
"name": "offset",
"in": "query",
},
@ -37,7 +98,9 @@ openapi_schema = {
"schema": {
"title": "Response Read Heroes Heroes Get",
"type": "array",
"items": {"$ref": "#/components/schemas/HeroRead"},
"items": {
"$ref": "#/components/schemas/HeroRead"
},
}
}
},
@ -60,7 +123,9 @@ openapi_schema = {
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroCreate"}
"schema": {
"$ref": "#/components/schemas/HeroCreate"
}
}
},
"required": True,
@ -70,7 +135,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
"schema": {
"$ref": "#/components/schemas/HeroRead"
}
}
},
},
@ -104,7 +171,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
"schema": {
"$ref": "#/components/schemas/HeroRead"
}
}
},
},
@ -162,7 +231,9 @@ openapi_schema = {
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroUpdate"}
"schema": {
"$ref": "#/components/schemas/HeroUpdate"
}
}
},
"required": True,
@ -172,7 +243,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
"schema": {
"$ref": "#/components/schemas/HeroRead"
}
}
},
},
@ -199,7 +272,9 @@ openapi_schema = {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": "#/components/schemas/ValidationError"},
"items": {
"$ref": "#/components/schemas/ValidationError"
},
}
},
},
@ -250,61 +325,3 @@ openapi_schema = {
}
},
}
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.session_with_dependency import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
hero3_data = {
"name": "Rusty-Man",
"secret_name": "Tommy Sharp",
"age": 48,
}
response = client.post("/heroes/", json=hero1_data)
assert response.status_code == 200, response.text
response = client.post("/heroes/", json=hero2_data)
assert response.status_code == 200, response.text
hero2 = response.json()
hero2_id = hero2["id"]
response = client.post("/heroes/", json=hero3_data)
assert response.status_code == 200, response.text
response = client.get(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == openapi_schema
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 3
response = client.patch(
f"/heroes/{hero2_id}", json={"secret_name": "Spider-Youngster"}
)
assert response.status_code == 200, response.text
response = client.patch("/heroes/9001", json={"name": "Dragon Cube X"})
assert response.status_code == 404, response.text
response = client.delete(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
response = client.delete("/heroes/9000")
assert response.status_code == 404, response.text

View File

@ -2,7 +2,99 @@ from fastapi.testclient import TestClient
from sqlmodel import create_engine
from sqlmodel.pool import StaticPool
openapi_schema = {
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.teams import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
hero3_data = {
"name": "Rusty-Man",
"secret_name": "Tommy Sharp",
"age": 48,
}
response = client.post("/heroes/", json=hero1_data)
assert response.status_code == 200, response.text
response = client.post("/heroes/", json=hero2_data)
assert response.status_code == 200, response.text
hero2 = response.json()
hero2_id = hero2["id"]
response = client.post("/heroes/", json=hero3_data)
assert response.status_code == 200, response.text
response = client.get(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 3
response = client.patch(
f"/heroes/{hero2_id}", json={"secret_name": "Spider-Youngster"}
)
assert response.status_code == 200, response.text
response = client.patch("/heroes/9001", json={"name": "Dragon Cube X"})
assert response.status_code == 404, response.text
response = client.delete(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
response = client.delete("/heroes/9000")
assert response.status_code == 404, response.text
team_preventers = {"name": "Preventers", "headquarters": "Sharp Tower"}
team_z_force = {"name": "Z-Force", "headquarters": "Sister Margarets Bar"}
response = client.post("/teams/", json=team_preventers)
assert response.status_code == 200, response.text
team_preventers_data = response.json()
team_preventers_id = team_preventers_data["id"]
response = client.post("/teams/", json=team_z_force)
assert response.status_code == 200, response.text
team_z_force_data = response.json()
team_z_force_data["id"]
response = client.get("/teams/")
data = response.json()
assert len(data) == 2
response = client.get(f"/teams/{team_preventers_id}")
data = response.json()
assert response.status_code == 200, response.text
assert data == team_preventers_data
response = client.get("/teams/9000")
assert response.status_code == 404, response.text
response = client.patch(
f"/teams/{team_preventers_id}", json={"headquarters": "Preventers Tower"}
)
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == team_preventers["name"]
assert data["headquarters"] == "Preventers Tower"
response = client.patch("/teams/9000", json={"name": "Freedom League"})
assert response.status_code == 404, response.text
response = client.delete(f"/teams/{team_preventers_id}")
assert response.status_code == 200, response.text
response = client.delete("/teams/9000")
assert response.status_code == 404, response.text
response = client.get("/teams/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 1
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
@ -13,7 +105,11 @@ openapi_schema = {
"parameters": [
{
"required": False,
"schema": {"title": "Offset", "type": "integer", "default": 0},
"schema": {
"title": "Offset",
"type": "integer",
"default": 0,
},
"name": "offset",
"in": "query",
},
@ -37,7 +133,9 @@ openapi_schema = {
"schema": {
"title": "Response Read Heroes Heroes Get",
"type": "array",
"items": {"$ref": "#/components/schemas/HeroRead"},
"items": {
"$ref": "#/components/schemas/HeroRead"
},
}
}
},
@ -60,7 +158,9 @@ openapi_schema = {
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroCreate"}
"schema": {
"$ref": "#/components/schemas/HeroCreate"
}
}
},
"required": True,
@ -70,7 +170,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
"schema": {
"$ref": "#/components/schemas/HeroRead"
}
}
},
},
@ -104,7 +206,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
"schema": {
"$ref": "#/components/schemas/HeroRead"
}
}
},
},
@ -162,7 +266,9 @@ openapi_schema = {
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroUpdate"}
"schema": {
"$ref": "#/components/schemas/HeroUpdate"
}
}
},
"required": True,
@ -172,7 +278,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
"schema": {
"$ref": "#/components/schemas/HeroRead"
}
}
},
},
@ -196,7 +304,11 @@ openapi_schema = {
"parameters": [
{
"required": False,
"schema": {"title": "Offset", "type": "integer", "default": 0},
"schema": {
"title": "Offset",
"type": "integer",
"default": 0,
},
"name": "offset",
"in": "query",
},
@ -220,7 +332,9 @@ openapi_schema = {
"schema": {
"title": "Response Read Teams Teams Get",
"type": "array",
"items": {"$ref": "#/components/schemas/TeamRead"},
"items": {
"$ref": "#/components/schemas/TeamRead"
},
}
}
},
@ -243,7 +357,9 @@ openapi_schema = {
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/TeamCreate"}
"schema": {
"$ref": "#/components/schemas/TeamCreate"
}
}
},
"required": True,
@ -253,7 +369,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/TeamRead"}
"schema": {
"$ref": "#/components/schemas/TeamRead"
}
}
},
},
@ -287,7 +405,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/TeamRead"}
"schema": {
"$ref": "#/components/schemas/TeamRead"
}
}
},
},
@ -345,7 +465,9 @@ openapi_schema = {
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/TeamUpdate"}
"schema": {
"$ref": "#/components/schemas/TeamUpdate"
}
}
},
"required": True,
@ -355,7 +477,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/TeamRead"}
"schema": {
"$ref": "#/components/schemas/TeamRead"
}
}
},
},
@ -382,7 +506,9 @@ openapi_schema = {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": "#/components/schemas/ValidationError"},
"items": {
"$ref": "#/components/schemas/ValidationError"
},
}
},
},
@ -463,96 +589,3 @@ openapi_schema = {
}
},
}
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.teams import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
hero3_data = {
"name": "Rusty-Man",
"secret_name": "Tommy Sharp",
"age": 48,
}
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == openapi_schema
response = client.post("/heroes/", json=hero1_data)
assert response.status_code == 200, response.text
response = client.post("/heroes/", json=hero2_data)
assert response.status_code == 200, response.text
hero2 = response.json()
hero2_id = hero2["id"]
response = client.post("/heroes/", json=hero3_data)
assert response.status_code == 200, response.text
response = client.get(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 3
response = client.patch(
f"/heroes/{hero2_id}", json={"secret_name": "Spider-Youngster"}
)
assert response.status_code == 200, response.text
response = client.patch("/heroes/9001", json={"name": "Dragon Cube X"})
assert response.status_code == 404, response.text
response = client.delete(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
response = client.delete("/heroes/9000")
assert response.status_code == 404, response.text
team_preventers = {"name": "Preventers", "headquarters": "Sharp Tower"}
team_z_force = {"name": "Z-Force", "headquarters": "Sister Margarets Bar"}
response = client.post("/teams/", json=team_preventers)
assert response.status_code == 200, response.text
team_preventers_data = response.json()
team_preventers_id = team_preventers_data["id"]
response = client.post("/teams/", json=team_z_force)
assert response.status_code == 200, response.text
team_z_force_data = response.json()
team_z_force_data["id"]
response = client.get("/teams/")
data = response.json()
assert len(data) == 2
response = client.get(f"/teams/{team_preventers_id}")
data = response.json()
assert response.status_code == 200, response.text
assert data == team_preventers_data
response = client.get("/teams/9000")
assert response.status_code == 404, response.text
response = client.patch(
f"/teams/{team_preventers_id}", json={"headquarters": "Preventers Tower"}
)
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == team_preventers["name"]
assert data["headquarters"] == "Preventers Tower"
response = client.patch("/teams/9000", json={"name": "Freedom League"})
assert response.status_code == 404, response.text
response = client.delete(f"/teams/{team_preventers_id}")
assert response.status_code == 200, response.text
response = client.delete("/teams/9000")
assert response.status_code == 404, response.text
response = client.get("/teams/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 1

View File

@ -2,7 +2,71 @@ from fastapi.testclient import TestClient
from sqlmodel import create_engine
from sqlmodel.pool import StaticPool
openapi_schema = {
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.update import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
hero3_data = {
"name": "Rusty-Man",
"secret_name": "Tommy Sharp",
"age": 48,
}
response = client.post("/heroes/", json=hero1_data)
assert response.status_code == 200, response.text
response = client.post("/heroes/", json=hero2_data)
assert response.status_code == 200, response.text
hero2 = response.json()
hero2_id = hero2["id"]
response = client.post("/heroes/", json=hero3_data)
assert response.status_code == 200, response.text
hero3 = response.json()
hero3_id = hero3["id"]
response = client.get(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 3
response = client.patch(
f"/heroes/{hero2_id}", json={"secret_name": "Spider-Youngster"}
)
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == hero2_data["name"], "The name should not be set to none"
assert (
data["secret_name"] == "Spider-Youngster"
), "The secret name should be updated"
response = client.patch(f"/heroes/{hero3_id}", json={"age": None})
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == hero3_data["name"]
assert data["age"] is None, (
"A field should be updatable to None, even if " "that's the default"
)
response = client.patch("/heroes/9001", json={"name": "Dragon Cube X"})
assert response.status_code == 404, response.text
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
@ -13,7 +77,11 @@ openapi_schema = {
"parameters": [
{
"required": False,
"schema": {"title": "Offset", "type": "integer", "default": 0},
"schema": {
"title": "Offset",
"type": "integer",
"default": 0,
},
"name": "offset",
"in": "query",
},
@ -37,7 +105,9 @@ openapi_schema = {
"schema": {
"title": "Response Read Heroes Heroes Get",
"type": "array",
"items": {"$ref": "#/components/schemas/HeroRead"},
"items": {
"$ref": "#/components/schemas/HeroRead"
},
}
}
},
@ -60,7 +130,9 @@ openapi_schema = {
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroCreate"}
"schema": {
"$ref": "#/components/schemas/HeroCreate"
}
}
},
"required": True,
@ -70,7 +142,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
"schema": {
"$ref": "#/components/schemas/HeroRead"
}
}
},
},
@ -104,7 +178,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
"schema": {
"$ref": "#/components/schemas/HeroRead"
}
}
},
},
@ -134,7 +210,9 @@ openapi_schema = {
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroUpdate"}
"schema": {
"$ref": "#/components/schemas/HeroUpdate"
}
}
},
"required": True,
@ -144,7 +222,9 @@ openapi_schema = {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
"schema": {
"$ref": "#/components/schemas/HeroRead"
}
}
},
},
@ -171,7 +251,9 @@ openapi_schema = {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": "#/components/schemas/ValidationError"},
"items": {
"$ref": "#/components/schemas/ValidationError"
},
}
},
},
@ -222,68 +304,3 @@ openapi_schema = {
}
},
}
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.update import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
hero3_data = {
"name": "Rusty-Man",
"secret_name": "Tommy Sharp",
"age": 48,
}
response = client.post("/heroes/", json=hero1_data)
assert response.status_code == 200, response.text
response = client.post("/heroes/", json=hero2_data)
assert response.status_code == 200, response.text
hero2 = response.json()
hero2_id = hero2["id"]
response = client.post("/heroes/", json=hero3_data)
assert response.status_code == 200, response.text
hero3 = response.json()
hero3_id = hero3["id"]
response = client.get(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == openapi_schema
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 3
response = client.patch(
f"/heroes/{hero2_id}", json={"secret_name": "Spider-Youngster"}
)
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == hero2_data["name"], "The name should not be set to none"
assert (
data["secret_name"] == "Spider-Youngster"
), "The secret name should be updated"
response = client.patch(f"/heroes/{hero3_id}", json={"age": None})
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == hero3_data["name"]
assert data["age"] is None, (
"A field should be updatable to None, even if " "that's the default"
)
response = client.patch("/heroes/9001", json={"name": "Dragon Cube X"})
assert response.status_code == 404, response.text