Coverage for tests/test_tutorial/test_schema_extra_example/test_tutorial001_pv1.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-05-05 00:03 +0000

1import importlib 1abcdef

2 

3import pytest 1abcdef

4from fastapi.testclient import TestClient 1abcdef

5 

6from ...utils import needs_py310, needs_pydanticv1 1abcdef

7 

8 

9@pytest.fixture( 1abcdef

10 name="client", 

11 params=[ 

12 "tutorial001_pv1", 

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

14 ], 

15) 

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

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

18 

19 client = TestClient(mod.app) 1abcdef

20 return client 1abcdef

21 

22 

23@needs_pydanticv1 1abcdef

24def test_post_body_example(client: TestClient): 1abcdef

25 response = client.put( 1mnopqr

26 "/items/5", 

27 json={ 

28 "name": "Foo", 

29 "description": "A very nice Item", 

30 "price": 35.4, 

31 "tax": 3.2, 

32 }, 

33 ) 

34 assert response.status_code == 200 1mnopqr

35 

36 

37@needs_pydanticv1 1abcdef

38def test_openapi_schema(client: TestClient): 1abcdef

39 response = client.get("/openapi.json") 1ghijkl

40 assert response.status_code == 200, response.text 1ghijkl

41 # insert_assert(response.json()) 

42 assert response.json() == { 1ghijkl

43 "openapi": "3.1.0", 

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

45 "paths": { 

46 "/items/{item_id}": { 

47 "put": { 

48 "summary": "Update Item", 

49 "operationId": "update_item_items__item_id__put", 

50 "parameters": [ 

51 { 

52 "required": True, 

53 "schema": {"type": "integer", "title": "Item Id"}, 

54 "name": "item_id", 

55 "in": "path", 

56 } 

57 ], 

58 "requestBody": { 

59 "content": { 

60 "application/json": { 

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

62 } 

63 }, 

64 "required": True, 

65 }, 

66 "responses": { 

67 "200": { 

68 "description": "Successful Response", 

69 "content": {"application/json": {"schema": {}}}, 

70 }, 

71 "422": { 

72 "description": "Validation Error", 

73 "content": { 

74 "application/json": { 

75 "schema": { 

76 "$ref": "#/components/schemas/HTTPValidationError" 

77 } 

78 } 

79 }, 

80 }, 

81 }, 

82 } 

83 } 

84 }, 

85 "components": { 

86 "schemas": { 

87 "HTTPValidationError": { 

88 "properties": { 

89 "detail": { 

90 "items": {"$ref": "#/components/schemas/ValidationError"}, 

91 "type": "array", 

92 "title": "Detail", 

93 } 

94 }, 

95 "type": "object", 

96 "title": "HTTPValidationError", 

97 }, 

98 "Item": { 

99 "properties": { 

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

101 "description": {"type": "string", "title": "Description"}, 

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

103 "tax": {"type": "number", "title": "Tax"}, 

104 }, 

105 "type": "object", 

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

107 "title": "Item", 

108 "examples": [ 

109 { 

110 "name": "Foo", 

111 "description": "A very nice Item", 

112 "price": 35.4, 

113 "tax": 3.2, 

114 } 

115 ], 

116 }, 

117 "ValidationError": { 

118 "properties": { 

119 "loc": { 

120 "items": { 

121 "anyOf": [{"type": "string"}, {"type": "integer"}] 

122 }, 

123 "type": "array", 

124 "title": "Location", 

125 }, 

126 "msg": {"type": "string", "title": "Message"}, 

127 "type": {"type": "string", "title": "Error Type"}, 

128 }, 

129 "type": "object", 

130 "required": ["loc", "msg", "type"], 

131 "title": "ValidationError", 

132 }, 

133 } 

134 }, 

135 }