Coverage for tests/test_repeated_dependency_schema.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from fastapi import Depends, FastAPI, Header, status 1abcde

2from fastapi.testclient import TestClient 1abcde

3 

4app = FastAPI() 1abcde

5 

6 

7def get_header(*, someheader: str = Header()): 1abcde

8 return someheader 1abcde

9 

10 

11def get_something_else(*, someheader: str = Depends(get_header)): 1abcde

12 return f"{someheader}123" 1abcde

13 

14 

15@app.get("/") 1abcde

16def get_deps(dep1: str = Depends(get_header), dep2: str = Depends(get_something_else)): 1abcde

17 return {"dep1": dep1, "dep2": dep2} 1abcde

18 

19 

20client = TestClient(app) 1abcde

21 

22schema = { 1abcde

23 "components": { 

24 "schemas": { 

25 "HTTPValidationError": { 

26 "properties": { 

27 "detail": { 

28 "items": {"$ref": "#/components/schemas/ValidationError"}, 

29 "title": "Detail", 

30 "type": "array", 

31 } 

32 }, 

33 "title": "HTTPValidationError", 

34 "type": "object", 

35 }, 

36 "ValidationError": { 

37 "properties": { 

38 "loc": { 

39 "items": {"anyOf": [{"type": "string"}, {"type": "integer"}]}, 

40 "title": "Location", 

41 "type": "array", 

42 }, 

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

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

45 }, 

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

47 "title": "ValidationError", 

48 "type": "object", 

49 }, 

50 } 

51 }, 

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

53 "openapi": "3.1.0", 

54 "paths": { 

55 "/": { 

56 "get": { 

57 "operationId": "get_deps__get", 

58 "parameters": [ 

59 { 

60 "in": "header", 

61 "name": "someheader", 

62 "required": True, 

63 "schema": {"title": "Someheader", "type": "string"}, 

64 } 

65 ], 

66 "responses": { 

67 "200": { 

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

69 "description": "Successful " "Response", 

70 }, 

71 "422": { 

72 "content": { 

73 "application/json": { 

74 "schema": { 

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

76 } 

77 } 

78 }, 

79 "description": "Validation " "Error", 

80 }, 

81 }, 

82 "summary": "Get Deps", 

83 } 

84 } 

85 }, 

86} 

87 

88 

89def test_schema(): 1abcde

90 response = client.get("/openapi.json") 1abcde

91 assert response.status_code == status.HTTP_200_OK 1abcde

92 actual_schema = response.json() 1abcde

93 assert actual_schema == schema 1abcde

94 assert ( 1abcde

95 len(actual_schema["paths"]["/"]["get"]["parameters"]) == 1 

96 ) # primary goal of this test 

97 

98 

99def test_response(): 1abcde

100 response = client.get("/", headers={"someheader": "hello"}) 1abcde

101 assert response.status_code == status.HTTP_200_OK 1abcde

102 assert response.json() == {"dep1": "hello", "dep2": "hello123"} 1abcde