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

27 statements  

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

1import json 

2 

3from fastapi.testclient import TestClient 

4from skema.isa.isa_service import app 

5from skema.rest.workflows import app as workflow_app 

6import skema.isa.data as isa_data 

7import pytest 

8 

9client = TestClient(app) 

10workflow_client = TestClient(workflow_app) 

11 

12@pytest.mark.ci_only 

13def test_align_eqns(): 

14 """Test case for /align-eqns endpoint.""" 

15 

16 halfar_dome_eqn = isa_data.mml 

17 mention_json1_content = "" 

18 mention_json2_content = "" 

19 data = { 

20 "mml1": halfar_dome_eqn, 

21 "mml2": halfar_dome_eqn, 

22 "mention_json1": mention_json1_content, 

23 "mention_json2": mention_json2_content, 

24 } 

25 

26 endpoint = "/isa/align-eqns" 

27 response = client.post(endpoint, params=data) 

28 expected = isa_data.expected 

29 

30 # check status code 

31 assert ( 

32 response.status_code == 200 

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

34 # check response of matching_ratio 

35 assert ( 

36 json.loads(response.text)["matching_ratio"] == 1.0 

37 ), f"Response should be 1.0, but instead received {response.text}" 

38 # check response of union_graph 

39 assert ( 

40 str(json.loads(response.text)["union_graph"]) == expected 

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

42 

43 

44@pytest.mark.ci_only 

45def test_align_code_eqn(): 

46 """Test case for /isa/code-eqn-align endpoint.""" 

47 

48 single_snippet_payload = { 

49 "system": { 

50 "files": ["code.py"], 

51 "blobs": [ 

52 'def sir(\n s: float, i: float, r: float, beta: float, gamma: float, n: float\n) -> Tuple[float, float, float]:\n """The SIR model, one time step."""\n s_n = (-beta * s * i) + s\n i_n = (beta * s * i - gamma * i) + i\n r_n = gamma * i + r\n scale = n / (s_n + i_n + r_n)\n return s_n * scale, i_n * scale, r_n * scale' 

53 ], 

54 }, 

55 "mml": """<math> 

56 <mfrac> 

57 <mrow> 

58 <mi>d</mi> 

59 <mi>I</mi> 

60 </mrow> 

61 <mrow> 

62 <mi>d</mi> 

63 <mi>t</mi> 

64 </mrow> 

65 </mfrac> 

66 <mo>=</mo> 

67 <mfrac> 

68 <mrow> 

69 <mi>&#x03B2;</mi> 

70 <mi>I</mi> 

71 <mi>S</mi> 

72 </mrow> 

73 <mi>N</mi> 

74 </mfrac> 

75 <mo>&#x2212;</mo> 

76 <mi>&#x03B3;</mi> 

77 <mi>I</mi> 

78 </math>""", 

79 } 

80 

81 endpoint = "/isa/code-eqn-align" 

82 response = workflow_client.post(endpoint, json=single_snippet_payload) 

83 

84 # check status code 

85 assert ( 

86 response.status_code == 200 

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

88 # check response of matching_ratio 

89 assert ( 

90 json.loads(response.text)["1"][0] == 0.8 

91 ), f"Matching ratio should be 0.8, but instead received {json.loads(response.text)}"