Home / File/ test_response_model_data_filter.py — fastapi Source File

test_response_model_data_filter.py — fastapi Source File

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

File python FastAPI Responses 3 imports 6 functions 5 classes

Entity Profile

Dependency Diagram

graph LR
  64b2c255_9420_0f2b_b461_e8bc315d51ef["test_response_model_data_filter.py"]
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  64b2c255_9420_0f2b_b461_e8bc315d51ef --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  a7c04dee_ee23_5891_b185_47ff6bed036d["testclient.py"]
  64b2c255_9420_0f2b_b461_e8bc315d51ef --> a7c04dee_ee23_5891_b185_47ff6bed036d
  6913fbd4_39df_d14b_44bb_522e99b65b90["pydantic"]
  64b2c255_9420_0f2b_b461_e8bc315d51ef --> 6913fbd4_39df_d14b_44bb_522e99b65b90
  style 64b2c255_9420_0f2b_b461_e8bc315d51ef fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from fastapi import FastAPI
from fastapi.testclient import TestClient
from pydantic import BaseModel

app = FastAPI()


class UserBase(BaseModel):
    email: str


class UserCreate(UserBase):
    password: str


class UserDB(UserBase):
    hashed_password: str


class PetDB(BaseModel):
    name: str
    owner: UserDB


class PetOut(BaseModel):
    name: str
    owner: UserBase


@app.post("/users/", response_model=UserBase)
async def create_user(user: UserCreate):
    return user


@app.get("/pets/{pet_id}", response_model=PetOut)
async def read_pet(pet_id: int):
    user = UserDB(
        email="johndoe@example.com",
        hashed_password="secrethashed",
    )
    pet = PetDB(name="Nibbler", owner=user)
    return pet


@app.get("/pets/", response_model=list[PetOut])
async def read_pets():
    user = UserDB(
        email="johndoe@example.com",
        hashed_password="secrethashed",
    )
    pet1 = PetDB(name="Nibbler", owner=user)
    pet2 = PetDB(name="Zoidberg", owner=user)
    return [pet1, pet2]


client = TestClient(app)


def test_filter_top_level_model():
    response = client.post(
        "/users", json={"email": "johndoe@example.com", "password": "secret"}
    )
    assert response.json() == {"email": "johndoe@example.com"}


def test_filter_second_level_model():
    response = client.get("/pets/1")
    assert response.json() == {
        "name": "Nibbler",
        "owner": {"email": "johndoe@example.com"},
    }


def test_list_of_models():
    response = client.get("/pets/")
    assert response.json() == [
        {"name": "Nibbler", "owner": {"email": "johndoe@example.com"}},
        {"name": "Zoidberg", "owner": {"email": "johndoe@example.com"}},
    ]

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does test_response_model_data_filter.py do?
test_response_model_data_filter.py is a source file in the fastapi codebase, written in python. It belongs to the FastAPI domain, Responses subdomain.
What functions are defined in test_response_model_data_filter.py?
test_response_model_data_filter.py defines 6 function(s): create_user, read_pet, read_pets, test_filter_second_level_model, test_filter_top_level_model, test_list_of_models.
What does test_response_model_data_filter.py depend on?
test_response_model_data_filter.py imports 3 module(s): __init__.py, pydantic, testclient.py.
Where is test_response_model_data_filter.py in the architecture?
test_response_model_data_filter.py is located at tests/test_response_model_data_filter.py (domain: FastAPI, subdomain: Responses, directory: tests).

Analyze Your Own Codebase

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

Try Supermodel Free