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

24 statements  

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

1from fastapi.testclient import TestClient 1abcd

2from inline_snapshot import snapshot 1abcd

3 

4from docs_src.path_params.tutorial005_py310 import app 1abcd

5 

6client = TestClient(app) 1abcd

7 

8 

9def test_get_enums_alexnet(): 1abcd

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

11 assert response.status_code == 200 1efg

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

13 

14 

15def test_get_enums_lenet(): 1abcd

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

17 assert response.status_code == 200 1hij

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

19 

20 

21def test_get_enums_resnet(): 1abcd

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

23 assert response.status_code == 200 1klm

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

25 

26 

27def test_get_enums_invalid(): 1abcd

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

29 assert response.status_code == 422 1nop

30 assert response.json() == { 1nop

31 "detail": [ 

32 { 

33 "type": "enum", 

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

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

36 "input": "foo", 

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

38 } 

39 ] 

40 } 

41 

42 

43def test_openapi_schema(): 1abcd

44 response = client.get("/openapi.json") 1qrs

45 assert response.status_code == 200, response.text 1qrs

46 assert response.json() == snapshot( 1qrs

47 { 

48 "openapi": "3.1.0", 

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

50 "paths": { 

51 "/models/{model_name}": { 

52 "get": { 

53 "summary": "Get Model", 

54 "operationId": "get_model_models__model_name__get", 

55 "parameters": [ 

56 { 

57 "required": True, 

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

59 "name": "model_name", 

60 "in": "path", 

61 } 

62 ], 

63 "responses": { 

64 "200": { 

65 "description": "Successful Response", 

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

67 }, 

68 "422": { 

69 "description": "Validation Error", 

70 "content": { 

71 "application/json": { 

72 "schema": { 

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

74 } 

75 } 

76 }, 

77 }, 

78 }, 

79 } 

80 } 

81 }, 

82 "components": { 

83 "schemas": { 

84 "HTTPValidationError": { 

85 "title": "HTTPValidationError", 

86 "type": "object", 

87 "properties": { 

88 "detail": { 

89 "title": "Detail", 

90 "type": "array", 

91 "items": { 

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

93 }, 

94 } 

95 }, 

96 }, 

97 "ModelName": { 

98 "title": "ModelName", 

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

100 "type": "string", 

101 }, 

102 "ValidationError": { 

103 "title": "ValidationError", 

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

105 "type": "object", 

106 "properties": { 

107 "loc": { 

108 "title": "Location", 

109 "type": "array", 

110 "items": { 

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

112 }, 

113 }, 

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

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

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

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

118 }, 

119 }, 

120 } 

121 }, 

122 } 

123 )