Coverage for test/creator_test.py: 99%
147 statements
« prev ^ index » next coverage.py v6.5.0, created at 2025-12-20 08:55 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2025-12-20 08:55 +0000
1import json
2import os
3import unittest
5import redis
6from oc_meta.core.creator import *
7from oc_meta.lib.file_manager import get_csv_data
8from oc_meta.lib.finder import ResourceFinder
9from oc_meta.plugins.multiprocess.resp_agents_creator import RespAgentsCreator
10from oc_ocdm.counter_handler.redis_counter_handler import RedisCounterHandler
11from rdflib import XSD, Graph, compare
12from rdflib.term import _toPythonMapping
13from sparqlite import SPARQLClient
15SERVER = 'http://127.0.0.1:8805/sparql'
17def reset_redis_counters():
18 redis_host = 'localhost'
19 redis_port = 6381
20 redis_db = 5
21 redis_client = redis.Redis(host=redis_host, port=redis_port, db=redis_db)
22 redis_client.flushdb()
24def reset_server(server:str=SERVER) -> None:
25 with SPARQLClient(server, timeout=60) as client:
26 client.update('DELETE WHERE { GRAPH ?g { ?s ?p ?o } }')
28# The following function has been added for handling gYear and gYearMonth correctly.
29# Source: https://github.com/opencitations/script/blob/master/ocdm/storer.py
30# More info at https://github.com/RDFLib/rdflib/issues/806.
32def hack_dates():
33 if XSD.gYear in _toPythonMapping:
34 _toPythonMapping.pop(XSD.gYear)
35 if XSD.gYearMonth in _toPythonMapping:
36 _toPythonMapping.pop(XSD.gYearMonth)
38def open_json(path):
39 path = os.path.abspath(path)
40 with open(path) as json_file:
41 data = json.load(json_file)
42 return data
44# creator executor
45def prepare2test(name):
46 reset_server()
47 reset_redis_counters()
48 data = get_csv_data("test/testcases/testcase_data/testcase_" + name + "_data.csv")
49 testcase_id_br = get_csv_data("test/testcases/testcase_data/indices/" + name + "/index_id_br_" + name + ".csv")
50 testcase_id_ra = get_csv_data("test/testcases/testcase_data/indices/" + name + "/index_id_ra_" + name + ".csv")
51 testcase_ar = get_csv_data("test/testcases/testcase_data/indices/" + name + "/index_ar_" + name + ".csv")
52 testcase_re = get_csv_data("test/testcases/testcase_data/indices/" + name + "/index_re_" + name + ".csv")
53 testcase_vi = open_json("test/testcases/testcase_data/indices/" + name + "/index_vi_" + name + ".json")
54 testcase_ttl = "test/testcases/testcase_" + name + ".ttl"
56 counter_handler = RedisCounterHandler(host='localhost', port=6381, db=5)
57 finder = ResourceFinder(ts_url=SERVER, base_iri="https://w3id.org/oc/meta/", local_g=Graph())
58 creator = Creator(data, finder, "https://w3id.org/oc/meta/", counter_handler, "060", 'https://orcid.org/0000-0002-8420-0696', testcase_id_ra, testcase_id_br,
59 testcase_re, testcase_ar, testcase_vi)
60 creator_setgraph = creator.creator()
61 test_graph = Graph()
62 hack_dates()
63 test_graph = test_graph.parse(testcase_ttl, format="ttl")
64 new_graph = Graph()
65 for g in creator_setgraph.graphs():
66 new_graph += g
67 return test_graph, new_graph
69class test_Creator(unittest.TestCase):
70 def setUp(self):
71 reset_server()
72 reset_redis_counters()
73 self.counter_handler = RedisCounterHandler(host='localhost', port=6381, db=5)
75 def tearDown(self):
76 reset_redis_counters()
78 def test_vvi_action(self):
79 base_iri = 'https://w3id.org/oc/meta/'
80 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'}}}}}}
81 finder = ResourceFinder(ts_url=SERVER, base_iri=base_iri, local_g=Graph())
82 creator = Creator([], finder, base_iri, self.counter_handler, "060", 'https://orcid.org/0000-0002-8420-0696', [], [], [], [], vvi)
83 creator.src = None
84 creator.type = 'journal article'
85 preexisting_graph = creator.finder.get_subgraph(URIRef(f'{base_iri}br/0601'))
86 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)
87 creator.vvi_action('OECD [omid:br/0602]', '107', '1')
88 output_graph = Graph()
89 for g in creator.setgraph.graphs():
90 output_graph += g
91 expected_data = '''
92 <https://w3id.org/oc/meta/br/0602> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/spar/fabio/Expression>.
93 <https://w3id.org/oc/meta/br/0602> <http://purl.org/dc/terms/title> "OECD"^^<http://www.w3.org/2001/XMLSchema#string>.
94 <https://w3id.org/oc/meta/br/0602> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/spar/fabio/Journal>.
95 <https://w3id.org/oc/meta/br/4733> <http://purl.org/spar/fabio/hasSequenceIdentifier> "107"^^<http://www.w3.org/2001/XMLSchema#string>.
96 <https://w3id.org/oc/meta/br/4733> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/spar/fabio/Expression>.
97 <https://w3id.org/oc/meta/br/4733> <http://purl.org/vocab/frbr/core#partOf> <https://w3id.org/oc/meta/br/0602>.
98 <https://w3id.org/oc/meta/br/4733> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/spar/fabio/JournalVolume>.
99 <https://w3id.org/oc/meta/br/4734> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/spar/fabio/JournalIssue>.
100 <https://w3id.org/oc/meta/br/4734> <http://purl.org/vocab/frbr/core#partOf> <https://w3id.org/oc/meta/br/4733>.
101 <https://w3id.org/oc/meta/br/4734> <http://purl.org/spar/fabio/hasSequenceIdentifier> "1"^^<http://www.w3.org/2001/XMLSchema#string>.
102 <https://w3id.org/oc/meta/br/4734> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/spar/fabio/Expression>.
103 <https://w3id.org/oc/meta/br/0601> <http://purl.org/vocab/frbr/core#partOf> <https://w3id.org/oc/meta/br/4734>.
104 <https://w3id.org/oc/meta/br/0601> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.org/spar/fabio/Expression>.
105 '''
106 expected_graph = Graph()
107 expected_graph = expected_graph.parse(data=expected_data, format="nt")
108 self.assertEqual(compare.isomorphic(output_graph, expected_graph), True)
110class test_RespAgentsCreator(unittest.TestCase):
111 def setUp(self):
112 reset_server()
113 reset_redis_counters()
114 self.counter_handler = RedisCounterHandler(host='localhost', port=6381, db=5)
116 def tearDown(self):
117 reset_redis_counters()
119 def test_creator(self):
120 data = get_csv_data("test/testcases/testcase_data/resp_agents_creator.csv")
121 testcase_id_ra = get_csv_data("test/testcases/testcase_data/indices/resp_agents_creator/index_id_ra.csv")
122 finder = ResourceFinder(ts_url=SERVER, base_iri="https://w3id.org/oc/meta/", local_g=Graph())
123 creator = RespAgentsCreator(data, finder, "https://w3id.org/oc/meta/", self.counter_handler, "060", 'https://orcid.org/0000-0002-8420-0696', testcase_id_ra)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
240if __name__ == '__main__': # pragma: no cover
241 unittest.main()