Valentyn 4 days ago
committed by GitHub
parent
commit
43e8a63971
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 25
      tests/test_tutorial/test_websockets/test_tutorial003.py
  2. 50
      tests/test_tutorial/test_websockets/test_tutorial003_py39.py

25
tests/test_tutorial/test_websockets/test_tutorial003.py

@ -1,16 +1,33 @@
import importlib
import pytest
from fastapi.testclient import TestClient
from docs_src.websockets.tutorial003 import app, html
from docs_src.websockets.tutorial003 import html
from ...utils import needs_py39
client = TestClient(app)
@pytest.fixture(
name="app",
params=[
"tutorial003",
pytest.param("tutorial003_py39", marks=needs_py39),
],
)
def get_app(request: pytest.FixtureRequest):
mod = importlib.import_module(f"docs_src.websockets.{request.param}")
return mod.app
def test_get():
def test_get(app):
client = TestClient(app)
response = client.get("/")
assert response.text == html
def test_websocket_handle_disconnection():
def test_websocket_handle_disconnection(app):
client = TestClient(app)
with client.websocket_connect("/ws/1234") as connection, client.websocket_connect(
"/ws/5678"
) as connection_two:

50
tests/test_tutorial/test_websockets/test_tutorial003_py39.py

@ -1,50 +0,0 @@
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from ...utils import needs_py39
@pytest.fixture(name="app")
def get_app():
from docs_src.websockets.tutorial003_py39 import app
return app
@pytest.fixture(name="html")
def get_html():
from docs_src.websockets.tutorial003_py39 import html
return html
@pytest.fixture(name="client")
def get_client(app: FastAPI):
client = TestClient(app)
return client
@needs_py39
def test_get(client: TestClient, html: str):
response = client.get("/")
assert response.text == html
@needs_py39
def test_websocket_handle_disconnection(client: TestClient):
with client.websocket_connect("/ws/1234") as connection, client.websocket_connect(
"/ws/5678"
) as connection_two:
connection.send_text("Hello from 1234")
data1 = connection.receive_text()
assert data1 == "You wrote: Hello from 1234"
data2 = connection_two.receive_text()
client1_says = "Client #1234 says: Hello from 1234"
assert data2 == client1_says
data1 = connection.receive_text()
assert data1 == client1_says
connection_two.close()
data1 = connection.receive_text()
assert data1 == "Client #5678 left the chat"
Loading…
Cancel
Save