Coverage for tests/test_modules_same_name_body/test_main.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-05-05 00:03 +0000

1import pytest 1abcdef

2from fastapi.testclient import TestClient 1abcdef

3 

4from .app.main import app 1abcdef

5 

6client = TestClient(app) 1abcdef

7 

8 

9@pytest.mark.parametrize( 1abcdef

10 "path", ["/a/compute", "/a/compute/", "/b/compute", "/b/compute/"] 

11) 

12def test_post(path): 1abcdef

13 data = {"a": 2, "b": "foo"} 1ghijkl

14 response = client.post(path, json=data) 1ghijkl

15 assert response.status_code == 200, response.text 1ghijkl

16 assert data == response.json() 1ghijkl

17 

18 

19@pytest.mark.parametrize( 1abcdef

20 "path", ["/a/compute", "/a/compute/", "/b/compute", "/b/compute/"] 

21) 

22def test_post_invalid(path): 1abcdef

23 data = {"a": "bar", "b": "foo"} 1mnopqr

24 response = client.post(path, json=data) 1mnopqr

25 assert response.status_code == 422, response.text 1mnopqr

26 

27 

28def test_openapi_schema(): 1abcdef

29 response = client.get("/openapi.json") 1stuvwx

30 assert response.status_code == 200, response.text 1stuvwx

31 assert response.json() == { 1stuvwx

32 "openapi": "3.1.0", 

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

34 "paths": { 

35 "/a/compute": { 

36 "post": { 

37 "responses": { 

38 "200": { 

39 "description": "Successful Response", 

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

41 }, 

42 "422": { 

43 "description": "Validation Error", 

44 "content": { 

45 "application/json": { 

46 "schema": { 

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

48 } 

49 } 

50 }, 

51 }, 

52 }, 

53 "summary": "Compute", 

54 "operationId": "compute_a_compute_post", 

55 "requestBody": { 

56 "content": { 

57 "application/json": { 

58 "schema": { 

59 "$ref": "#/components/schemas/Body_compute_a_compute_post" 

60 } 

61 } 

62 }, 

63 "required": True, 

64 }, 

65 } 

66 }, 

67 "/b/compute/": { 

68 "post": { 

69 "responses": { 

70 "200": { 

71 "description": "Successful Response", 

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

73 }, 

74 "422": { 

75 "description": "Validation Error", 

76 "content": { 

77 "application/json": { 

78 "schema": { 

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

80 } 

81 } 

82 }, 

83 }, 

84 }, 

85 "summary": "Compute", 

86 "operationId": "compute_b_compute__post", 

87 "requestBody": { 

88 "content": { 

89 "application/json": { 

90 "schema": { 

91 "$ref": "#/components/schemas/Body_compute_b_compute__post" 

92 } 

93 } 

94 }, 

95 "required": True, 

96 }, 

97 } 

98 }, 

99 }, 

100 "components": { 

101 "schemas": { 

102 "Body_compute_b_compute__post": { 

103 "title": "Body_compute_b_compute__post", 

104 "required": ["a", "b"], 

105 "type": "object", 

106 "properties": { 

107 "a": {"title": "A", "type": "integer"}, 

108 "b": {"title": "B", "type": "string"}, 

109 }, 

110 }, 

111 "Body_compute_a_compute_post": { 

112 "title": "Body_compute_a_compute_post", 

113 "required": ["a", "b"], 

114 "type": "object", 

115 "properties": { 

116 "a": {"title": "A", "type": "integer"}, 

117 "b": {"title": "B", "type": "string"}, 

118 }, 

119 }, 

120 "ValidationError": { 

121 "title": "ValidationError", 

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

123 "type": "object", 

124 "properties": { 

125 "loc": { 

126 "title": "Location", 

127 "type": "array", 

128 "items": { 

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

130 }, 

131 }, 

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

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

134 }, 

135 }, 

136 "HTTPValidationError": { 

137 "title": "HTTPValidationError", 

138 "type": "object", 

139 "properties": { 

140 "detail": { 

141 "title": "Detail", 

142 "type": "array", 

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

144 } 

145 }, 

146 }, 

147 } 

148 }, 

149 }