Coverage for test/cleaner_test.py: 100%

95 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2025-07-14 14:06 +0000

1import unittest 

2 

3from oc_meta.lib.cleaner import Cleaner 

4 

5 

6class test_Cleaner(unittest.TestCase): 

7 def test_clen_hyphen(self): 

8 broken_strings = ['100­101', '100−101', '100–101', '100–101', '100—101', '100⁃101', '100−101', '100➖101', '100Ⲻ101', '100﹘101'] 

9 fixed_strings = list() 

10 for string in broken_strings: 

11 fixed_string = Cleaner(string).normalize_hyphens() 

12 fixed_strings.append(fixed_string) 

13 expected_output = ['100-101', '100-101', '100-101', '100-101', '100-101', '100-101', '100-101', '100-101', '100-101', '100-101'] 

14 self.assertEqual(fixed_strings, expected_output) 

15 

16 def test_clean_title(self): 

17 inputs = [ 

18 'OpenCitations, an infrastructure organization for open scholarship', 

19 'OPENCITATIONS, AN INFRASTRUCTURE ORGANIZATION FOR OPEN SCHOLARSHIP', 

20 'opencitations, an infrastructure organization for open scholarship', 

21 'OpenCitations, an infrastructure organization for open scholarship', 

22 ' ""agile"" "Knowledge" graph testing ù ò à with TESTaLOD (!incredible!) έτος 汉字', 

23 'Elsevier BV' 

24 ] 

25 outputs = list() 

26 for input in inputs: 

27 outputs.append(Cleaner(input).clean_title()) 

28 expected_output = [ 

29 'OpenCitations, An Infrastructure Organization For Open Scholarship', 

30 'Opencitations, An Infrastructure Organization For Open Scholarship', 

31 'Opencitations, An Infrastructure Organization For Open Scholarship', 

32 'OpenCitations, An Infrastructure Organization For Open Scholarship', 

33 '""Agile"" "Knowledge" Graph Testing Ù Ò À With TESTaLOD (!Incredible!) Έτος 汉字', 

34 'Elsevier BV' 

35 ] 

36 self.assertEqual(outputs, expected_output) 

37 

38 def test_clean_date_all_valid(self): 

39 inputs = ['2020-13-50', '2020-02-50', '2020-02-11', '2020-12-12', '2000', '2000-12', '2000-13'] 

40 outputs = list() 

41 for input in inputs: 

42 outputs.append(Cleaner(input).clean_date()) 

43 expected_output = ['2020', '2020-02', '2020-02-11', '2020-12-12', '2000', '2000-12', '2000'] 

44 self.assertEqual(outputs, expected_output) 

45 

46 def test_clean_date_all_invalid(self): 

47 inputs = ['100000-13-50', '02-11', '11', '100000', 'godopoli'] 

48 outputs = list() 

49 for input in inputs: 

50 outputs.append(Cleaner(input).clean_date()) 

51 expected_output = ['', '', '', '', ''] 

52 self.assertEqual(outputs, expected_output) 

53 

54 def test_clean_name(self): 

55 names = ['Peroni, Silvio', 'Peroni, S.', ' Peroni , Silvio ', 'PERONI, SILVIO', '', 'peroni', 'peroni, Silvio', 'McSorley, Stephen', 'OECD', ','] 

56 outputs = list() 

57 for name in names: 

58 outputs.append(Cleaner(name).clean_name()) 

59 expected_output = ['Peroni, Silvio', 'Peroni, S.', 'Peroni, Silvio', 'Peroni, Silvio', '', 'Peroni', 'Peroni, Silvio', 'McSorley, Stephen', 'Oecd', ''] 

60 self.assertEqual(outputs, expected_output) 

61 

62 def test_remove_unwanted_characters(self): 

63 names = ['Edward ].', 'Bernacki', 'Tom??&OV0165;', 'Gavin E.', 'Andr[eacute]', 'Albers\u2010Miller', "O'Connor", 'O\u2019Connell', 'Gonz\u0301alez-Santiago', 'Gonz\u00e1lez-Benito', 'André'] 

64 outputs = list() 

65 for name in names: 

66 outputs.append(Cleaner(name).remove_unwanted_characters()) 

