Coverage for tests / test_param_in_path_and_dependency.py: 100%

16 statements  

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

1from fastapi import Depends, FastAPI 1abcd

2from fastapi.testclient import TestClient 1abcd

3from inline_snapshot import snapshot 1abcd

4 

5app = FastAPI() 1abcd

6 

7 

8async def user_exists(user_id: int): 1abcd

9 return True 1efg

10 

11 

12@app.get("/users/{user_id}", dependencies=[Depends(user_exists)]) 1abcd

13async def read_users(user_id: int): 1abcd

14 pass 1efg

15 

16 

17client = TestClient(app) 1abcd

18 

19 

20def test_read_users(): 1abcd

21 response = client.get("/users/42") 1efg

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

23 

24 

25def test_openapi_schema(): 1abcd

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

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

28 { 

29 "openapi": "3.1.0", 

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

31 "paths": { 

32 "/users/{user_id}": { 

33 "get": { 

34 "summary": "Read Users", 

35 "operationId": "read_users_users__user_id__get", 

36 "parameters": [ 

37 { 

38 "required": True, 

39 "schema": {"title": "User Id", "type": "integer"}, 

40 "name": "user_id", 

41 "in": "path", 

42 }, 

43 ], 

44 "responses": { 

45 "200": { 

46 "description": "Successful Response", 

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

48 }, 

49 "422": { 

50 "description": "Validation Error", 

51 "content": { 

52 "application/json": { 

53 "schema": { 

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

55 } 

56 } 

57 }, 

58 }, 

59 }, 

60 } 

61 } 

62 }, 

63 "components": { 

64 "schemas": { 

65 "HTTPValidationError": { 

66 "title": "HTTPValidationError", 

67 "type": "object", 

68 "properties": { 

69 "detail": { 

70 "title": "Detail", 

71 "type": "array", 

72 "items": { 

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

74 }, 

75 } 

76 }, 

77 }, 

78 "ValidationError": { 

79 "title": "ValidationError", 

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

81 "type": "object", 

82 "properties": { 

83 "loc": { 

84 "title": "Location", 

85 "type": "array", 

86 "items": { 

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

88 }, 

89 }, 

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

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

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

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

94 }, 

95 }, 

96 } 

97 }, 

98 } 

99 )