Home / File/ test_tutorial002.py — fastapi Source File

test_tutorial002.py — fastapi Source File

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

File python FastAPI Responses 6 imports 7 functions

Entity Profile

Dependency Diagram

graph LR
  45a8bc94_3841_f038_9f98_489f355c96bd["test_tutorial002.py"]
  b8f0130d_8455_5194_6519_dad030b08b7e["utils"]
  45a8bc94_3841_f038_9f98_489f355c96bd --> b8f0130d_8455_5194_6519_dad030b08b7e
  83e01a9c_50a9_bec9_6f86_8e729106de01["importlib"]
  45a8bc94_3841_f038_9f98_489f355c96bd --> 83e01a9c_50a9_bec9_6f86_8e729106de01
  5befe8bf_65d1_d058_6b78_4a597a8488e9["pytest"]
  45a8bc94_3841_f038_9f98_489f355c96bd --> 5befe8bf_65d1_d058_6b78_4a597a8488e9
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  45a8bc94_3841_f038_9f98_489f355c96bd --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  a7c04dee_ee23_5891_b185_47ff6bed036d["testclient.py"]
  45a8bc94_3841_f038_9f98_489f355c96bd --> a7c04dee_ee23_5891_b185_47ff6bed036d
  75b7515c_c403_b332_0929_cfe34deb0c70["websockets.py"]
  45a8bc94_3841_f038_9f98_489f355c96bd --> 75b7515c_c403_b332_0929_cfe34deb0c70
  style 45a8bc94_3841_f038_9f98_489f355c96bd fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import importlib

import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from fastapi.websockets import WebSocketDisconnect

from ...utils import needs_py310


@pytest.fixture(
    name="app",
    params=[
        pytest.param("tutorial002_py39"),
        pytest.param("tutorial002_py310", marks=needs_py310),
        pytest.param("tutorial002_an_py39"),
        pytest.param("tutorial002_an_py310", marks=needs_py310),
    ],
)
def get_app(request: pytest.FixtureRequest):
    mod = importlib.import_module(f"docs_src.websockets.{request.param}")

    return mod.app


def test_main(app: FastAPI):
    client = TestClient(app)
    response = client.get("/")
    assert response.status_code == 200, response.text
    assert b"<!DOCTYPE html>" in response.content


def test_websocket_with_cookie(app: FastAPI):
    client = TestClient(app, cookies={"session": "fakesession"})
    with pytest.raises(WebSocketDisconnect):
        with client.websocket_connect("/items/foo/ws") as websocket:
            message = "Message one"
            websocket.send_text(message)
            data = websocket.receive_text()
            assert data == "Session cookie or query token value is: fakesession"
            data = websocket.receive_text()
            assert data == f"Message text was: {message}, for item ID: foo"
            message = "Message two"
            websocket.send_text(message)
            data = websocket.receive_text()
            assert data == "Session cookie or query token value is: fakesession"
            data = websocket.receive_text()
            assert data == f"Message text was: {message}, for item ID: foo"


def test_websocket_with_header(app: FastAPI):
    client = TestClient(app)
    with pytest.raises(WebSocketDisconnect):
        with client.websocket_connect("/items/bar/ws?token=some-token") as websocket:
            message = "Message one"
            websocket.send_text(message)
            data = websocket.receive_text()
            assert data == "Session cookie or query token value is: some-token"
            data = websocket.receive_text()
            assert data == f"Message text was: {message}, for item ID: bar"
            message = "Message two"
            websocket.send_text(message)
            data = websocket.receive_text()
            assert data == "Session cookie or query token value is: some-token"
            data = websocket.receive_text()
            assert data == f"Message text was: {message}, for item ID: bar"


def test_websocket_with_header_and_query(app: FastAPI):
    client = TestClient(app)
    with pytest.raises(WebSocketDisconnect):
        with client.websocket_connect("/items/2/ws?q=3&token=some-token") as websocket:
            message = "Message one"
            websocket.send_text(message)
            data = websocket.receive_text()
            assert data == "Session cookie or query token value is: some-token"
            data = websocket.receive_text()
            assert data == "Query parameter q is: 3"
            data = websocket.receive_text()
            assert data == f"Message text was: {message}, for item ID: 2"
            message = "Message two"
            websocket.send_text(message)
            data = websocket.receive_text()
            assert data == "Session cookie or query token value is: some-token"
            data = websocket.receive_text()
            assert data == "Query parameter q is: 3"
            data = websocket.receive_text()
            assert data == f"Message text was: {message}, for item ID: 2"


def test_websocket_no_credentials(app: FastAPI):
    client = TestClient(app)
    with pytest.raises(WebSocketDisconnect):
        with client.websocket_connect("/items/foo/ws"):
            pytest.fail(
                "did not raise WebSocketDisconnect on __enter__"
            )  # pragma: no cover


def test_websocket_invalid_data(app: FastAPI):
    client = TestClient(app)
    with pytest.raises(WebSocketDisconnect):
        with client.websocket_connect("/items/foo/ws?q=bar&token=some-token"):
            pytest.fail(
                "did not raise WebSocketDisconnect on __enter__"
            )  # pragma: no cover

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does test_tutorial002.py do?
test_tutorial002.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_tutorial002.py?
test_tutorial002.py defines 7 function(s): get_app, test_main, test_websocket_invalid_data, test_websocket_no_credentials, test_websocket_with_cookie, test_websocket_with_header, test_websocket_with_header_and_query.
What does test_tutorial002.py depend on?
test_tutorial002.py imports 6 module(s): __init__.py, importlib, pytest, testclient.py, utils, websockets.py.
Where is test_tutorial002.py in the architecture?
test_tutorial002.py is located at tests/test_tutorial/test_websockets/test_tutorial002.py (domain: FastAPI, subdomain: Responses, directory: tests/test_tutorial/test_websockets).

Analyze Your Own Codebase

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

Try Supermodel Free