Coverage for tests/test_tutorial/test_schema_extra_example/test_tutorial001.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_pydanticv2 1abcdef

7 

8 

9@pytest.fixture( 1abcdef

10 name="client", 

11 params=[ 

12 "tutorial001", 

13 pytest.param("tutorial001_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_pydanticv2 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_pydanticv2 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 "name": "item_id", 

53 "in": "path", 

54 "required": True, 

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

56 } 

57 ], 

58 "requestBody": { 

59 "required": True, 

60 "content": { 

61 "application/json": { 

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

63 } 

64 }, 

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": { 

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

103 "title": "Description", 

104 }, 

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

106 "tax": { 

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

108 "title": "Tax", 

109 }, 

110 }, 

111 "type": "object", 

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

113 "title": "Item", 

114 "examples": [ 

115 { 

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

117 "name": "Foo", 

118 "price": 35.4, 

119 "tax": 3.2, 

120 } 

121 ], 

122 }, 

123 "ValidationError": { 

124 "properties": { 

125 "loc": { 

126 "items": { 

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

128 }, 

129 "type": "array", 

130 "title": "Location", 

131 }, 

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

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

134 }, 

135 "type": "object", 

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

137 "title": "ValidationError", 

138 }, 

139 } 

140 }, 

141 }