Home / File/ tutorial004_py39.py — fastapi Source File

tutorial004_py39.py — fastapi Source File

Architecture documentation for tutorial004_py39.py, a python file in the fastapi codebase. 3 imports, 1 dependents.

File python FastAPI Applications 3 imports 1 dependents 3 functions

Entity Profile

Dependency Diagram

graph LR
  928274a7_8738_3478_e87f_645fde08f652["tutorial004_py39.py"]
  a88eb002_9197_73f3_410d_ee5315767f34["contextlib"]
  928274a7_8738_3478_e87f_645fde08f652 --> a88eb002_9197_73f3_410d_ee5315767f34
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  928274a7_8738_3478_e87f_645fde08f652 --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  a7c04dee_ee23_5891_b185_47ff6bed036d["testclient.py"]
  928274a7_8738_3478_e87f_645fde08f652 --> a7c04dee_ee23_5891_b185_47ff6bed036d
  240498ac_d38a_2f28_1b70_c6343388cbe1["test_tutorial004.py"]
  240498ac_d38a_2f28_1b70_c6343388cbe1 --> 928274a7_8738_3478_e87f_645fde08f652
  style 928274a7_8738_3478_e87f_645fde08f652 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from contextlib import asynccontextmanager

from fastapi import FastAPI
from fastapi.testclient import TestClient

items = {}


@asynccontextmanager
async def lifespan(app: FastAPI):
    items["foo"] = {"name": "Fighters"}
    items["bar"] = {"name": "Tenders"}
    yield
    # clean up items
    items.clear()


app = FastAPI(lifespan=lifespan)


@app.get("/items/{item_id}")
async def read_items(item_id: str):
    return items[item_id]


def test_read_items():
    # Before the lifespan starts, "items" is still empty
    assert items == {}

    with TestClient(app) as client:
        # Inside the "with TestClient" block, the lifespan starts and items added
        assert items == {"foo": {"name": "Fighters"}, "bar": {"name": "Tenders"}}

        response = client.get("/items/foo")
        assert response.status_code == 200
        assert response.json() == {"name": "Fighters"}

        # After the requests is done, the items are still there
        assert items == {"foo": {"name": "Fighters"}, "bar": {"name": "Tenders"}}

    # The end of the "with TestClient" block simulates terminating the app, so
    # the lifespan ends and items are cleaned up
    assert items == {}

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does tutorial004_py39.py do?
tutorial004_py39.py is a source file in the fastapi codebase, written in python. It belongs to the FastAPI domain, Applications subdomain.
What functions are defined in tutorial004_py39.py?
tutorial004_py39.py defines 3 function(s): lifespan, read_items, test_read_items.
What does tutorial004_py39.py depend on?
tutorial004_py39.py imports 3 module(s): __init__.py, contextlib, testclient.py.
What files import tutorial004_py39.py?
tutorial004_py39.py is imported by 1 file(s): test_tutorial004.py.
Where is tutorial004_py39.py in the architecture?
tutorial004_py39.py is located at docs_src/app_testing/tutorial004_py39.py (domain: FastAPI, subdomain: Applications, directory: docs_src/app_testing).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free