Add tests

This commit is contained in:
Sebastián Ramírez
2021-08-24 15:15:26 +02:00
parent 362f2e2e17
commit 79089f7a6c
114 changed files with 6742 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
import importlib
from typing import Any, Dict, List, Union
from unittest.mock import patch
from sqlalchemy import inspect
from sqlalchemy.engine.reflection import Inspector
from sqlmodel import create_engine
from fastapi.testclient import TestClient
from sqlmodel.pool import StaticPool
from docs_src.tutorial.fastapi.app_testing.tutorial001 import test_main_001 as test_mod
from docs_src.tutorial.fastapi.app_testing.tutorial001 import main as app_mod
import pytest
@pytest.fixture(name="prepare", autouse=True)
def prepare_fixture(clear_sqlmodel):
# Trigger side effects of registering table models in SQLModel
# This has to be called after clear_sqlmodel
importlib.reload(app_mod)
importlib.reload(test_mod)
def test_tutorial():
test_mod.test_create_hero()

View File

@@ -0,0 +1,24 @@
import importlib
from typing import Any, Dict, List, Union
from unittest.mock import patch
from sqlalchemy import inspect
from sqlalchemy.engine.reflection import Inspector
from sqlmodel import create_engine
from fastapi.testclient import TestClient
from sqlmodel.pool import StaticPool
from docs_src.tutorial.fastapi.app_testing.tutorial001 import test_main_002 as test_mod
from docs_src.tutorial.fastapi.app_testing.tutorial001 import main as app_mod
import pytest
@pytest.fixture(name="prepare", autouse=True)
def prepare_fixture(clear_sqlmodel):
# Trigger side effects of registering table models in SQLModel
# This has to be called after clear_sqlmodel
importlib.reload(app_mod)
importlib.reload(test_mod)
def test_tutorial():
test_mod.test_create_hero()

View File

@@ -0,0 +1,24 @@
import importlib
from typing import Any, Dict, List, Union
from unittest.mock import patch
from sqlalchemy import inspect
from sqlalchemy.engine.reflection import Inspector
from sqlmodel import create_engine
from fastapi.testclient import TestClient
from sqlmodel.pool import StaticPool
from docs_src.tutorial.fastapi.app_testing.tutorial001 import test_main_003 as test_mod
from docs_src.tutorial.fastapi.app_testing.tutorial001 import main as app_mod
import pytest
@pytest.fixture(name="prepare", autouse=True)
def prepare_fixture(clear_sqlmodel):
# Trigger side effects of registering table models in SQLModel
# This has to be called after clear_sqlmodel
importlib.reload(app_mod)
importlib.reload(test_mod)
def test_tutorial():
test_mod.test_create_hero()

View File

@@ -0,0 +1,24 @@
import importlib
from typing import Any, Dict, List, Union
from unittest.mock import patch
from sqlalchemy import inspect
from sqlalchemy.engine.reflection import Inspector
from sqlmodel import create_engine
from fastapi.testclient import TestClient
from sqlmodel.pool import StaticPool
from docs_src.tutorial.fastapi.app_testing.tutorial001 import test_main_004 as test_mod
from docs_src.tutorial.fastapi.app_testing.tutorial001 import main as app_mod
import pytest
@pytest.fixture(name="prepare", autouse=True)
def prepare_fixture(clear_sqlmodel):
# Trigger side effects of registering table models in SQLModel
# This has to be called after clear_sqlmodel
importlib.reload(app_mod)
importlib.reload(test_mod)
def test_tutorial():
test_mod.test_create_hero()

View File

@@ -0,0 +1,31 @@
import importlib
from contextlib import contextmanager
from typing import Any, Dict, List, Union
from unittest.mock import patch
import pytest
from docs_src.tutorial.fastapi.app_testing.tutorial001 import main as app_mod
from docs_src.tutorial.fastapi.app_testing.tutorial001 import test_main_005 as test_mod
from docs_src.tutorial.fastapi.app_testing.tutorial001.test_main_005 import (
session_fixture,
)
from fastapi.testclient import TestClient
from sqlalchemy import inspect
from sqlalchemy.engine.reflection import Inspector
from sqlmodel import Session, create_engine
from sqlmodel.pool import StaticPool
assert session_fixture, "This keeps the session fixture used below"
@pytest.fixture(name="prepare")
def prepare_fixture(clear_sqlmodel):
# Trigger side effects of registering table models in SQLModel
# This has to be called after clear_sqlmodel, but before the session_fixture
# That's why the extra custom fixture here
importlib.reload(app_mod)
def test_tutorial(prepare, session: Session):
test_mod.test_create_hero(session)

View File

@@ -0,0 +1,34 @@
import importlib
from contextlib import contextmanager
from typing import Any, Dict, List, Union
from unittest.mock import patch
import pytest
from docs_src.tutorial.fastapi.app_testing.tutorial001 import main as app_mod
from docs_src.tutorial.fastapi.app_testing.tutorial001 import test_main_006 as test_mod
from docs_src.tutorial.fastapi.app_testing.tutorial001.test_main_006 import (
session_fixture,
client_fixture
)
from fastapi.testclient import TestClient
from sqlalchemy import inspect
from sqlalchemy.engine.reflection import Inspector
from sqlmodel import Session, create_engine
from sqlmodel.pool import StaticPool
assert session_fixture, "This keeps the session fixture used below"
assert client_fixture, "This keeps the client fixture used below"
@pytest.fixture(name="prepare")
def prepare_fixture(clear_sqlmodel):
# Trigger side effects of registering table models in SQLModel
# This has to be called after clear_sqlmodel, but before the session_fixture
# That's why the extra custom fixture here
importlib.reload(app_mod)
def test_tutorial(prepare, session: Session, client: TestClient):
test_mod.test_create_hero(client)

View File

