Browse Source

remove deprecated event warnings and TODOs

pull/14773/head
Pravin Kamble 6 months ago
parent
commit
157aa23b14
  1. 4
      tests/test_tutorial/test_events/test_tutorial001.py
  2. 4
      tests/test_tutorial/test_events/test_tutorial002.py
  3. 174
      tests/test_tutorial/test_sql_databases/test_tutorial001.py
  4. 172
      tests/test_tutorial/test_sql_databases/test_tutorial002.py
  5. 7
      tests/test_tutorial/test_testing/test_tutorial003.py

4
tests/test_tutorial/test_events/test_tutorial001.py

@ -5,8 +5,8 @@ from fastapi.testclient import TestClient
@pytest.fixture(name="app", scope="module")
def get_app():
with pytest.warns(DeprecationWarning):
from docs_src.events.tutorial001_py39 import app
from docs_src.events.tutorial001_py39 import app
yield app

4
tests/test_tutorial/test_events/test_tutorial002.py

@ -5,8 +5,8 @@ from fastapi.testclient import TestClient
@pytest.fixture(name="app", scope="module")
def get_app():
with pytest.warns(DeprecationWarning):
from docs_src.events.tutorial002_py39 import app
from docs_src.events.tutorial002_py39 import app
yield app

174
tests/test_tutorial/test_sql_databases/test_tutorial001.py

@ -1,5 +1,4 @@
import importlib
import warnings
import pytest
from dirty_equals import IsInt
@ -30,12 +29,9 @@ def clear_sqlmodel():
)
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 = 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
@ -48,99 +44,95 @@ def get_client(request: pytest.FixtureRequest):
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() == []
# 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
# Create a hero
response = client.post(
"/heroes/",
json={
"id": 999,
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",
},
)
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",
},
]
)
{
"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",
}
]
)
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})
# 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.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"})
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):

172
tests/test_tutorial/test_sql_databases/test_tutorial002.py

@ -1,5 +1,4 @@
import importlib
import warnings
import pytest
from dirty_equals import IsInt
@ -30,12 +29,9 @@ def clear_sqlmodel():
)
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 = 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
@ -48,103 +44,93 @@ def get_client(request: pytest.FixtureRequest):
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() == []
# 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"
)
# 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 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
# 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/")
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()}]
)
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": Is(hero_id)}
)
# 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": Is(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": Is(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": Is(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})
# 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
# 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"})
# 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"})
# 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):

7
tests/test_tutorial/test_testing/test_tutorial003.py

@ -1,7 +1,4 @@
import pytest
def test_main():
with pytest.warns(DeprecationWarning):
from docs_src.app_testing.tutorial003_py39 import test_read_items
from docs_src.app_testing.tutorial003_py39 import test_read_items
test_read_items()

Loading…
Cancel
Save