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

19 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1import importlib 1abdc

2 

3import pytest 1abdc

4from fastapi.testclient import TestClient 1abdc

5from inline_snapshot import snapshot 1abdc

6 

7from tests.utils import needs_py310 1abdc

8 

9 

10@pytest.fixture( 1abdc

11 name="client", 

12 params=[ 

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

14 ], 

15) 

16def get_client(request: pytest.FixtureRequest): 1abdc

17 mod = importlib.import_module(f"docs_src.dataclasses_.{request.param}") 1abc

18 

19 client = TestClient(mod.app) 1abc

20 client.headers.clear() 1abc

21 return client 1abc

22 

23 

24def test_get_item(client: TestClient): 1abdc

25 response = client.get("/items/next") 1efg

26 assert response.status_code == 200 1efg

27 assert response.json() == { 1efg

28 "name": "Island In The Moon", 

29 "price": 12.99, 

30 "description": "A place to be playin' and havin' fun", 

31 "tags": ["breater"], 

32 "tax": None, 

33 } 

34 

35 

36def test_openapi_schema(client: TestClient): 1abdc

37 response = client.get("/openapi.json") 1hij

38 assert response.status_code == 200 1hij

39 assert response.json() == snapshot( 1hij

40 { 

41 "openapi": "3.1.0", 

42 "info": {"title": "FastAPI", "version": "0.1.0"}, 

43 "paths": { 

44 "/items/next": { 

45 "get": { 

46 "summary": "Read Next Item", 

47 "operationId": "read_next_item_items_next_get", 

48 "responses": { 

49 "200": { 

50 "description": "Successful Response", 

51 "content": { 

52 "application/json": { 

53 "schema": {"$ref": "#/components/schemas/Item"} 

54 } 

55 }, 

56 } 

57 }, 

58 } 

59 } 

60 }, 

61 "components": { 

62 "schemas": { 

63 "Item": { 

64 "title": "Item", 

65 "required": ["name", "price"], 

66 "type": "object", 

67 "properties": { 

68 "name": {"title": "Name", "type": "string"}, 

69 "price": {"title": "Price", "type": "number"}, 

70 "tags": { 

71 "title": "Tags", 

72 "type": "array", 

73 "items": {"type": "string"}, 

74 }, 

75 "description": { 

76 "title": "Description", 

77 "anyOf": [{"type": "string"}, {"type": "null"}], 

78 }, 

79 "tax": { 

80 "title": "Tax", 

81 "anyOf": [{"type": "number"}, {"type": "null"}], 

82 }, 

83 }, 

84 } 

85 } 

86 }, 

87 } 

88 )