@@ -0,0 +1,87 @@
import importlib
from contextlib import contextmanager
from typing import Any, Dict, List, Union
from unittest.mock import patch
import pytest
from docs_src.tutorial.fastapi.app_testing.tutorial001 import main as app_mod
from docs_src.tutorial.fastapi.app_testing.tutorial001 import test_main as test_mod
from docs_src.tutorial.fastapi.app_testing.tutorial001.test_main import (
session_fixture,
client_fixture,
)
from fastapi.testclient import TestClient
from sqlalchemy import inspect
from sqlalchemy.engine.reflection import Inspector
from sqlmodel import Session, create_engine
from sqlmodel.pool import StaticPool
assert session_fixture, "This keeps the session fixture used below"
assert client_fixture, "This keeps the client fixture used below"
@pytest.fixture(name="prepare", autouse=True)
def prepare_fixture(clear_sqlmodel):
# Trigger side effects of registering table models in SQLModel
# This has to be called after clear_sqlmodel, but before the session_fixture
# That's why the extra custom fixture here
importlib.reload(app_mod)
importlib.reload(test_mod)
def test_create_hero(session: Session, client: TestClient):
test_mod.test_create_hero(client)
def test_create_hero_incomplete(session: Session, client: TestClient):
test_mod.test_create_hero_incomplete(client)
def test_create_hero_invalid(session: Session, client: TestClient):
test_mod.test_create_hero_invalid(client)
def test_read_heroes(session: Session, client: TestClient):
test_mod.test_read_heroes(session=session, client=client)
def test_read_hero(session: Session, client: TestClient):
test_mod.test_read_hero(session=session, client=client)
def test_update_hero(session: Session, client: TestClient):
test_mod.test_update_hero(session=session, client=client)
def test_delete_hero(session: Session, client: TestClient):
test_mod.test_delete_hero(session=session, client=client)
def test_startup():
app_mod.engine = create_engine("sqlite://")
app_mod.on_startup()
insp: Inspector = inspect(app_mod.engine)
assert insp.has_table(str(app_mod.Hero.__tablename__))
def test_get_session():
app_mod.engine = create_engine("sqlite://")
for session in app_mod.get_session():
assert isinstance(session, Session)
assert session.bind == app_mod.engine
def test_read_hero_not_found(client: TestClient):
response = client.get("/heroes/9000")
assert response.status_code == 404
def test_update_hero_not_found(client: TestClient):
response = client.patch("/heroes/9000", json={"name": "Very-Rusty-Man"})
assert response.status_code == 404
def test_delete_hero_not_found(client: TestClient):
response = client.delete("/heroes/9000")
assert response.status_code == 404

View File

