Coverage for test/creator_test.py: 100%

146 statements  

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

1import json 

2import os 

3import unittest 

4 

5import redis 

6from oc_meta.core.creator import * 

7from oc_meta.lib.file_manager import get_csv_data 

8from oc_meta.plugins.multiprocess.resp_agents_creator import RespAgentsCreator 

9from rdflib import XSD, Graph, compare 

10from rdflib.term import _toPythonMapping 

11from SPARQLWrapper import POST, SPARQLWrapper 

12 

13from oc_ocdm.counter_handler.redis_counter_handler import RedisCounterHandler 

14 

15SERVER = 'http://127.0.0.1:8805/sparql' 

16SERVER = 'http://127.0.0.1:8805/sparql' 

17 

18def reset_redis_counters(): 

19 redis_host = 'localhost' 

20 redis_port = 6379 

21 redis_db = 5 

22 redis_client = redis.Redis(host=redis_host, port=redis_port, db=redis_db) 

23 redis_client.flushdb() 

24 

25def reset_server(server:str=SERVER) -> None: 

26 ts = SPARQLWrapper(server) 

27 ts.setQuery('DELETE WHERE { GRAPH ?g { ?s ?p ?o } }') 

28 ts.setMethod(POST) 

29 ts.query() 

30 

31# The following function has been added for handling gYear and gYearMonth correctly. 

32# Source: https://github.com/opencitations/script/blob/master/ocdm/storer.py 

33# More info at https://github.com/RDFLib/rdflib/issues/806. 

34 

35def hack_dates(): 

36 if XSD.gYear in _toPythonMapping: 

37 _toPythonMapping.pop(XSD.gYear) 

38 if XSD.gYearMonth in _toPythonMapping: 

39 _toPythonMapping.pop(XSD.gYearMonth) 

40 

41def open_json(path): 

42 path = os.path.abspath(path) 

43 with open(path) as json_file: 

44 data = json.load(json_file) 

45 return data 

46 

47# creator executor 

48def prepare2test(name): 

49 reset_server() 

50 reset_redis_counters() 

51 data = get_csv_data("test/testcases/testcase_data/testcase_" + name + "_data.csv") 

52 testcase_id_br = get_csv_data("test/testcases/testcase_data/indices/" + name + "/index_id_br_" + name + ".csv") 

53 testcase_id_ra = get_csv_data("test/testcases/testcase_data/indices/" + name + "/index_id_ra_" + name + ".csv") 

54 testcase_ar = get_csv_data("test/testcases/testcase_data/indices/" + name + "/index_ar_" + name + ".csv") 

55 testcase_re = get_csv_data("test/testcases/testcase_data/indices/" + name + "/index_re_" + name + ".csv") 

56 testcase_vi = open_json("test/testcases/testcase_data/indices/" + name + "/index_vi_" + name + ".json") 

57 testcase_ttl = "test/testcases/testcase_" + name + ".ttl" 

58 

59 counter_handler = RedisCounterHandler(host='localhost', port=6379, db=5) 

60 creator = Creator(data, SERVER, "https://w3id.org/oc/meta/", counter_handler, "060", 'https://orcid.org/0000-0002-8420-0696', testcase_id_ra, testcase_id_br, 

61 testcase_re, testcase_ar, testcase_vi, set(), Graph()) 

62 creator_setgraph = creator.creator() 

63 test_graph = Graph() 

64 hack_dates() 

65 test_graph = test_graph.parse(testcase_ttl, format="ttl") 

66 new_graph = Graph() 

67 for g in creator_setgraph.graphs(): 

68 new_graph += g 

69 return test_graph, new_graph 

70 

71class test_Creator(unittest.TestCase): 

72 def setUp(self): 

73 reset_server() 

74 reset_redis_counters() 

75 self.counter_handler = RedisCounterHandler(host='localhost', port=6379, db=5) 

76 

77 def tearDown(self): 

78 reset_redis_counters() 

79 

80 def test_vvi_action(self): 

81 base_iri = 'https://w3id.org/oc/meta/' 

