Coverage for tests / test_get_model_definitions_formfeed_escape.py: 100%

28 statements  

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

1import pytest 1abdc

2from fastapi import FastAPI 1abdc

3from fastapi.testclient import TestClient 1abdc

4from inline_snapshot import snapshot 1abdc

5 

6 

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

8def client_fixture() -> TestClient: 1abdc

9 from pydantic import BaseModel 1abc

10 

11 class Address(BaseModel): 1abc

12 """ 

13 This is a public description of an Address 

14 \f 

15 You can't see this part of the docstring, it's private! 

16 """ 

17 

18 line_1: str 1abc

19 city: str 1abc

20 state_province: str 1abc

21 

22 class Facility(BaseModel): 1abc

23 id: str 1abc

24 address: Address 1abc

25 

26 app = FastAPI() 1abc

27 

28 @app.get("/facilities/{facility_id}") 1abc

29 def get_facility(facility_id: str) -> Facility: 1abc

30 return Facility( 1efg

31 id=facility_id, 

32 address=Address(line_1="123 Main St", city="Anytown", state_province="CA"), 

33 ) 

34 

35 client = TestClient(app) 1abc

36 return client 1abc

37 

38 

39def test_get(client: TestClient): 1abdc

40 response = client.get("/facilities/42") 1efg

41 assert response.status_code == 200, response.text 1efg

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

43 "id": "42", 

44 "address": { 

45 "line_1": "123 Main St", 

46 "city": "Anytown", 

47 "state_province": "CA", 

48 }, 

49 } 

50 

51 

52def test_openapi_schema(client: TestClient): 1abdc

53 """ 

54 Sanity check to ensure our app's openapi schema renders as we expect 

55 """ 

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

57 assert response.status_code == 200, response.text 1hij

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

59 { 

60 "components": { 

61 "schemas": { 

62 "Address": { 

63 # NOTE: the description of this model shows only the public-facing text, before the `\f` in docstring 

64 "description": "This is a public description of an Address\n", 

65 "properties": { 

66 "city": {"title": "City", "type": "string"}, 

67 "line_1": {"title": "Line 1", "type": "string"}, 

68 "state_province": { 

69 "title": "State Province", 

70 "type": "string", 

71 }, 

72 }, 

73 "required": ["line_1", "city", "state_province"], 

74 "title": "Address", 

75 "type": "object", 

76 }, 

77 "Facility": { 

78 "properties": { 

79 "address": {"$ref": "#/components/schemas/Address"}, 

80 "id": {"title": "Id", "type": "string"}, 

81 }, 

82 "required": ["id", "address"], 

83 "title": "Facility", 

84 "type": "object", 

85 }, 

86 "HTTPValidationError": { 

87 "properties": { 

88 "detail": { 

89 "items": { 

90 "$ref": "#/components/schemas/ValidationError" 

91 }, 

92 "title": "Detail", 

93 "type": "array", 

94 } 

95 }, 

96 "title": "HTTPValidationError", 

97 "type": "object", 

98 }, 

99 "ValidationError": { 

100 "properties": { 

101 "ctx": {"title": "Context", "type": "object"}, 

102 "input": {"title": "Input"}, 

103 "loc": { 

104 "items": { 

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

106 }, 

107 "title": "Location", 

108 "type": "array", 

109 }, 

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

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

112 }, 

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

114 "title": "ValidationError", 

115 "type": "object", 

116 }, 

117 } 

118 }, 

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

120 "openapi": "3.1.0", 

121 "paths": { 

122 "/facilities/{facility_id}": { 

123 "get": { 

124 "operationId": "get_facility_facilities__facility_id__get", 

125 "parameters": [ 

126 { 

127 "in": "path", 

128 "name": "facility_id", 

129 "required": True, 

130 "schema": {"title": "Facility Id", "type": "string"}, 

131 } 

132 ], 

133 "responses": { 

134 "200": { 

135 "content": { 

136 "application/json": { 

137 "schema": { 

138 "$ref": "#/components/schemas/Facility" 

139 } 

140 } 

141 }, 

142 "description": "Successful Response", 

143 }, 

144 "422": { 

145 "content": { 

146 "application/json": { 

147 "schema": { 

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

149 } 

150 } 

151 }, 

152 "description": "Validation Error", 

153 }, 

154 }, 

155 "summary": "Get Facility", 

156 } 

157 } 

158 }, 

159 } 

160 )