@@ -0,0 +1,317 @@
from typing import Any, Dict, List, Union
from unittest.mock import patch
from sqlalchemy import inspect
from sqlalchemy.engine.reflection import Inspector
from sqlmodel import create_engine
from fastapi.testclient import TestClient
from sqlmodel.pool import StaticPool
openapi_schema = {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/heroes/": {
"get": {
"summary": "Read Heroes",
"operationId": "read_heroes_heroes__get",
"parameters": [
{
"required": False,
"schema": {"title": "Offset", "type": "integer", "default": 0},
"name": "offset",
"in": "query",
},
{
"required": False,
"schema": {
"title": "Limit",
"type": "integer",
"default": 100,
"lte": 100,
},
"name": "limit",
"in": "query",
},
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"title": "Response Read Heroes Heroes Get",
"type": "array",
"items": {"$ref": "#/components/schemas/HeroRead"},
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"post": {
"summary": "Create Hero",
"operationId": "create_hero_heroes__post",
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroCreate"}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
},
"/heroes/{hero_id}": {
"get": {
"summary": "Read Hero",
"operationId": "read_hero_heroes__hero_id__get",
"parameters": [
{
"required": True,
"schema": {"title": "Hero Id", "type": "integer"},
"name": "hero_id",
"in": "path",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"delete": {
"summary": "Delete Hero",
"operationId": "delete_hero_heroes__hero_id__delete",
"parameters": [
{
"required": True,
"schema": {"title": "Hero Id", "type": "integer"},
"name": "hero_id",
"in": "path",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"patch": {
"summary": "Update Hero",
"operationId": "update_hero_heroes__hero_id__patch",
"parameters": [
{
"required": True,
"schema": {"title": "Hero Id", "type": "integer"},
"name": "hero_id",
"in": "path",
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroUpdate"}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
},
},
"components": {
"schemas": {
"HTTPValidationError": {
"title": "HTTPValidationError",
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": "#/components/schemas/ValidationError"},
}
},
},
"HeroCreate": {
"title": "HeroCreate",
"required": ["name", "secret_name"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
},
},
"HeroRead": {
"title": "HeroRead",
"required": ["name", "secret_name", "id"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
"id": {"title": "Id", "type": "integer"},
},
},
"HeroUpdate": {
"title": "HeroUpdate",
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
},
},
"ValidationError": {
"title": "ValidationError",
"required": ["loc", "msg", "type"],
"type": "object",
"properties": {
"loc": {
"title": "Location",
"type": "array",
"items": {"type": "string"},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
},
},
}
},
}
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.delete import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
hero3_data = {
"name": "Rusty-Man",
"secret_name": "Tommy Sharp",
"age": 48,
}
response = client.post("/heroes/", json=hero1_data)
assert response.status_code == 200, response.text
response = client.post("/heroes/", json=hero2_data)
assert response.status_code == 200, response.text
hero2 = response.json()
hero2_id = hero2["id"]
response = client.post("/heroes/", json=hero3_data)
assert response.status_code == 200, response.text
response = client.get(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == openapi_schema
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 3
response = client.patch(
f"/heroes/{hero2_id}", json={"secret_name": "Spider-Youngster"}
)
assert response.status_code == 200, response.text
response = client.patch("/heroes/9001", json={"name": "Dragon Cube X"})
assert response.status_code == 404, response.text
response = client.delete(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
response = client.delete("/heroes/9000")
assert response.status_code == 404, response.text

View File

@@ -0,0 +1,245 @@
from typing import Any, Dict, List, Union
from unittest.mock import patch
from sqlalchemy import inspect
from sqlalchemy.engine.reflection import Inspector
from sqlmodel import create_engine
from fastapi.testclient import TestClient
from sqlmodel.pool import StaticPool
openapi_schema = {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/heroes/": {
"get": {
"summary": "Read Heroes",
"operationId": "read_heroes_heroes__get",
"parameters": [
{
"required": False,
"schema": {"title": "Offset", "type": "integer", "default": 0},
"name": "offset",
"in": "query",
},
{
"required": False,
"schema": {
"title": "Limit",
"type": "integer",
"default": 100,
"lte": 100,
},
"name": "limit",
"in": "query",
},
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"title": "Response Read Heroes Heroes Get",
"type": "array",
"items": {"$ref": "#/components/schemas/HeroRead"},
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"post": {
"summary": "Create Hero",
"operationId": "create_hero_heroes__post",
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroCreate"}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
},
"/heroes/{hero_id}": {
"get": {
"summary": "Read Hero",
"operationId": "read_hero_heroes__hero_id__get",
"parameters": [
{
"required": True,
"schema": {"title": "Hero Id", "type": "integer"},
"name": "hero_id",
"in": "path",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
}
},
},
"components": {
"schemas": {
"HTTPValidationError": {
"title": "HTTPValidationError",
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": "#/components/schemas/ValidationError"},
}
},
},
"HeroCreate": {
"title": "HeroCreate",
"required": ["name", "secret_name"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
},
},
"HeroRead": {
"title": "HeroRead",
"required": ["name", "secret_name", "id"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
"id": {"title": "Id", "type": "integer"},
},
},
"ValidationError": {
"title": "ValidationError",
"required": ["loc", "msg", "type"],
"type": "object",
"properties": {
"loc": {
"title": "Location",
"type": "array",
"items": {"type": "string"},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
},
},
}
},
}
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.limit_and_offset import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
hero3_data = {
"name": "Rusty-Man",
"secret_name": "Tommy Sharp",
"age": 48,
}
response = client.post("/heroes/", json=hero1_data)
assert response.status_code == 200, response.text
response = client.post("/heroes/", json=hero2_data)
assert response.status_code == 200, response.text
hero2 = response.json()
hero_id = hero2["id"]
response = client.post("/heroes/", json=hero3_data)
assert response.status_code == 200, response.text
response = client.get(f"/heroes/{hero_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == openapi_schema
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 3
response = client.get("/heroes/", params={"limit": 2})
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
assert data[0]["name"] == hero1_data["name"]
assert data[1]["name"] == hero2_data["name"]
response = client.get("/heroes/", params={"offset": 1})
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
assert data[0]["name"] == hero2_data["name"]
assert data[1]["name"] == hero3_data["name"]
response = client.get("/heroes/", params={"offset": 1, "limit": 1})
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 1
assert data[0]["name"] == hero2_data["name"]

View File

@@ -0,0 +1,174 @@
from typing import Any, Dict, List, Union
from unittest.mock import patch
from sqlalchemy import inspect
from sqlalchemy.engine.reflection import Inspector
from sqlmodel import create_engine
from fastapi.testclient import TestClient
from sqlmodel.pool import StaticPool
openapi_schema = {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/heroes/": {
"get": {
"summary": "Read Heroes",
"operationId": "read_heroes_heroes__get",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"title": "Response Read Heroes Heroes Get",
"type": "array",
"items": {"$ref": "#/components/schemas/HeroRead"},
}
}
},
}
},
},
"post": {
"summary": "Create Hero",
"operationId": "create_hero_heroes__post",
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroCreate"}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
}
},
"components": {
"schemas": {
"HTTPValidationError": {
"title": "HTTPValidationError",
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": "#/components/schemas/ValidationError"},
}
},
},
"HeroCreate": {
"title": "HeroCreate",
"required": ["name", "secret_name"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
},
},
"HeroRead": {
"title": "HeroRead",
"required": ["id", "name", "secret_name"],
"type": "object",
"properties": {
"id": {"title": "Id", "type": "integer"},
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
},
},
"ValidationError": {
"title": "ValidationError",
"required": ["loc", "msg", "type"],
"type": "object",
"properties": {
"loc": {
"title": "Location",
"type": "array",
"items": {"type": "string"},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
},
},
}
},
}
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.multiple_models import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
response = client.post("/heroes/", json=hero1_data)
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == hero1_data["name"]
assert data["secret_name"] == hero1_data["secret_name"]
assert data["id"] is not None
assert data["age"] is None
response = client.post("/heroes/", json=hero2_data)
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == hero2_data["name"]
assert data["secret_name"] == hero2_data["secret_name"]
assert data["id"] != hero2_data["id"], (
"Now it's not possible to predefine the ID from the request, "
"it's now set by the database"
)
assert data["age"] is None
response = client.get("/heroes/")
data = response.json()
assert response.status_code == 200, response.text
assert len(data) == 2
assert data[0]["name"] == hero1_data["name"]
assert data[0]["secret_name"] == hero1_data["secret_name"]
assert data[1]["name"] == hero2_data["name"]
assert data[1]["secret_name"] == hero2_data["secret_name"]
assert data[1]["id"] != hero2_data["id"]
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == openapi_schema

View File

@@ -0,0 +1,174 @@
from typing import Any, Dict, List, Union
from unittest.mock import patch
from sqlalchemy import inspect
from sqlalchemy.engine.reflection import Inspector
from sqlmodel import create_engine
from fastapi.testclient import TestClient
from sqlmodel.pool import StaticPool
openapi_schema = {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/heroes/": {
"get": {
"summary": "Read Heroes",
"operationId": "read_heroes_heroes__get",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"title": "Response Read Heroes Heroes Get",
"type": "array",
"items": {"$ref": "#/components/schemas/HeroRead"},
}
}
},
}
},
},
"post": {
"summary": "Create Hero",
"operationId": "create_hero_heroes__post",
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroCreate"}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
}
},
"components": {
"schemas": {
"HTTPValidationError": {
"title": "HTTPValidationError",
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": "#/components/schemas/ValidationError"},
}
},
},
"HeroCreate": {
"title": "HeroCreate",
"required": ["name", "secret_name"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
},
},
"HeroRead": {
"title": "HeroRead",
"required": ["name", "secret_name", "id"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
"id": {"title": "Id", "type": "integer"},
},
},
"ValidationError": {
"title": "ValidationError",
"required": ["loc", "msg", "type"],
"type": "object",
"properties": {
"loc": {
"title": "Location",
"type": "array",
"items": {"type": "string"},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
},
},
}
},
}
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.multiple_models import tutorial002 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
response = client.post("/heroes/", json=hero1_data)
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == hero1_data["name"]
assert data["secret_name"] == hero1_data["secret_name"]
assert data["id"] is not None
assert data["age"] is None
response = client.post("/heroes/", json=hero2_data)
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == hero2_data["name"]
assert data["secret_name"] == hero2_data["secret_name"]
assert data["id"] != hero2_data["id"], (
"Now it's not possible to predefine the ID from the request, "
"it's now set by the database"
)
assert data["age"] is None
response = client.get("/heroes/")
data = response.json()
assert response.status_code == 200, response.text
assert len(data) == 2
assert data[0]["name"] == hero1_data["name"]
assert data[0]["secret_name"] == hero1_data["secret_name"]
assert data[1]["name"] == hero2_data["name"]
assert data[1]["secret_name"] == hero2_data["secret_name"]
assert data[1]["id"] != hero2_data["id"]
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == openapi_schema

