Coverage for tests\test_get_gui_projects_texts.py: 100.00%

81 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_texts.py 

5* Description: Unittests for `app` module on the `GET /gui/projects/{project_id}/texts` 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}/texts` route. 

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}/texts` works. 

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

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}/texts` 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 texts before test. 

76 response_get_texts = await async_client.get( 

77 url="/api/projects/0a_INITIALIZATION_WITHOUT_MODELIZATION/texts?without_deleted_texts=false" 

78 ) 

79 assert response_get_texts.status_code == 200 

80 

81 # Assert route `GET /gui/projects/{project_id}/texts` works. 

82 response_get = await async_client.get(url="/gui/projects/0a_INITIALIZATION_WITHOUT_MODELIZATION/texts") 

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

84 assert response_get.status_code == 200 

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

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

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

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

89 assert "<!-- CONTAINER TEXTS -->" in parsed_response_get 

90 assert "<!-- CONTAINER TEXTS - TITLE -->" in parsed_response_get 

91 assert "Iteration (0): Texts synthesis" in parsed_response_get 

92 assert "<!-- CONTAINER TEXTS - CONTENT -->" in parsed_response_get 

93 assert "<!-- CONTAINER TEXTS - INFORMATIONS - PROJECT ID -->" in parsed_response_get 

94 assert "0a_INITIALIZATION_WITHOUT_MODELIZATION" in parsed_response_get 

95 assert "<!-- CONTAINER TEXTS - INFORMATIONS - TEXTS -->" in parsed_response_get 

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

97 assert "<!-- CONTAINER TEXTS - INFORMATIONS - MODELIZATION -->" not in parsed_response_get 

98 assert "<!-- CONTAINER TEXTS - ACTIONS -->" in parsed_response_get 

99 assert "<!-- CONTAINER TEXTS LIST -->" in parsed_response_get 

100 assert "<!-- CONTAINER TEXTS LIST - TITLE -->" in parsed_response_get 

101 assert "List of texts" in parsed_response_get 

102 assert "<!-- CONTAINER TEXTS LIST - CONTENT -->" in parsed_response_get 

103 assert "<!-- CONTAINER TEXTS LIST - TEXT TABLE -->" in parsed_response_get 

104 for text_id, text_value in response_get_texts.json()["texts"].items(): 

105 assert "<!-- TEXT " + text_id + " -->" in parsed_response_get 

106 assert 'id="' + text_id + '">' in parsed_response_get 

107 assert text_value["text_preprocessed"] in parsed_response_get 

108 

109 

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

111# test_ok_2 

112# ============================================================================== 

113 

114 

115@pytest.mark.asyncio() 

116async def test_ok_2(async_client, tmp_path): 

117 """ 

118 Test the `GET /gui/projects/{project_id}/texts` route. 

119 

120 Arguments: 

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

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

123 """ 

124 # Assert HTTP client is created. 

125 assert async_client 

126 

127 # Create dummy projects. 

128 create_dummy_projects( 

129 tmp_path=tmp_path, 

130 list_of_dummy_project_ids=[ 

131 "0e_CLUSTERING_PENDING", 

132 ], 

133 ) 

134 

135 # Get texts before test. 

136 response_get_texts = await async_client.get( 

137 url="/api/projects/0e_CLUSTERING_PENDING/texts?without_deleted_texts=false" 

138 ) 

139 assert response_get_texts.status_code == 200 

140 

141 # Assert route `GET /gui/projects/{project_id}/texts` works. 

142 response_get = await async_client.get(url="/gui/projects/0e_CLUSTERING_PENDING/texts") 

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

144 assert response_get.status_code == 200 

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

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

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

148 assert "<!-- CONTAINER TEXTS -->" in parsed_response_get 

149 assert "<!-- CONTAINER TEXTS - TITLE -->" in parsed_response_get 

150 assert "Iteration (0): Texts synthesis" in parsed_response_get 

151 assert "<!-- CONTAINER TEXTS - CONTENT -->" in parsed_response_get 

152 assert "<!-- CONTAINER TEXTS - INFORMATIONS - PROJECT ID -->" in parsed_response_get 

153 assert "0e_CLUSTERING_PENDING" in parsed_response_get 

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

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

156 assert "<!-- CONTAINER TEXTS - INFORMATIONS - MODELIZATION -->" not in parsed_response_get 

157 assert "<!-- CONTAINER TEXTS - ACTIONS -->" in parsed_response_get 

158 assert "<!-- CONTAINER TEXTS LIST -->" in parsed_response_get 

159 assert "<!-- CONTAINER TEXTS LIST - TITLE -->" in parsed_response_get 

160 assert "List of texts" in parsed_response_get 

161 assert "<!-- CONTAINER TEXTS LIST - CONTENT -->" in parsed_response_get 

162 assert "<!-- CONTAINER TEXTS LIST - TEXT TABLE -->" in parsed_response_get 

163 assert parsed_response_get.count("<!-- TEXT ") == len(response_get_texts.json()["texts"].keys()) 

164 for text_id, text_value in response_get_texts.json()["texts"].items(): 

165 assert "<!-- TEXT " + text_id + " -->" in parsed_response_get 

166 assert 'id="' + text_id + '">' in parsed_response_get 

167 assert text_value["text_preprocessed"] in parsed_response_get