67 expected_output = ['Edward', 'Bernacki', 'Tom&OV0165', 'Gavin E.', 'Andreacute', 'Albers-Miller', "O'Connor", 'O’Connell', 'Gonźalez-Santiago', 'González-Benito', 'André'] 

68 self.assertEqual(outputs, expected_output) 

69 

70 def test_clean_ra_list(self): 

71 names = ['Not Available, Not Available', 'Peroni, Not Available', 'Not Available, Silvio', 'Not Available', 'Peroni, Silvio', ','] 

72 output = Cleaner.clean_ra_list(names) 

73 expected_output = ['Peroni, ', 'Peroni, Silvio'] 

74 self.assertEqual(output, expected_output) 

75 

76 def test_clean_ra_list_duplicates(self): 

77 names = ['Peroni, Silvio [orcid:0000-0003-0530-4305 viaf:1]', 'Peroni, Not Available', 'Peroni, Silvio [orcid:0000-0003-0530-4305]', 'Massari, Arcangelo'] 

78 output = Cleaner.clean_ra_list(names) 

79 expected_output = ['Peroni, Silvio [orcid:0000-0003-0530-4305 viaf:1]', 'Peroni, ', 'Massari, Arcangelo'] 

80 self.assertEqual(output, expected_output) 

81 

82 def test_clean_ra_list_remove_ids(self): 

83 names = ['Peroni, Silvio [orcid:0000-0003-0530-4305 viaf:1]', 'Peroni, Not Available', 'Perone, Silvio [orcid:0000-0003-0530-4305]', 'Massari, Arcangelo'] 

84 output = Cleaner.clean_ra_list(names) 

85 expected_output = ['Peroni, Silvio [viaf:1]', 'Peroni, ', 'Perone, Silvio', 'Massari, Arcangelo'] 

86 self.assertEqual(output, expected_output) 

87 

88 def test_clean_ra_list_only_ids(self): 

89 names = ['Peroni, Silvio [orcid:0000-0003-0530-4305]', '[orcid:0000-0003-0530-4305 viaf:1]', '[orcid:0000-0003-0530-4306]'] 

90 output = Cleaner.clean_ra_list(names) 

91 expected_output = ['Peroni, Silvio', '[viaf:1]', '[orcid:0000-0003-0530-4306]'] 

92 self.assertEqual(output, expected_output) 

93 

94 def test_normalize_spaces(self): 

95 broken_strings = ['100\u0009101', '100\u00A0101', '100\u200B101', '100\u202F101'] 

96 fixed_strings = list() 

97 for string in broken_strings: 

98 fixed_string = Cleaner(string).normalize_spaces() 

99 fixed_strings.append(fixed_string) 

100 expected_output = ['100 101', '100 101', '100 101', '100 101'] 

101 self.assertEqual(fixed_strings, expected_output) 

102 

103 def test_normalize_id(self): 

104 identifiers = ['doi:10.1123/ijatt.2015-0070', 'doi:1', 'orcid:0000-0003-0530-4305', 'orcid:0000-0000', 'issn:1479-6708', 'issn:0000-0000', 'isbn:9783319403120', 'isbn:0000-0000'] 

105 output = list() 

106 csv_manager = dict() 

107 for id in identifiers: 

108 output.append(Cleaner(id).normalize_id(valid_dois_cache=csv_manager)) 

109 expected_output = ['doi:10.1123/ijatt.2015-0070', None, 'orcid:0000-0003-0530-4305', None, 'issn:1479-6708', None, 'isbn:9783319403120', None] 

110 self.assertEqual(output, expected_output) 

111 

112 def test_clean_volume_and_issue(self): 

