Simplify tests for code examples, one test file for multiple variants (#1664)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Motov Yurii
2025-12-24 16:47:05 +01:00
committed by GitHub
parent 4c4d5b44ff
commit 4ea6a32f6f
186 changed files with 1769 additions and 14628 deletions

View File

@@ -1,18 +1,34 @@
import importlib
from types import ModuleType
import pytest
from dirty_equals import IsDict
from fastapi.testclient import TestClient
from sqlmodel import create_engine
from sqlmodel.pool import StaticPool
from tests.conftest import needs_py39, needs_py310
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.read_one import tutorial001 as mod
@pytest.fixture(
name="module",
params=[
"tutorial001",
pytest.param("tutorial001_py39", marks=needs_py39),
pytest.param("tutorial001_py310", marks=needs_py310),
],
)
def get_module(request: pytest.FixtureRequest) -> ModuleType:
mod = importlib.import_module(f"docs_src.tutorial.fastapi.read_one.{request.param}")
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
return mod
with TestClient(mod.app) as client:
def test_tutorial(module: ModuleType):
with TestClient(module.app) as client:
hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
hero2_data = {
"name": "Spider-Boy",

View File

@@ -1,219 +0,0 @@
from dirty_equals import IsDict
from fastapi.testclient import TestClient
from sqlmodel import create_engine
from sqlmodel.pool import StaticPool
from ....conftest import needs_py310
@needs_py310
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.read_one import tutorial001_py310 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,
}
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()
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
hero_id = hero2["id"]
response = client.get(f"/heroes/{hero_id}")
assert response.status_code == 200, response.text
data = response.json()
assert data == hero2
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == {
"openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/heroes/": {
"get": {
"summary": "Read Heroes",
"operationId": "read_heroes_heroes__get",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"title": "Response Read Heroes Heroes Get",
"type": "array",
"items": {
"$ref": "#/components/schemas/HeroPublic"
},
}
}
},
}
},
},
"post": {
"summary": "Create Hero",
"operationId": "create_hero_heroes__post",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HeroCreate"
}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HeroPublic"
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
},
"/heroes/{hero_id}": {
"get": {
"summary": "Read Hero",
"operationId": "read_hero_heroes__hero_id__get",
"parameters": [
{
"required": True,
"schema": {"title": "Hero Id", "type": "integer"},
"name": "hero_id",
"in": "path",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HeroPublic"
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
}
},
},
"components": {
"schemas": {
"HTTPValidationError": {
"title": "HTTPValidationError",
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "array",
"items": {
"$ref": "#/components/schemas/ValidationError"
},
}
},
},
"HeroCreate": {
"title": "HeroCreate",
"required": ["name", "secret_name"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": IsDict(
{
"title": "Age",
"anyOf": [{"type": "integer"}, {"type": "null"}],
}
)
| IsDict(
# TODO: remove when deprecating Pydantic v1
{"title": "Age", "type": "integer"}
),
},
},
"HeroPublic": {
"title": "HeroPublic",
"required": ["name", "secret_name", "id"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": IsDict(
{
"title": "Age",
"anyOf": [{"type": "integer"}, {"type": "null"}],
}
)
| IsDict(
# TODO: remove when deprecating Pydantic v1
{"title": "Age", "type": "integer"}
),
"id": {"title": "Id", "type": "integer"},
},
},
"ValidationError": {
"title": "ValidationError",
"required": ["loc", "msg", "type"],
"type": "object",
"properties": {
"loc": {
"title": "Location",
"type": "array",
"items": {
"anyOf": [{"type": "string"}, {"type": "integer"}]
},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
},
},
}
},
}

View File

@@ -1,219 +0,0 @@
from dirty_equals import IsDict
from fastapi.testclient import TestClient
from sqlmodel import create_engine
from sqlmodel.pool import StaticPool
from ....conftest import needs_py39
@needs_py39
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.read_one import tutorial001_py39 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,
}
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()
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
hero_id = hero2["id"]
response = client.get(f"/heroes/{hero_id}")
assert response.status_code == 200, response.text
data = response.json()
assert data == hero2
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/openapi.json")
assert response.status_code == 200, response.text
assert response.json() == {
"openapi": "3.1.0",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/heroes/": {
"get": {
"summary": "Read Heroes",
"operationId": "read_heroes_heroes__get",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"title": "Response Read Heroes Heroes Get",
"type": "array",
"items": {
"$ref": "#/components/schemas/HeroPublic"
},
}
}
},
}
},
},
"post": {
"summary": "Create Hero",
"operationId": "create_hero_heroes__post",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HeroCreate"
}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HeroPublic"
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
},
"/heroes/{hero_id}": {
"get": {
"summary": "Read Hero",
"operationId": "read_hero_heroes__hero_id__get",
"parameters": [
{
"required": True,
"schema": {"title": "Hero Id", "type": "integer"},
"name": "hero_id",
"in": "path",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HeroPublic"
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
}
},
},
"components": {
"schemas": {
"HTTPValidationError": {
"title": "HTTPValidationError",
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "array",
"items": {
"$ref": "#/components/schemas/ValidationError"
},
}
},
},
"HeroCreate": {
"title": "HeroCreate",
"required": ["name", "secret_name"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": IsDict(
{
"title": "Age",
"anyOf": [{"type": "integer"}, {"type": "null"}],
}
)
| IsDict(
# TODO: remove when deprecating Pydantic v1
{"title": "Age", "type": "integer"}
),
},
},
"HeroPublic": {
"title": "HeroPublic",
"required": ["name", "secret_name", "id"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": IsDict(
{
"title": "Age",
"anyOf": [{"type": "integer"}, {"type": "null"}],
}
)
| IsDict(
# TODO: remove when deprecating Pydantic v1
{"title": "Age", "type": "integer"}
),
"id": {"title": "Id", "type": "integer"},
},
},
"ValidationError": {
"title": "ValidationError",
"required": ["loc", "msg", "type"],
"type": "object",
"properties": {
"loc": {
"title": "Location",
"type": "array",
"items": {
"anyOf": [{"type": "string"}, {"type": "integer"}]
},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
},
},
}
},
}