mirror of https://github.com/tiangolo/fastapi.git
Remove code examples for Python 3.8 in `app_testing`
This commit is contained in:
parent
0d064b6507
commit
90f06e12f8
|
|
@ -2,11 +2,11 @@
|
|||
|
||||
When you need `lifespan` to run in your tests, you can use the `TestClient` with a `with` statement:
|
||||
|
||||
{* ../../docs_src/app_testing/tutorial004.py hl[9:15,18,27:28,30:32,41:43] *}
|
||||
{* ../../docs_src/app_testing/tutorial004_py39.py hl[9:15,18,27:28,30:32,41:43] *}
|
||||
|
||||
|
||||
You can read more details about the ["Running lifespan in tests in the official Starlette documentation site."](https://www.starlette.dev/lifespan/#running-lifespan-in-tests)
|
||||
|
||||
For the deprecated `startup` and `shutdown` events, you can use the `TestClient` as follows:
|
||||
|
||||
{* ../../docs_src/app_testing/tutorial003.py hl[9:12,20:24] *}
|
||||
{* ../../docs_src/app_testing/tutorial003_py39.py hl[9:12,20:24] *}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ You can use the same `TestClient` to test WebSockets.
|
|||
|
||||
For this, you use the `TestClient` in a `with` statement, connecting to the WebSocket:
|
||||
|
||||
{* ../../docs_src/app_testing/tutorial002.py hl[27:31] *}
|
||||
{* ../../docs_src/app_testing/tutorial002_py39.py hl[27:31] *}
|
||||
|
||||
/// note
|
||||
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ Use the `TestClient` object the same way as you do with `httpx`.
|
|||
|
||||
Write simple `assert` statements with the standard Python expressions that you need to check (again, standard `pytest`).
|
||||
|
||||
{* ../../docs_src/app_testing/tutorial001.py hl[2,12,15:18] *}
|
||||
{* ../../docs_src/app_testing/tutorial001_py39.py hl[2,12,15:18] *}
|
||||
|
||||
/// tip
|
||||
|
||||
|
|
@ -76,7 +76,7 @@ Let's say you have a file structure as described in [Bigger Applications](bigger
|
|||
In the file `main.py` you have your **FastAPI** app:
|
||||
|
||||
|
||||
{* ../../docs_src/app_testing/main.py *}
|
||||
{* ../../docs_src/app_testing/app_a_py39/main.py *}
|
||||
|
||||
### Testing file { #testing-file }
|
||||
|
||||
|
|
@ -92,7 +92,7 @@ Then you could have a file `test_main.py` with your tests. It could live on the
|
|||
|
||||
Because this file is in the same package, you can use relative imports to import the object `app` from the `main` module (`main.py`):
|
||||
|
||||
{* ../../docs_src/app_testing/test_main.py hl[3] *}
|
||||
{* ../../docs_src/app_testing/app_a_py39/test_main.py hl[3] *}
|
||||
|
||||
|
||||
...and have the code for the tests just like before.
|
||||
|
|
|
|||
|
|
@ -1,39 +0,0 @@
|
|||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Header, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from typing_extensions import Annotated
|
||||
|
||||
fake_secret_token = "coneofsilence"
|
||||
|
||||
fake_db = {
|
||||
"foo": {"id": "foo", "title": "Foo", "description": "There goes my hero"},
|
||||
"bar": {"id": "bar", "title": "Bar", "description": "The bartenders"},
|
||||
}
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class Item(BaseModel):
|
||||
id: str
|
||||
title: str
|
||||
description: Union[str, None] = None
|
||||
|
||||
|
||||
@app.get("/items/{item_id}", response_model=Item)
|
||||
async def read_main(item_id: str, x_token: Annotated[str, Header()]):
|
||||
if x_token != fake_secret_token:
|
||||
raise HTTPException(status_code=400, detail="Invalid X-Token header")
|
||||
if item_id not in fake_db:
|
||||
raise HTTPException(status_code=404, detail="Item not found")
|
||||
return fake_db[item_id]
|
||||
|
||||
|
||||
@app.post("/items/", response_model=Item)
|
||||
async def create_item(item: Item, x_token: Annotated[str, Header()]):
|
||||
if x_token != fake_secret_token:
|
||||
raise HTTPException(status_code=400, detail="Invalid X-Token header")
|
||||
if item.id in fake_db:
|
||||
raise HTTPException(status_code=409, detail="Item already exists")
|
||||
fake_db[item.id] = item
|
||||
return item
|
||||
|
|
@ -1,65 +0,0 @@
|
|||
from fastapi.testclient import TestClient
|
||||
|
||||
from .main import app
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_read_item():
|
||||
response = client.get("/items/foo", headers={"X-Token": "coneofsilence"})
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {
|
||||
"id": "foo",
|
||||
"title": "Foo",
|
||||
"description": "There goes my hero",
|
||||
}
|
||||
|
||||
|
||||
def test_read_item_bad_token():
|
||||
response = client.get("/items/foo", headers={"X-Token": "hailhydra"})
|
||||
assert response.status_code == 400
|
||||
assert response.json() == {"detail": "Invalid X-Token header"}
|
||||
|
||||
|
||||
def test_read_nonexistent_item():
|
||||
response = client.get("/items/baz", headers={"X-Token": "coneofsilence"})
|
||||
assert response.status_code == 404
|
||||
assert response.json() == {"detail": "Item not found"}
|
||||
|
||||
|
||||
def test_create_item():
|
||||
response = client.post(
|
||||
"/items/",
|
||||
headers={"X-Token": "coneofsilence"},
|
||||
json={"id": "foobar", "title": "Foo Bar", "description": "The Foo Barters"},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {
|
||||
"id": "foobar",
|
||||
"title": "Foo Bar",
|
||||
"description": "The Foo Barters",
|
||||
}
|
||||
|
||||
|
||||
def test_create_item_bad_token():
|
||||
response = client.post(
|
||||
"/items/",
|
||||
headers={"X-Token": "hailhydra"},
|
||||
json={"id": "bazz", "title": "Bazz", "description": "Drop the bazz"},
|
||||
)
|
||||
assert response.status_code == 400
|
||||
assert response.json() == {"detail": "Invalid X-Token header"}
|
||||
|
||||
|
||||
def test_create_existing_item():
|
||||
response = client.post(
|
||||
"/items/",
|
||||
headers={"X-Token": "coneofsilence"},
|
||||
json={
|
||||
"id": "foo",
|
||||
"title": "The Foo ID Stealers",
|
||||
"description": "There goes my stealer",
|
||||
},
|
||||
)
|
||||
assert response.status_code == 409
|
||||
assert response.json() == {"detail": "Item already exists"}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
from docs_src.app_testing.test_main import client, test_read_main
|
||||
from docs_src.app_testing.app_a_py39.test_main import client, test_read_main
|
||||
|
||||
|
||||
def test_main():
|
||||
|
|
@ -3,16 +3,15 @@ from types import ModuleType
|
|||
|
||||
import pytest
|
||||
|
||||
from ...utils import needs_py39, needs_py310
|
||||
from ...utils import needs_py310
|
||||
|
||||
|
||||
@pytest.fixture(
|
||||
name="test_module",
|
||||
params=[
|
||||
"app_b.test_main",
|
||||
"app_b_py39.test_main",
|
||||
pytest.param("app_b_py310.test_main", marks=needs_py310),
|
||||
"app_b_an.test_main",
|
||||
pytest.param("app_b_an_py39.test_main", marks=needs_py39),
|
||||
"app_b_an_py39.test_main",
|
||||
pytest.param("app_b_an_py310.test_main", marks=needs_py310),
|
||||
],
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from docs_src.app_testing.tutorial001 import client, test_read_main
|
||||
from docs_src.app_testing.tutorial001_py39 import client, test_read_main
|
||||
|
||||
|
||||
def test_main():
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from docs_src.app_testing.tutorial002 import test_read_main, test_websocket
|
||||
from docs_src.app_testing.tutorial002_py39 import test_read_main, test_websocket
|
||||
|
||||
|
||||
def test_main():
|
||||
|
|
|
|||
|
|
@ -3,5 +3,5 @@ import pytest
|
|||
|
||||
def test_main():
|
||||
with pytest.warns(DeprecationWarning):
|
||||
from docs_src.app_testing.tutorial003 import test_read_items
|
||||
from docs_src.app_testing.tutorial003_py39 import test_read_items
|
||||
test_read_items()
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from docs_src.app_testing.tutorial004 import test_read_items
|
||||
from docs_src.app_testing.tutorial004_py39 import test_read_items
|
||||
|
||||
|
||||
def test_main():
|
||||
|
|
|
|||
Loading…
Reference in New Issue