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

25 statements  

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

1from dirty_equals import IsDict 1abcde

2from fastapi.testclient import TestClient 1abcde

3 

4from docs_src.path_params.tutorial005 import app 1abcde

5 

6client = TestClient(app) 1abcde

7 

8 

9def test_get_enums_alexnet(): 1abcde

10 response = client.get("/models/alexnet") 1abcde

11 assert response.status_code == 200 1abcde

12 assert response.json() == {"model_name": "alexnet", "message": "Deep Learning FTW!"} 1abcde

13 

14 

15def test_get_enums_lenet(): 1abcde

16 response = client.get("/models/lenet") 1abcde

17 assert response.status_code == 200 1abcde

18 assert response.json() == {"model_name": "lenet", "message": "LeCNN all the images"} 1abcde

19 

20 

21def test_get_enums_resnet(): 1abcde

22 response = client.get("/models/resnet") 1abcde

23 assert response.status_code == 200 1abcde

24 assert response.json() == {"model_name": "resnet", "message": "Have some residuals"} 1abcde

25 

26 

27def test_get_enums_invalid(): 1abcde

28 response = client.get("/models/foo") 1abcde

29 assert response.status_code == 422 1abcde

30 assert response.json() == IsDict( 1abcde

31 { 

32 "detail": [ 

33 { 

34 "type": "enum", 

35 "loc": ["path", "model_name"], 

36 "msg": "Input should be 'alexnet', 'resnet' or 'lenet'", 

37 "input": "foo", 

38 "ctx": {"expected": "'alexnet', 'resnet' or 'lenet'"}, 

39 } 

40 ] 

41 } 

42 ) | IsDict( 

43 # TODO: remove when deprecating Pydantic v1 

44 { 

45 "detail": [ 

46 { 

47 "ctx": {"enum_values": ["alexnet", "resnet", "lenet"]}, 

48 "loc": ["path", "model_name"], 

49 "msg": "value is not a valid enumeration member; permitted: 'alexnet', 'resnet', 'lenet'", 

50 "type": "type_error.enum", 

51 } 

52 ] 

53 } 

54 ) 

55 

56 

57def test_openapi_schema(): 1abcde

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

59 assert response.status_code == 200, response.text 1abcde

60 data = response.json() 1abcde

61 assert data == { 1abcde

62 "openapi": "3.1.0", 

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

64 "paths": { 

65 "/models/{model_name}": { 

66 "get": { 

67 "summary": "Get Model", 

68 "operationId": "get_model_models__model_name__get", 

69 "parameters": [ 

70 { 

71 "required": True, 

72 "schema": {"$ref": "#/components/schemas/ModelName"}, 

73 "name": "model_name", 

74 "in": "path", 

75 } 

76 ], 

77 "responses": { 

78 "200": { 

79 "description": "Successful Response", 

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

81 }, 

82 "422": { 

83 "description": "Validation Error", 

84 "content": { 

85 "application/json": { 

86 "schema": { 

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

88 } 

89 } 

90 }, 

91 }, 

92 }, 

93 } 

94 } 

95 }, 

96 "components": { 

97 "schemas": { 

98 "HTTPValidationError": { 

99 "title": "HTTPValidationError", 

100 "type": "object", 

101 "properties": { 

102 "detail": { 

103 "title": "Detail", 

104 "type": "array", 

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

106 } 

107 }, 

108 }, 

109 "ModelName": IsDict( 

110 { 

111 "title": "ModelName", 

112 "enum": ["alexnet", "resnet", "lenet"], 

113 "type": "string", 

114 } 

115 ) 

116 | IsDict( 

117 { 

118 # TODO: remove when deprecating Pydantic v1 

119 "title": "ModelName", 

120 "enum": ["alexnet", "resnet", "lenet"], 

121 "type": "string", 

122 "description": "An enumeration.", 

123 } 

124 ), 

125 "ValidationError": { 

126 "title": "ValidationError", 

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

128 "type": "object", 

129 "properties": { 

130 "loc": { 

131 "title": "Location", 

132 "type": "array", 

133 "items": { 

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

135 }, 

136 }, 

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

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

139 }, 

140 }, 

141 } 

142 }, 

143 }