Coverage for tests / test_tutorial / test_path_operation_configurations / test_tutorial005.py: 100%

18 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 

7from ...utils import needs_py310 1abdc

8 

9 

10@pytest.fixture( 1abdc

11 name="client", 

12 params=[ 

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

14 ], 

15) 

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

17 mod = importlib.import_module( 1abc

18 f"docs_src.path_operation_configuration.{request.param}" 

19 ) 

20 

21 client = TestClient(mod.app) 1abc

22 return client 1abc

23 

24 

25def test_query_params_str_validations(client: TestClient): 1abdc

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

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

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

29 "name": "Foo", 

30 "price": 42, 

31 "description": None, 

32 "tax": None, 

33 "tags": [], 

34 } 

35 

36 

37def test_openapi_schema(client: TestClient): 1abdc

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

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

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

41 { 

42 "openapi": "3.1.0", 

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

44 "paths": { 

45 "/items/": { 

46 "post": { 

47 "responses": { 

48 "200": { 

49 "description": "The created item", 

50 "content": { 

51 "application/json": { 

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

53 } 

54 }, 

55 }, 

56 "422": { 

57 "description": "Validation Error", 

58 "content": { 

59 "application/json": { 

60 "schema": { 

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

62 } 

63 } 

64 }, 

65 }, 

66 }, 

67 "summary": "Create an item", 

68 "description": "Create an item with all the information:\n\n- **name**: each item must have a name\n- **description**: a long description\n- **price**: required\n- **tax**: if the item doesn't have tax, you can omit this\n- **tags**: a set of unique tag strings for this item", 

69 "operationId": "create_item_items__post", 

70 "requestBody": { 

71 "content": { 

72 "application/json": { 

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

74 } 

75 }, 

76 "required": True, 

77 }, 

78 } 

79 } 

80 }, 

81 "components": { 

82 "schemas": { 

83 "Item": { 

84 "title": "Item", 

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

86 "type": "object", 

87 "properties": { 

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

89 "description": { 

90 "title": "Description", 

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

92 }, 

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

94 "tax": { 

95 "title": "Tax", 

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

97 }, 

98 "tags": { 

99 "title": "Tags", 

100 "uniqueItems": True, 

101 "type": "array", 

102 "items": {"type": "string"}, 

103 "default": [], 

104 }, 

105 }, 

106 }, 

107 "ValidationError": { 

108 "title": "ValidationError", 

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

110 "type": "object", 

111 "properties": { 

112 "loc": { 

113 "title": "Location", 

114 "type": "array", 

115 "items": { 

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

117 }, 

118 }, 

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

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

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

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

123 }, 

124 }, 

125 "HTTPValidationError": { 

126 "title": "HTTPValidationError", 

127 "type": "object", 

128 "properties": { 

129 "detail": { 

130 "title": "Detail", 

131 "type": "array", 

132 "items": { 

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

134 }, 

135 } 

136 }, 

137 }, 

138 } 

139 }, 

140 } 

141 )