Coverage for tests / test_tutorial / test_custom_request_and_route / test_tutorial002.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1import importlib 1abcd

2 

3import pytest 1abcd

4from dirty_equals import IsOneOf 1abcd

5from fastapi.testclient import TestClient 1abcd

6 

7from tests.utils import needs_py310 1abcd

8 

9 

10@pytest.fixture( 1abcd

11 name="client", 

12 params=[ 

13 pytest.param("tutorial002_py310", marks=needs_py310), 

14 pytest.param("tutorial002_an_py310", marks=needs_py310), 

15 ], 

16) 

17def get_client(request: pytest.FixtureRequest): 1abcd

18 mod = importlib.import_module(f"docs_src.custom_request_and_route.{request.param}") 1efghijklmnopqr

19 

20 client = TestClient(mod.app) 1efghijklmnopqr

21 return client 1efghijklmnopqr

22 

23 

24def test_endpoint_works(client: TestClient): 1abcd

25 response = client.post("/", json=[1, 2, 3]) 1stuvwxyz

26 assert response.json() == 6 1stuvwxyz

27 

28 

29def test_exception_handler_body_access(client: TestClient): 1abcd

30 response = client.post("/", json={"numbers": [1, 2, 3]}) 1ABCDEFGH

31 assert response.json() == { 1ABCDEFGH

32 "detail": { 

33 "errors": [ 

34 { 

35 "type": "list_type", 

36 "loc": ["body"], 

37 "msg": "Input should be a valid list", 

38 "input": {"numbers": [1, 2, 3]}, 

39 } 

40 ], 

41 # httpx 0.28.0 switches to compact JSON https://github.com/encode/httpx/issues/3363 

42 "body": IsOneOf('{"numbers": [1, 2, 3]}', '{"numbers":[1,2,3]}'), 

43 } 

44 }