Browse Source

Remove code examples for Python 3.8 in `app_testing`

pull/14510/head
Yurii Motov 7 months ago
parent
commit
90f06e12f8
  1. 4
      docs/en/docs/advanced/testing-events.md
  2. 2
      docs/en/docs/advanced/testing-websockets.md
  3. 6
      docs/en/docs/tutorial/testing.md
  4. 0
      docs_src/app_testing/app_a_py39/__init__.py
  5. 0
      docs_src/app_testing/app_a_py39/main.py
  6. 0
      docs_src/app_testing/app_a_py39/test_main.py
  7. 39
      docs_src/app_testing/app_b_an/main.py
  8. 65
      docs_src/app_testing/app_b_an/test_main.py
  9. 0
      docs_src/app_testing/app_b_py39/__init__.py
  10. 0
      docs_src/app_testing/app_b_py39/main.py
  11. 0
      docs_src/app_testing/app_b_py39/test_main.py
  12. 0
      docs_src/app_testing/tutorial001_py39.py
  13. 0
      docs_src/app_testing/tutorial002_py39.py
  14. 0
      docs_src/app_testing/tutorial003_py39.py
  15. 0
      docs_src/app_testing/tutorial004_py39.py
  16. 2
      tests/test_tutorial/test_testing/test_main_a.py
  17. 7
      tests/test_tutorial/test_testing/test_main_b.py
  18. 2
      tests/test_tutorial/test_testing/test_tutorial001.py
  19. 2
      tests/test_tutorial/test_testing/test_tutorial002.py
  20. 2
      tests/test_tutorial/test_testing/test_tutorial003.py
  21. 2
      tests/test_tutorial/test_testing/test_tutorial004.py

4
docs/en/docs/advanced/testing-events.md

@ -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] *}

2
docs/en/docs/advanced/testing-websockets.md

@ -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

6
docs/en/docs/tutorial/testing.md

@ -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.

0
docs_src/app_testing/app_b/__init__.py → docs_src/app_testing/app_a_py39/__init__.py

0
docs_src/app_testing/main.py → docs_src/app_testing/app_a_py39/main.py

0
docs_src/app_testing/test_main.py → docs_src/app_testing/app_a_py39/test_main.py

39
docs_src/app_testing/app_b_an/main.py

@ -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

65
docs_src/app_testing/app_b_an/test_main.py

@ -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"}

0
docs_src/app_testing/app_b_an/__init__.py → docs_src/app_testing/app_b_py39/__init__.py

0
docs_src/app_testing/app_b/main.py → docs_src/app_testing/app_b_py39/main.py

0
docs_src/app_testing/app_b/test_main.py → docs_src/app_testing/app_b_py39/test_main.py

0
docs_src/app_testing/tutorial001.py → docs_src/app_testing/tutorial001_py39.py

0
docs_src/app_testing/tutorial002.py → docs_src/app_testing/tutorial002_py39.py

0
docs_src/app_testing/tutorial003.py → docs_src/app_testing/tutorial003_py39.py

0
docs_src/app_testing/tutorial004.py → docs_src/app_testing/tutorial004_py39.py

2
tests/test_tutorial/test_testing/test_main.py → tests/test_tutorial/test_testing/test_main_a.py

@ -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():

7
tests/test_tutorial/test_testing/test_main_b.py

@ -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),
],
)

2
tests/test_tutorial/test_testing/test_tutorial001.py

@ -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():

2
tests/test_tutorial/test_testing/test_tutorial002.py

@ -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():

2
tests/test_tutorial/test_testing/test_tutorial003.py

@ -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()

2
tests/test_tutorial/test_testing/test_tutorial004.py

@ -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…
Cancel
Save