113 invalid_vi_rows = [ 

114 {'pub_date': '','volume': 'Volume 15-Issue 1', 'issue': '', 'type': ''}, 

115 {'pub_date': '','volume': '', 'issue': 'Vol 2 Núm 3', 'type': ''}, 

116 {'pub_date': '','volume': '', 'issue': 'Lang.- Lit. Volume 10 numéro 2', 'type': ''}, 

117 {'pub_date': '','volume': 'Vol. 14 Issue 1', 'issue': '', 'type': ''}, 

118 {'pub_date': '','volume': '', 'issue': 'Vol. 39 N°1', 'type': ''}, 

119 {'pub_date': '','volume': 'Vol. 10, N° 2-3', 'issue': '', 'type': ''}, 

120 {'pub_date': '','volume': '', 'issue': 'Vol. 35 N° spécial 1', 'type': ''}, 

121 {'pub_date': '','volume': 'Vol. XXXIII N° 2', 'issue': '', 'type': ''}, 

122 {'pub_date': '','volume': '', 'issue': 'Volume 14 Issue 5', 'type': ''}, 

123 {'pub_date': '','volume': 'Vol.10, No.3', 'issue': '', 'type': ''}, 

124 {'pub_date': '','volume': '-1', 'issue': '', 'type': ''}, 

125 {'pub_date': '','volume': 'Tome II - N°1', 'issue': '', 'type': ''}, 

126 {'pub_date': '','volume': '>6', 'issue': '13,N°2', 'type': ''}, 

127 {'pub_date': '','volume': '9, n° 4', 'issue': '', 'type': ''}, 

128 {'pub_date': '','volume': '${articleBase.volume}', 'issue': 'Tập 55, Số 3', 'type': ''}, 

129 {'pub_date': '','volume': 'Issue 1 Volume 21, 2020', 'issue': '', 'type': ''}, 

130 {'pub_date': '','volume': '', 'issue': "14 (2'2018)", 'type': ''}, 

131 {'pub_date': '','volume': 'Cilt:13 Sayı:3', 'issue': '', 'type': ''}, 

132 {'pub_date': '','volume': '/', 'issue': 'Cilt 21 Sayı 3 Temmuz 2020', 'type': ''}, 

133 {'pub_date': '','volume': '&NA;', 'issue': '&NA;', 'type': ''}, 

134 {'pub_date': '','volume': 'n/a', 'issue': 'n/a', 'type': ''}, 

135 {'pub_date': '','volume': '.', 'issue': '-', 'type': ''}, 

136 {'pub_date': '','volume': '`', 'issue': 'ё', 'type': ''}, 

137 {'pub_date': '','volume': '.38', 'issue': '/4', 'type': ''}, 

138 {'pub_date': '','volume': '74,', 'issue': '501.', 'type': ''}, 

139 {'pub_date': '','volume': '1(3)/', 'issue': '19`', 'type': ''}, 

140 {'pub_date': '','volume': 'No. 4.', 'issue': '3()', 'type': ''}, 

141 {'pub_date': '','volume': '5â\x80\x926', 'issue': '12���13', 'type': ''}, 

142 {'pub_date': '','volume': '38â39', 'issue': '3???4', 'type': ''}, 

143 {'pub_date': '','volume': 'n�183', 'issue': 'N�31-32', 'type': ''}, 

144 {'pub_date': '','volume': 'N?44', 'issue': 'N��49', 'type': ''}, 

145 {'pub_date': '','volume': 'N�1,NF', 'issue': '85 (First Serie', 'type': ''}, 

146 {'pub_date': '','volume': 'issue 2', 'issue': 'Original Series, Volume 1', 'type': ''}, 

147 {'pub_date': '','volume': 'Special Issue 2', 'issue': 'volume 3', 'type': ''}, 

148 {'pub_date': '','volume': '1 special issue', 'issue': 'Vol, 7', 'type': ''}, 

149 {'pub_date': '','volume': 'Special Issue "Urban Morphology”', 'issue': 'vol.7', 'type': ''}, 

150 {'pub_date': '','volume': '', 'issue': 'Tome 1', 'type': ''}, 

151 {'pub_date': '','volume': 'Special_Issue_Number_2', 'issue': '', 'type': ''}, 

152 {'pub_date': '','volume': 'Special-Issue-1', 'issue': '', 'type': ''}, 

153 {'pub_date': '','volume': 'Special 13', 'issue': '', 'type': ''}, 

154 {'pub_date': '','volume': 'Especial 2', 'issue': '', 'type': ''}, 

155 {'pub_date': '','volume': 'esp.2', 'issue': '', 'type': ''}, 

156 {'pub_date': '','volume': 'spe.2', 'issue': '', 'type': ''}, 

157 {'pub_date': '','volume': '1 S.2', 'issue': '', 'type': ''}, 

158 {'pub_date': '','volume': 'Özel Sayı 5', 'issue': '', 'type': 'journal volume'}, 

159 {'pub_date': '','volume': 'ÖS1', 'issue': '', 'type': ''}, 

160 {'pub_date': '','volume': 'N° Hors série 10', 'issue': '', 'type': ''}, 

161 {'pub_date': '','volume': 'Hors-série 5', 'issue': '', 'type': ''}, 

162 {'pub_date': '','volume': '특별호', 'issue': '', 'type': ''}, 

163 {'pub_date': '','volume': '(13/72) Education', 'issue': '', 'type': ''}, 

164 {'pub_date': '','volume': '(13/72) Language-Literature', 'issue': '', 'type': ''}, 

165 {'pub_date': '','volume': 'Sayı: 24', 'issue': '', 'type': ''}, 

166 {'pub_date': '','volume': 'Issue 11, Supplement 6', 'issue': '', 'type': ''}, 

167 {'pub_date': '','volume': 'Issue 2. pp. 94-185', 'issue': '', 'type': ''}, 

168 {'pub_date': '', 'volume': '5', 'issue': '6', 'type': ''}, 

169 {'pub_date': '', 'volume': 'Issue 2. pp. 94-185', 'issue': '6', 'type': ''}, 

170 {'pub_date': '', 'volume': '', 'issue': '6', 'type': ''}, 

171 {'pub_date': '', 'volume': '5', 'issue': '', 'type': ''}, 

172 {'pub_date': '', 'volume': 'Not Available', 'issue': 'not available', 'type': ''} 

173 ] 

