Home / File/ test_route_scope.py — fastapi Source File

test_route_scope.py — fastapi Source File

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

File python FastAPI Routing 6 imports 7 functions

Entity Profile

Dependency Diagram

graph LR
  05260da9_779c_1f21_9e8e_e94b8647758e["test_route_scope.py"]
  5befe8bf_65d1_d058_6b78_4a597a8488e9["pytest"]
  05260da9_779c_1f21_9e8e_e94b8647758e --> 5befe8bf_65d1_d058_6b78_4a597a8488e9
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  05260da9_779c_1f21_9e8e_e94b8647758e --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  de395a51_26f8_3424_1af0_2f5bef39c893["routing.py"]
  05260da9_779c_1f21_9e8e_e94b8647758e --> de395a51_26f8_3424_1af0_2f5bef39c893
  aa28685f_bb97_e988_ff40_3e5385960f32["APIRoute"]
  05260da9_779c_1f21_9e8e_e94b8647758e --> aa28685f_bb97_e988_ff40_3e5385960f32
  400570c6_159a_07b5_2e94_ba32f34336f9["APIWebSocketRoute"]
  05260da9_779c_1f21_9e8e_e94b8647758e --> 400570c6_159a_07b5_2e94_ba32f34336f9
  a7c04dee_ee23_5891_b185_47ff6bed036d["testclient.py"]
  05260da9_779c_1f21_9e8e_e94b8647758e --> a7c04dee_ee23_5891_b185_47ff6bed036d
  style 05260da9_779c_1f21_9e8e_e94b8647758e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import pytest
from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect
from fastapi.routing import APIRoute, APIWebSocketRoute
from fastapi.testclient import TestClient

app = FastAPI()


@app.get("/users/{user_id}")
async def get_user(user_id: str, request: Request):
    route: APIRoute = request.scope["route"]
    return {"user_id": user_id, "path": route.path}


@app.websocket("/items/{item_id}")
async def websocket_item(item_id: str, websocket: WebSocket):
    route: APIWebSocketRoute = websocket.scope["route"]
    await websocket.accept()
    await websocket.send_json({"item_id": item_id, "path": route.path})


client = TestClient(app)


def test_get():
    response = client.get("/users/rick")
    assert response.status_code == 200, response.text
    assert response.json() == {"user_id": "rick", "path": "/users/{user_id}"}


def test_invalid_method_doesnt_match():
    response = client.post("/users/rick")
    assert response.status_code == 405, response.text


def test_invalid_path_doesnt_match():
    response = client.post("/usersx/rick")
    assert response.status_code == 404, response.text


def test_websocket():
    with client.websocket_connect("/items/portal-gun") as websocket:
        data = websocket.receive_json()
        assert data == {"item_id": "portal-gun", "path": "/items/{item_id}"}


def test_websocket_invalid_path_doesnt_match():
    with pytest.raises(WebSocketDisconnect):
        with client.websocket_connect("/itemsx/portal-gun"):
            pass  # pragma: no cover

Domain

Subdomains

Frequently Asked Questions

What does test_route_scope.py do?
test_route_scope.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_route_scope.py?
test_route_scope.py defines 7 function(s): get_user, test_get, test_invalid_method_doesnt_match, test_invalid_path_doesnt_match, test_websocket, test_websocket_invalid_path_doesnt_match, websocket_item.
What does test_route_scope.py depend on?
test_route_scope.py imports 6 module(s): APIRoute, APIWebSocketRoute, __init__.py, pytest, routing.py, testclient.py.
Where is test_route_scope.py in the architecture?
test_route_scope.py is located at tests/test_route_scope.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