View File

@@ -0,0 +1,195 @@
from typing import Any, Dict, List, Union
from unittest.mock import patch
from sqlalchemy import inspect
from sqlalchemy.engine.reflection import Inspector
from sqlmodel import create_engine
from fastapi.testclient import TestClient
from sqlmodel.pool import StaticPool
openapi_schema = {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/heroes/": {
"get": {
"summary": "Read Heroes",
"operationId": "read_heroes_heroes__get",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"title": "Response Read Heroes Heroes Get",
"type": "array",
"items": {"$ref": "#/components/schemas/HeroRead"},
}
}
},
}
},
},
"post": {
"summary": "Create Hero",
"operationId": "create_hero_heroes__post",
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroCreate"}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
},
"/heroes/{hero_id}": {
"get": {
"summary": "Read Hero",
"operationId": "read_hero_heroes__hero_id__get",
"parameters": [
{
"required": True,
"schema": {"title": "Hero Id", "type": "integer"},
"name": "hero_id",
"in": "path",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
}
},
},
"components": {
"schemas": {
"HTTPValidationError": {
"title": "HTTPValidationError",
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": "#/components/schemas/ValidationError"},
}
},
},
"HeroCreate": {
"title": "HeroCreate",
"required": ["name", "secret_name"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
},
},
"HeroRead": {
"title": "HeroRead",
"required": ["name", "secret_name", "id"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
"id": {"title": "Id", "type": "integer"},
},
},
"ValidationError": {
"title": "ValidationError",
"required": ["loc", "msg", "type"],
"type": "object",
"properties": {
"loc": {
"title": "Location",
"type": "array",
"items": {"type": "string"},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
},
},
}
},
}
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.read_one import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
response = client.post("/heroes/", json=hero1_data)
assert response.status_code == 200, response.text
response = client.post("/heroes/", json=hero2_data)
assert response.status_code == 200, response.text
hero2 = response.json()
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
hero_id = hero2["id"]
response = client.get(f"/heroes/{hero_id}")
assert response.status_code == 200, response.text
data = response.json()
assert data == hero2
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == openapi_schema

View File

