Home / File/ test_dependency_after_yield_raise.py — fastapi Source File

test_dependency_after_yield_raise.py — fastapi Source File

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

File python FastAPI Applications 4 imports 8 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  2213b42a_570d_4b49_233d_4031fcf3f00c["test_dependency_after_yield_raise.py"]
  0dda2280_3359_8460_301c_e98c77e78185["typing"]
  2213b42a_570d_4b49_233d_4031fcf3f00c --> 0dda2280_3359_8460_301c_e98c77e78185
  5befe8bf_65d1_d058_6b78_4a597a8488e9["pytest"]
  2213b42a_570d_4b49_233d_4031fcf3f00c --> 5befe8bf_65d1_d058_6b78_4a597a8488e9
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  2213b42a_570d_4b49_233d_4031fcf3f00c --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  a7c04dee_ee23_5891_b185_47ff6bed036d["testclient.py"]
  2213b42a_570d_4b49_233d_4031fcf3f00c --> a7c04dee_ee23_5891_b185_47ff6bed036d
  style 2213b42a_570d_4b49_233d_4031fcf3f00c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from typing import Annotated, Any

import pytest
from fastapi import Depends, FastAPI, HTTPException
from fastapi.testclient import TestClient


class CustomError(Exception):
    pass


def catching_dep() -> Any:
    try:
        yield "s"
    except CustomError as err:
        raise HTTPException(status_code=418, detail="Session error") from err


def broken_dep() -> Any:
    yield "s"
    raise ValueError("Broken after yield")


app = FastAPI()


@app.get("/catching")
def catching(d: Annotated[str, Depends(catching_dep)]) -> Any:
    raise CustomError("Simulated error during streaming")


@app.get("/broken")
def broken(d: Annotated[str, Depends(broken_dep)]) -> Any:
    return {"message": "all good?"}


client = TestClient(app)


def test_catching():
    response = client.get("/catching")
    assert response.status_code == 418
    assert response.json() == {"detail": "Session error"}


def test_broken_raise():
    with pytest.raises(ValueError, match="Broken after yield"):
        client.get("/broken")


def test_broken_no_raise():
    """
    When a dependency with yield raises after the yield (not in an except), the
    response is already "successfully" sent back to the client, but there's still
    an error in the server afterwards, an exception is raised and captured or shown
    in the server logs.
    """
    with TestClient(app, raise_server_exceptions=False) as client:
        response = client.get("/broken")
        assert response.status_code == 200
        assert response.json() == {"message": "all good?"}


def test_broken_return_finishes():
    client = TestClient(app, raise_server_exceptions=False)
    response = client.get("/broken")
    assert response.status_code == 200
    assert response.json() == {"message": "all good?"}

Domain

Subdomains

Classes

Dependencies

Frequently Asked Questions

What does test_dependency_after_yield_raise.py do?
test_dependency_after_yield_raise.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 test_dependency_after_yield_raise.py?
test_dependency_after_yield_raise.py defines 8 function(s): broken, broken_dep, catching, catching_dep, test_broken_no_raise, test_broken_raise, test_broken_return_finishes, test_catching.
What does test_dependency_after_yield_raise.py depend on?
test_dependency_after_yield_raise.py imports 4 module(s): __init__.py, pytest, testclient.py, typing.
Where is test_dependency_after_yield_raise.py in the architecture?
test_dependency_after_yield_raise.py is located at tests/test_dependency_after_yield_raise.py (domain: FastAPI, subdomain: Applications, directory: tests).

Analyze Your Own Codebase

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

Try Supermodel Free