82 vvi = {'0602': {'issue': {}, 'volume': {'107': {'id': '4733', 'issue': {'1': {'id': '4734'}, '2': {'id': '4735'}, '3': {'id': '4736'}, '4': {'id': '4737'}, '5': {'id': '4738'}, '6': {'id': '4739'}}}, '108': {'id': '4740', 'issue': {'1': {'id': '4741'}, '2': {'id': '4742'}, '3': {'id': '4743'}, '4': {'id': '4744'}}}, '104': {'id': '4712', 'issue': {'1': {'id': '4713'}, '2': {'id': '4714'}, '3': {'id': '4715'}, '4': {'id': '4716'}, '5': {'id': '4717'}, '6': {'id': '4718'}}}, '148': {'id': '4417', 'issue': {'12': {'id': '4418'}, '11': {'id': '4419'}}}, '100': {'id': '4684', 'issue': {'1': {'id': '4685'}, '2': {'id': '4686'}, '3': {'id': '4687'}, '4': {'id': '4688'}, '5': {'id': '4689'}, '6': {'id': '4690'}}}, '101': {'id': '4691', 'issue': {'1': {'id': '4692'}, '2': {'id': '4693'}, '3': {'id': '4694'}, '4': {'id': '4695'}, '5': {'id': '4696'}, '6': {'id': '4697'}}}, '102': {'id': '4698', 'issue': {'1': {'id': '4699'}, '2': {'id': '4700'}, '3': {'id': '4701'}, '4': {'id': '4702'}, '5': {'id': '4703'}, '6': {'id': '4704'}}}, '103': {'id': '4705', 'issue': {'1': {'id': '4706'}, '2': {'id': '4707'}, '3': {'id': '4708'}, '4': {'id': '4709'}, '5': {'id': '4710'}, '6': {'id': '4711'}}}, '105': {'id': '4719', 'issue': {'1': {'id': '4720'}, '2': {'id': '4721'}, '3': {'id': '4722'}, '4': {'id': '4723'}, '5': {'id': '4724'}, '6': {'id': '4725'}}}, '106': {'id': '4726', 'issue': {'6': {'id': '4732'}, '1': {'id': '4727'}, '2': {'id': '4728'}, '3': {'id': '4729'}, '4': {'id': '4730'}, '5': {'id': '4731'}}}}}} 

83 creator = Creator([], SERVER, base_iri, self.counter_handler, "060", 'https://orcid.org/0000-0002-8420-0696', [], [], [], [], vvi, set(), Graph()) 

84 creator.src = None 

85 creator.type = 'journal article' 

86 preexisting_graph = creator.finder.get_subgraph(URIRef(f'{base_iri}br/0601'), dict()) 

87 creator.br_graph = creator.setgraph.add_br('https://orcid.org/0000-0002-8420-0696', None, URIRef(f'{base_iri}br/0601'), preexisting_graph=preexisting_graph) 

88 creator.vvi_action('OECD [omid:br/0602]', '107', '1') 

89 output_graph = Graph() 

90 for g in creator.setgraph.graphs(): 

91 output_graph += g 

92 expected_data = ''' 

93 <https://w3id.org/oc/meta/br/0602> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/spar/fabio/Expression>. 

94 <https://w3id.org/oc/meta/br/0602> <http://purl.org/dc/terms/title> "OECD"^^<http://www.w3.org/2001/XMLSchema#string>. 

95 <https://w3id.org/oc/meta/br/0602> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/spar/fabio/Journal>. 

96 <https://w3id.org/oc/meta/br/4733> <http://purl.org/spar/fabio/hasSequenceIdentifier> "107"^^<http://www.w3.org/2001/XMLSchema#string>. 

97 <https://w3id.org/oc/meta/br/4733> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/spar/fabio/Expression>. 

98 <https://w3id.org/oc/meta/br/4733> <http://purl.org/vocab/frbr/core#partOf> <https://w3id.org/oc/meta/br/0602>. 

99 <https://w3id.org/oc/meta/br/4733> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/spar/fabio/JournalVolume>. 

100 <https://w3id.org/oc/meta/br/4734> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/spar/fabio/JournalIssue>. 

101 <https://w3id.org/oc/meta/br/4734> <http://purl.org/vocab/frbr/core#partOf> <https://w3id.org/oc/meta/br/4733>. 

102 <https://w3id.org/oc/meta/br/4734> <http://purl.org/spar/fabio/hasSequenceIdentifier> "1"^^<http://www.w3.org/2001/XMLSchema#string>. 

103 <https://w3id.org/oc/meta/br/4734> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/spar/fabio/Expression>. 

104 <https://w3id.org/oc/meta/br/0601> <http://purl.org/vocab/frbr/core#partOf> <https://w3id.org/oc/meta/br/4734>. 

105 <https://w3id.org/oc/meta/br/0601> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/spar/fabio/Expression>. 

106 ''' 