@@ -0,0 +1,612 @@
from typing import Any, Dict, List, Union
from unittest.mock import patch
from sqlalchemy import inspect
from sqlalchemy.engine.reflection import Inspector
from sqlmodel import create_engine
from fastapi.testclient import TestClient
from sqlmodel.pool import StaticPool
openapi_schema = {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/heroes/": {
"get": {
"summary": "Read Heroes",
"operationId": "read_heroes_heroes__get",
"parameters": [
{
"required": False,
"schema": {"title": "Offset", "type": "integer", "default": 0},
"name": "offset",
"in": "query",
},
{
"required": False,
"schema": {
"title": "Limit",
"type": "integer",
"default": 100,
"lte": 100,
},
"name": "limit",
"in": "query",
},
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"title": "Response Read Heroes Heroes Get",
"type": "array",
"items": {"$ref": "#/components/schemas/HeroRead"},
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"post": {
"summary": "Create Hero",
"operationId": "create_hero_heroes__post",
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroCreate"}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
},
"/heroes/{hero_id}": {
"get": {
"summary": "Read Hero",
"operationId": "read_hero_heroes__hero_id__get",
"parameters": [
{
"required": True,
"schema": {"title": "Hero Id", "type": "integer"},
"name": "hero_id",
"in": "path",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HeroReadWithTeam"
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"delete": {
"summary": "Delete Hero",
"operationId": "delete_hero_heroes__hero_id__delete",
"parameters": [
{
"required": True,
"schema": {"title": "Hero Id", "type": "integer"},
"name": "hero_id",
"in": "path",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"patch": {
"summary": "Update Hero",
"operationId": "update_hero_heroes__hero_id__patch",
"parameters": [
{
"required": True,
"schema": {"title": "Hero Id", "type": "integer"},
"name": "hero_id",
"in": "path",
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroUpdate"}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
},
"/teams/": {
"get": {
"summary": "Read Teams",
"operationId": "read_teams_teams__get",
"parameters": [
{
"required": False,
"schema": {"title": "Offset", "type": "integer", "default": 0},
"name": "offset",
"in": "query",
},
{
"required": False,
"schema": {
"title": "Limit",
"type": "integer",
"default": 100,
"lte": 100,
},
"name": "limit",
"in": "query",
},
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"title": "Response Read Teams Teams Get",
"type": "array",
"items": {"$ref": "#/components/schemas/TeamRead"},
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"post": {
"summary": "Create Team",
"operationId": "create_team_teams__post",
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/TeamCreate"}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/TeamRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
},
"/teams/{team_id}": {
"get": {
"summary": "Read Team",
"operationId": "read_team_teams__team_id__get",
"parameters": [
{
"required": True,
"schema": {"title": "Team Id", "type": "integer"},
"name": "team_id",
"in": "path",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/TeamReadWithHeroes"
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"delete": {
"summary": "Delete Team",
"operationId": "delete_team_teams__team_id__delete",
"parameters": [
{
"required": True,
"schema": {"title": "Team Id", "type": "integer"},
"name": "team_id",
"in": "path",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"patch": {
"summary": "Update Team",
"operationId": "update_team_teams__team_id__patch",
"parameters": [
{
"required": True,
"schema": {"title": "Team Id", "type": "integer"},
"name": "team_id",
"in": "path",
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/TeamUpdate"}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/TeamRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
},
},
"components": {
"schemas": {
"HTTPValidationError": {
"title": "HTTPValidationError",
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": "#/components/schemas/ValidationError"},
}
},
},
"HeroCreate": {
"title": "HeroCreate",
"required": ["name", "secret_name"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
"team_id": {"title": "Team Id", "type": "integer"},
},
},
"HeroRead": {
"title": "HeroRead",
"required": ["name", "secret_name", "id"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
"team_id": {"title": "Team Id", "type": "integer"},
"id": {"title": "Id", "type": "integer"},
},
},
"HeroReadWithTeam": {
"title": "HeroReadWithTeam",
"required": ["name", "secret_name", "id"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
"team_id": {"title": "Team Id", "type": "integer"},
"id": {"title": "Id", "type": "integer"},
"team": {"$ref": "#/components/schemas/TeamRead"},
},
},
"HeroUpdate": {
"title": "HeroUpdate",
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
"team_id": {"title": "Team Id", "type": "integer"},
},
},
"TeamCreate": {
"title": "TeamCreate",
"required": ["name", "headquarters"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"headquarters": {"title": "Headquarters", "type": "string"},
},
},
"TeamRead": {
"title": "TeamRead",
"required": ["name", "headquarters", "id"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"headquarters": {"title": "Headquarters", "type": "string"},
"id": {"title": "Id", "type": "integer"},
},
},
"TeamReadWithHeroes": {
"title": "TeamReadWithHeroes",
"required": ["name", "headquarters", "id"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"headquarters": {"title": "Headquarters", "type": "string"},
"id": {"title": "Id", "type": "integer"},
"heroes": {
"title": "Heroes",
"type": "array",
"items": {"$ref": "#/components/schemas/HeroRead"},
"default": [],
},
},
},
"TeamUpdate": {
"title": "TeamUpdate",
"type": "object",
"properties": {
"id": {"title": "Id", "type": "integer"},
"name": {"title": "Name", "type": "string"},
"headquarters": {"title": "Headquarters", "type": "string"},
},
},
"ValidationError": {
"title": "ValidationError",
"required": ["loc", "msg", "type"],
"type": "object",
"properties": {
"loc": {
"title": "Location",
"type": "array",
"items": {"type": "string"},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
},
},
}
},
}
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.relationships import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == openapi_schema
team_preventers = {"name": "Preventers", "headquarters": "Sharp Tower"}
team_z_force = {"name": "Z-Force", "headquarters": "Sister Margarets Bar"}
response = client.post("/teams/", json=team_preventers)
assert response.status_code == 200, response.text
team_preventers_data = response.json()
team_preventers_id = team_preventers_data["id"]
response = client.post("/teams/", json=team_z_force)
assert response.status_code == 200, response.text
team_z_force_data = response.json()
team_z_force_id = team_z_force_data["id"]
response = client.get("/teams/")
data = response.json()
assert len(data) == 2
response = client.get("/teams/9000")
assert response.status_code == 404, response.text
response = client.patch(
f"/teams/{team_preventers_id}", json={"headquarters": "Preventers Tower"}
)
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == team_preventers["name"]
assert data["headquarters"] == "Preventers Tower"
response = client.patch("/teams/9000", json={"name": "Freedom League"})
assert response.status_code == 404, response.text
hero1_data = {
"name": "Deadpond",
"secret_name": "Dive Wilson",
"team_id": team_z_force_id,
}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
hero3_data = {
"name": "Rusty-Man",
"secret_name": "Tommy Sharp",
"age": 48,
"team_id": team_preventers_id,
}
response = client.post("/heroes/", json=hero1_data)
assert response.status_code == 200, response.text
hero1 = response.json()
hero1_id = hero1["id"]
response = client.post("/heroes/", json=hero2_data)
assert response.status_code == 200, response.text
hero2 = response.json()
hero2_id = hero2["id"]
response = client.post("/heroes/", json=hero3_data)
assert response.status_code == 200, response.text
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 3
response = client.get(f"/heroes/{hero1_id}")
assert response.status_code == 200, response.text
data = response.json()
assert data["name"] == hero1_data["name"]
assert data["team"]["name"] == team_z_force["name"]
response = client.patch(
f"/heroes/{hero2_id}", json={"secret_name": "Spider-Youngster"}
)
assert response.status_code == 200, response.text
response = client.patch("/heroes/9001", json={"name": "Dragon Cube X"})
assert response.status_code == 404, response.text
response = client.delete(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
response = client.delete("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get(f"/teams/{team_preventers_id}")
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == team_preventers_data["name"]
assert data["heroes"][0]["name"] == hero3_data["name"]
response = client.delete(f"/teams/{team_preventers_id}")
assert response.status_code == 200, response.text
response = client.delete("/teams/9000")
assert response.status_code == 404, response.text
response = client.get("/teams/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 1

View File

@@ -0,0 +1,144 @@
from typing import Any, Dict, List, Union
from unittest.mock import patch
from sqlalchemy import inspect
from sqlalchemy.engine.reflection import Inspector
from sqlmodel import create_engine
from fastapi.testclient import TestClient
from sqlmodel.pool import StaticPool
openapi_schema = {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/heroes/": {
"get": {
"summary": "Read Heroes",
"operationId": "read_heroes_heroes__get",
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"title": "Response Read Heroes Heroes Get",
"type": "array",
"items": {"$ref": "#/components/schemas/Hero"},
}
}
},
}
},
},
"post": {
"summary": "Create Hero",
"operationId": "create_hero_heroes__post",
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/Hero"}
}
},
"required": True,
},
"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"
}
}
},
},
},
},
}
},
"components": {
"schemas": {
"HTTPValidationError": {
"title": "HTTPValidationError",
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": "#/components/schemas/ValidationError"},
}
},
},
"Hero": {
"title": "Hero",
"required": ["name", "secret_name"],
"type": "object",
"properties": {
"id": {"title": "Id", "type": "integer"},
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
},
},
"ValidationError": {
"title": "ValidationError",
"required": ["loc", "msg", "type"],
"type": "object",
"properties": {
"loc": {
"title": "Location",
"type": "array",
"items": {"type": "string"},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
},
},
}
},
}
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.response_model import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
hero_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
response = client.post("/heroes/", json=hero_data)
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == hero_data["name"]
assert data["secret_name"] == hero_data["secret_name"]
assert data["id"] is not None
assert data["age"] is None
response = client.get("/heroes/")
data = response.json()
assert response.status_code == 200, response.text
assert len(data) == 1
assert data[0]["name"] == hero_data["name"]
assert data[0]["secret_name"] == hero_data["secret_name"]
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == openapi_schema

View File

@@ -0,0 +1,317 @@
from typing import Any, Dict, List, Union
from unittest.mock import patch
from sqlalchemy import inspect
from sqlalchemy.engine.reflection import Inspector
from sqlmodel import create_engine
from fastapi.testclient import TestClient
from sqlmodel.pool import StaticPool
openapi_schema = {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/heroes/": {
"get": {
"summary": "Read Heroes",
"operationId": "read_heroes_heroes__get",
"parameters": [
{
"required": False,
"schema": {"title": "Offset", "type": "integer", "default": 0},
"name": "offset",
"in": "query",
},
{
"required": False,
"schema": {
"title": "Limit",
"type": "integer",
"default": 100,
"lte": 100,
},
"name": "limit",
"in": "query",
},
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"title": "Response Read Heroes Heroes Get",
"type": "array",
"items": {"$ref": "#/components/schemas/HeroRead"},
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"post": {
"summary": "Create Hero",
"operationId": "create_hero_heroes__post",
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroCreate"}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
},
"/heroes/{hero_id}": {
"get": {
"summary": "Read Hero",
"operationId": "read_hero_heroes__hero_id__get",
"parameters": [
{
"required": True,
"schema": {"title": "Hero Id", "type": "integer"},
"name": "hero_id",
"in": "path",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"delete": {
"summary": "Delete Hero",
"operationId": "delete_hero_heroes__hero_id__delete",
"parameters": [
{
"required": True,
"schema": {"title": "Hero Id", "type": "integer"},
"name": "hero_id",
"in": "path",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"patch": {
"summary": "Update Hero",
"operationId": "update_hero_heroes__hero_id__patch",
"parameters": [
{
"required": True,
"schema": {"title": "Hero Id", "type": "integer"},
"name": "hero_id",
"in": "path",
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroUpdate"}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
},
},
"components": {
"schemas": {
"HTTPValidationError": {
"title": "HTTPValidationError",
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": "#/components/schemas/ValidationError"},
}
},
},
"HeroCreate": {
"title": "HeroCreate",
"required": ["name", "secret_name"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
},
},
"HeroRead": {
"title": "HeroRead",
"required": ["name", "secret_name", "id"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
"id": {"title": "Id", "type": "integer"},
},
},
"HeroUpdate": {
"title": "HeroUpdate",
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
},
},
"ValidationError": {
"title": "ValidationError",
"required": ["loc", "msg", "type"],
"type": "object",
"properties": {
"loc": {
"title": "Location",
"type": "array",
"items": {"type": "string"},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
},
},
}
},
}
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.session_with_dependency import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
hero3_data = {
"name": "Rusty-Man",
"secret_name": "Tommy Sharp",
"age": 48,
}
response = client.post("/heroes/", json=hero1_data)
assert response.status_code == 200, response.text
response = client.post("/heroes/", json=hero2_data)
assert response.status_code == 200, response.text
hero2 = response.json()
hero2_id = hero2["id"]
response = client.post("/heroes/", json=hero3_data)
assert response.status_code == 200, response.text
response = client.get(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == openapi_schema
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 3
response = client.patch(
f"/heroes/{hero2_id}", json={"secret_name": "Spider-Youngster"}
)
assert response.status_code == 200, response.text
response = client.patch("/heroes/9001", json={"name": "Dragon Cube X"})
assert response.status_code == 404, response.text
response = client.delete(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
response = client.delete("/heroes/9000")
assert response.status_code == 404, response.text

View File

@@ -0,0 +1,152 @@
from typing import Any, Dict, List, Union
from unittest.mock import patch
from sqlalchemy import inspect
from sqlalchemy.engine.reflection import Inspector
from sqlmodel import create_engine
from fastapi.testclient import TestClient
from sqlmodel.pool import StaticPool
openapi_schema = {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/heroes/": {
"get": {
"summary": "Read Heroes",
"operationId": "read_heroes_heroes__get",
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
}
},
},
"post": {
"summary": "Create Hero",
"operationId": "create_hero_heroes__post",
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/Hero"}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
}
},
"components": {
"schemas": {
"HTTPValidationError": {
"title": "HTTPValidationError",
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": "#/components/schemas/ValidationError"},
}
},
},
"Hero": {
"title": "Hero",
"required": ["name", "secret_name"],
"type": "object",
"properties": {
"id": {"title": "Id", "type": "integer"},
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
},
},
"ValidationError": {
"title": "ValidationError",
"required": ["loc", "msg", "type"],
"type": "object",
"properties": {
"loc": {
"title": "Location",
"type": "array",
"items": {"type": "string"},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
},
},
}
},
}
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.simple_hero_api import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
response = client.post("/heroes/", json=hero1_data)
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == hero1_data["name"]
assert data["secret_name"] == hero1_data["secret_name"]
assert data["id"] is not None
assert data["age"] is None
response = client.post("/heroes/", json=hero2_data)
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == hero2_data["name"]
assert data["secret_name"] == hero2_data["secret_name"]
assert data["id"] == hero2_data["id"], (
"Up to this point it's still possible to "
"set the ID of the hero in the request"
)
assert data["age"] is None
response = client.get("/heroes/")
data = response.json()
assert response.status_code == 200, response.text
assert len(data) == 2
assert data[0]["name"] == hero1_data["name"]
assert data[0]["secret_name"] == hero1_data["secret_name"]
assert data[1]["name"] == hero2_data["name"]
assert data[1]["secret_name"] == hero2_data["secret_name"]
assert data[1]["id"] == hero2_data["id"]
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == openapi_schema

View File

@@ -0,0 +1,566 @@
from typing import Any, Dict, List, Union
from unittest.mock import patch
from sqlalchemy import inspect
from sqlalchemy.engine.reflection import Inspector
from sqlmodel import create_engine
from fastapi.testclient import TestClient
from sqlmodel.pool import StaticPool
openapi_schema = {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/heroes/": {
"get": {
"summary": "Read Heroes",
"operationId": "read_heroes_heroes__get",
"parameters": [
{
"required": False,
"schema": {"title": "Offset", "type": "integer", "default": 0},
"name": "offset",
"in": "query",
},
{
"required": False,
"schema": {
"title": "Limit",
"type": "integer",
"default": 100,
"lte": 100,
},
"name": "limit",
"in": "query",
},
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"title": "Response Read Heroes Heroes Get",
"type": "array",
"items": {"$ref": "#/components/schemas/HeroRead"},
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"post": {
"summary": "Create Hero",
"operationId": "create_hero_heroes__post",
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroCreate"}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
},
"/heroes/{hero_id}": {
"get": {
"summary": "Read Hero",
"operationId": "read_hero_heroes__hero_id__get",
"parameters": [
{
"required": True,
"schema": {"title": "Hero Id", "type": "integer"},
"name": "hero_id",
"in": "path",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"delete": {
"summary": "Delete Hero",
"operationId": "delete_hero_heroes__hero_id__delete",
"parameters": [
{
"required": True,
"schema": {"title": "Hero Id", "type": "integer"},
"name": "hero_id",
"in": "path",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"patch": {
"summary": "Update Hero",
"operationId": "update_hero_heroes__hero_id__patch",
"parameters": [
{
"required": True,
"schema": {"title": "Hero Id", "type": "integer"},
"name": "hero_id",
"in": "path",
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroUpdate"}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
},
"/teams/": {
"get": {
"summary": "Read Teams",
"operationId": "read_teams_teams__get",
"parameters": [
{
"required": False,
"schema": {"title": "Offset", "type": "integer", "default": 0},
"name": "offset",
"in": "query",
},
{
"required": False,
"schema": {
"title": "Limit",
"type": "integer",
"default": 100,
"lte": 100,
},
"name": "limit",
"in": "query",
},
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"title": "Response Read Teams Teams Get",
"type": "array",
"items": {"$ref": "#/components/schemas/TeamRead"},
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"post": {
"summary": "Create Team",
"operationId": "create_team_teams__post",
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/TeamCreate"}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/TeamRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
},
"/teams/{team_id}": {
"get": {
"summary": "Read Team",
"operationId": "read_team_teams__team_id__get",
"parameters": [
{
"required": True,
"schema": {"title": "Team Id", "type": "integer"},
"name": "team_id",
"in": "path",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/TeamRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"delete": {
"summary": "Delete Team",
"operationId": "delete_team_teams__team_id__delete",
"parameters": [
{
"required": True,
"schema": {"title": "Team Id", "type": "integer"},
"name": "team_id",
"in": "path",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {"application/json": {"schema": {}}},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"patch": {
"summary": "Update Team",
"operationId": "update_team_teams__team_id__patch",
"parameters": [
{
"required": True,
"schema": {"title": "Team Id", "type": "integer"},
"name": "team_id",
"in": "path",
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/TeamUpdate"}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/TeamRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
},
},
"components": {
"schemas": {
"HTTPValidationError": {
"title": "HTTPValidationError",
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": "#/components/schemas/ValidationError"},
}
},
},
"HeroCreate": {
"title": "HeroCreate",
"required": ["name", "secret_name"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
"team_id": {"title": "Team Id", "type": "integer"},
},
},
"HeroRead": {
"title": "HeroRead",
"required": ["name", "secret_name", "id"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
"team_id": {"title": "Team Id", "type": "integer"},
"id": {"title": "Id", "type": "integer"},
},
},
"HeroUpdate": {
"title": "HeroUpdate",
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
"team_id": {"title": "Team Id", "type": "integer"},
},
},
"TeamCreate": {
"title": "TeamCreate",
"required": ["name", "headquarters"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"headquarters": {"title": "Headquarters", "type": "string"},
},
},
"TeamRead": {
"title": "TeamRead",
"required": ["name", "headquarters", "id"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"headquarters": {"title": "Headquarters", "type": "string"},
"id": {"title": "Id", "type": "integer"},
},
},
"TeamUpdate": {
"title": "TeamUpdate",
"type": "object",
"properties": {
"id": {"title": "Id", "type": "integer"},
"name": {"title": "Name", "type": "string"},
"headquarters": {"title": "Headquarters", "type": "string"},
},
},
"ValidationError": {
"title": "ValidationError",
"required": ["loc", "msg", "type"],
"type": "object",
"properties": {
"loc": {
"title": "Location",
"type": "array",
"items": {"type": "string"},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
},
},
}
},
}
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.teams import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
hero3_data = {
"name": "Rusty-Man",
"secret_name": "Tommy Sharp",
"age": 48,
}
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == openapi_schema
response = client.post("/heroes/", json=hero1_data)
assert response.status_code == 200, response.text
response = client.post("/heroes/", json=hero2_data)
assert response.status_code == 200, response.text
hero2 = response.json()
hero2_id = hero2["id"]
response = client.post("/heroes/", json=hero3_data)
assert response.status_code == 200, response.text
response = client.get(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 3
response = client.patch(
f"/heroes/{hero2_id}", json={"secret_name": "Spider-Youngster"}
)
assert response.status_code == 200, response.text
response = client.patch("/heroes/9001", json={"name": "Dragon Cube X"})
assert response.status_code == 404, response.text
response = client.delete(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 2
response = client.delete("/heroes/9000")
assert response.status_code == 404, response.text
team_preventers = {"name": "Preventers", "headquarters": "Sharp Tower"}
team_z_force = {"name": "Z-Force", "headquarters": "Sister Margarets Bar"}
response = client.post("/teams/", json=team_preventers)
assert response.status_code == 200, response.text
team_preventers_data = response.json()
team_preventers_id = team_preventers_data["id"]
response = client.post("/teams/", json=team_z_force)
assert response.status_code == 200, response.text
team_z_force_data = response.json()
team_z_force_id = team_z_force_data["id"]
response = client.get("/teams/")
data = response.json()
assert len(data) == 2
response = client.get(f"/teams/{team_preventers_id}")
data = response.json()
assert response.status_code == 200, response.text
assert data == team_preventers_data
response = client.get("/teams/9000")
assert response.status_code == 404, response.text
response = client.patch(
f"/teams/{team_preventers_id}", json={"headquarters": "Preventers Tower"}
)
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == team_preventers["name"]
assert data["headquarters"] == "Preventers Tower"
response = client.patch("/teams/9000", json={"name": "Freedom League"})
assert response.status_code == 404, response.text
response = client.delete(f"/teams/{team_preventers_id}")
assert response.status_code == 200, response.text
response = client.delete("/teams/9000")
assert response.status_code == 404, response.text
response = client.get("/teams/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 1

View File

@@ -0,0 +1,296 @@
from typing import Any, Dict, List, Union
from unittest.mock import patch
from sqlalchemy import inspect
from sqlalchemy.engine.reflection import Inspector
from sqlmodel import create_engine
from fastapi.testclient import TestClient
from sqlmodel.pool import StaticPool
openapi_schema = {
"openapi": "3.0.2",
"info": {"title": "FastAPI", "version": "0.1.0"},
"paths": {
"/heroes/": {
"get": {
"summary": "Read Heroes",
"operationId": "read_heroes_heroes__get",
"parameters": [
{
"required": False,
"schema": {"title": "Offset", "type": "integer", "default": 0},
"name": "offset",
"in": "query",
},
{
"required": False,
"schema": {
"title": "Limit",
"type": "integer",
"default": 100,
"lte": 100,
},
"name": "limit",
"in": "query",
},
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"title": "Response Read Heroes Heroes Get",
"type": "array",
"items": {"$ref": "#/components/schemas/HeroRead"},
}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"post": {
"summary": "Create Hero",
"operationId": "create_hero_heroes__post",
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroCreate"}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
},
"/heroes/{hero_id}": {
"get": {
"summary": "Read Hero",
"operationId": "read_hero_heroes__hero_id__get",
"parameters": [
{
"required": True,
"schema": {"title": "Hero Id", "type": "integer"},
"name": "hero_id",
"in": "path",
}
],
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
"patch": {
"summary": "Update Hero",
"operationId": "update_hero_heroes__hero_id__patch",
"parameters": [
{
"required": True,
"schema": {"title": "Hero Id", "type": "integer"},
"name": "hero_id",
"in": "path",
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroUpdate"}
}
},
"required": True,
},
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/HeroRead"}
}
},
},
"422": {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HTTPValidationError"
}
}
},
},
},
},
},
},
"components": {
"schemas": {
"HTTPValidationError": {
"title": "HTTPValidationError",
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": "#/components/schemas/ValidationError"},
}
},
},
"HeroCreate": {
"title": "HeroCreate",
"required": ["name", "secret_name"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
},
},
"HeroRead": {
"title": "HeroRead",
"required": ["name", "secret_name", "id"],
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
"id": {"title": "Id", "type": "integer"},
},
},
"HeroUpdate": {
"title": "HeroUpdate",
"type": "object",
"properties": {
"name": {"title": "Name", "type": "string"},
"secret_name": {"title": "Secret Name", "type": "string"},
"age": {"title": "Age", "type": "integer"},
},
},
"ValidationError": {
"title": "ValidationError",
"required": ["loc", "msg", "type"],
"type": "object",
"properties": {
"loc": {
"title": "Location",
"type": "array",
"items": {"type": "string"},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
},
},
}
},
}
def test_tutorial(clear_sqlmodel):
from docs_src.tutorial.fastapi.update import tutorial001 as mod
mod.sqlite_url = "sqlite://"
mod.engine = create_engine(
mod.sqlite_url, connect_args=mod.connect_args, poolclass=StaticPool
)
with TestClient(mod.app) as client:
hero1_data = {"name": "Deadpond", "secret_name": "Dive Wilson"}
hero2_data = {
"name": "Spider-Boy",
"secret_name": "Pedro Parqueador",
"id": 9000,
}
hero3_data = {
"name": "Rusty-Man",
"secret_name": "Tommy Sharp",
"age": 48,
}
response = client.post("/heroes/", json=hero1_data)
assert response.status_code == 200, response.text
response = client.post("/heroes/", json=hero2_data)
assert response.status_code == 200, response.text
hero2 = response.json()
hero2_id = hero2["id"]
response = client.post("/heroes/", json=hero3_data)
assert response.status_code == 200, response.text
hero3 = response.json()
hero3_id = hero3["id"]
response = client.get(f"/heroes/{hero2_id}")
assert response.status_code == 200, response.text
response = client.get("/heroes/9000")
assert response.status_code == 404, response.text
response = client.get("/openapi.json")
data = response.json()
assert response.status_code == 200, response.text
assert data == openapi_schema
response = client.get("/heroes/")
assert response.status_code == 200, response.text
data = response.json()
assert len(data) == 3
response = client.patch(
f"/heroes/{hero2_id}", json={"secret_name": "Spider-Youngster"}
)
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == hero2_data["name"], "The name should not be set to none"
assert (
data["secret_name"] == "Spider-Youngster"
), "The secret name should be updated"
response = client.patch(f"/heroes/{hero3_id}", json={"age": None})
data = response.json()
assert response.status_code == 200, response.text
assert data["name"] == hero3_data["name"]
assert data["age"] is None, (
"A field should be updatable to None, even if " "that's the default"
)
response = client.patch("/heroes/9001", json={"name": "Dragon Cube X"})
assert response.status_code == 404, response.text