|
2 | 2 | import os
|
3 | 3 | import unittest
|
4 | 4 |
|
| 5 | +from sys import version_info |
| 6 | + |
5 | 7 | import yaml
|
6 | 8 |
|
7 |
| -from owlrl import DeductiveClosure, RDFS_Semantics |
| 9 | +if version_info > (3, 4): |
| 10 | + from owlrl import DeductiveClosure, RDFS_Semantics |
8 | 11 |
|
9 | 12 | from rdflib import URIRef, Literal
|
10 | 13 | from rdflib.namespace import Namespace, RDF, RDFS, XSD
|
@@ -358,10 +361,11 @@ def test_rdf_custom_subclasses(self):
|
358 | 361 | doc = odml.Document()
|
359 | 362 | _ = odml.Section(name="test_subclassing", type=sub_class_type, parent=doc)
|
360 | 363 |
|
361 |
| - with self.assertWarns(UserWarning): |
362 |
| - rdf_writer = RDFWriter([doc], custom_subclasses=custom_class_dict) |
363 |
| - self.assertNotIn("odml:Cell", rdf_writer.get_rdf_str()) |
364 |
| - self.assertIn("odml:Neuron", rdf_writer.get_rdf_str()) |
| 364 | + if version_info > (3, 4): |
| 365 | + with self.assertWarns(UserWarning): |
| 366 | + rdf_writer = RDFWriter([doc], custom_subclasses=custom_class_dict) |
| 367 | + self.assertNotIn("odml:Cell", rdf_writer.get_rdf_str()) |
| 368 | + self.assertIn("odml:Neuron", rdf_writer.get_rdf_str()) |
365 | 369 |
|
366 | 370 | def test_rdf_subclassing_definitions(self):
|
367 | 371 | """
|
@@ -417,102 +421,103 @@ def test_rdf_subclassing_queries(self):
|
417 | 421 | Test the proper implementation of the RDF subclassing feature. Tests ensure, that queries
|
418 | 422 | relying on RDF Subclasses return appropriate results.
|
419 | 423 | """
|
420 |
| - namespace_map = {"odml": Namespace(ODML_NS), "rdf": RDF, "rdfs": RDFS} |
| 424 | + if version_info > (3, 4): |
| 425 | + namespace_map = {"odml": Namespace(ODML_NS), "rdf": RDF, "rdfs": RDFS} |
421 | 426 |
|
422 |
| - doc = odml.Document() |
423 |
| - _ = odml.Section(name="test_subclass", type="cell", parent=doc) |
424 |
| - _ = odml.Section(name="test_regular_class", type="regular", parent=doc) |
| 427 | + doc = odml.Document() |
| 428 | + _ = odml.Section(name="test_subclass", type="cell", parent=doc) |
| 429 | + _ = odml.Section(name="test_regular_class", type="regular", parent=doc) |
425 | 430 |
|
426 |
| - rdf_writer = RDFWriter([doc]) |
427 |
| - _ = rdf_writer.get_rdf_str() |
| 431 | + rdf_writer = RDFWriter([doc]) |
| 432 | + _ = rdf_writer.get_rdf_str() |
428 | 433 |
|
429 |
| - use_graph = rdf_writer.graph |
430 |
| - DeductiveClosure(RDFS_Semantics).expand(use_graph) |
| 434 | + use_graph = rdf_writer.graph |
| 435 | + DeductiveClosure(RDFS_Semantics).expand(use_graph) |
431 | 436 |
|
432 |
| - q_string = "SELECT * WHERE {?s rdf:type odml:Section .}" |
433 |
| - curr_query = prepareQuery(q_string, initNs=namespace_map) |
| 437 | + q_string = "SELECT * WHERE {?s rdf:type odml:Section .}" |
| 438 | + curr_query = prepareQuery(q_string, initNs=namespace_map) |
434 | 439 |
|
435 |
| - # Make sure the query finds two sections |
436 |
| - self.assertIs(len(use_graph.query(curr_query)), 2) |
| 440 | + # Make sure the query finds two sections |
| 441 | + self.assertIs(len(use_graph.query(curr_query)), 2) |
437 | 442 |
|
438 |
| - # Make sure the query finds |
439 |
| - result_section = [] |
440 |
| - for row in use_graph.query(curr_query): |
441 |
| - result_section.append(row.s) |
| 443 | + # Make sure the query finds |
| 444 | + result_section = [] |
| 445 | + for row in use_graph.query(curr_query): |
| 446 | + result_section.append(row.s) |
442 | 447 |
|
443 |
| - q_string = "SELECT * WHERE {?s rdf:type odml:Cell .}" |
444 |
| - curr_query = prepareQuery(q_string, initNs=namespace_map) |
| 448 | + q_string = "SELECT * WHERE {?s rdf:type odml:Cell .}" |
| 449 | + curr_query = prepareQuery(q_string, initNs=namespace_map) |
445 | 450 |
|
446 |
| - self.assertIs(len(use_graph.query(curr_query)), 1) |
447 |
| - for row in use_graph.query(curr_query): |
448 |
| - self.assertIn(row.s, result_section) |
| 451 | + self.assertIs(len(use_graph.query(curr_query)), 1) |
| 452 | + for row in use_graph.query(curr_query): |
| 453 | + self.assertIn(row.s, result_section) |
449 | 454 |
|
450 |
| - # -- Test custom subclassing queries |
451 |
| - type_custom_class = "species" |
452 |
| - type_overwrite_class = "cell" |
453 |
| - custom_class_dict = {type_custom_class: "Species", type_overwrite_class: "Neuron"} |
| 455 | + # -- Test custom subclassing queries |
| 456 | + type_custom_class = "species" |
| 457 | + type_overwrite_class = "cell" |
| 458 | + custom_class_dict = {type_custom_class: "Species", type_overwrite_class: "Neuron"} |
454 | 459 |
|
455 |
| - doc = odml.Document() |
456 |
| - sec = odml.Section(name="test_subclass", type="species", parent=doc) |
457 |
| - _ = odml.Section(name="test_subclass_overwrite", type="cell", parent=sec) |
458 |
| - _ = odml.Section(name="test_regular_class", type="regular", parent=sec) |
| 460 | + doc = odml.Document() |
| 461 | + sec = odml.Section(name="test_subclass", type="species", parent=doc) |
| 462 | + _ = odml.Section(name="test_subclass_overwrite", type="cell", parent=sec) |
| 463 | + _ = odml.Section(name="test_regular_class", type="regular", parent=sec) |
459 | 464 |
|
460 |
| - rdf_writer = RDFWriter([doc], custom_subclasses=custom_class_dict) |
461 |
| - _ = rdf_writer.get_rdf_str() |
| 465 | + rdf_writer = RDFWriter([doc], custom_subclasses=custom_class_dict) |
| 466 | + _ = rdf_writer.get_rdf_str() |
462 | 467 |
|
463 |
| - use_graph = rdf_writer.graph |
464 |
| - DeductiveClosure(RDFS_Semantics).expand(use_graph) |
| 468 | + use_graph = rdf_writer.graph |
| 469 | + DeductiveClosure(RDFS_Semantics).expand(use_graph) |
465 | 470 |
|
466 |
| - q_string = "SELECT * WHERE {?s rdf:type odml:Section .}" |
467 |
| - curr_query = prepareQuery(q_string, initNs=namespace_map) |
| 471 | + q_string = "SELECT * WHERE {?s rdf:type odml:Section .}" |
| 472 | + curr_query = prepareQuery(q_string, initNs=namespace_map) |
468 | 473 |
|
469 |
| - # Make sure the query finds three sections |
470 |
| - self.assertIs(len(use_graph.query(curr_query)), 3) |
| 474 | + # Make sure the query finds three sections |
| 475 | + self.assertIs(len(use_graph.query(curr_query)), 3) |
471 | 476 |
|
472 |
| - # Make sure the query finds |
473 |
| - result_section = [] |
474 |
| - for row in use_graph.query(curr_query): |
475 |
| - result_section.append(row.s) |
| 477 | + # Make sure the query finds |
| 478 | + result_section = [] |
| 479 | + for row in use_graph.query(curr_query): |
| 480 | + result_section.append(row.s) |
476 | 481 |
|
477 |
| - # Custom class 'Species' should be found. |
478 |
| - q_string = "SELECT * WHERE {?s rdf:type odml:Species .}" |
479 |
| - curr_query = prepareQuery(q_string, initNs=namespace_map) |
| 482 | + # Custom class 'Species' should be found. |
| 483 | + q_string = "SELECT * WHERE {?s rdf:type odml:Species .}" |
| 484 | + curr_query = prepareQuery(q_string, initNs=namespace_map) |
480 | 485 |
|
481 |
| - self.assertIs(len(use_graph.query(curr_query)), 1) |
482 |
| - for row in use_graph.query(curr_query): |
483 |
| - self.assertIn(row.s, result_section) |
| 486 | + self.assertIs(len(use_graph.query(curr_query)), 1) |
| 487 | + for row in use_graph.query(curr_query): |
| 488 | + self.assertIn(row.s, result_section) |
484 | 489 |
|
485 |
| - # Custom class 'Neuron' should be found. |
486 |
| - q_string = "SELECT * WHERE {?s rdf:type odml:Neuron .}" |
487 |
| - curr_query = prepareQuery(q_string, initNs=namespace_map) |
| 490 | + # Custom class 'Neuron' should be found. |
| 491 | + q_string = "SELECT * WHERE {?s rdf:type odml:Neuron .}" |
| 492 | + curr_query = prepareQuery(q_string, initNs=namespace_map) |
488 | 493 |
|
489 |
| - self.assertIs(len(use_graph.query(curr_query)), 1) |
490 |
| - for row in use_graph.query(curr_query): |
491 |
| - self.assertIn(row.s, result_section) |
| 494 | + self.assertIs(len(use_graph.query(curr_query)), 1) |
| 495 | + for row in use_graph.query(curr_query): |
| 496 | + self.assertIn(row.s, result_section) |
492 | 497 |
|
493 |
| - # Default class 'Cell' was replaced and should not return any result. |
494 |
| - q_string = "SELECT * WHERE {?s rdf:type odml:Cell .}" |
495 |
| - curr_query = prepareQuery(q_string, initNs=namespace_map) |
| 498 | + # Default class 'Cell' was replaced and should not return any result. |
| 499 | + q_string = "SELECT * WHERE {?s rdf:type odml:Cell .}" |
| 500 | + curr_query = prepareQuery(q_string, initNs=namespace_map) |
496 | 501 |
|
497 |
| - self.assertIs(len(use_graph.query(curr_query)), 0) |
| 502 | + self.assertIs(len(use_graph.query(curr_query)), 0) |
498 | 503 |
|
499 |
| - # -- Test inactivated subclassing |
500 |
| - doc = odml.Document() |
501 |
| - _ = odml.Section(name="test_regular_class", type="regular", parent=doc) |
502 |
| - _ = odml.Section(name="test_subclass", type="cell", parent=doc) |
| 504 | + # -- Test inactivated subclassing |
| 505 | + doc = odml.Document() |
| 506 | + _ = odml.Section(name="test_regular_class", type="regular", parent=doc) |
| 507 | + _ = odml.Section(name="test_subclass", type="cell", parent=doc) |
503 | 508 |
|
504 |
| - rdf_writer = RDFWriter([doc], rdf_subclassing=False) |
505 |
| - _ = rdf_writer.get_rdf_str() |
| 509 | + rdf_writer = RDFWriter([doc], rdf_subclassing=False) |
| 510 | + _ = rdf_writer.get_rdf_str() |
506 | 511 |
|
507 |
| - use_graph = rdf_writer.graph |
508 |
| - DeductiveClosure(RDFS_Semantics).expand(use_graph) |
| 512 | + use_graph = rdf_writer.graph |
| 513 | + DeductiveClosure(RDFS_Semantics).expand(use_graph) |
509 | 514 |
|
510 |
| - q_string = "SELECT * WHERE {?s rdf:type odml:Section .}" |
511 |
| - curr_query = prepareQuery(q_string, initNs=namespace_map) |
| 515 | + q_string = "SELECT * WHERE {?s rdf:type odml:Section .}" |
| 516 | + curr_query = prepareQuery(q_string, initNs=namespace_map) |
512 | 517 |
|
513 |
| - self.assertIs(len(use_graph.query(curr_query)), 2) |
| 518 | + self.assertIs(len(use_graph.query(curr_query)), 2) |
514 | 519 |
|
515 |
| - q_string = "SELECT * WHERE {?s rdf:type odml:Cell .}" |
516 |
| - curr_query = prepareQuery(q_string, initNs=namespace_map) |
| 520 | + q_string = "SELECT * WHERE {?s rdf:type odml:Cell .}" |
| 521 | + curr_query = prepareQuery(q_string, initNs=namespace_map) |
517 | 522 |
|
518 |
| - self.assertIs(len(use_graph.query(curr_query)), 0) |
| 523 | + self.assertIs(len(use_graph.query(curr_query)), 0) |
0 commit comments