107 expected_graph = Graph() 

108 expected_graph = expected_graph.parse(data=expected_data, format="nt") 

109 self.assertEqual(compare.isomorphic(output_graph, expected_graph), True) 

110 

111class test_RespAgentsCreator(unittest.TestCase): 

112 def setUp(self): 

113 reset_server() 

114 reset_redis_counters() 

115 self.counter_handler = RedisCounterHandler(host='localhost', port=6379, db=5) 

116 

117 def tearDown(self): 

118 reset_redis_counters() 

119 

120 def test_creator(self): 

121 data = get_csv_data("test/testcases/testcase_data/resp_agents_creator.csv") 

122 testcase_id_ra = get_csv_data("test/testcases/testcase_data/indices/resp_agents_creator/index_id_ra.csv") 

123 creator = RespAgentsCreator(data, SERVER, "https://w3id.org/oc/meta/", self.counter_handler, "060", 'https://orcid.org/0000-0002-8420-0696', testcase_id_ra, set(), Graph()) 

124 creator_graphset = creator.creator() 

125 output_graph = Graph() 

126 for g in creator_graphset.graphs(): 

127 output_graph += g 

128 expected_data = ''' 

129 <https://w3id.org/oc/meta/ra/0601> <http://purl.org/spar/datacite/hasIdentifier> <https://w3id.org/oc/meta/id/0601> . 

130 <https://w3id.org/oc/meta/ra/0601> <http://xmlns.com/foaf/0.1/givenName> "Ron J."^^<http://www.w3.org/2001/XMLSchema#string> . 

131 <https://w3id.org/oc/meta/ra/0601> <http://xmlns.com/foaf/0.1/familyName> "Deckert"^^<http://www.w3.org/2001/XMLSchema#string> . 

132 <https://w3id.org/oc/meta/ra/0601> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Agent> . 

133 <https://w3id.org/oc/meta/id/0601> <http://www.essepuntato.it/2010/06/literalreification/hasLiteralValue> "0000-0003-2100-6412"^^<http://www.w3.org/2001/XMLSchema#string> . 

134 <https://w3id.org/oc/meta/id/0601> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/spar/datacite/Identifier> . 

135 <https://w3id.org/oc/meta/id/0601> <http://purl.org/spar/datacite/usesIdentifierScheme> <http://purl.org/spar/datacite/orcid> . 

136 <https://w3id.org/oc/meta/ra/0602> <http://xmlns.com/foaf/0.1/givenName> "Juan M."^^<http://www.w3.org/2001/XMLSchema#string> . 

137 <https://w3id.org/oc/meta/ra/0602> <http://xmlns.com/foaf/0.1/familyName> "Ruso"^^<http://www.w3.org/2001/XMLSchema#string> . 

138 <https://w3id.org/oc/meta/ra/0602> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Agent> . 

139 <https://w3id.org/oc/meta/ra/0602> <http://purl.org/spar/datacite/hasIdentifier> <https://w3id.org/oc/meta/id/0602> . 

140 <https://w3id.org/oc/meta/id/0602> <http://www.essepuntato.it/2010/06/literalreification/hasLiteralValue> "0000-0001-5909-6754"^^<http://www.w3.org/2001/XMLSchema#string> . 

141 <https://w3id.org/oc/meta/id/0602> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/spar/datacite/Identifier> . 

142 <https://w3id.org/oc/meta/id/0602> <http://purl.org/spar/datacite/usesIdentifierScheme> <http://purl.org/spar/datacite/orcid> . 

143 <https://w3id.org/oc/meta/ra/0603> <http://xmlns.com/foaf/0.1/familyName> "Sarmiento"^^<http://www.w3.org/2001/XMLSchema#string> . 

144 <https://w3id.org/oc/meta/ra/0603> <http://purl.org/spar/datacite/hasIdentifier> <https://w3id.org/oc/meta/id/0603> . 

145 <https://w3id.org/oc/meta/ra/0603> <http://xmlns.com/foaf/0.1/givenName> "Félix"^^<http://www.w3.org/2001/XMLSchema#string> . 

146 <https://w3id.org/oc/meta/ra/0603> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Agent> . 

147 <https://w3id.org/oc/meta/id/0603> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/spar/datacite/Identifier> . 

148 <https://w3id.org/oc/meta/id/0603> <http://www.essepuntato.it/2010/06/literalreification/hasLiteralValue> "0000-0002-4487-6894"^^<http://www.w3.org/2001/XMLSchema#string> . 

149 <https://w3id.org/oc/meta/id/0603> <http://purl.org/spar/datacite/usesIdentifierScheme> <http://purl.org/spar/datacite/orcid> . 

150 <https://w3id.org/oc/meta/ra/0604> <http://xmlns.com/foaf/0.1/name> "Pub1"^^<http://www.w3.org/2001/XMLSchema#string> . 

151 <https://w3id.org/oc/meta/ra/0604> <http://purl.org/spar/datacite/hasIdentifier> <https://w3id.org/oc/meta/id/0604> . 

152 <https://w3id.org/oc/meta/ra/0604> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Agent> . 

153 <https://w3id.org/oc/meta/id/0604> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/spar/datacite/Identifier> . 

154 <https://w3id.org/oc/meta/id/0604> <http://www.essepuntato.it/2010/06/literalreification/hasLiteralValue> "1111"^^<http://www.w3.org/2001/XMLSchema#string> . 

155 <https://w3id.org/oc/meta/id/0604> <http://purl.org/spar/datacite/usesIdentifierScheme> <http://purl.org/spar/datacite/crossref> . 

156 ''' 

