Coverage for tests / test_tutorial / test_path_params / test_tutorial003.py: 100%

14 statements  

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

1import pytest 1abcd

2from fastapi.testclient import TestClient 1abcd

3from inline_snapshot import snapshot 1abcd

4 

5from docs_src.path_params.tutorial003_py310 import app 1abcd

6 

7client = TestClient(app) 1abcd

8 

9 

10@pytest.mark.parametrize( 1abcd

11 ("user_id", "expected_response"), 

12 [ 

13 ("me", {"user_id": "the current user"}), 

14 ("alice", {"user_id": "alice"}), 

15 ], 

16) 

17def test_get_users(user_id: str, expected_response: dict): 1abcd

18 response = client.get(f"/users/{user_id}") 1efg

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

20 assert response.json() == expected_response 1efg

21 

22 

23def test_openapi_schema(): 1abcd

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

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

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

27 { 

28 "openapi": "3.1.0", 

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

30 "paths": { 

31 "/users/me": { 

32 "get": { 

33 "operationId": "read_user_me_users_me_get", 

34 "responses": { 

35 "200": { 

36 "content": { 

37 "application/json": { 

38 "schema": {}, 

39 }, 

40 }, 

41 "description": "Successful Response", 

42 }, 

43 }, 

44 "summary": "Read User Me", 

45 }, 

46 }, 

47 "/users/{user_id}": { 

48 "get": { 

49 "operationId": "read_user_users__user_id__get", 

50 "parameters": [ 

51 { 

52 "in": "path", 

53 "name": "user_id", 

54 "required": True, 

55 "schema": { 

56 "title": "User Id", 

57 "type": "string", 

58 }, 

59 }, 

60 ], 

61 "responses": { 

62 "200": { 

63 "content": { 

64 "application/json": { 

65 "schema": {}, 

66 }, 

67 }, 

68 "description": "Successful Response", 

69 }, 

70 "422": { 

71 "content": { 

72 "application/json": { 

73 "schema": { 

74 "$ref": "#/components/schemas/HTTPValidationError", 

75 }, 

76 }, 

77 }, 

78 "description": "Validation Error", 

79 }, 

80 }, 

81 "summary": "Read User", 

82 }, 

83 }, 

84 }, 

85 "components": { 

86 "schemas": { 

87 "HTTPValidationError": { 

88 "properties": { 

89 "detail": { 

90 "items": { 

91 "$ref": "#/components/schemas/ValidationError", 

92 }, 

93 "title": "Detail", 

94 "type": "array", 

95 }, 

96 }, 

97 "title": "HTTPValidationError", 

98 "type": "object", 

99 }, 

100 "ValidationError": { 

101 "properties": { 

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

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

104 "loc": { 

105 "items": { 

106 "anyOf": [ 

107 { 

108 "type": "string", 

109 }, 

110 { 

111 "type": "integer", 

112 }, 

113 ], 

114 }, 

115 "title": "Location", 

116 "type": "array", 

117 }, 

118 "msg": { 

119 "title": "Message", 

120 "type": "string", 

121 }, 

122 "type": { 

123 "title": "Error Type", 

124 "type": "string", 

125 }, 

126 }, 

127 "required": [ 

128 "loc", 

129 "msg", 

130 "type", 

131 ], 

132 "title": "ValidationError", 

133 "type": "object", 

134 }, 

135 }, 

136 }, 

137 } 

138 )