174 for row in invalid_vi_rows: 

175 Cleaner.clean_volume_and_issue(row) 

176 expected_output = [ 

177 {'pub_date': '', 'volume': '15', 'issue': '1', 'type': ''}, 

178 {'pub_date': '', 'volume': '2', 'issue': '3', 'type': ''}, 

179 {'pub_date': '', 'volume': '10', 'issue': '2', 'type': ''}, 

180 {'pub_date': '', 'volume': '14', 'issue': '1', 'type': ''}, 

181 {'pub_date': '', 'volume': '39', 'issue': '1', 'type': ''}, 

182 {'pub_date': '', 'volume': '10', 'issue': '2-3', 'type': ''}, 

183 {'pub_date': '', 'volume': '35', 'issue': '1', 'type': ''}, 

184 {'pub_date': '', 'volume': 'XXXIII', 'issue': '2', 'type': ''}, 

185 {'pub_date': '', 'volume': '14', 'issue': '5', 'type': ''}, 

186 {'pub_date': '', 'volume': '10', 'issue': '3', 'type': ''}, 

187 {'pub_date': '', 'volume': '-1', 'issue': '', 'type': ''}, 

188 {'pub_date': '', 'volume': 'II', 'issue': '1', 'type': ''}, 

189 {'pub_date': '', 'volume': '>6', 'issue': '2', 'type': ''}, 

190 {'pub_date': '', 'volume': '9', 'issue': '4', 'type': ''}, 

191 {'pub_date': '', 'volume': '55', 'issue': '3', 'type': ''}, 

192 {'pub_date': '2020', 'volume': '21', 'issue': '1', 'type': ''}, 

193 {'pub_date': '2018', 'volume': '14', 'issue': '2', 'type': ''}, 

194 {'pub_date': '', 'volume': '13', 'issue': '3', 'type': ''}, 

195 {'pub_date': '2020', 'volume': '21', 'issue': '3', 'type': ''}, 

196 {'pub_date': '', 'volume': '', 'issue': '', 'type': ''}, 

197 {'pub_date': '', 'volume': '', 'issue': '', 'type': ''}, 

198 {'pub_date': '', 'volume': '', 'issue': '', 'type': ''}, 

199 {'pub_date': '', 'volume': '', 'issue': '', 'type': ''}, 

200 {'pub_date': '', 'volume': '.38', 'issue': '4', 'type': ''}, 

201 {'pub_date': '', 'volume': '74', 'issue': '501.', 'type': ''}, 

202 {'pub_date': '', 'volume': '1(3)', 'issue': '19', 'type': ''}, 

203 {'pub_date': '', 'volume': 'No. 4.', 'issue': '3', 'type': ''}, 

204 {'pub_date': '', 'volume': '5-6', 'issue': '12-13', 'type': ''}, 

205 {'pub_date': '', 'volume': '38-39', 'issue': '3-4', 'type': ''}, 

206 {'pub_date': '', 'volume': '183', 'issue': '31-32', 'type': ''}, 

207 {'pub_date': '', 'volume': '44', 'issue': '49', 'type': ''}, 

208 {'pub_date': '', 'volume': '1,NF', 'issue': '85 (First Series)', 'type': ''}, 

209 {'pub_date': '', 'volume': 'Original Series, Volume 1', 'issue': 'issue 2', 'type': ''}, 

210 {'pub_date': '', 'volume': 'volume 3', 'issue': 'Special Issue 2', 'type': ''}, 

211 {'pub_date': '', 'volume': 'Vol, 7', 'issue': '1 special issue', 'type': ''}, 

212 {'pub_date': '', 'volume': 'vol.7', 'issue': 'Special Issue "Urban Morphology”', 'type': ''}, 

213 {'pub_date': '', 'volume': 'Tome 1', 'issue': '', 'type': ''}, 

214 {'pub_date': '', 'volume': '', 'issue': 'Special_Issue_Number_2', 'type': ''}, 

215 {'pub_date': '', 'volume': '', 'issue': 'Special-Issue-1', 'type': ''}, 

216 {'pub_date': '', 'volume': '', 'issue': 'Special 13', 'type': ''}, 

217 {'pub_date': '', 'volume': '', 'issue': 'Especial 2', 'type': ''}, 

218 {'pub_date': '', 'volume': '', 'issue': 'esp.2', 'type': ''}, 

219 {'pub_date': '', 'volume': '', 'issue': 'spe.2', 'type': ''}, 

220 {'pub_date': '', 'volume': '', 'issue': '1 S.2', 'type': ''}, 

221 {'pub_date': '', 'volume': '', 'issue': 'Özel Sayı 5', 'type': 'journal issue'}, 

222 {'pub_date': '', 'volume': '', 'issue': 'ÖS1', 'type': ''}, 

223 {'pub_date': '', 'volume': '', 'issue': 'N° Hors série 10', 'type': ''}, 

224 {'pub_date': '', 'volume': '', 'issue': 'Hors-série 5', 'type': ''}, 

225 {'pub_date': '', 'volume': '', 'issue': '특별호', 'type': ''}, 

226 {'pub_date': '', 'volume': '(13/72) Education', 'issue': '', 'type': ''}, 

227 {'pub_date': '', 'volume': '(13/72) Language-Literature', 'issue': '', 'type': ''}, 

228 {'pub_date': '', 'volume': '', 'issue': 'Sayı: 24', 'type': ''}, 

229 {'pub_date': '', 'volume': '', 'issue': 'Issue 11, Supplement 6', 'type': ''}, 

230 {'pub_date': '', 'volume': '', 'issue': 'Issue 2. pp. 94-185', 'type': ''}, 

231 {'pub_date': '', 'volume': '5', 'issue': '6', 'type': ''}, 

232 {'pub_date': '', 'volume': 'Issue 2. pp. 94-185', 'issue': '6', 'type': ''}, 

233 {'pub_date': '', 'volume': '', 'issue': '6', 'type': ''}, 

234 {'pub_date': '', 'volume': '5', 'issue': '', 'type': ''}, 

235 {'pub_date': '', 'volume': '', 'issue': '', 'type': ''} 

236 ] 

237 self.assertEqual(invalid_vi_rows, expected_output) 

238 

239 def test_remove_ascii(self): 

240 clean_strings = [] 

241 broken_strings = ['5â\x80\x926'] 

242 for string in broken_strings: 

243 clean_strings.append(Cleaner(string).remove_ascii()) 

244 expected_output = ['5 6'] 

245 self.assertEqual(clean_strings, expected_output) 

246 

247 

248if __name__ == '__main__': # pragma: no cover 

249 unittest.main()