157 expected_graph = Graph() 

158 expected_graph = expected_graph.parse(data=expected_data, format="nt") 

159 self.assertEqual(compare.isomorphic(output_graph, expected_graph), True) 

160 

161class testcase_01(unittest.TestCase): 

162 def test(self): 

163 # testcase1: 2 different issues of the same venue (no volume) 

164 name = "01" 

165 test_graph, new_graph = prepare2test(name) 

166 self.assertEqual(compare.isomorphic(new_graph, test_graph), True) 

167 

168 

169class testcase_02(unittest.TestCase): 

170 def test(self): 

171 # testcase2: 2 different volumes of the same venue (no issue) 

172 name = "02" 

173 test_graph, new_graph = prepare2test(name) 

174 self.assertEqual(compare.isomorphic(new_graph, test_graph), True) 

175 

176 

177class testcase_03(unittest.TestCase): 

178 def test(self): 

179 # testcase3: 2 different issues of the same volume 

180 name = "03" 

181 test_graph, new_graph = prepare2test(name) 

182 self.assertEqual(compare.isomorphic(new_graph, test_graph), True) 

183 

184 

185class testcase_04(unittest.TestCase): 

186 def test(self): 

187 # testcase4: 2 new IDS and different date format (yyyy-mm and yyyy-mm-dd) 

188 name = "04" 

189 test_graph, new_graph = prepare2test(name) 

190 self.assertEqual(compare.isomorphic(new_graph, test_graph), True) 

191 

192 

193class testcase_05(unittest.TestCase): 

194 def test(self): 

195 # testcase5: NO ID scenario 

196 name = "05" 

197 test_graph, new_graph = prepare2test(name) 

198 self.assertEqual(compare.isomorphic(new_graph, test_graph), True) 

199 

200class testcase_06(unittest.TestCase): 

201 def test(self): 

202 # testcase6: ALL types test 

203 name = "06" 

204 test_graph, new_graph = prepare2test(name) 

205 self.assertEqual(compare.isomorphic(new_graph, test_graph), True) 

206 

207 

208class testcase_07(unittest.TestCase): 

209 def test(self): 

210 # testcase7: all journal related types with an editor 

211 name = "07" 

212 test_graph, new_graph = prepare2test(name) 

213 self.assertEqual(compare.isomorphic(new_graph, test_graph), True) 

214 

215 

216class testcase_08(unittest.TestCase): 

217 def test(self): 

218 # testcase8: all book related types with an editor 

219 name = "08" 

220 test_graph, new_graph = prepare2test(name) 

221 self.assertEqual(compare.isomorphic(new_graph, test_graph), True) 

222 

223 

224class testcase_09(unittest.TestCase): 

225 def test(self): 

226 # testcase9: all proceeding related types with an editor 

227 name = "09" 

228 test_graph, new_graph = prepare2test(name) 

229 self.assertEqual(compare.isomorphic(new_graph, test_graph), True) 

230 

231 

232class testcase_10(unittest.TestCase): 

233 def test(self): 

234 # testcase10: a book inside a book series and a book inside a book set 

235 name = "10" 

236 test_graph, new_graph = prepare2test(name) 

237 self.assertEqual(compare.isomorphic(new_graph, test_graph), True) 

238 

239 

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

241 unittest.main()