Add missing tests for code examples (#14569)

Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nils-Hero Lindemann <nilsherolindemann@proton.me>
This commit is contained in:
Motov Yurii
2025-12-26 11:43:02 +01:00
committed by GitHub
parent 5eb8d6ed8a
commit 3063ada72f
183 changed files with 10459 additions and 86 deletions

View File

@@ -0,0 +1,116 @@
import pytest
from fastapi.testclient import TestClient
from docs_src.path_params.tutorial001_py39 import app
client = TestClient(app)
@pytest.mark.parametrize(
("item_id", "expected_response"),
[
(1, {"item_id": "1"}),
("alice", {"item_id": "alice"}),
],
)
def test_get_items(item_id, expected_response):
response = client.get(f"/items/{item_id}")
assert response.status_code == 200, response.text
assert response.json() == expected_response
def test_openapi_schema():
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": {
"/items/{item_id}": {
"get": {
"operationId": "read_item_items__item_id__get",
"parameters": [
{
"in": "path",
"name": "item_id",
"required": True,
"schema": {
"title": "Item Id",
},
},
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {},
},
},
"description": "Successful Response",
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError",
},
},
},
"description": "Validation Error",
},
},
"summary": "Read Item",
},
},
},
"components": {
"schemas": {
"HTTPValidationError": {
"properties": {
"detail": {
"items": {
"$ref": "#/components/schemas/ValidationError",
},
"title": "Detail",
"type": "array",
},
},
"title": "HTTPValidationError",
"type": "object",
},
"ValidationError": {
"properties": {
"loc": {
"items": {
"anyOf": [
{
"type": "string",
},
{
"type": "integer",
},
],
},
"title": "Location",
"type": "array",
},
"msg": {
"title": "Message",
"type": "string",
},
"type": {
"title": "Error Type",
"type": "string",
},
},
"required": [
"loc",
"msg",
"type",
],
"title": "ValidationError",
"type": "object",
},
},
},
}

View File

@@ -0,0 +1,124 @@
from fastapi.testclient import TestClient
from docs_src.path_params.tutorial002_py39 import app
client = TestClient(app)
def test_get_items():
response = client.get("/items/1")
assert response.status_code == 200, response.text
assert response.json() == {"item_id": 1}
def test_get_items_invalid_id():
response = client.get("/items/item1")
assert response.status_code == 422, response.text
assert response.json() == {
"detail": [
{
"input": "item1",
"loc": ["path", "item_id"],
"msg": "Input should be a valid integer, unable to parse string as an integer",
"type": "int_parsing",
}
]
}
def test_openapi_schema():
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": {
"/items/{item_id}": {
"get": {
"operationId": "read_item_items__item_id__get",
"parameters": [
{
"in": "path",
"name": "item_id",
"required": True,
"schema": {
"title": "Item Id",
"type": "integer",
},
},
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {},
},
},
"description": "Successful Response",
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError",
},
},
},
"description": "Validation Error",
},
},
"summary": "Read Item",
},
},
},
"components": {
"schemas": {
"HTTPValidationError": {
"properties": {
"detail": {
"items": {
"$ref": "#/components/schemas/ValidationError",
},
"title": "Detail",
"type": "array",
},
},
"title": "HTTPValidationError",
"type": "object",
},
"ValidationError": {
"properties": {
"loc": {
"items": {
"anyOf": [
{
"type": "string",
},
{
"type": "integer",
},
],
},
"title": "Location",
"type": "array",
},
"msg": {
"title": "Message",
"type": "string",
},
"type": {
"title": "Error Type",
"type": "string",
},
},
"required": [
"loc",
"msg",
"type",
],
"title": "ValidationError",
"type": "object",
},
},
},
}

View File

@@ -0,0 +1,133 @@
import pytest
from fastapi.testclient import TestClient
from docs_src.path_params.tutorial003_py39 import app
client = TestClient(app)
@pytest.mark.parametrize(
("user_id", "expected_response"),
[
("me", {"user_id": "the current user"}),
("alice", {"user_id": "alice"}),
],
)
def test_get_users(user_id: str, expected_response: dict):
response = client.get(f"/users/{user_id}")
assert response.status_code == 200, response.text
assert response.json() == expected_response
def test_openapi_schema():
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": {
"/users/me": {
"get": {
"operationId": "read_user_me_users_me_get",
"responses": {
"200": {
"content": {
"application/json": {
"schema": {},
},
},
"description": "Successful Response",
},
},
"summary": "Read User Me",
},
},
"/users/{user_id}": {
"get": {
"operationId": "read_user_users__user_id__get",
"parameters": [
{
"in": "path",
"name": "user_id",
"required": True,
"schema": {
"title": "User Id",
"type": "string",
},
},
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {},
},
},
"description": "Successful Response",
},
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError",
},
},
},
"description": "Validation Error",
},
},
"summary": "Read User",
},
},
},
"components": {
"schemas": {
"HTTPValidationError": {
"properties": {
"detail": {
"items": {
"$ref": "#/components/schemas/ValidationError",
},
"title": "Detail",
"type": "array",
},
},
"title": "HTTPValidationError",
"type": "object",
},
"ValidationError": {
"properties": {
"loc": {
"items": {
"anyOf": [
{
"type": "string",
},
{
"type": "integer",
},
],
},
"title": "Location",
"type": "array",
},
"msg": {
"title": "Message",
"type": "string",
},
"type": {
"title": "Error Type",
"type": "string",
},
},
"required": [
"loc",
"msg",
"type",
],
"title": "ValidationError",
"type": "object",
},
},
},
}

View File

@@ -0,0 +1,44 @@
import asyncio
from fastapi.testclient import TestClient
from docs_src.path_params.tutorial003b_py39 import app, read_users2
client = TestClient(app)
def test_get_users():
response = client.get("/users")
assert response.status_code == 200, response.text
assert response.json() == ["Rick", "Morty"]
def test_read_users2(): # Just for coverage
assert asyncio.run(read_users2()) == ["Bean", "Elfo"]
def test_openapi_schema():
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": {
"/users": {
"get": {
"operationId": "read_users2_users_get",
"responses": {
"200": {
"content": {
"application/json": {
"schema": {},
},
},
"description": "Successful Response",
},
},
"summary": "Read Users2",
},
},
},
}