Coverage for tests/test_tutorial/test_websockets/test_tutorial003_py39.py: 100%

34 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1import pytest 1eabcd

2from fastapi import FastAPI 1eabcd

3from fastapi.testclient import TestClient 1eabcd

4 

5from ...utils import needs_py39 1eabcd

6 

7 

8@pytest.fixture(name="app") 1eabcd

9def get_app(): 1eabcd

10 from docs_src.websockets.tutorial003_py39 import app 1abcd

11 

12 return app 1abcd

13 

14 

15@pytest.fixture(name="html") 1eabcd

16def get_html(): 1eabcd

17 from docs_src.websockets.tutorial003_py39 import html 1abcd

18 

19 return html 1abcd

20 

21 

22@pytest.fixture(name="client") 1eabcd

23def get_client(app: FastAPI): 1eabcd

24 client = TestClient(app) 1abcd

25 

26 return client 1abcd

27 

28 

29@needs_py39 1eabcd

30def test_get(client: TestClient, html: str): 1eabcd

31 response = client.get("/") 1abcd

32 assert response.text == html 1abcd

33 

34 

35@needs_py39 1eabcd

36def test_websocket_handle_disconnection(client: TestClient): 1eabcd

37 with client.websocket_connect("/ws/1234") as connection, client.websocket_connect( 1abcd

38 "/ws/5678" 

39 ) as connection_two: 

40 connection.send_text("Hello from 1234") 1abcd

41 data1 = connection.receive_text() 1abcd

42 assert data1 == "You wrote: Hello from 1234" 1abcd

43 data2 = connection_two.receive_text() 1abcd

44 client1_says = "Client #1234 says: Hello from 1234" 1abcd

45 assert data2 == client1_says 1abcd

46 data1 = connection.receive_text() 1abcd

47 assert data1 == client1_says 1abcd

48 connection_two.close() 1abcd

49 data1 = connection.receive_text() 1abcd

50 assert data1 == "Client #5678 left the chat" 1abcd