-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_load_GraphHandler.py
478 lines (420 loc) · 16.4 KB
/
test_load_GraphHandler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
import numpy as np
np.float_ = np.float64
import pytest
import sys
import os
import json
import pandas as pd
from typing import List, Tuple
from unittest.mock import Mock
from pandas import Timestamp
from rdflib import Graph, URIRef, Literal, BNode
from datetime import datetime
sys.path.append(".")
from load.core.GraphHandler import GraphHandler
from load.core.dbHandler.SQLHandler import SQLHandler
from load.core.dbHandler.RDFHandler import RDFHandler
from load.core.dbHandler.IndexHandler import IndexHandler
class TestGraphHandler:
"""
Test class for GraphHandler
"""
@classmethod
def setup_class(self):
self.m4ml_example_dataframe = pd.read_json(
"./tests/Test_files/load_files/hf_transformed_fair4ml_example.json"
)
@pytest.fixture
def setup_sql_handler(self) -> SQLHandler:
sql_handler = SQLHandler(
host="postgres",
user="test_user",
password="test_password",
database="test_DB",
)
sql_handler.connect()
sql_handler.delete_all_tables()
yield sql_handler
# disconnect and close the connection
sql_handler.disconnect()
@pytest.fixture
def setup_virtuoso_handler(self) -> RDFHandler:
kg_files_directory = "./tests/Test_files/load_files/virtuoso_data/kg_files"
rdfHandler = RDFHandler(
container_name="virtuoso",
kg_files_directory=kg_files_directory,
_user="dba",
_password="my_strong_password",
sparql_endpoint="http://virtuoso:8890/sparql",
)
rdfHandler.reset_db()
return rdfHandler
@pytest.fixture
def setup_elasticsearch_handler(self) -> IndexHandler:
elasticsearch_handler = IndexHandler(
es_host="elastic",
es_port=9200,
)
elasticsearch_handler.initialize_HF_index(index_name="test_hf_models")
yield elasticsearch_handler
elasticsearch_handler.clean_indices()
elasticsearch_handler.es.close()
@pytest.fixture
def setup_mock_graph_handler(self) -> GraphHandler:
mock_SQLHandler = Mock(spec=SQLHandler)
mock_RDFHandler = Mock(spec=RDFHandler)
mock_IndexHandler = Mock(spec=IndexHandler)
graph_handler = GraphHandler(
mock_SQLHandler,
mock_RDFHandler,
mock_IndexHandler,
kg_files_directory="./tests/Test_files/load_files/virtuoso_data/kg_files",
)
graph_handler.load_df(self.m4ml_example_dataframe)
return graph_handler
@pytest.fixture
def setup_graph_handler(
self, setup_sql_handler, setup_virtuoso_handler, setup_elasticsearch_handler
) -> GraphHandler:
# Initializing the database handlers
graph_handler = GraphHandler(
setup_sql_handler,
setup_virtuoso_handler,
setup_elasticsearch_handler,
kg_files_directory=setup_virtuoso_handler.kg_files_directory,
)
return graph_handler
def create_graph(self, source_file_path: str, graph_handler: GraphHandler):
df_example = pd.read_json(source_file_path)
graph_handler.load_df(df_example)
graph_handler.update_graph()
def assert_sql_db_state(
self,
expected_triplets: int,
expected_models: int,
expected_ranges: int,
expected_extraction_info: int,
graph_handler: GraphHandler,
expected_deprecated: int = 0,
print_df: bool = False,
):
triplets_df = graph_handler.SQLHandler.query('SELECT * FROM "Triplet"')
ranges_df = graph_handler.SQLHandler.query('SELECT * FROM "Version_Range"')
extraction_info_df = graph_handler.SQLHandler.query(
'SELECT * FROM "Triplet_Extraction_Info"'
)
if print_df:
print("TRIPlETS\n", triplets_df)
print("RANGES\n", ranges_df)
print("EXTRACTION_INFO\n", extraction_info_df)
assert len(extraction_info_df) == expected_extraction_info
assert len(triplets_df) == expected_triplets
assert len(ranges_df) == expected_ranges
assert len(triplets_df["subject"].unique()) == expected_models
assert len(ranges_df[ranges_df["deprecated"] == True]) == expected_deprecated
def assert_virtuoso_db_state(
self,
expected_triplets: int,
expected_models: int,
graph_handler: GraphHandler,
print_graph=False,
):
result_graph = graph_handler.RDFHandler.query(
"http://virtuoso:8890/sparql",
"""CONSTRUCT { ?s ?p ?o } WHERE {GRAPH <http://example.com/data_1> {?s ?p ?o}}""",
)
# Check there are the number of expected models
# print the result_graph
if print_graph:
for i, (s, p, o) in enumerate(result_graph):
print(f"{i}: {s} {p} {o}")
assert len(result_graph) == expected_triplets
result_count = result_graph.query(
"""SELECT (COUNT(DISTINCT ?s) AS ?count) WHERE{?s ?p ?o}"""
)
for triple in result_count:
assert triple.asdict()["count"]._value == expected_models
# assert result_count[0]["count"] == expected_models
# assert
def assert_elasticsearch_state(
self,
expected_models: int,
graph_handler: GraphHandler,
print_info=False,
):
graph_handler.IndexHandler.es.indices.refresh(index="test_hf_models")
result = graph_handler.IndexHandler.es.search(
index="test_hf_models",
body={"query": {"match_all": {}}},
)
print("Check Elasticsearch: ", result, "\n")
result_count = result["hits"]["total"]["value"]
assert result_count == expected_models
def assert_dbs_states(
self,
expected_triplets: int,
expected_models: int,
expected_ranges: int,
expected_extraction_info: int,
graph_handler: GraphHandler,
expected_deprecated: int = 0,
print_df: bool = False,
):
self.assert_sql_db_state(
expected_triplets,
expected_models,
expected_ranges,
expected_extraction_info,
graph_handler,
expected_deprecated,
print_df,
)
self.assert_virtuoso_db_state(
expected_triplets - expected_deprecated, expected_models, graph_handler
)
self.assert_elasticsearch_state(expected_models, graph_handler)
def test_one_new_triplet_creation(self, setup_graph_handler: GraphHandler):
graph_handler = setup_graph_handler
extraction_info = {
"extraction_method": "Parsed_from_HF_dataset",
"confidence": 1.0,
"extraction_time": "2024-08-15_09-08-26",
}
subject = URIRef("subject")
predicate = URIRef("predicate")
object = URIRef("object")
graph_handler.process_triplet(subject, predicate, object, extraction_info)
# Ensure that the triplet was created
new_triplet_df = graph_handler.SQLHandler.query(
f"""
SELECT * FROM \"Triplet\"
WHERE subject = '{str(subject.n3())}'
AND predicate = '{str(predicate.n3())}'
AND object = '{str(object.n3())}'
"""
)
assert len(new_triplet_df) == 1
# print(new_triplet_df)
new_triplet_id = new_triplet_df.iloc[0]["id"]
# Check if a new extraction_info was created
new_extraction_info_df = graph_handler.SQLHandler.query(
"SELECT * FROM \"Triplet_Extraction_Info\" WHERE method_description='Parsed_from_HF_dataset' AND extraction_confidence=1.0"
)
assert len(new_extraction_info_df) == 1
new_extraction_info_id = new_extraction_info_df.iloc[0]["id"]
# Check if a new version_range was created
new_version_range_df = graph_handler.SQLHandler.query(
f"""SELECT * FROM \"Version_Range\" WHERE
triplet_id = '{new_triplet_id}'
AND extraction_info_id = '{new_extraction_info_id}'"""
)
assert len(new_version_range_df) == 1
assert new_version_range_df.iloc[0]["use_start"] == Timestamp(
"2024-08-15 09:08:26"
)
assert new_version_range_df.iloc[0]["use_end"] == Timestamp(
"2024-08-15 09:08:26"
)
assert new_version_range_df.iloc[0]["deprecated"] == False
# print(new_version_range_df)
def test_small_graph_creation(self, setup_graph_handler: GraphHandler):
graph_handler = setup_graph_handler
# Read dataframe from json file
self.create_graph(
source_file_path="./tests/Test_files/load_files/hf_transformed_fair4ml_example_small_1.json",
graph_handler=graph_handler,
)
self.assert_dbs_states(
expected_triplets=16,
expected_models=2,
expected_ranges=16,
expected_extraction_info=2,
expected_deprecated=0,
graph_handler=graph_handler,
print_df=False,
)
def test_small_graph_update_same_models(self, setup_graph_handler: GraphHandler):
# The idea of this test is to evaluate if the graph is correctly updated when
# the same models are loaded again but with changes in their properties.
graph_handler = setup_graph_handler
self.create_graph(
source_file_path="./tests/Test_files/load_files/hf_transformed_fair4ml_example_small_1.json",
graph_handler=graph_handler,
)
self.assert_dbs_states(
expected_triplets=16,
expected_models=2,
expected_ranges=16,
expected_extraction_info=2,
expected_deprecated=0,
graph_handler=graph_handler,
print_df=False,
)
self.create_graph(
source_file_path="./tests/Test_files/load_files/hf_transformed_fair4ml_example_small_2.json",
graph_handler=graph_handler,
)
self.assert_dbs_states(
expected_triplets=19,
expected_models=2,
expected_ranges=20,
expected_extraction_info=3,
expected_deprecated=3,
graph_handler=graph_handler,
print_df=False,
)
def test_small_graph_add_new_models(self, setup_graph_handler: GraphHandler):
graph_handler = setup_graph_handler
self.create_graph(
source_file_path="./tests/Test_files/load_files/hf_transformed_fair4ml_example_small_1.json",
graph_handler=graph_handler,
)
self.assert_dbs_states(
expected_triplets=16,
expected_models=2,
expected_ranges=16,
expected_extraction_info=2,
expected_deprecated=0,
graph_handler=graph_handler,
print_df=False,
)
self.create_graph(
source_file_path="./tests/Test_files/load_files/hf_transformed_fair4ml_example_small_3.json",
graph_handler=graph_handler,
)
self.assert_dbs_states(
expected_triplets=23,
expected_models=3,
expected_ranges=23,
expected_extraction_info=2,
expected_deprecated=0,
graph_handler=graph_handler,
print_df=False,
)
def test_small_graph_update_and_add_new_models(
self, setup_graph_handler: GraphHandler
):
graph_handler = setup_graph_handler
self.create_graph(
source_file_path="./tests/Test_files/load_files/hf_transformed_fair4ml_example_small_1.json",
graph_handler=graph_handler,
)
self.assert_dbs_states(
expected_triplets=16,
expected_models=2,
expected_ranges=16,
expected_extraction_info=2,
expected_deprecated=0,
graph_handler=graph_handler,
print_df=False,
)
self.create_graph(
source_file_path="./tests/Test_files/load_files/hf_transformed_fair4ml_example_small_3.json",
graph_handler=graph_handler,
)
self.assert_dbs_states(
expected_triplets=23,
expected_models=3,
expected_ranges=23,
expected_extraction_info=2,
expected_deprecated=0,
graph_handler=graph_handler,
print_df=False,
)
self.create_graph(
source_file_path="./tests/Test_files/load_files/hf_transformed_fair4ml_example_small_2.json",
graph_handler=graph_handler,
)
self.assert_dbs_states(
expected_triplets=26,
expected_models=3,
expected_ranges=27,
expected_extraction_info=3,
expected_deprecated=0,
graph_handler=graph_handler,
print_df=False,
)
def test_triplet_deprecation(self, setup_graph_handler: GraphHandler):
graph_handler = setup_graph_handler
graph_handler.curr_update_date = datetime.strptime(
"2024-07-16_09-14-40", "%Y-%m-%d_%H-%M-%S"
)
graph_handler.process_triplet(
URIRef("http://example.com/model1"),
URIRef("http://example.com/property1"),
URIRef("http://example.com/object1"),
extraction_info={
"extraction_method": "Parsed_from_HF_dataset",
"confidence": 1.0,
"extraction_time": "2024-07-16_09-14-40",
},
)
self.assert_sql_db_state(
expected_triplets=1,
expected_models=1,
expected_ranges=1,
expected_extraction_info=1,
expected_deprecated=0,
graph_handler=graph_handler,
)
graph_handler.curr_update_date = datetime.strptime(
"2025-07-16_09-14-40", "%Y-%m-%d_%H-%M-%S"
)
graph_handler.process_triplet(
URIRef("http://example.com/model1"),
URIRef("http://example.com/property1"),
URIRef("http://example.com/object2"),
extraction_info={
"extraction_method": "Parsed_from_HF_dataset",
"confidence": 1.0,
"extraction_time": "2025-07-16_09-14-40",
},
)
graph_handler.deprecate_old_triplets(URIRef("http://example.com/model1"))
self.assert_sql_db_state(
expected_triplets=2,
expected_models=1,
expected_ranges=2,
expected_extraction_info=1,
expected_deprecated=1,
graph_handler=graph_handler,
print_df=False,
)
def test_small_graph_multiple_deprecations(self, setup_graph_handler: GraphHandler):
graph_handler = setup_graph_handler
self.create_graph(
source_file_path="./tests/Test_files/load_files/hf_transformed_fair4ml_example_small_1.json",
graph_handler=graph_handler,
)
self.assert_dbs_states(
expected_triplets=16,
expected_models=2,
expected_ranges=16,
expected_extraction_info=2,
expected_deprecated=0,
graph_handler=graph_handler,
print_df=False,
)
self.create_graph(
source_file_path="./tests/Test_files/load_files/hf_transformed_fair4ml_example_small_4.json",
graph_handler=graph_handler,
)
self.assert_dbs_states(
expected_triplets=16,
expected_models=2,
expected_ranges=16,
expected_extraction_info=2,
expected_deprecated=3,
graph_handler=graph_handler,
print_df=False,
)
def test_large_dataset(self, setup_graph_handler: GraphHandler):
graph_handler = setup_graph_handler
self.create_graph(
"./tests/Test_files/load_files/hf_transformed_fair4ml_example.json",
graph_handler,
)
# def test_malformed_input(self, setup_graph_handler: GraphHandler):
# graph_handler = setup_graph_handler
# with pytest.raises(ValueError):
# self.create_graph("./tests/Test_files/load_files/malformed_data.json", graph_handler)