Coverage for tests / test_tutorial / test_generate_clients / test_tutorial001.py: 100%

21 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 

7 

8@pytest.fixture( 1abdc

9 name="client", 

10 params=[ 

11 pytest.param("tutorial001_py310"), 

12 ], 

13) 

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

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

16 client = TestClient(mod.app) 1abc

17 return client 1abc

18 

19 

20def test_post_items(client: TestClient): 1abdc

21 response = client.post("/items/", json={"name": "Foo", "price": 5}) 1efg

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

23 assert response.json() == {"message": "item received"} 1efg

24 

25 

26def test_get_items(client: TestClient): 1abdc

27 response = client.get("/items/") 1hij

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

29 assert response.json() == [ 1hij

30 {"name": "Plumbus", "price": 3}, 

31 {"name": "Portal Gun", "price": 9001}, 

32 ] 

33 

34 

35def test_openapi_schema(client: TestClient): 1abdc

36 response = client.get("/openapi.json") 1klm

37 assert response.status_code == 200, response.text 1klm

38 assert response.json() == snapshot( 1klm

39 { 

40 "openapi": "3.1.0", 

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

42 "paths": { 

43 "/items/": { 

44 "get": { 

45 "summary": "Get Items", 

46 "operationId": "get_items_items__get", 

47 "responses": { 

48 "200": { 

49 "description": "Successful Response", 

50 "content": { 

51 "application/json": { 

52 "schema": { 

53 "title": "Response Get Items Items Get", 

54 "type": "array", 

55 "items": { 

56 "$ref": "#/components/schemas/Item" 

57 }, 

58 } 

59 } 

60 }, 

61 } 

62 }, 

63 }, 

64 "post": { 

65 "summary": "Create Item", 

66 "operationId": "create_item_items__post", 

67 "requestBody": { 

68 "content": { 

69 "application/json": { 

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

71 } 

72 }, 

73 "required": True, 

74 }, 

75 "responses": { 

76 "200": { 

77 "description": "Successful Response", 

78 "content": { 

79 "application/json": { 

80 "schema": { 

81 "$ref": "#/components/schemas/ResponseMessage" 

82 } 

83 } 

84 }, 

85 }, 

86 "422": { 

87 "description": "Validation Error", 

88 "content": { 

89 "application/json": { 

90 "schema": { 

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

92 } 

93 } 

94 }, 

95 }, 

96 }, 

97 }, 

98 }, 

99 }, 

100 "components": { 

101 "schemas": { 

102 "HTTPValidationError": { 

103 "title": "HTTPValidationError", 

104 "type": "object", 

105 "properties": { 

106 "detail": { 

107 "title": "Detail", 

108 "type": "array", 

109 "items": { 

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

111 }, 

112 } 

113 }, 

114 }, 

115 "Item": { 

116 "title": "Item", 

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

118 "type": "object", 

119 "properties": { 

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

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

122 }, 

123 }, 

124 "ResponseMessage": { 

125 "title": "ResponseMessage", 

126 "required": ["message"], 

127 "type": "object", 

128 "properties": { 

129 "message": {"title": "Message", "type": "string"} 

130 }, 

131 }, 

132 "ValidationError": { 

133 "title": "ValidationError", 

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

135 "type": "object", 

136 "properties": { 

137 "loc": { 

138 "title": "Location", 

139 "type": "array", 

140 "items": { 

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

142 }, 

143 }, 

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

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

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

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

148 }, 

149 }, 

150 } 

151 }, 

152 } 

153 )