|
| 1 | +"""Tests for the owl:deprecated term filter in the ontologies transform.""" |
| 2 | + |
| 3 | +import json |
| 4 | +from pathlib import Path |
| 5 | +from unittest import TestCase |
| 6 | + |
| 7 | +from kg_microbe.transform_utils.ontologies.ontologies_transform import OntologiesTransform |
| 8 | + |
| 9 | + |
| 10 | +def _make_obograph(): |
| 11 | + """Build a tiny obograph with one active node, two deprecated nodes, and edges.""" |
| 12 | + return { |
| 13 | + "graphs": [ |
| 14 | + { |
| 15 | + "id": "https://w3id.org/metpo/test.json", |
| 16 | + "nodes": [ |
| 17 | + {"id": "https://w3id.org/metpo/1000001", "lbl": "active term", "type": "CLASS"}, |
| 18 | + { |
| 19 | + "id": "https://w3id.org/metpo/0000001", |
| 20 | + "lbl": "obsolete term (basicPropertyValues)", |
| 21 | + "type": "CLASS", |
| 22 | + "meta": { |
| 23 | + "basicPropertyValues": [ |
| 24 | + {"pred": "http://www.w3.org/2002/07/owl#deprecated", "val": "true"} |
| 25 | + ] |
| 26 | + }, |
| 27 | + }, |
| 28 | + { |
| 29 | + "id": "https://w3id.org/metpo/0000002", |
| 30 | + "lbl": "obsolete term (meta.deprecated)", |
| 31 | + "type": "CLASS", |
| 32 | + "meta": {"deprecated": True}, |
| 33 | + }, |
| 34 | + ], |
| 35 | + "edges": [ |
| 36 | + # edge between two active-ish nodes (subject active, object active) -> kept |
| 37 | + { |
| 38 | + "sub": "https://w3id.org/metpo/1000001", |
| 39 | + "pred": "is_a", |
| 40 | + "obj": "https://w3id.org/metpo/1000001", |
| 41 | + }, |
| 42 | + # edge touching a deprecated node -> dropped |
| 43 | + { |
| 44 | + "sub": "https://w3id.org/metpo/1000001", |
| 45 | + "pred": "is_a", |
| 46 | + "obj": "https://w3id.org/metpo/0000001", |
| 47 | + }, |
| 48 | + ], |
| 49 | + } |
| 50 | + ] |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | +class TestDeprecatedFilter(TestCase): |
| 55 | + |
| 56 | + """Test owl:deprecated term removal from obograph JSON before KGX load.""" |
| 57 | + |
| 58 | + def setUp(self): |
| 59 | + """Instantiate the transform without running __init__ side effects.""" |
| 60 | + # The filter methods only rely on self._is_deprecated_node; bypass the |
| 61 | + # base Transform.__init__ (which sets up source dirs) to keep the test |
| 62 | + # isolated and side-effect free. |
| 63 | + self.transform = OntologiesTransform.__new__(OntologiesTransform) |
| 64 | + |
| 65 | + def test_is_deprecated_node_detects_both_encodings(self): |
| 66 | + """Both basicPropertyValues and meta.deprecated encodings are detected.""" |
| 67 | + graph = _make_obograph()["graphs"][0] |
| 68 | + active, dep_bpv, dep_meta = graph["nodes"] |
| 69 | + self.assertFalse(self.transform._is_deprecated_node(active)) |
| 70 | + self.assertTrue(self.transform._is_deprecated_node(dep_bpv)) |
| 71 | + self.assertTrue(self.transform._is_deprecated_node(dep_meta)) |
| 72 | + |
| 73 | + def test_drop_deprecated_terms_removes_nodes_and_dangling_edges(self): |
| 74 | + """Deprecated nodes and any edges touching them are removed in place.""" |
| 75 | + path = Path(self.tmp_json()) |
| 76 | + self.transform._drop_deprecated_terms(path) |
| 77 | + |
| 78 | + result = json.loads(path.read_text()) |
| 79 | + graph = result["graphs"][0] |
| 80 | + node_ids = {n["id"] for n in graph["nodes"]} |
| 81 | + |
| 82 | + # Only the active term survives. |
| 83 | + self.assertEqual(node_ids, {"https://w3id.org/metpo/1000001"}) |
| 84 | + # No node flagged deprecated remains. |
| 85 | + self.assertFalse(any(self.transform._is_deprecated_node(n) for n in graph["nodes"])) |
| 86 | + # The edge touching a deprecated node is gone; the active-only edge stays. |
| 87 | + self.assertEqual(len(graph["edges"]), 1) |
| 88 | + self.assertEqual(graph["edges"][0]["obj"], "https://w3id.org/metpo/1000001") |
| 89 | + |
| 90 | + def test_active_only_graph_is_untouched(self): |
| 91 | + """A graph with no deprecated terms is left unchanged (no rewrite).""" |
| 92 | + data = {"graphs": [{"nodes": [{"id": "https://w3id.org/metpo/1000001", "lbl": "x"}], "edges": []}]} |
| 93 | + path = Path(self.tmp_json(data)) |
| 94 | + before = path.read_text() |
| 95 | + self.transform._drop_deprecated_terms(path) |
| 96 | + self.assertEqual(path.read_text(), before) |
| 97 | + |
| 98 | + def tmp_json(self, data=None): |
| 99 | + """Write an obograph dict to a temp file and return its path.""" |
| 100 | + import tempfile |
| 101 | + |
| 102 | + if data is None: |
| 103 | + data = _make_obograph() |
| 104 | + fd = tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) |
| 105 | + json.dump(data, fd) |
| 106 | + fd.close() |
| 107 | + return fd.name |
0 commit comments