Coverage for tests\test_get_gui_projects_constraints.py: 100.00%

79 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-03-22 23:23 +0100

1# -*- coding: utf-8 -*- 

2 

3""" 

4* Name: interactive-clustering-gui/tests/test_get_gui_projects_constraints.py 

5* Description: Unittests for `app` module on the `GET /gui/projects/{project_id}/constraints` route. 

6* Author: Erwan Schild 

7* Created: 22/02/2022 

8* Licence: CeCILL (https://cecill.info/licences.fr.html) 

9""" 

10 

11# ============================================================================== 

12# IMPORT PYTHON DEPENDENCIES 

13# ============================================================================== 

14 

15import html 

16 

17import pytest 

18 

19from tests.dummies_utils import create_dummy_projects 

20 

21# ============================================================================== 

22# test_ko_not_found 

23# ============================================================================== 

24 

25 

26@pytest.mark.asyncio() 

27async def test_ko_not_found(async_client): 

28 """ 

29 Test the `GET /gui/projects/{project_id}/constraints` route with not existing project. 

30 

31 Arguments: 

32 async_client: Fixture providing an HTTP client, declared in `conftest.py`. 

33 """ 

34 # Assert HTTP client is created. 

35 assert async_client 

36 

37 # Assert route `GET /gui/projects/{project_id}/constraints` works. 

38 response_get = await async_client.get(url="/gui/projects/UNKNOWN_PROJECT/constraints") 

39 parsed_response_get = html.unescape(bytes.decode(response_get.content, encoding="utf-8")) 

40 assert response_get.status_code == 404 

41 assert response_get.headers["content-type"] == "text/html; charset=utf-8" 

42 assert "<!-- HEAD -->" in parsed_response_get 

43 assert "<title>Interactive Clustering</title>" in parsed_response_get 

44 assert "<!-- BODY -->" in parsed_response_get 

45 assert "<!-- CONTAINER ERROR -->" in parsed_response_get 

46 assert "Error: 404" in parsed_response_get 

47 assert "The project with id 'UNKNOWN_PROJECT' doesn't exist." in parsed_response_get 

48 

49 

50# ============================================================================== 

51# test_ok_1 

52# ============================================================================== 

53 

54 

55@pytest.mark.asyncio() 

56async def test_ok_1(async_client, tmp_path): 

57 """ 

58 Test the `GET /gui/projects/{project_id}/constraints` route. 

59 

60 Arguments: 

61 async_client: Fixture providing an HTTP client, declared in `conftest.py`. 

62 tmp_path: The temporary path given for this test, declared in `conftest.py`. 

63 """ 

64 # Assert HTTP client is created. 

65 assert async_client 

66 

67 # Create dummy projects. 

68 create_dummy_projects( 

69 tmp_path=tmp_path, 

70 list_of_dummy_project_ids=[ 

71 "0a_INITIALIZATION_WITHOUT_MODELIZATION", 

72 ], 

73 ) 

74 

75 # Get constraints before test. 

76 response_get_constraints = await async_client.get( 

77 url="/api/projects/0a_INITIALIZATION_WITHOUT_MODELIZATION/constraints?without_hidden_constraints=true" 

78 ) 

79 assert response_get_constraints.status_code == 200 

80 assert response_get_constraints.json()["constraints"] == {} # noqa: WPS520 

81 

82 # Assert route `GET /gui/projects/{project_id}/constraints` works. 

83 response_get = await async_client.get(url="/gui/projects/0a_INITIALIZATION_WITHOUT_MODELIZATION/constraints") 

84 parsed_response_get = html.unescape(bytes.decode(response_get.content, encoding="utf-8")) 

85 assert response_get.status_code == 200 

86 assert response_get.headers["content-type"] == "text/html; charset=utf-8" 

87 assert "<!-- HEAD -->" in parsed_response_get 

88 assert "<title>Interactive Clustering</title>" in parsed_response_get 

89 assert "<!-- BODY -->" in parsed_response_get 

90 assert "<!-- CONTAINER CONSTRAINTS -->" in parsed_response_get 

91 assert "<!-- CONTAINER CONSTRAINTS - TITLE -->" in parsed_response_get 

92 assert "Iteration (0): Constraints synthesis" in parsed_response_get 

93 assert "<!-- CONTAINER CONSTRAINTS - CONTENT -->" in parsed_response_get 

94 assert "<!-- CONTAINER CONSTRAINTS - INFORMATIONS - PROJECT ID -->" in parsed_response_get 

95 assert "0a_INITIALIZATION_WITHOUT_MODELIZATION" in parsed_response_get 

96 assert "<!-- CONTAINER CONSTRAINTS - INFORMATIONS - TEXTS -->" in parsed_response_get 

