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

10 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-09-09 09:16 +0000

1from dirty_equals import IsDict, IsOneOf 1abcdef

2from fastapi.testclient import TestClient 1abcdef

3 

4from docs_src.custom_request_and_route.tutorial002 import app 1abcdef

5 

6client = TestClient(app) 1abcdef

7 

8 

9def test_endpoint_works(): 1abcdef

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

11 assert response.json() == 6 1ghijkl

12 

13 

14def test_exception_handler_body_access(): 1abcdef

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

16 assert response.json() == IsDict( 1mnopqr

17 { 

18 "detail": { 

19 "errors": [ 

20 { 

21 "type": "list_type", 

22 "loc": ["body"], 

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

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

25 } 

26 ], 

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

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

29 } 

30 } 

31 ) | IsDict( 

32 # TODO: remove when deprecating Pydantic v1 

33 { 

34 "detail": { 

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

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

37 "errors": [ 

38 { 

39 "loc": ["body"], 

40 "msg": "value is not a valid list", 

41 "type": "type_error.list", 

42 } 

43 ], 

44 } 

45 } 

46 )