Coverage for tests / test_enforce_once_required_parameter.py: 100%

26 statements  

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

1from typing import Optional 1abcd

2 

3from fastapi import Depends, FastAPI, Query 1abcd

4from fastapi.testclient import TestClient 1abcd

5from inline_snapshot import snapshot 1abcd

6 

7app = FastAPI() 1abcd

8 

9 

10def _get_client_key(client_id: str = Query(...)) -> str: 1abcd

11 return f"{client_id}_key" 1efg

12 

13 

14def _get_client_tag(client_id: Optional[str] = Query(None)) -> Optional[str]: 1abcd

15 if client_id is None: 1heifjg

16 return None 1hij

17 return f"{client_id}_tag" 1efg

18 

19 

20@app.get("/foo") 1abcd

21def foo_handler( 1abcd

22 client_key: str = Depends(_get_client_key), 

23 client_tag: Optional[str] = Depends(_get_client_tag), 

24): 

25 return {"client_id": client_key, "client_tag": client_tag} 1efg

26 

27 

28client = TestClient(app) 1abcd

29 

30 

31def test_get_invalid(): 1abcd

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

33 assert response.status_code == 422 1hij

34 

35 

36def test_get_valid(): 1abcd

37 response = client.get("/foo", params={"client_id": "bar"}) 1efg

38 assert response.status_code == 200 1efg

39 assert response.json() == {"client_id": "bar_key", "client_tag": "bar_tag"} 1efg

40 

41 

42def test_openapi_schema(): 1abcd

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

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

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

46 { 

47 "components": { 

48 "schemas": { 

49 "HTTPValidationError": { 

50 "properties": { 

51 "detail": { 

52 "items": { 

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

54 }, 

55 "title": "Detail", 

56 "type": "array", 

57 } 

58 }, 

59 "title": "HTTPValidationError", 

60 "type": "object", 

61 }, 

62 "ValidationError": { 

63 "properties": { 

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

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

66 "loc": { 

67 "items": { 

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

69 }, 

70 "title": "Location", 

71 "type": "array", 

72 }, 

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

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

75 }, 

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

77 "title": "ValidationError", 

78 "type": "object", 

79 }, 

80 } 

81 }, 

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

83 "openapi": "3.1.0", 

84 "paths": { 

85 "/foo": { 

86 "get": { 

87 "operationId": "foo_handler_foo_get", 

88 "parameters": [ 

89 { 

90 "in": "query", 

91 "name": "client_id", 

92 "required": True, 

93 "schema": {"title": "Client Id", "type": "string"}, 

94 }, 

95 ], 

96 "responses": { 

97 "200": { 

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

99 "description": "Successful Response", 

100 }, 

101 "422": { 

102 "content": { 

103 "application/json": { 

104 "schema": { 

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

106 } 

107 } 

108 }, 

109 "description": "Validation Error", 

110 }, 

111 }, 

112 "summary": "Foo Handler", 

113 } 

114 } 

115 }, 

116 } 

117 )