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

34 statements  

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

1import base64 

2from pathlib import Path 

3from httpx import AsyncClient 

4from skema.rest.workflows import app 

5import pytest 

6import json 

7 

8 

9@pytest.mark.ci_only 

10@pytest.mark.asyncio 

11async def test_post_image_to_latex(): 

12 """Test case for /images/equations-to-latex endpoint.""" 

13 

14 cwd = Path(__file__).parents[0] 

15 image_path = cwd / "data" / "img2latex" / "halfar.png" 

16 files = { 

17 "data": open(image_path, "rb"), 

18 } 

19 

20 endpoint = "/images/equations-to-latex" 

21 # see https://fastapi.tiangolo.com/advanced/async-tests/#async-tests 

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

23 response = await ac.post(endpoint, files=files) 

24 expected = "\\frac{\\partial H}{\\partial t}=\\nabla \\cdot {(\\Gamma*H^{n+2}*\\left|\\nabla{H}\\right|^{n-1}*\\nabla{H})}" 

25 # check for route's existence 

26 assert ( 

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

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

29 # check status code 

30 assert ( 

31 response.status_code == 200 

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

33 # check response 

34 assert ( 

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

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

37 

38 

39@pytest.mark.ci_only 

40@pytest.mark.asyncio 

41async def test_post_image_to_latex_base64(): 

42 """Test case for /images/base64/equations-to-latex endpoint.""" 

43 cwd = Path(__file__).parents[0] 

44 image_path = cwd / "data" / "img2latex" / "halfar.png" 

45 with Path(image_path).open("rb") as infile: 

46 img_bytes = infile.read() 

47 img_b64 = base64.b64encode(img_bytes).decode("utf-8") 

48 

49 endpoint = "/images/base64/equations-to-latex" 

50 # see https://fastapi.tiangolo.com/advanced/async-tests/#async-tests 

51 async with AsyncClient(app=app, base_url="http://eqn-to-latex-base64-test") as ac: 

52 response = await ac.post(endpoint, data=img_b64) 

53 expected = "\\frac{\\partial H}{\\partial t}=\\nabla \\cdot {(\\Gamma*H^{n+2}*\\left|\\nabla{H}\\right|^{n-1}*\\nabla{H})}" 

54 # check for route's existence 

55 assert ( 

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

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

58 # check status code 

59 assert ( 

60 response.status_code == 200 

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

62 # check response 

63 assert ( 

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

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