test_dependency_yield_except_httpexception.py — fastapi Source File
Architecture documentation for test_dependency_yield_except_httpexception.py, a python file in the fastapi codebase. 3 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR f6f3823e_de1c_c0d8_8c4a_03fc7fc4718a["test_dependency_yield_except_httpexception.py"] 5befe8bf_65d1_d058_6b78_4a597a8488e9["pytest"] f6f3823e_de1c_c0d8_8c4a_03fc7fc4718a --> 5befe8bf_65d1_d058_6b78_4a597a8488e9 534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"] f6f3823e_de1c_c0d8_8c4a_03fc7fc4718a --> 534f6e44_61b8_3c38_8b89_6934a6df9802 a7c04dee_ee23_5891_b185_47ff6bed036d["testclient.py"] f6f3823e_de1c_c0d8_8c4a_03fc7fc4718a --> a7c04dee_ee23_5891_b185_47ff6bed036d style f6f3823e_de1c_c0d8_8c4a_03fc7fc4718a fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import pytest
from fastapi import Body, Depends, FastAPI, HTTPException
from fastapi.testclient import TestClient
initial_fake_database = {"rick": "Rick Sanchez"}
fake_database = initial_fake_database.copy()
initial_state = {"except": False, "finally": False}
state = initial_state.copy()
app = FastAPI()
async def get_database():
temp_database = fake_database.copy()
try:
yield temp_database
fake_database.update(temp_database)
except HTTPException:
state["except"] = True
raise
finally:
state["finally"] = True
@app.put("/invalid-user/{user_id}")
def put_invalid_user(
user_id: str, name: str = Body(), db: dict = Depends(get_database)
):
db[user_id] = name
raise HTTPException(status_code=400, detail="Invalid user")
@app.put("/user/{user_id}")
def put_user(user_id: str, name: str = Body(), db: dict = Depends(get_database)):
db[user_id] = name
return {"message": "OK"}
@pytest.fixture(autouse=True)
def reset_state_and_db():
global fake_database
global state
fake_database = initial_fake_database.copy()
state = initial_state.copy()
client = TestClient(app)
def test_dependency_gets_exception():
assert state["except"] is False
assert state["finally"] is False
response = client.put("/invalid-user/rick", json="Morty")
assert response.status_code == 400, response.text
assert response.json() == {"detail": "Invalid user"}
assert state["except"] is True
assert state["finally"] is True
assert fake_database["rick"] == "Rick Sanchez"
def test_dependency_no_exception():
assert state["except"] is False
assert state["finally"] is False
response = client.put("/user/rick", json="Morty")
assert response.status_code == 200, response.text
assert response.json() == {"message": "OK"}
assert state["except"] is False
assert state["finally"] is True
assert fake_database["rick"] == "Morty"
Domain
Subdomains
Functions
Dependencies
- __init__.py
- pytest
- testclient.py
Source
Frequently Asked Questions
What does test_dependency_yield_except_httpexception.py do?
test_dependency_yield_except_httpexception.py is a source file in the fastapi codebase, written in python. It belongs to the FastAPI domain, Routing subdomain.
What functions are defined in test_dependency_yield_except_httpexception.py?
test_dependency_yield_except_httpexception.py defines 6 function(s): get_database, put_invalid_user, put_user, reset_state_and_db, test_dependency_gets_exception, test_dependency_no_exception.
What does test_dependency_yield_except_httpexception.py depend on?
test_dependency_yield_except_httpexception.py imports 3 module(s): __init__.py, pytest, testclient.py.
Where is test_dependency_yield_except_httpexception.py in the architecture?
test_dependency_yield_except_httpexception.py is located at tests/test_dependency_yield_except_httpexception.py (domain: FastAPI, subdomain: Routing, directory: tests).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free