Coverage for skema/rest/tests/test_eqns_to_mets.py: 100%

26 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-30 17:15 +0000

1from httpx import AsyncClient 

2from skema.rest.workflows import app 

3import pytest 

4import json 

5 

6 

7@pytest.mark.ci_only 

8@pytest.mark.asyncio 

9async def test_post_eqns_to_mets_mathml_latex(): 

10 """ 

11 Test case for /equations-to-met. 

12 """ 

13 latex_equations = [ 

14 """ 

15 <math xmlns="http://www.w3.org/1998/Math/MathML"> 

16 <mi>E</mi> 

17 <mo>=</mo> 

18 <mi>m</mi> 

19 <msup> 

20 <mi>c</mi> 

21 <mn>2</mn> 

22 </msup> 

23 </math> 

24 """, 

25 "c=\\frac{a}{b}", 

26 ] 

27 

28 endpoint = "/equations-to-met" 

29 

30 async with AsyncClient(app=app, base_url="http://latex-to-mets-test") as ac: 

31 response = await ac.post(endpoint, json={"equations": latex_equations}) 

32 expected = [ 

33 { 

34 "Cons": [ 

35 "Equals", 

36 [ 

37 { 

38 "Atom": { 

39 "Ci": { 

40 "type": None, 

41 "content": {"Mi": "E"}, 

42 "func_of": None, 

43 "notation": None, 

44 } 

45 } 

46 }, 

47 { 

48 "Cons": [ 

49 "Multiply", 

50 [ 

51 { 

52 "Atom": { 

53 "Ci": { 

54 "type": None, 

55 "content": {"Mi": "m"}, 

56 "func_of": None, 

57 "notation": None, 

58 } 

59 } 

60 }, 

61 { 

62 "Cons": [ 

63 "Power", 

64 [ 

65 { 

66 "Atom": { 

67 "Ci": { 

68 "type": None, 

69 "content": {"Mi": "c"}, 

70 "func_of": None, 

71 "notation": None, 

72 } 

73 } 

74 }, 

75 {"Atom": {"Mn": "2"}}, 

76 ], 

77 ] 

78 }, 

79 ], 

80 ] 

81 }, 

82 ], 

83 ] 

84 }, 

85 { 

86 "Cons": [ 

87 "Equals", 

88 [ 

89 { 

90 "Atom": { 

91 "Ci": { 

92 "type": None, 

93 "content": {"Mi": "c"}, 

94 "func_of": None, 

95 "notation": None, 

96 } 

97 } 

98 }, 

99 { 

100 "Cons": [ 

101 "Divide", 

102 [ 

103 { 

104 "Atom": { 

105 "Ci": { 

106 "type": None, 

107 "content": {"Mi": "a"}, 

108 "func_of": None, 

109 "notation": None, 

110 } 

111 } 

112 }, 

113 { 

114 "Atom": { 

115 "Ci": { 

116 "type": None, 

117 "content": {"Mi": "b"}, 

118 "func_of": None, 

119 "notation": None, 

120 } 

121 } 

122 }, 

123 ], 

124 ] 

125 }, 

126 ], 

127 ] 

128 }, 

129 ] 

130 

131 # check for route's existence 

132 assert ( 

133 any(route.path == endpoint for route in app.routes) == True 

134 ), "{endpoint} does not exist for app" 

135 # check status code 

136 assert ( 

137 response.status_code == 200 

138 ), f"Request was unsuccessful (status code was {response.status_code} instead of 200)" 

139 # check response 

140 assert ( 

141 json.loads(response.text) == expected 

142 ), f"Response should be {expected}, but instead received {response.text}" 

143 

144 

145@pytest.mark.ci_only 

146@pytest.mark.asyncio 

147async def test_post_eqns_to_mets_latex_mathml(): 

148 """ 

149 Test case for /equations-to-met. 

150 """ 

151 mathml_equations = [ 

152 "E=mc^2", 

153 """ 

154 <math xmlns="http://www.w3.org/1998/Math/MathML"> 

155 <mi>c</mi> 

156 <mo>=</mo> 

157 <mfrac> 

158 <mrow> 

159 <mi>a</mi> 

160 </mrow> 

161 <mrow> 

162 <mi>b</mi> 

163 </mrow> 

164 </mfrac> 

165 </math> 

166 """, 

167 ] 

168 

169 endpoint = "/equations-to-met" 

170 

171 async with AsyncClient(app=app, base_url="http://mathml-to-mets-test") as ac: 

172 response = await ac.post(endpoint, json={"equations": mathml_equations}) 

173 expected = [ 

174 { 

175 "Cons": [ 

176 "Equals", 

177 [ 

178 { 

179 "Atom": { 

180 "Ci": { 

181 "type": None, 

182 "content": {"Mi": "E"}, 

183 "func_of": None, 

184 "notation": None, 

185 } 

186 } 

187 }, 

188 { 

189 "Cons": [ 

190 "Multiply", 

191 [ 

192 { 

193 "Atom": { 

194 "Ci": { 

195 "type": None, 

196 "content": {"Mi": "m"}, 

197 "func_of": None, 

198 "notation": None, 

199 } 

200 } 

201 }, 

202 { 

203 "Cons": [ 

204 "Power", 

205 [ 

206 { 

207 "Atom": { 

208 "Ci": { 

209 "type": None, 

210 "content": {"Mi": "c"}, 

211 "func_of": None, 

212 "notation": None, 

213 } 

214 } 

215 }, 

216 {"Atom": {"Mn": "2"}}, 

217 ], 

218 ] 

219 }, 

220 ], 

221 ] 

222 }, 

223 ], 

224 ] 

225 }, 

226 { 

227 "Cons": [ 

228 "Equals", 

229 [ 

230 { 

231 "Atom": { 

232 "Ci": { 

233 "type": None, 

234 "content": {"Mi": "c"}, 

235 "func_of": None, 

236 "notation": None, 

237 } 

238 } 

239 }, 

240 { 

241 "Cons": [ 

242 "Divide", 

243 [ 

244 { 

245 "Atom": { 

246 "Ci": { 

247 "type": None, 

248 "content": {"Mi": "a"}, 

249 "func_of": None, 

250 "notation": None, 

251 } 

252 } 

253 }, 

254 { 

255 "Atom": { 

256 "Ci": { 

257 "type": None, 

258 "content": {"Mi": "b"}, 

259 "func_of": None, 

260 "notation": None, 

261 } 

262 } 

263 }, 

264 ], 

265 ] 

266 }, 

267 ], 

268 ] 

269 }, 

270 ] 

271 

272 # check for route's existence 

273 assert ( 

274 any(route.path == endpoint for route in app.routes) == True 

275 ), "{endpoint} does not exist for app" 

276 # check status code 

277 assert ( 

278 response.status_code == 200 

279 ), f"Request was unsuccessful (status code was {response.status_code} instead of 200)" 

280 # check response 

281 assert ( 

282 json.loads(response.text) == expected 

283 ), f"Response should be {expected}, but instead received {response.text}"