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

16 statements  

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

1from fastapi.testclient import TestClient 1abcd

2from inline_snapshot import snapshot 1abcd

3 

4from docs_src.query_params.tutorial005_py310 import app 1abcd

5 

6client = TestClient(app) 1abcd

7 

8 

9def test_foo_needy_very(): 1abcd

10 response = client.get("/items/foo?needy=very") 1efg

11 assert response.status_code == 200 1efg

12 assert response.json() == {"item_id": "foo", "needy": "very"} 1efg

13 

14 

15def test_foo_no_needy(): 1abcd

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

17 assert response.status_code == 422 1hij

18 assert response.json() == { 1hij

19 "detail": [ 

20 { 

21 "type": "missing", 

22 "loc": ["query", "needy"], 

23 "msg": "Field required", 

24 "input": None, 

25 } 

26 ] 

27 } 

28 

29 

30def test_openapi_schema(): 1abcd

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

32 assert response.status_code == 200 1klm

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

34 { 

35 "openapi": "3.1.0", 

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

37 "paths": { 

38 "/items/{item_id}": { 

39 "get": { 

40 "responses": { 

41 "200": { 

42 "description": "Successful Response", 

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

44 }, 

45 "422": { 

46 "description": "Validation Error", 

47 "content": { 

48 "application/json": { 

49 "schema": { 

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

51 } 

52 } 

53 }, 

54 }, 

55 }, 

56 "summary": "Read User Item", 

57 "operationId": "read_user_item_items__item_id__get", 

58 "parameters": [ 

59 { 

60 "required": True, 

61 "schema": {"title": "Item Id", "type": "string"}, 

62 "name": "item_id", 

63 "in": "path", 

64 }, 

65 { 

66 "required": True, 

67 "schema": {"title": "Needy", "type": "string"}, 

68 "name": "needy", 

69 "in": "query", 

70 }, 

71 ], 

72 } 

73 } 

74 }, 

75 "components": { 

76 "schemas": { 

77 "ValidationError": { 

78 "title": "ValidationError", 

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

80 "type": "object", 

81 "properties": { 

82 "loc": { 

83 "title": "Location", 

84 "type": "array", 

85 "items": { 

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

87 }, 

88 }, 

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

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

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

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

93 }, 

94 }, 

95 "HTTPValidationError": { 

96 "title": "HTTPValidationError", 

97 "type": "object", 

98 "properties": { 

99 "detail": { 

100 "title": "Detail", 

101 "type": "array", 

102 "items": { 

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

104 }, 

105 } 

106 }, 

107 }, 

108 } 

109 }, 

110 } 

111 )