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

19 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1import pytest 1deabc

2from fastapi.testclient import TestClient 1deabc

3 

4from ...utils import needs_py310, needs_pydanticv2 1deabc

5 

6 

7@pytest.fixture(name="client") 1deabc

8def get_client(): 1deabc

9 from docs_src.schema_extra_example.tutorial001_py310 import app 1abc

10 

11 client = TestClient(app) 1abc

12 return client 1abc

13 

14 

15@needs_py310 1deabc

16@needs_pydanticv2 1deabc

17def test_post_body_example(client: TestClient): 1deabc

18 response = client.put( 1abc

19 "/items/5", 

20 json={ 

21 "name": "Foo", 

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

23 "price": 35.4, 

24 "tax": 3.2, 

25 }, 

26 ) 

27 assert response.status_code == 200 1abc

28 

29 

30@needs_py310 1deabc

31@needs_pydanticv2 1deabc

32def test_openapi_schema(client: TestClient): 1deabc

33 response = client.get("/openapi.json") 1abc

34 assert response.status_code == 200, response.text 1abc

35 # insert_assert(response.json()) 

36 assert response.json() == { 1abc

37 "openapi": "3.1.0", 

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

39 "paths": { 

40 "/items/{item_id}": { 

41 "put": { 

42 "summary": "Update Item", 

43 "operationId": "update_item_items__item_id__put", 

44 "parameters": [ 

45 { 

46 "name": "item_id", 

47 "in": "path", 

48 "required": True, 

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

50 } 

51 ], 

52 "requestBody": { 

53 "required": True, 

54 "content": { 

55 "application/json": { 

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

57 } 

58 }, 

59 }, 

60 "responses": { 

61 "200": { 

62 "description": "Successful Response", 

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

64 }, 

65 "422": { 

66 "description": "Validation Error", 

67 "content": { 

68 "application/json": { 

69 "schema": { 

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

71 } 

72 } 

73 }, 

74 }, 

75 }, 

76 } 

77 } 

78 }, 

79 "components": { 

80 "schemas": { 

81 "HTTPValidationError": { 

82 "properties": { 

83 "detail": { 

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

85 "type": "array", 

86 "title": "Detail", 

87 } 

88 }, 

89 "type": "object", 

90 "title": "HTTPValidationError", 

91 }, 

92 "Item": { 

93 "properties": { 

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

95 "description": { 

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

97 "title": "Description", 

98 }, 

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

100 "tax": { 

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

102 "title": "Tax", 

103 }, 

104 }, 

105 "type": "object", 

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

107 "title": "Item", 

108 "examples": [ 

109 { 

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

111 "name": "Foo", 

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 }