mirror of https://github.com/tiangolo/fastapi.git
Simplify tests for variants `websockets.tutorial003`
This commit is contained in:
parent
f6bfde23c8
commit
099878b3d8
|
|
@ -1,16 +1,45 @@
|
|||
import importlib
|
||||
from types import ModuleType
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from docs_src.websockets.tutorial003 import app, html
|
||||
|
||||
client = TestClient(app)
|
||||
from ...utils import needs_py39
|
||||
|
||||
|
||||
def test_get():
|
||||
@pytest.fixture(
|
||||
name="mod",
|
||||
params=[
|
||||
pytest.param("tutorial003"),
|
||||
pytest.param("tutorial003_py39", marks=needs_py39),
|
||||
],
|
||||
)
|
||||
def get_mod(request: pytest.FixtureRequest):
|
||||
mod = importlib.import_module(f"docs_src.websockets.{request.param}")
|
||||
|
||||
return mod
|
||||
|
||||
|
||||
@pytest.fixture(name="html")
|
||||
def get_html(mod: ModuleType):
|
||||
return mod.html
|
||||
|
||||
|
||||
@pytest.fixture(name="client")
|
||||
def get_client(mod: ModuleType):
|
||||
client = TestClient(mod.app)
|
||||
|
||||
return client
|
||||
|
||||
|
||||
@needs_py39
|
||||
def test_get(client: TestClient, html: str):
|
||||
response = client.get("/")
|
||||
assert response.text == html
|
||||
|
||||
|
||||
def test_websocket_handle_disconnection():
|
||||
@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:
|
||||
|
|
|
|||
|
|
@ -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…
Reference in New Issue