97 assert "<!-- CONTAINER CONSTRAINTS - INFORMATIONS - CONSTRAINTS -->" in parsed_response_get 

98 assert "<!-- CONTAINER CONSTRAINTS - INFORMATIONS - MODELIZATION -->" not in parsed_response_get 

99 assert "<!-- CONTAINER CONSTRAINTS - ACTIONS -->" in parsed_response_get 

100 assert "<!-- CONTAINER CONSTRAINTS LIST -->" in parsed_response_get 

101 assert "<!-- CONTAINER CONSTRAINTS LIST - TITLE -->" in parsed_response_get 

102 assert "List of constraints" in parsed_response_get 

103 assert "<!-- CONTAINER CONSTRAINTS LIST - CONTENT -->" in parsed_response_get 

104 assert "<!-- CONTAINER CONSTRAINTS LIST - CONSTRAINT TABLE -->" in parsed_response_get 

105 assert "<!-- CONSTRAINT " not in parsed_response_get 

106 

107 

108# ============================================================================== 

109# test_ok_2 

110# ============================================================================== 

111 

112 

113@pytest.mark.asyncio() 

114async def test_ok_2(async_client, tmp_path): 

115 """ 

116 Test the `GET /gui/projects/{project_id}/constraints` route. 

117 

118 Arguments: 

119 async_client: Fixture providing an HTTP client, declared in `conftest.py`. 

120 tmp_path: The temporary path given for this test, declared in `conftest.py`. 

121 """ 

122 # Assert HTTP client is created. 

123 assert async_client 

124 

125 # Create dummy projects. 

126 create_dummy_projects( 

127 tmp_path=tmp_path, 

128 list_of_dummy_project_ids=[ 

129 "1l_ANNOTATION_WITH_UPTODATE_MODELIZATION", 

130 ], 

131 ) 

132 

133 # Get constraints before test. 

134 response_get_constraints = await async_client.get( 

135 url="/api/projects/1l_ANNOTATION_WITH_UPTODATE_MODELIZATION/constraints?without_hidden_constraints=true" 

136 ) 

137 assert response_get_constraints.status_code == 200 

138 

139 # Assert route `GET /gui/projects/{project_id}/constraints` works. 

140 response_get = await async_client.get(url="/gui/projects/1l_ANNOTATION_WITH_UPTODATE_MODELIZATION/constraints") 

141 parsed_response_get = html.unescape(bytes.decode(response_get.content, encoding="utf-8")) 

142 assert response_get.status_code == 200 

143 assert response_get.headers["content-type"] == "text/html; charset=utf-8" 

144 assert "<!-- HEAD -->" in parsed_response_get 

145 assert "<title>Interactive Clustering</title>" in parsed_response_get 

146 assert "<!-- BODY -->" in parsed_response_get 

147 assert "<!-- CONTAINER CONSTRAINTS -->" in parsed_response_get 

148 assert "<!-- CONTAINER CONSTRAINTS - TITLE -->" in parsed_response_get 

149 assert "Iteration (1): Constraints synthesis" in parsed_response_get 

150 assert "<!-- CONTAINER CONSTRAINTS - CONTENT -->" in parsed_response_get 

151 assert "<!-- CONTAINER CONSTRAINTS - INFORMATIONS - PROJECT ID -->" in parsed_response_get 

152 assert "1l_ANNOTATION_WITH_UPTODATE_MODELIZATION" in parsed_response_get 

153 assert "<!-- CONTAINER CONSTRAINTS - INFORMATIONS - TEXTS -->" in parsed_response_get 

154 assert "<!-- CONTAINER CONSTRAINTS - INFORMATIONS - CONSTRAINTS -->" in parsed_response_get 

155 assert "<!-- CONTAINER CONSTRAINTS - INFORMATIONS - MODELIZATION -->" in parsed_response_get 

156 assert "<!-- CONTAINER CONSTRAINTS - ACTIONS -->" in parsed_response_get 

157 assert "<!-- CONTAINER CONSTRAINTS LIST -->" in parsed_response_get 

158 assert "<!-- CONTAINER CONSTRAINTS LIST - TITLE -->" in parsed_response_get 

159 assert "List of constraints" in parsed_response_get 

160 assert "<!-- CONTAINER CONSTRAINTS LIST - CONTENT -->" in parsed_response_get 

161 assert "<!-- CONTAINER CONSTRAINTS LIST - CONSTRAINT TABLE -->" in parsed_response_get 

162 assert parsed_response_get.count("<!-- CONSTRAINT ") == len(response_get_constraints.json()["constraints"].keys()) 

163 for constraint_id in response_get_constraints.json()["constraints"].keys(): 

164 assert "<!-- CONSTRAINT " + constraint_id + " -->" in parsed_response_get 

165 assert 'id="' + constraint_id + '">' in parsed_response_get