mirror of
https://github.com/fastapi/fastapi.git
synced 2026-04-12 02:51:27 -04:00
✨ Add new tutorial for SQL databases with SQLModel (#12285)
This commit is contained in:
committed by
GitHub
parent
13a18f80b3
commit
7daaac2bc3
@@ -1,419 +0,0 @@
|
||||
import importlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from dirty_equals import IsDict
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from ...utils import needs_pydanticv1
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def client(tmp_path_factory: pytest.TempPathFactory):
|
||||
tmp_path = tmp_path_factory.mktemp("data")
|
||||
cwd = os.getcwd()
|
||||
os.chdir(tmp_path)
|
||||
test_db = Path("./sql_app.db")
|
||||
if test_db.is_file(): # pragma: nocover
|
||||
test_db.unlink()
|
||||
# Import while creating the client to create the DB after starting the test session
|
||||
from docs_src.sql_databases.sql_app import main
|
||||
|
||||
# Ensure import side effects are re-executed
|
||||
importlib.reload(main)
|
||||
with TestClient(main.app) as c:
|
||||
yield c
|
||||
if test_db.is_file(): # pragma: nocover
|
||||
test_db.unlink()
|
||||
os.chdir(cwd)
|
||||
|
||||
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_create_user(client):
|
||||
test_user = {"email": "johndoe@example.com", "password": "secret"}
|
||||
response = client.post("/users/", json=test_user)
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert test_user["email"] == data["email"]
|
||||
assert "id" in data
|
||||
response = client.post("/users/", json=test_user)
|
||||
assert response.status_code == 400, response.text
|
||||
|
||||
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_get_user(client):
|
||||
response = client.get("/users/1")
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert "email" in data
|
||||
assert "id" in data
|
||||
|
||||
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_nonexistent_user(client):
|
||||
response = client.get("/users/999")
|
||||
assert response.status_code == 404, response.text
|
||||
|
||||
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_get_users(client):
|
||||
response = client.get("/users/")
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert "email" in data[0]
|
||||
assert "id" in data[0]
|
||||
|
||||
|
||||
# TODO: pv2 add Pydantic v2 version
|
||||
@needs_pydanticv1
|
||||
def test_create_item(client):
|
||||
item = {"title": "Foo", "description": "Something that fights"}
|
||||
response = client.post("/users/1/items/", json=item)
|
||||
assert response.status_code == 200, response.text
|
||||
item_data = response.json()
|
||||
assert item["title"] == item_data["title"]
|
||||
assert item["description"] == item_data["description"]
|
||||
assert "id" in item_data
|
||||
assert "owner_id" in item_data
|
||||
response = client.get("/users/1")
|
||||
assert response.status_code == 200, response.text
|
||||
user_data = response.json()
|
||||
item_to_check = [it for it in user_data["items"] if it["id"] == item_data["id"]][0]
|
||||
assert item_to_check["title"] == item["title"]
|
||||
assert item_to_check["description"] == item["description"]
|
||||
|
||||
|
||||
# TODO: pv2 add Pydantic v2 version
|
||||
@needs_pydanticv1
|
||||
def test_read_items(client):
|
||||
response = client.get("/items/")
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert data
|
||||
first_item = data[0]
|
||||
assert "title" in first_item
|
||||
assert "description" in first_item
|
||||
|
||||
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_openapi_schema(client: TestClient):
|
||||
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": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"title": "Response Read Users Users Get",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/User"},
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Read Users",
|
||||
"operationId": "read_users_users__get",
|
||||
"parameters": [
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Skip",
|
||||
"type": "integer",
|
||||
"default": 0,
|
||||
},
|
||||
"name": "skip",
|
||||
"in": "query",
|
||||
},
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Limit",
|
||||
"type": "integer",
|
||||
"default": 100,
|
||||
},
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
},
|
||||
],
|
||||
},
|
||||
"post": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/User"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Create User",
|
||||
"operationId": "create_user_users__post",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/UserCreate"}
|
||||
}
|
||||
},
|
||||
"required": True,
|
||||
},
|
||||
},
|
||||
},
|
||||
"/users/{user_id}": {
|
||||
"get": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/User"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Read User",
|
||||
"operationId": "read_user_users__user_id__get",
|
||||
"parameters": [
|
||||
{
|
||||
"required": True,
|
||||
"schema": {"title": "User Id", "type": "integer"},
|
||||
"name": "user_id",
|
||||
"in": "path",
|
||||
}
|
||||
],
|
||||
}
|
||||
},
|
||||
"/users/{user_id}/items/": {
|
||||
"post": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/Item"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Create Item For User",
|
||||
"operationId": "create_item_for_user_users__user_id__items__post",
|
||||
"parameters": [
|
||||
{
|
||||
"required": True,
|
||||
"schema": {"title": "User Id", "type": "integer"},
|
||||
"name": "user_id",
|
||||
"in": "path",
|
||||
}
|
||||
],
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/ItemCreate"}
|
||||
}
|
||||
},
|
||||
"required": True,
|
||||
},
|
||||
}
|
||||
},
|
||||
"/items/": {
|
||||
"get": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"title": "Response Read Items Items Get",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/Item"},
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Read Items",
|
||||
"operationId": "read_items_items__get",
|
||||
"parameters": [
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Skip",
|
||||
"type": "integer",
|
||||
"default": 0,
|
||||
},
|
||||
"name": "skip",
|
||||
"in": "query",
|
||||
},
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Limit",
|
||||
"type": "integer",
|
||||
"default": 100,
|
||||
},
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
},
|
||||
],
|
||||
}
|
||||
},
|
||||
},
|
||||
"components": {
|
||||
"schemas": {
|
||||
"ItemCreate": {
|
||||
"title": "ItemCreate",
|
||||
"required": ["title"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"title": {"title": "Title", "type": "string"},
|
||||
"description": IsDict(
|
||||
{
|
||||
"title": "Description",
|
||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{"title": "Description", "type": "string"}
|
||||
),
|
||||
},
|
||||
},
|
||||
"Item": {
|
||||
"title": "Item",
|
||||
"required": ["title", "id", "owner_id"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"title": {"title": "Title", "type": "string"},
|
||||
"description": IsDict(
|
||||
{
|
||||
"title": "Description",
|
||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{"title": "Description", "type": "string"},
|
||||
),
|
||||
"id": {"title": "Id", "type": "integer"},
|
||||
"owner_id": {"title": "Owner Id", "type": "integer"},
|
||||
},
|
||||
},
|
||||
"User": {
|
||||
"title": "User",
|
||||
"required": ["email", "id", "is_active"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"email": {"title": "Email", "type": "string"},
|
||||
"id": {"title": "Id", "type": "integer"},
|
||||
"is_active": {"title": "Is Active", "type": "boolean"},
|
||||
"items": {
|
||||
"title": "Items",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/Item"},
|
||||
"default": [],
|
||||
},
|
||||
},
|
||||
},
|
||||
"UserCreate": {
|
||||
"title": "UserCreate",
|
||||
"required": ["email", "password"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"email": {"title": "Email", "type": "string"},
|
||||
"password": {"title": "Password", "type": "string"},
|
||||
},
|
||||
},
|
||||
"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"},
|
||||
},
|
||||
},
|
||||
"HTTPValidationError": {
|
||||
"title": "HTTPValidationError",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"detail": {
|
||||
"title": "Detail",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/ValidationError"},
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -1,421 +0,0 @@
|
||||
import importlib
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from dirty_equals import IsDict
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from ...utils import needs_pydanticv1
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def client():
|
||||
test_db = Path("./sql_app.db")
|
||||
if test_db.is_file(): # pragma: nocover
|
||||
test_db.unlink()
|
||||
# Import while creating the client to create the DB after starting the test session
|
||||
from docs_src.sql_databases.sql_app import alt_main
|
||||
|
||||
# Ensure import side effects are re-executed
|
||||
importlib.reload(alt_main)
|
||||
|
||||
with TestClient(alt_main.app) as c:
|
||||
yield c
|
||||
if test_db.is_file(): # pragma: nocover
|
||||
test_db.unlink()
|
||||
|
||||
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_create_user(client):
|
||||
test_user = {"email": "johndoe@example.com", "password": "secret"}
|
||||
response = client.post("/users/", json=test_user)
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert test_user["email"] == data["email"]
|
||||
assert "id" in data
|
||||
response = client.post("/users/", json=test_user)
|
||||
assert response.status_code == 400, response.text
|
||||
|
||||
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_get_user(client):
|
||||
response = client.get("/users/1")
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert "email" in data
|
||||
assert "id" in data
|
||||
|
||||
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_nonexistent_user(client):
|
||||
response = client.get("/users/999")
|
||||
assert response.status_code == 404, response.text
|
||||
|
||||
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_get_users(client):
|
||||
response = client.get("/users/")
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert "email" in data[0]
|
||||
assert "id" in data[0]
|
||||
|
||||
|
||||
# TODO: pv2 add Pydantic v2 version
|
||||
@needs_pydanticv1
|
||||
def test_create_item(client):
|
||||
item = {"title": "Foo", "description": "Something that fights"}
|
||||
response = client.post("/users/1/items/", json=item)
|
||||
assert response.status_code == 200, response.text
|
||||
item_data = response.json()
|
||||
assert item["title"] == item_data["title"]
|
||||
assert item["description"] == item_data["description"]
|
||||
assert "id" in item_data
|
||||
assert "owner_id" in item_data
|
||||
response = client.get("/users/1")
|
||||
assert response.status_code == 200, response.text
|
||||
user_data = response.json()
|
||||
item_to_check = [it for it in user_data["items"] if it["id"] == item_data["id"]][0]
|
||||
assert item_to_check["title"] == item["title"]
|
||||
assert item_to_check["description"] == item["description"]
|
||||
response = client.get("/users/1")
|
||||
assert response.status_code == 200, response.text
|
||||
user_data = response.json()
|
||||
item_to_check = [it for it in user_data["items"] if it["id"] == item_data["id"]][0]
|
||||
assert item_to_check["title"] == item["title"]
|
||||
assert item_to_check["description"] == item["description"]
|
||||
|
||||
|
||||
# TODO: pv2 add Pydantic v2 version
|
||||
@needs_pydanticv1
|
||||
def test_read_items(client):
|
||||
response = client.get("/items/")
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert data
|
||||
first_item = data[0]
|
||||
assert "title" in first_item
|
||||
assert "description" in first_item
|
||||
|
||||
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_openapi_schema(client: TestClient):
|
||||
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": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"title": "Response Read Users Users Get",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/User"},
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Read Users",
|
||||
"operationId": "read_users_users__get",
|
||||
"parameters": [
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Skip",
|
||||
"type": "integer",
|
||||
"default": 0,
|
||||
},
|
||||
"name": "skip",
|
||||
"in": "query",
|
||||
},
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Limit",
|
||||
"type": "integer",
|
||||
"default": 100,
|
||||
},
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
},
|
||||
],
|
||||
},
|
||||
"post": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/User"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Create User",
|
||||
"operationId": "create_user_users__post",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/UserCreate"}
|
||||
}
|
||||
},
|
||||
"required": True,
|
||||
},
|
||||
},
|
||||
},
|
||||
"/users/{user_id}": {
|
||||
"get": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/User"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Read User",
|
||||
"operationId": "read_user_users__user_id__get",
|
||||
"parameters": [
|
||||
{
|
||||
"required": True,
|
||||
"schema": {"title": "User Id", "type": "integer"},
|
||||
"name": "user_id",
|
||||
"in": "path",
|
||||
}
|
||||
],
|
||||
}
|
||||
},
|
||||
"/users/{user_id}/items/": {
|
||||
"post": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/Item"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Create Item For User",
|
||||
"operationId": "create_item_for_user_users__user_id__items__post",
|
||||
"parameters": [
|
||||
{
|
||||
"required": True,
|
||||
"schema": {"title": "User Id", "type": "integer"},
|
||||
"name": "user_id",
|
||||
"in": "path",
|
||||
}
|
||||
],
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/ItemCreate"}
|
||||
}
|
||||
},
|
||||
"required": True,
|
||||
},
|
||||
}
|
||||
},
|
||||
"/items/": {
|
||||
"get": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"title": "Response Read Items Items Get",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/Item"},
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Read Items",
|
||||
"operationId": "read_items_items__get",
|
||||
"parameters": [
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Skip",
|
||||
"type": "integer",
|
||||
"default": 0,
|
||||
},
|
||||
"name": "skip",
|
||||
"in": "query",
|
||||
},
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Limit",
|
||||
"type": "integer",
|
||||
"default": 100,
|
||||
},
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
},
|
||||
],
|
||||
}
|
||||
},
|
||||
},
|
||||
"components": {
|
||||
"schemas": {
|
||||
"ItemCreate": {
|
||||
"title": "ItemCreate",
|
||||
"required": ["title"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"title": {"title": "Title", "type": "string"},
|
||||
"description": IsDict(
|
||||
{
|
||||
"title": "Description",
|
||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{"title": "Description", "type": "string"}
|
||||
),
|
||||
},
|
||||
},
|
||||
"Item": {
|
||||
"title": "Item",
|
||||
"required": ["title", "id", "owner_id"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"title": {"title": "Title", "type": "string"},
|
||||
"description": IsDict(
|
||||
{
|
||||
"title": "Description",
|
||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{"title": "Description", "type": "string"},
|
||||
),
|
||||
"id": {"title": "Id", "type": "integer"},
|
||||
"owner_id": {"title": "Owner Id", "type": "integer"},
|
||||
},
|
||||
},
|
||||
"User": {
|
||||
"title": "User",
|
||||
"required": ["email", "id", "is_active"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"email": {"title": "Email", "type": "string"},
|
||||
"id": {"title": "Id", "type": "integer"},
|
||||
"is_active": {"title": "Is Active", "type": "boolean"},
|
||||
"items": {
|
||||
"title": "Items",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/Item"},
|
||||
"default": [],
|
||||
},
|
||||
},
|
||||
},
|
||||
"UserCreate": {
|
||||
"title": "UserCreate",
|
||||
"required": ["email", "password"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"email": {"title": "Email", "type": "string"},
|
||||
"password": {"title": "Password", "type": "string"},
|
||||
},
|
||||
},
|
||||
"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"},
|
||||
},
|
||||
},
|
||||
"HTTPValidationError": {
|
||||
"title": "HTTPValidationError",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"detail": {
|
||||
"title": "Detail",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/ValidationError"},
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -1,433 +0,0 @@
|
||||
import importlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from dirty_equals import IsDict
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from ...utils import needs_py310, needs_pydanticv1
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def client(tmp_path_factory: pytest.TempPathFactory):
|
||||
tmp_path = tmp_path_factory.mktemp("data")
|
||||
cwd = os.getcwd()
|
||||
os.chdir(tmp_path)
|
||||
test_db = Path("./sql_app.db")
|
||||
if test_db.is_file(): # pragma: nocover
|
||||
test_db.unlink()
|
||||
# Import while creating the client to create the DB after starting the test session
|
||||
from docs_src.sql_databases.sql_app_py310 import alt_main
|
||||
|
||||
# Ensure import side effects are re-executed
|
||||
importlib.reload(alt_main)
|
||||
|
||||
with TestClient(alt_main.app) as c:
|
||||
yield c
|
||||
if test_db.is_file(): # pragma: nocover
|
||||
test_db.unlink()
|
||||
os.chdir(cwd)
|
||||
|
||||
|
||||
@needs_py310
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_create_user(client):
|
||||
test_user = {"email": "johndoe@example.com", "password": "secret"}
|
||||
response = client.post("/users/", json=test_user)
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert test_user["email"] == data["email"]
|
||||
assert "id" in data
|
||||
response = client.post("/users/", json=test_user)
|
||||
assert response.status_code == 400, response.text
|
||||
|
||||
|
||||
@needs_py310
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_get_user(client):
|
||||
response = client.get("/users/1")
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert "email" in data
|
||||
assert "id" in data
|
||||
|
||||
|
||||
@needs_py310
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_nonexistent_user(client):
|
||||
response = client.get("/users/999")
|
||||
assert response.status_code == 404, response.text
|
||||
|
||||
|
||||
@needs_py310
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_get_users(client):
|
||||
response = client.get("/users/")
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert "email" in data[0]
|
||||
assert "id" in data[0]
|
||||
|
||||
|
||||
@needs_py310
|
||||
# TODO: pv2 add Pydantic v2 version
|
||||
@needs_pydanticv1
|
||||
def test_create_item(client):
|
||||
item = {"title": "Foo", "description": "Something that fights"}
|
||||
response = client.post("/users/1/items/", json=item)
|
||||
assert response.status_code == 200, response.text
|
||||
item_data = response.json()
|
||||
assert item["title"] == item_data["title"]
|
||||
assert item["description"] == item_data["description"]
|
||||
assert "id" in item_data
|
||||
assert "owner_id" in item_data
|
||||
response = client.get("/users/1")
|
||||
assert response.status_code == 200, response.text
|
||||
user_data = response.json()
|
||||
item_to_check = [it for it in user_data["items"] if it["id"] == item_data["id"]][0]
|
||||
assert item_to_check["title"] == item["title"]
|
||||
assert item_to_check["description"] == item["description"]
|
||||
response = client.get("/users/1")
|
||||
assert response.status_code == 200, response.text
|
||||
user_data = response.json()
|
||||
item_to_check = [it for it in user_data["items"] if it["id"] == item_data["id"]][0]
|
||||
assert item_to_check["title"] == item["title"]
|
||||
assert item_to_check["description"] == item["description"]
|
||||
|
||||
|
||||
@needs_py310
|
||||
# TODO: pv2 add Pydantic v2 version
|
||||
@needs_pydanticv1
|
||||
def test_read_items(client):
|
||||
response = client.get("/items/")
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert data
|
||||
first_item = data[0]
|
||||
assert "title" in first_item
|
||||
assert "description" in first_item
|
||||
|
||||
|
||||
@needs_py310
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_openapi_schema(client: TestClient):
|
||||
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": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"title": "Response Read Users Users Get",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/User"},
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Read Users",
|
||||
"operationId": "read_users_users__get",
|
||||
"parameters": [
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Skip",
|
||||
"type": "integer",
|
||||
"default": 0,
|
||||
},
|
||||
"name": "skip",
|
||||
"in": "query",
|
||||
},
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Limit",
|
||||
"type": "integer",
|
||||
"default": 100,
|
||||
},
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
},
|
||||
],
|
||||
},
|
||||
"post": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/User"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Create User",
|
||||
"operationId": "create_user_users__post",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/UserCreate"}
|
||||
}
|
||||
},
|
||||
"required": True,
|
||||
},
|
||||
},
|
||||
},
|
||||
"/users/{user_id}": {
|
||||
"get": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/User"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Read User",
|
||||
"operationId": "read_user_users__user_id__get",
|
||||
"parameters": [
|
||||
{
|
||||
"required": True,
|
||||
"schema": {"title": "User Id", "type": "integer"},
|
||||
"name": "user_id",
|
||||
"in": "path",
|
||||
}
|
||||
],
|
||||
}
|
||||
},
|
||||
"/users/{user_id}/items/": {
|
||||
"post": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/Item"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Create Item For User",
|
||||
"operationId": "create_item_for_user_users__user_id__items__post",
|
||||
"parameters": [
|
||||
{
|
||||
"required": True,
|
||||
"schema": {"title": "User Id", "type": "integer"},
|
||||
"name": "user_id",
|
||||
"in": "path",
|
||||
}
|
||||
],
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/ItemCreate"}
|
||||
}
|
||||
},
|
||||
"required": True,
|
||||
},
|
||||
}
|
||||
},
|
||||
"/items/": {
|
||||
"get": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"title": "Response Read Items Items Get",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/Item"},
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Read Items",
|
||||
"operationId": "read_items_items__get",
|
||||
"parameters": [
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Skip",
|
||||
"type": "integer",
|
||||
"default": 0,
|
||||
},
|
||||
"name": "skip",
|
||||
"in": "query",
|
||||
},
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Limit",
|
||||
"type": "integer",
|
||||
"default": 100,
|
||||
},
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
},
|
||||
],
|
||||
}
|
||||
},
|
||||
},
|
||||
"components": {
|
||||
"schemas": {
|
||||
"ItemCreate": {
|
||||
"title": "ItemCreate",
|
||||
"required": ["title"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"title": {"title": "Title", "type": "string"},
|
||||
"description": IsDict(
|
||||
{
|
||||
"title": "Description",
|
||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{"title": "Description", "type": "string"}
|
||||
),
|
||||
},
|
||||
},
|
||||
"Item": {
|
||||
"title": "Item",
|
||||
"required": ["title", "id", "owner_id"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"title": {"title": "Title", "type": "string"},
|
||||
"description": IsDict(
|
||||
{
|
||||
"title": "Description",
|
||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{"title": "Description", "type": "string"},
|
||||
),
|
||||
"id": {"title": "Id", "type": "integer"},
|
||||
"owner_id": {"title": "Owner Id", "type": "integer"},
|
||||
},
|
||||
},
|
||||
"User": {
|
||||
"title": "User",
|
||||
"required": ["email", "id", "is_active"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"email": {"title": "Email", "type": "string"},
|
||||
"id": {"title": "Id", "type": "integer"},
|
||||
"is_active": {"title": "Is Active", "type": "boolean"},
|
||||
"items": {
|
||||
"title": "Items",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/Item"},
|
||||
"default": [],
|
||||
},
|
||||
},
|
||||
},
|
||||
"UserCreate": {
|
||||
"title": "UserCreate",
|
||||
"required": ["email", "password"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"email": {"title": "Email", "type": "string"},
|
||||
"password": {"title": "Password", "type": "string"},
|
||||
},
|
||||
},
|
||||
"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"},
|
||||
},
|
||||
},
|
||||
"HTTPValidationError": {
|
||||
"title": "HTTPValidationError",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"detail": {
|
||||
"title": "Detail",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/ValidationError"},
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -1,433 +0,0 @@
|
||||
import importlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from dirty_equals import IsDict
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from ...utils import needs_py39, needs_pydanticv1
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def client(tmp_path_factory: pytest.TempPathFactory):
|
||||
tmp_path = tmp_path_factory.mktemp("data")
|
||||
cwd = os.getcwd()
|
||||
os.chdir(tmp_path)
|
||||
test_db = Path("./sql_app.db")
|
||||
if test_db.is_file(): # pragma: nocover
|
||||
test_db.unlink()
|
||||
# Import while creating the client to create the DB after starting the test session
|
||||
from docs_src.sql_databases.sql_app_py39 import alt_main
|
||||
|
||||
# Ensure import side effects are re-executed
|
||||
importlib.reload(alt_main)
|
||||
|
||||
with TestClient(alt_main.app) as c:
|
||||
yield c
|
||||
if test_db.is_file(): # pragma: nocover
|
||||
test_db.unlink()
|
||||
os.chdir(cwd)
|
||||
|
||||
|
||||
@needs_py39
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_create_user(client):
|
||||
test_user = {"email": "johndoe@example.com", "password": "secret"}
|
||||
response = client.post("/users/", json=test_user)
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert test_user["email"] == data["email"]
|
||||
assert "id" in data
|
||||
response = client.post("/users/", json=test_user)
|
||||
assert response.status_code == 400, response.text
|
||||
|
||||
|
||||
@needs_py39
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_get_user(client):
|
||||
response = client.get("/users/1")
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert "email" in data
|
||||
assert "id" in data
|
||||
|
||||
|
||||
@needs_py39
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_nonexistent_user(client):
|
||||
response = client.get("/users/999")
|
||||
assert response.status_code == 404, response.text
|
||||
|
||||
|
||||
@needs_py39
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_get_users(client):
|
||||
response = client.get("/users/")
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert "email" in data[0]
|
||||
assert "id" in data[0]
|
||||
|
||||
|
||||
@needs_py39
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_create_item(client):
|
||||
item = {"title": "Foo", "description": "Something that fights"}
|
||||
response = client.post("/users/1/items/", json=item)
|
||||
assert response.status_code == 200, response.text
|
||||
item_data = response.json()
|
||||
assert item["title"] == item_data["title"]
|
||||
assert item["description"] == item_data["description"]
|
||||
assert "id" in item_data
|
||||
assert "owner_id" in item_data
|
||||
response = client.get("/users/1")
|
||||
assert response.status_code == 200, response.text
|
||||
user_data = response.json()
|
||||
item_to_check = [it for it in user_data["items"] if it["id"] == item_data["id"]][0]
|
||||
assert item_to_check["title"] == item["title"]
|
||||
assert item_to_check["description"] == item["description"]
|
||||
response = client.get("/users/1")
|
||||
assert response.status_code == 200, response.text
|
||||
user_data = response.json()
|
||||
item_to_check = [it for it in user_data["items"] if it["id"] == item_data["id"]][0]
|
||||
assert item_to_check["title"] == item["title"]
|
||||
assert item_to_check["description"] == item["description"]
|
||||
|
||||
|
||||
@needs_py39
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_read_items(client):
|
||||
response = client.get("/items/")
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert data
|
||||
first_item = data[0]
|
||||
assert "title" in first_item
|
||||
assert "description" in first_item
|
||||
|
||||
|
||||
@needs_py39
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_openapi_schema(client: TestClient):
|
||||
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": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"title": "Response Read Users Users Get",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/User"},
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Read Users",
|
||||
"operationId": "read_users_users__get",
|
||||
"parameters": [
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Skip",
|
||||
"type": "integer",
|
||||
"default": 0,
|
||||
},
|
||||
"name": "skip",
|
||||
"in": "query",
|
||||
},
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Limit",
|
||||
"type": "integer",
|
||||
"default": 100,
|
||||
},
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
},
|
||||
],
|
||||
},
|
||||
"post": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/User"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Create User",
|
||||
"operationId": "create_user_users__post",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/UserCreate"}
|
||||
}
|
||||
},
|
||||
"required": True,
|
||||
},
|
||||
},
|
||||
},
|
||||
"/users/{user_id}": {
|
||||
"get": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/User"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Read User",
|
||||
"operationId": "read_user_users__user_id__get",
|
||||
"parameters": [
|
||||
{
|
||||
"required": True,
|
||||
"schema": {"title": "User Id", "type": "integer"},
|
||||
"name": "user_id",
|
||||
"in": "path",
|
||||
}
|
||||
],
|
||||
}
|
||||
},
|
||||
"/users/{user_id}/items/": {
|
||||
"post": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/Item"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Create Item For User",
|
||||
"operationId": "create_item_for_user_users__user_id__items__post",
|
||||
"parameters": [
|
||||
{
|
||||
"required": True,
|
||||
"schema": {"title": "User Id", "type": "integer"},
|
||||
"name": "user_id",
|
||||
"in": "path",
|
||||
}
|
||||
],
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/ItemCreate"}
|
||||
}
|
||||
},
|
||||
"required": True,
|
||||
},
|
||||
}
|
||||
},
|
||||
"/items/": {
|
||||
"get": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"title": "Response Read Items Items Get",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/Item"},
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Read Items",
|
||||
"operationId": "read_items_items__get",
|
||||
"parameters": [
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Skip",
|
||||
"type": "integer",
|
||||
"default": 0,
|
||||
},
|
||||
"name": "skip",
|
||||
"in": "query",
|
||||
},
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Limit",
|
||||
"type": "integer",
|
||||
"default": 100,
|
||||
},
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
},
|
||||
],
|
||||
}
|
||||
},
|
||||
},
|
||||
"components": {
|
||||
"schemas": {
|
||||
"ItemCreate": {
|
||||
"title": "ItemCreate",
|
||||
"required": ["title"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"title": {"title": "Title", "type": "string"},
|
||||
"description": IsDict(
|
||||
{
|
||||
"title": "Description",
|
||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{"title": "Description", "type": "string"}
|
||||
),
|
||||
},
|
||||
},
|
||||
"Item": {
|
||||
"title": "Item",
|
||||
"required": ["title", "id", "owner_id"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"title": {"title": "Title", "type": "string"},
|
||||
"description": IsDict(
|
||||
{
|
||||
"title": "Description",
|
||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{"title": "Description", "type": "string"},
|
||||
),
|
||||
"id": {"title": "Id", "type": "integer"},
|
||||
"owner_id": {"title": "Owner Id", "type": "integer"},
|
||||
},
|
||||
},
|
||||
"User": {
|
||||
"title": "User",
|
||||
"required": ["email", "id", "is_active"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"email": {"title": "Email", "type": "string"},
|
||||
"id": {"title": "Id", "type": "integer"},
|
||||
"is_active": {"title": "Is Active", "type": "boolean"},
|
||||
"items": {
|
||||
"title": "Items",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/Item"},
|
||||
"default": [],
|
||||
},
|
||||
},
|
||||
},
|
||||
"UserCreate": {
|
||||
"title": "UserCreate",
|
||||
"required": ["email", "password"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"email": {"title": "Email", "type": "string"},
|
||||
"password": {"title": "Password", "type": "string"},
|
||||
},
|
||||
},
|
||||
"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"},
|
||||
},
|
||||
},
|
||||
"HTTPValidationError": {
|
||||
"title": "HTTPValidationError",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"detail": {
|
||||
"title": "Detail",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/ValidationError"},
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -1,432 +0,0 @@
|
||||
import importlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from dirty_equals import IsDict
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from ...utils import needs_py310, needs_pydanticv1
|
||||
|
||||
|
||||
@pytest.fixture(scope="module", name="client")
|
||||
def get_client(tmp_path_factory: pytest.TempPathFactory):
|
||||
tmp_path = tmp_path_factory.mktemp("data")
|
||||
cwd = os.getcwd()
|
||||
os.chdir(tmp_path)
|
||||
test_db = Path("./sql_app.db")
|
||||
if test_db.is_file(): # pragma: nocover
|
||||
test_db.unlink()
|
||||
# Import while creating the client to create the DB after starting the test session
|
||||
from docs_src.sql_databases.sql_app_py310 import main
|
||||
|
||||
# Ensure import side effects are re-executed
|
||||
importlib.reload(main)
|
||||
with TestClient(main.app) as c:
|
||||
yield c
|
||||
if test_db.is_file(): # pragma: nocover
|
||||
test_db.unlink()
|
||||
os.chdir(cwd)
|
||||
|
||||
|
||||
@needs_py310
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_create_user(client):
|
||||
test_user = {"email": "johndoe@example.com", "password": "secret"}
|
||||
response = client.post("/users/", json=test_user)
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert test_user["email"] == data["email"]
|
||||
assert "id" in data
|
||||
response = client.post("/users/", json=test_user)
|
||||
assert response.status_code == 400, response.text
|
||||
|
||||
|
||||
@needs_py310
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_get_user(client):
|
||||
response = client.get("/users/1")
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert "email" in data
|
||||
assert "id" in data
|
||||
|
||||
|
||||
@needs_py310
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_nonexistent_user(client):
|
||||
response = client.get("/users/999")
|
||||
assert response.status_code == 404, response.text
|
||||
|
||||
|
||||
@needs_py310
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_get_users(client):
|
||||
response = client.get("/users/")
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert "email" in data[0]
|
||||
assert "id" in data[0]
|
||||
|
||||
|
||||
@needs_py310
|
||||
# TODO: pv2 add Pydantic v2 version
|
||||
@needs_pydanticv1
|
||||
def test_create_item(client):
|
||||
item = {"title": "Foo", "description": "Something that fights"}
|
||||
response = client.post("/users/1/items/", json=item)
|
||||
assert response.status_code == 200, response.text
|
||||
item_data = response.json()
|
||||
assert item["title"] == item_data["title"]
|
||||
assert item["description"] == item_data["description"]
|
||||
assert "id" in item_data
|
||||
assert "owner_id" in item_data
|
||||
response = client.get("/users/1")
|
||||
assert response.status_code == 200, response.text
|
||||
user_data = response.json()
|
||||
item_to_check = [it for it in user_data["items"] if it["id"] == item_data["id"]][0]
|
||||
assert item_to_check["title"] == item["title"]
|
||||
assert item_to_check["description"] == item["description"]
|
||||
response = client.get("/users/1")
|
||||
assert response.status_code == 200, response.text
|
||||
user_data = response.json()
|
||||
item_to_check = [it for it in user_data["items"] if it["id"] == item_data["id"]][0]
|
||||
assert item_to_check["title"] == item["title"]
|
||||
assert item_to_check["description"] == item["description"]
|
||||
|
||||
|
||||
@needs_py310
|
||||
# TODO: pv2 add Pydantic v2 version
|
||||
@needs_pydanticv1
|
||||
def test_read_items(client):
|
||||
response = client.get("/items/")
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert data
|
||||
first_item = data[0]
|
||||
assert "title" in first_item
|
||||
assert "description" in first_item
|
||||
|
||||
|
||||
@needs_py310
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_openapi_schema(client: TestClient):
|
||||
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": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"title": "Response Read Users Users Get",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/User"},
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Read Users",
|
||||
"operationId": "read_users_users__get",
|
||||
"parameters": [
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Skip",
|
||||
"type": "integer",
|
||||
"default": 0,
|
||||
},
|
||||
"name": "skip",
|
||||
"in": "query",
|
||||
},
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Limit",
|
||||
"type": "integer",
|
||||
"default": 100,
|
||||
},
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
},
|
||||
],
|
||||
},
|
||||
"post": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/User"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Create User",
|
||||
"operationId": "create_user_users__post",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/UserCreate"}
|
||||
}
|
||||
},
|
||||
"required": True,
|
||||
},
|
||||
},
|
||||
},
|
||||
"/users/{user_id}": {
|
||||
"get": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/User"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Read User",
|
||||
"operationId": "read_user_users__user_id__get",
|
||||
"parameters": [
|
||||
{
|
||||
"required": True,
|
||||
"schema": {"title": "User Id", "type": "integer"},
|
||||
"name": "user_id",
|
||||
"in": "path",
|
||||
}
|
||||
],
|
||||
}
|
||||
},
|
||||
"/users/{user_id}/items/": {
|
||||
"post": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/Item"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Create Item For User",
|
||||
"operationId": "create_item_for_user_users__user_id__items__post",
|
||||
"parameters": [
|
||||
{
|
||||
"required": True,
|
||||
"schema": {"title": "User Id", "type": "integer"},
|
||||
"name": "user_id",
|
||||
"in": "path",
|
||||
}
|
||||
],
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/ItemCreate"}
|
||||
}
|
||||
},
|
||||
"required": True,
|
||||
},
|
||||
}
|
||||
},
|
||||
"/items/": {
|
||||
"get": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"title": "Response Read Items Items Get",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/Item"},
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Read Items",
|
||||
"operationId": "read_items_items__get",
|
||||
"parameters": [
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Skip",
|
||||
"type": "integer",
|
||||
"default": 0,
|
||||
},
|
||||
"name": "skip",
|
||||
"in": "query",
|
||||
},
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Limit",
|
||||
"type": "integer",
|
||||
"default": 100,
|
||||
},
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
},
|
||||
],
|
||||
}
|
||||
},
|
||||
},
|
||||
"components": {
|
||||
"schemas": {
|
||||
"ItemCreate": {
|
||||
"title": "ItemCreate",
|
||||
"required": ["title"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"title": {"title": "Title", "type": "string"},
|
||||
"description": IsDict(
|
||||
{
|
||||
"title": "Description",
|
||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{"title": "Description", "type": "string"}
|
||||
),
|
||||
},
|
||||
},
|
||||
"Item": {
|
||||
"title": "Item",
|
||||
"required": ["title", "id", "owner_id"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"title": {"title": "Title", "type": "string"},
|
||||
"description": IsDict(
|
||||
{
|
||||
"title": "Description",
|
||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{"title": "Description", "type": "string"},
|
||||
),
|
||||
"id": {"title": "Id", "type": "integer"},
|
||||
"owner_id": {"title": "Owner Id", "type": "integer"},
|
||||
},
|
||||
},
|
||||
"User": {
|
||||
"title": "User",
|
||||
"required": ["email", "id", "is_active"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"email": {"title": "Email", "type": "string"},
|
||||
"id": {"title": "Id", "type": "integer"},
|
||||
"is_active": {"title": "Is Active", "type": "boolean"},
|
||||
"items": {
|
||||
"title": "Items",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/Item"},
|
||||
"default": [],
|
||||
},
|
||||
},
|
||||
},
|
||||
"UserCreate": {
|
||||
"title": "UserCreate",
|
||||
"required": ["email", "password"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"email": {"title": "Email", "type": "string"},
|
||||
"password": {"title": "Password", "type": "string"},
|
||||
},
|
||||
},
|
||||
"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"},
|
||||
},
|
||||
},
|
||||
"HTTPValidationError": {
|
||||
"title": "HTTPValidationError",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"detail": {
|
||||
"title": "Detail",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/ValidationError"},
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -1,432 +0,0 @@
|
||||
import importlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from dirty_equals import IsDict
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from ...utils import needs_py39, needs_pydanticv1
|
||||
|
||||
|
||||
@pytest.fixture(scope="module", name="client")
|
||||
def get_client(tmp_path_factory: pytest.TempPathFactory):
|
||||
tmp_path = tmp_path_factory.mktemp("data")
|
||||
cwd = os.getcwd()
|
||||
os.chdir(tmp_path)
|
||||
test_db = Path("./sql_app.db")
|
||||
if test_db.is_file(): # pragma: nocover
|
||||
test_db.unlink()
|
||||
# Import while creating the client to create the DB after starting the test session
|
||||
from docs_src.sql_databases.sql_app_py39 import main
|
||||
|
||||
# Ensure import side effects are re-executed
|
||||
importlib.reload(main)
|
||||
with TestClient(main.app) as c:
|
||||
yield c
|
||||
if test_db.is_file(): # pragma: nocover
|
||||
test_db.unlink()
|
||||
os.chdir(cwd)
|
||||
|
||||
|
||||
@needs_py39
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_create_user(client):
|
||||
test_user = {"email": "johndoe@example.com", "password": "secret"}
|
||||
response = client.post("/users/", json=test_user)
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert test_user["email"] == data["email"]
|
||||
assert "id" in data
|
||||
response = client.post("/users/", json=test_user)
|
||||
assert response.status_code == 400, response.text
|
||||
|
||||
|
||||
@needs_py39
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_get_user(client):
|
||||
response = client.get("/users/1")
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert "email" in data
|
||||
assert "id" in data
|
||||
|
||||
|
||||
@needs_py39
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_nonexistent_user(client):
|
||||
response = client.get("/users/999")
|
||||
assert response.status_code == 404, response.text
|
||||
|
||||
|
||||
@needs_py39
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_get_users(client):
|
||||
response = client.get("/users/")
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert "email" in data[0]
|
||||
assert "id" in data[0]
|
||||
|
||||
|
||||
@needs_py39
|
||||
# TODO: pv2 add Pydantic v2 version
|
||||
@needs_pydanticv1
|
||||
def test_create_item(client):
|
||||
item = {"title": "Foo", "description": "Something that fights"}
|
||||
response = client.post("/users/1/items/", json=item)
|
||||
assert response.status_code == 200, response.text
|
||||
item_data = response.json()
|
||||
assert item["title"] == item_data["title"]
|
||||
assert item["description"] == item_data["description"]
|
||||
assert "id" in item_data
|
||||
assert "owner_id" in item_data
|
||||
response = client.get("/users/1")
|
||||
assert response.status_code == 200, response.text
|
||||
user_data = response.json()
|
||||
item_to_check = [it for it in user_data["items"] if it["id"] == item_data["id"]][0]
|
||||
assert item_to_check["title"] == item["title"]
|
||||
assert item_to_check["description"] == item["description"]
|
||||
response = client.get("/users/1")
|
||||
assert response.status_code == 200, response.text
|
||||
user_data = response.json()
|
||||
item_to_check = [it for it in user_data["items"] if it["id"] == item_data["id"]][0]
|
||||
assert item_to_check["title"] == item["title"]
|
||||
assert item_to_check["description"] == item["description"]
|
||||
|
||||
|
||||
@needs_py39
|
||||
# TODO: pv2 add Pydantic v2 version
|
||||
@needs_pydanticv1
|
||||
def test_read_items(client):
|
||||
response = client.get("/items/")
|
||||
assert response.status_code == 200, response.text
|
||||
data = response.json()
|
||||
assert data
|
||||
first_item = data[0]
|
||||
assert "title" in first_item
|
||||
assert "description" in first_item
|
||||
|
||||
|
||||
@needs_py39
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_openapi_schema(client: TestClient):
|
||||
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": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"title": "Response Read Users Users Get",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/User"},
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Read Users",
|
||||
"operationId": "read_users_users__get",
|
||||
"parameters": [
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Skip",
|
||||
"type": "integer",
|
||||
"default": 0,
|
||||
},
|
||||
"name": "skip",
|
||||
"in": "query",
|
||||
},
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Limit",
|
||||
"type": "integer",
|
||||
"default": 100,
|
||||
},
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
},
|
||||
],
|
||||
},
|
||||
"post": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/User"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Create User",
|
||||
"operationId": "create_user_users__post",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/UserCreate"}
|
||||
}
|
||||
},
|
||||
"required": True,
|
||||
},
|
||||
},
|
||||
},
|
||||
"/users/{user_id}": {
|
||||
"get": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/User"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Read User",
|
||||
"operationId": "read_user_users__user_id__get",
|
||||
"parameters": [
|
||||
{
|
||||
"required": True,
|
||||
"schema": {"title": "User Id", "type": "integer"},
|
||||
"name": "user_id",
|
||||
"in": "path",
|
||||
}
|
||||
],
|
||||
}
|
||||
},
|
||||
"/users/{user_id}/items/": {
|
||||
"post": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/Item"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Create Item For User",
|
||||
"operationId": "create_item_for_user_users__user_id__items__post",
|
||||
"parameters": [
|
||||
{
|
||||
"required": True,
|
||||
"schema": {"title": "User Id", "type": "integer"},
|
||||
"name": "user_id",
|
||||
"in": "path",
|
||||
}
|
||||
],
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/ItemCreate"}
|
||||
}
|
||||
},
|
||||
"required": True,
|
||||
},
|
||||
}
|
||||
},
|
||||
"/items/": {
|
||||
"get": {
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"title": "Response Read Items Items Get",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/Item"},
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"summary": "Read Items",
|
||||
"operationId": "read_items_items__get",
|
||||
"parameters": [
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Skip",
|
||||
"type": "integer",
|
||||
"default": 0,
|
||||
},
|
||||
"name": "skip",
|
||||
"in": "query",
|
||||
},
|
||||
{
|
||||
"required": False,
|
||||
"schema": {
|
||||
"title": "Limit",
|
||||
"type": "integer",
|
||||
"default": 100,
|
||||
},
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
},
|
||||
],
|
||||
}
|
||||
},
|
||||
},
|
||||
"components": {
|
||||
"schemas": {
|
||||
"ItemCreate": {
|
||||
"title": "ItemCreate",
|
||||
"required": ["title"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"title": {"title": "Title", "type": "string"},
|
||||
"description": IsDict(
|
||||
{
|
||||
"title": "Description",
|
||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{"title": "Description", "type": "string"}
|
||||
),
|
||||
},
|
||||
},
|
||||
"Item": {
|
||||
"title": "Item",
|
||||
"required": ["title", "id", "owner_id"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"title": {"title": "Title", "type": "string"},
|
||||
"description": IsDict(
|
||||
{
|
||||
"title": "Description",
|
||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{"title": "Description", "type": "string"},
|
||||
),
|
||||
"id": {"title": "Id", "type": "integer"},
|
||||
"owner_id": {"title": "Owner Id", "type": "integer"},
|
||||
},
|
||||
},
|
||||
"User": {
|
||||
"title": "User",
|
||||
"required": ["email", "id", "is_active"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"email": {"title": "Email", "type": "string"},
|
||||
"id": {"title": "Id", "type": "integer"},
|
||||
"is_active": {"title": "Is Active", "type": "boolean"},
|
||||
"items": {
|
||||
"title": "Items",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/Item"},
|
||||
"default": [],
|
||||
},
|
||||
},
|
||||
},
|
||||
"UserCreate": {
|
||||
"title": "UserCreate",
|
||||
"required": ["email", "password"],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"email": {"title": "Email", "type": "string"},
|
||||
"password": {"title": "Password", "type": "string"},
|
||||
},
|
||||
},
|
||||
"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"},
|
||||
},
|
||||
},
|
||||
"HTTPValidationError": {
|
||||
"title": "HTTPValidationError",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"detail": {
|
||||
"title": "Detail",
|
||||
"type": "array",
|
||||
"items": {"$ref": "#/components/schemas/ValidationError"},
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
import importlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from ...utils import needs_pydanticv1
|
||||
|
||||
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_testing_dbs(tmp_path_factory: pytest.TempPathFactory):
|
||||
tmp_path = tmp_path_factory.mktemp("data")
|
||||
cwd = os.getcwd()
|
||||
os.chdir(tmp_path)
|
||||
test_db = Path("./test.db")
|
||||
if test_db.is_file(): # pragma: nocover
|
||||
test_db.unlink()
|
||||
# Import while creating the client to create the DB after starting the test session
|
||||
from docs_src.sql_databases.sql_app.tests import test_sql_app
|
||||
|
||||
# Ensure import side effects are re-executed
|
||||
importlib.reload(test_sql_app)
|
||||
test_sql_app.test_create_user()
|
||||
if test_db.is_file(): # pragma: nocover
|
||||
test_db.unlink()
|
||||
os.chdir(cwd)
|
||||
@@ -1,28 +0,0 @@
|
||||
import importlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from ...utils import needs_py310, needs_pydanticv1
|
||||
|
||||
|
||||
@needs_py310
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_testing_dbs_py39(tmp_path_factory: pytest.TempPathFactory):
|
||||
tmp_path = tmp_path_factory.mktemp("data")
|
||||
cwd = os.getcwd()
|
||||
os.chdir(tmp_path)
|
||||
test_db = Path("./test.db")
|
||||
if test_db.is_file(): # pragma: nocover
|
||||
test_db.unlink()
|
||||
# Import while creating the client to create the DB after starting the test session
|
||||
from docs_src.sql_databases.sql_app_py310.tests import test_sql_app
|
||||
|
||||
# Ensure import side effects are re-executed
|
||||
importlib.reload(test_sql_app)
|
||||
test_sql_app.test_create_user()
|
||||
if test_db.is_file(): # pragma: nocover
|
||||
test_db.unlink()
|
||||
os.chdir(cwd)
|
||||
@@ -1,28 +0,0 @@
|
||||
import importlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from ...utils import needs_py39, needs_pydanticv1
|
||||
|
||||
|
||||
@needs_py39
|
||||
# TODO: pv2 add version with Pydantic v2
|
||||
@needs_pydanticv1
|
||||
def test_testing_dbs_py39(tmp_path_factory: pytest.TempPathFactory):
|
||||
tmp_path = tmp_path_factory.mktemp("data")
|
||||
cwd = os.getcwd()
|
||||
os.chdir(tmp_path)
|
||||
test_db = Path("./test.db")
|
||||
if test_db.is_file(): # pragma: nocover
|
||||
test_db.unlink()
|
||||
# Import while creating the client to create the DB after starting the test session
|
||||
from docs_src.sql_databases.sql_app_py39.tests import test_sql_app
|
||||
|
||||
# Ensure import side effects are re-executed
|
||||
importlib.reload(test_sql_app)
|
||||
test_sql_app.test_create_user()
|
||||
if test_db.is_file(): # pragma: nocover
|
||||
test_db.unlink()
|
||||
os.chdir(cwd)
|
||||
373
tests/test_tutorial/test_sql_databases/test_tutorial001.py
Normal file
373
tests/test_tutorial/test_sql_databases/test_tutorial001.py
Normal file
@@ -0,0 +1,373 @@
|
||||
import importlib
|
||||
import warnings
|
||||
|
||||
import pytest
|
||||
from dirty_equals import IsDict, IsInt
|
||||
from fastapi.testclient import TestClient
|
||||
from inline_snapshot import snapshot
|
||||
from sqlalchemy import StaticPool
|
||||
from sqlmodel import SQLModel, create_engine
|
||||
from sqlmodel.main import default_registry
|
||||
|
||||
from tests.utils import needs_py39, needs_py310
|
||||
|
||||
|
||||
def clear_sqlmodel():
|
||||
# Clear the tables in the metadata for the default base model
|
||||
SQLModel.metadata.clear()
|
||||
# Clear the Models associated with the registry, to avoid warnings
|
||||
default_registry.dispose()
|
||||
|
||||
|
||||
@pytest.fixture(
|
||||
name="client",
|
||||
params=[
|
||||
"tutorial001",
|
||||
pytest.param("tutorial001_py39", marks=needs_py39),
|
||||
pytest.param("tutorial001_py310", marks=needs_py310),
|
||||
"tutorial001_an",
|
||||
pytest.param("tutorial001_an_py39", marks=needs_py39),
|
||||
pytest.param("tutorial001_an_py310", marks=needs_py310),
|
||||
],
|
||||
)
|
||||
def get_client(request: pytest.FixtureRequest):
|
||||
clear_sqlmodel()
|
||||
# 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 = "sqlite://"
|
||||
mod.engine = create_engine(
|
||||
mod.sqlite_url, connect_args={"check_same_thread": False}, poolclass=StaticPool
|
||||
)
|
||||
|
||||
with TestClient(mod.app) as c:
|
||||
yield c
|
||||
|
||||
|
||||
def test_crud_app(client: TestClient):
|
||||
# TODO: this warns that SQLModel.from_orm is deprecated in Pydantic v1, refactor
|
||||
# this if using obj.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 == 200, response.text
|
||||
assert response.json() == []
|
||||
|
||||
# Create a hero
|
||||
response = client.post(
|
||||
"/heroes/",
|
||||
json={
|
||||
"id": 999,
|
||||
"name": "Dead Pond",
|
||||
"age": 30,
|
||||
"secret_name": "Dive Wilson",
|
||||
},
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == snapshot(
|
||||
{"age": 30, "secret_name": "Dive Wilson", "id": 999, "name": "Dead Pond"}
|
||||
)
|
||||
|
||||
# Read a hero
|
||||
hero_id = response.json()["id"]
|
||||
response = client.get(f"/heroes/{hero_id}")
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == snapshot(
|
||||
{"name": "Dead Pond", "age": 30, "id": 999, "secret_name": "Dive Wilson"}
|
||||
)
|
||||
|
||||
# Read all heroes
|
||||
# Create more heroes first
|
||||
response = client.post(
|
||||
"/heroes/",
|
||||
json={"name": "Spider-Boy", "age": 18, "secret_name": "Pedro Parqueador"},
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
response = client.post(
|
||||
"/heroes/", json={"name": "Rusty-Man", "secret_name": "Tommy Sharp"}
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
|
||||
response = client.get("/heroes/")
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == snapshot(
|
||||
[
|
||||
{
|
||||
"name": "Dead Pond",
|
||||
"age": 30,
|
||||
"id": IsInt(),
|
||||
"secret_name": "Dive Wilson",
|
||||
},
|
||||
{
|
||||
"name": "Spider-Boy",
|
||||
"age": 18,
|
||||
"id": IsInt(),
|
||||
"secret_name": "Pedro Parqueador",
|
||||
},
|
||||
{
|
||||
"name": "Rusty-Man",
|
||||
"age": None,
|
||||
"id": IsInt(),
|
||||
"secret_name": "Tommy Sharp",
|
||||
},
|
||||
]
|
||||
)
|
||||
|
||||
response = client.get("/heroes/?offset=1&limit=1")
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == snapshot(
|
||||
[
|
||||
{
|
||||
"name": "Spider-Boy",
|
||||
"age": 18,
|
||||
"id": IsInt(),
|
||||
"secret_name": "Pedro Parqueador",
|
||||
}
|
||||
]
|
||||
)
|
||||
|
||||
# Delete a hero
|
||||
response = client.delete(f"/heroes/{hero_id}")
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == snapshot({"ok": True})
|
||||
|
||||
response = client.get(f"/heroes/{hero_id}")
|
||||
assert response.status_code == 404, response.text
|
||||
|
||||
response = client.delete(f"/heroes/{hero_id}")
|
||||
assert response.status_code == 404, response.text
|
||||
assert response.json() == snapshot({"detail": "Hero not found"})
|
||||
|
||||
|
||||
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": {"title": "FastAPI", "version": "0.1.0"},
|
||||
"paths": {
|
||||
"/heroes/": {
|
||||
"post": {
|
||||
"summary": "Create Hero",
|
||||
"operationId": "create_hero_heroes__post",
|
||||
"requestBody": {
|
||||
"required": True,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/Hero"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/Hero"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"get": {
|
||||
"summary": "Read Heroes",
|
||||
"operationId": "read_heroes_heroes__get",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "offset",
|
||||
"in": "query",
|
||||
"required": False,
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"default": 0,
|
||||
"title": "Offset",
|
||||
},
|
||||
},
|
||||
{
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
"required": False,
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"maximum": 100,
|
||||
"default": 100,
|
||||
"title": "Limit",
|
||||
},
|
||||
},
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/Hero"
|
||||
},
|
||||
"title": "Response Read Heroes Heroes Get",
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
"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": [
|
||||
{
|
||||
"name": "hero_id",
|
||||
"in": "path",
|
||||
"required": True,
|
||||
"schema": {"type": "integer", "title": "Hero Id"},
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {"$ref": "#/components/schemas/Hero"}
|
||||
}
|
||||
},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"delete": {
|
||||
"summary": "Delete Hero",
|
||||
"operationId": "delete_hero_heroes__hero_id__delete",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "hero_id",
|
||||
"in": "path",
|
||||
"required": True,
|
||||
"schema": {"type": "integer", "title": "Hero Id"},
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {"application/json": {"schema": {}}},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"components": {
|
||||
"schemas": {
|
||||
"HTTPValidationError": {
|
||||
"properties": {
|
||||
"detail": {
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/ValidationError"
|
||||
},
|
||||
"type": "array",
|
||||
"title": "Detail",
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"title": "HTTPValidationError",
|
||||
},
|
||||
"Hero": {
|
||||
"properties": {
|
||||
"id": IsDict(
|
||||
{
|
||||
"anyOf": [{"type": "integer"}, {"type": "null"}],
|
||||
"title": "Id",
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{
|
||||
"type": "integer",
|
||||
"title": "Id",
|
||||
}
|
||||
),
|
||||
"name": {"type": "string", "title": "Name"},
|
||||
"age": IsDict(
|
||||
{
|
||||
"anyOf": [{"type": "integer"}, {"type": "null"}],
|
||||
"title": "Age",
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{
|
||||
"type": "integer",
|
||||
"title": "Age",
|
||||
}
|
||||
),
|
||||
"secret_name": {"type": "string", "title": "Secret Name"},
|
||||
},
|
||||
"type": "object",
|
||||
"required": ["name", "secret_name"],
|
||||
"title": "Hero",
|
||||
},
|
||||
"ValidationError": {
|
||||
"properties": {
|
||||
"loc": {
|
||||
"items": {
|
||||
"anyOf": [{"type": "string"}, {"type": "integer"}]
|
||||
},
|
||||
"type": "array",
|
||||
"title": "Location",
|
||||
},
|
||||
"msg": {"type": "string", "title": "Message"},
|
||||
"type": {"type": "string", "title": "Error Type"},
|
||||
},
|
||||
"type": "object",
|
||||
"required": ["loc", "msg", "type"],
|
||||
"title": "ValidationError",
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
481
tests/test_tutorial/test_sql_databases/test_tutorial002.py
Normal file
481
tests/test_tutorial/test_sql_databases/test_tutorial002.py
Normal file
@@ -0,0 +1,481 @@
|
||||
import importlib
|
||||
import warnings
|
||||
|
||||
import pytest
|
||||
from dirty_equals import IsDict, IsInt
|
||||
from fastapi.testclient import TestClient
|
||||
from inline_snapshot import snapshot
|
||||
from sqlalchemy import StaticPool
|
||||
from sqlmodel import SQLModel, create_engine
|
||||
from sqlmodel.main import default_registry
|
||||
|
||||
from tests.utils import needs_py39, needs_py310
|
||||
|
||||
|
||||
def clear_sqlmodel():
|
||||
# Clear the tables in the metadata for the default base model
|
||||
SQLModel.metadata.clear()
|
||||
# Clear the Models associated with the registry, to avoid warnings
|
||||
default_registry.dispose()
|
||||
|
||||
|
||||
@pytest.fixture(
|
||||
name="client",
|
||||
params=[
|
||||
"tutorial002",
|
||||
pytest.param("tutorial002_py39", marks=needs_py39),
|
||||
pytest.param("tutorial002_py310", marks=needs_py310),
|
||||
"tutorial002_an",
|
||||
pytest.param("tutorial002_an_py39", marks=needs_py39),
|
||||
pytest.param("tutorial002_an_py310", marks=needs_py310),
|
||||
],
|
||||
)
|
||||
def get_client(request: pytest.FixtureRequest):
|
||||
clear_sqlmodel()
|
||||
# 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 = "sqlite://"
|
||||
mod.engine = create_engine(
|
||||
mod.sqlite_url, connect_args={"check_same_thread": False}, poolclass=StaticPool
|
||||
)
|
||||
|
||||
with TestClient(mod.app) as c:
|
||||
yield c
|
||||
|
||||
|
||||
def test_crud_app(client: TestClient):
|
||||
# TODO: this warns that SQLModel.from_orm is deprecated in Pydantic v1, refactor
|
||||
# this if using obj.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 == 200, response.text
|
||||
assert response.json() == []
|
||||
|
||||
# Create a hero
|
||||
response = client.post(
|
||||
"/heroes/",
|
||||
json={
|
||||
"id": 9000,
|
||||
"name": "Dead Pond",
|
||||
"age": 30,
|
||||
"secret_name": "Dive Wilson",
|
||||
},
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == snapshot(
|
||||
{"age": 30, "id": IsInt(), "name": "Dead Pond"}
|
||||
)
|
||||
assert (
|
||||
response.json()["id"] != 9000
|
||||
), "The ID should be generated by the database"
|
||||
|
||||
# Read a hero
|
||||
hero_id = response.json()["id"]
|
||||
response = client.get(f"/heroes/{hero_id}")
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == snapshot(
|
||||
{"name": "Dead Pond", "age": 30, "id": IsInt()}
|
||||
)
|
||||
|
||||
# Read all heroes
|
||||
# Create more heroes first
|
||||
response = client.post(
|
||||
"/heroes/",
|
||||
json={"name": "Spider-Boy", "age": 18, "secret_name": "Pedro Parqueador"},
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
response = client.post(
|
||||
"/heroes/", json={"name": "Rusty-Man", "secret_name": "Tommy Sharp"}
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
|
||||
response = client.get("/heroes/")
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == snapshot(
|
||||
[
|
||||
{"name": "Dead Pond", "age": 30, "id": IsInt()},
|
||||
{"name": "Spider-Boy", "age": 18, "id": IsInt()},
|
||||
{"name": "Rusty-Man", "age": None, "id": IsInt()},
|
||||
]
|
||||
)
|
||||
|
||||
response = client.get("/heroes/?offset=1&limit=1")
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == snapshot(
|
||||
[{"name": "Spider-Boy", "age": 18, "id": IsInt()}]
|
||||
)
|
||||
|
||||
# Update a hero
|
||||
response = client.patch(
|
||||
f"/heroes/{hero_id}", json={"name": "Dog Pond", "age": None}
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == snapshot(
|
||||
{"name": "Dog Pond", "age": None, "id": hero_id}
|
||||
)
|
||||
|
||||
# Get updated hero
|
||||
response = client.get(f"/heroes/{hero_id}")
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == snapshot(
|
||||
{"name": "Dog Pond", "age": None, "id": hero_id}
|
||||
)
|
||||
|
||||
# Delete a hero
|
||||
response = client.delete(f"/heroes/{hero_id}")
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == snapshot({"ok": True})
|
||||
|
||||
# The hero is no longer found
|
||||
response = client.get(f"/heroes/{hero_id}")
|
||||
assert response.status_code == 404, response.text
|
||||
|
||||
# Delete a hero that does not exist
|
||||
response = client.delete(f"/heroes/{hero_id}")
|
||||
assert response.status_code == 404, response.text
|
||||
assert response.json() == snapshot({"detail": "Hero not found"})
|
||||
|
||||
# Update a hero that does not exist
|
||||
response = client.patch(f"/heroes/{hero_id}", json={"name": "Dog Pond"})
|
||||
assert response.status_code == 404, response.text
|
||||
assert response.json() == snapshot({"detail": "Hero not found"})
|
||||
|
||||
|
||||
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": {"title": "FastAPI", "version": "0.1.0"},
|
||||
"paths": {
|
||||
"/heroes/": {
|
||||
"post": {
|
||||
"summary": "Create Hero",
|
||||
"operationId": "create_hero_heroes__post",
|
||||
"requestBody": {
|
||||
"required": True,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HeroCreate"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
"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"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"get": {
|
||||
"summary": "Read Heroes",
|
||||
"operationId": "read_heroes_heroes__get",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "offset",
|
||||
"in": "query",
|
||||
"required": False,
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"default": 0,
|
||||
"title": "Offset",
|
||||
},
|
||||
},
|
||||
{
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
"required": False,
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"maximum": 100,
|
||||
"default": 100,
|
||||
"title": "Limit",
|
||||
},
|
||||
},
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/HeroPublic"
|
||||
},
|
||||
"title": "Response Read Heroes Heroes Get",
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
"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": [
|
||||
{
|
||||
"name": "hero_id",
|
||||
"in": "path",
|
||||
"required": True,
|
||||
"schema": {"type": "integer", "title": "Hero Id"},
|
||||
}
|
||||
],
|
||||
"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"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"patch": {
|
||||
"summary": "Update Hero",
|
||||
"operationId": "update_hero_heroes__hero_id__patch",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "hero_id",
|
||||
"in": "path",
|
||||
"required": True,
|
||||
"schema": {"type": "integer", "title": "Hero Id"},
|
||||
}
|
||||
],
|
||||
"requestBody": {
|
||||
"required": True,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HeroUpdate"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
"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"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"delete": {
|
||||
"summary": "Delete Hero",
|
||||
"operationId": "delete_hero_heroes__hero_id__delete",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "hero_id",
|
||||
"in": "path",
|
||||
"required": True,
|
||||
"schema": {"type": "integer", "title": "Hero Id"},
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {"application/json": {"schema": {}}},
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"components": {
|
||||
"schemas": {
|
||||
"HTTPValidationError": {
|
||||
"properties": {
|
||||
"detail": {
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/ValidationError"
|
||||
},
|
||||
"type": "array",
|
||||
"title": "Detail",
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"title": "HTTPValidationError",
|
||||
},
|
||||
"HeroCreate": {
|
||||
"properties": {
|
||||
"name": {"type": "string", "title": "Name"},
|
||||
"age": IsDict(
|
||||
{
|
||||
"anyOf": [{"type": "integer"}, {"type": "null"}],
|
||||
"title": "Age",
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{
|
||||
"type": "integer",
|
||||
"title": "Age",
|
||||
}
|
||||
),
|
||||
"secret_name": {"type": "string", "title": "Secret Name"},
|
||||
},
|
||||
"type": "object",
|
||||
"required": ["name", "secret_name"],
|
||||
"title": "HeroCreate",
|
||||
},
|
||||
"HeroPublic": {
|
||||
"properties": {
|
||||
"name": {"type": "string", "title": "Name"},
|
||||
"age": IsDict(
|
||||
{
|
||||
"anyOf": [{"type": "integer"}, {"type": "null"}],
|
||||
"title": "Age",
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{
|
||||
"type": "integer",
|
||||
"title": "Age",
|
||||
}
|
||||
),
|
||||
"id": {"type": "integer", "title": "Id"},
|
||||
},
|
||||
"type": "object",
|
||||
"required": ["name", "id"],
|
||||
"title": "HeroPublic",
|
||||
},
|
||||
"HeroUpdate": {
|
||||
"properties": {
|
||||
"name": IsDict(
|
||||
{
|
||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
||||
"title": "Name",
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{
|
||||
"type": "string",
|
||||
"title": "Name",
|
||||
}
|
||||
),
|
||||
"age": IsDict(
|
||||
{
|
||||
"anyOf": [{"type": "integer"}, {"type": "null"}],
|
||||
"title": "Age",
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{
|
||||
"type": "integer",
|
||||
"title": "Age",
|
||||
}
|
||||
),
|
||||
"secret_name": IsDict(
|
||||
{
|
||||
"anyOf": [{"type": "string"}, {"type": "null"}],
|
||||
"title": "Secret Name",
|
||||
}
|
||||
)
|
||||
| IsDict(
|
||||
# TODO: remove when deprecating Pydantic v1
|
||||
{
|
||||
"type": "string",
|
||||
"title": "Secret Name",
|
||||
}
|
||||
),
|
||||
},
|
||||
"type": "object",
|
||||
"title": "HeroUpdate",
|
||||
},
|
||||
"ValidationError": {
|
||||
"properties": {
|
||||
"loc": {
|
||||
"items": {
|
||||
"anyOf": [{"type": "string"}, {"type": "integer"}]
|
||||
},
|
||||
"type": "array",
|
||||
"title": "Location",
|
||||
},
|
||||
"msg": {"type": "string", "title": "Message"},
|
||||
"type": {"type": "string", "title": "Error Type"},
|
||||
},
|
||||
"type": "object",
|
||||
"required": ["loc", "msg", "type"],
|
||||
"title": "ValidationError",
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user