|
| 1 | +"""#4859: extracted fact owns an ID over an inference that also spells it. |
| 2 | +
|
| 3 | +A doc beside a code folder (`pkg.md` beside `pkg/test_x.py`) can mint the code |
| 4 | +file's id. Both paths define the id, so the existing definer key ties and the |
| 5 | +shorter label used to decide the survivor. The doc then owned `source_file`, |
| 6 | +and the next doc re-extract deleted the code node. Provenance is the missing key. |
| 7 | +""" |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import copy |
| 11 | +import json |
| 12 | + |
| 13 | +import pytest |
| 14 | +from networkx.readwrite import json_graph |
| 15 | + |
| 16 | +from graphify.build import build_merge |
| 17 | +from graphify.dedup import deduplicate_entities |
| 18 | + |
| 19 | + |
| 20 | +CODE = { |
| 21 | + "id": "pkg_test_x", |
| 22 | + "label": "test_x.py", |
| 23 | + "file_type": "code", |
| 24 | + "source_file": "pkg/test_x.py", |
| 25 | + "source_location": "L1", |
| 26 | + "_origin": "ast", |
| 27 | +} |
| 28 | +CODE_METHOD = { |
| 29 | + "id": "pkg_test_x_run", |
| 30 | + "label": "run()", |
| 31 | + "file_type": "code", |
| 32 | + "source_file": "pkg/test_x.py", |
| 33 | + "source_location": "L3", |
| 34 | + "_origin": "ast", |
| 35 | +} |
| 36 | +DOC_NODE = { |
| 37 | + "id": "pkg_test_x", |
| 38 | + "label": "Tests", |
| 39 | + "file_type": "code", |
| 40 | + "source_file": "pkg.md", |
| 41 | + "_origin": "semantic", |
| 42 | +} |
| 43 | +DOC_OVERVIEW = { |
| 44 | + "id": "pkg_overview", |
| 45 | + "label": "Overview", |
| 46 | + "file_type": "document", |
| 47 | + "source_file": "pkg.md", |
| 48 | + "_origin": "semantic", |
| 49 | +} |
| 50 | +DOC_EDGE = { |
| 51 | + "source": "pkg_overview", |
| 52 | + "target": "pkg_test_x", |
| 53 | + "relation": "describes", |
| 54 | + "confidence": "EXTRACTED", |
| 55 | + "source_file": "pkg.md", |
| 56 | + "_origin": "semantic", |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize( |
| 61 | + "doc_label", |
| 62 | + ["Tests", "test_x.py", "test_x.py (tests)"], |
| 63 | + ids=["short-label", "equal-label", "long-label"], |
| 64 | +) |
| 65 | +@pytest.mark.parametrize("doc_first", [False, True], ids=["code-first", "doc-first"]) |
| 66 | +def test_ast_node_keeps_id_for_every_doc_label_length(doc_label, doc_first): |
| 67 | + doc = dict(copy.deepcopy(DOC_NODE), label=doc_label) |
| 68 | + code = copy.deepcopy(CODE) |
| 69 | + inputs = [doc, code] if doc_first else [code, doc] |
| 70 | + |
| 71 | + nodes, edges = deduplicate_entities( |
| 72 | + inputs, [copy.deepcopy(DOC_EDGE)], communities={} |
| 73 | + ) |
| 74 | + |
| 75 | + survivor = next(n for n in nodes if n["id"] == "pkg_test_x") |
| 76 | + assert survivor["source_file"] == "pkg/test_x.py" |
| 77 | + assert survivor["label"] == "test_x.py" |
| 78 | + assert survivor["_origin"] == "ast" |
| 79 | + assert any(e["target"] == "pkg_test_x" for e in edges) |
| 80 | + |
| 81 | + |
| 82 | +def test_ast_owner_survives_a_doc_reextract(tmp_path): |
| 83 | + graph_path = tmp_path / "graph.json" |
| 84 | + graph_path.write_text( |
| 85 | + json.dumps( |
| 86 | + { |
| 87 | + "directed": False, |
| 88 | + "multigraph": False, |
| 89 | + "graph": {}, |
| 90 | + "hyperedges": [], |
| 91 | + "nodes": [copy.deepcopy(CODE), copy.deepcopy(CODE_METHOD)], |
| 92 | + "links": [ |
| 93 | + { |
| 94 | + "source": "pkg_test_x", |
| 95 | + "target": "pkg_test_x_run", |
| 96 | + "relation": "contains", |
| 97 | + "confidence": "EXTRACTED", |
| 98 | + "source_file": "pkg/test_x.py", |
| 99 | + "_origin": "ast", |
| 100 | + } |
| 101 | + ], |
| 102 | + } |
| 103 | + ), |
| 104 | + encoding="utf-8", |
| 105 | + ) |
| 106 | + doc = { |
| 107 | + "nodes": [copy.deepcopy(DOC_NODE), copy.deepcopy(DOC_OVERVIEW)], |
| 108 | + "edges": [copy.deepcopy(DOC_EDGE)], |
| 109 | + "hyperedges": [], |
| 110 | + } |
| 111 | + |
| 112 | + merged = build_merge([doc], graph_path=graph_path) |
| 113 | + assert merged.nodes["pkg_test_x"]["source_file"] == "pkg/test_x.py" |
| 114 | + assert merged.nodes["pkg_test_x"]["label"] == "test_x.py" |
| 115 | + |
| 116 | + graph2 = tmp_path / "graph2.json" |
| 117 | + graph2.write_text( |
| 118 | + json.dumps(json_graph.node_link_data(merged, edges="links")), |
| 119 | + encoding="utf-8", |
| 120 | + ) |
| 121 | + doc_without_claim = { |
| 122 | + "nodes": [copy.deepcopy(DOC_OVERVIEW)], |
| 123 | + "edges": [], |
| 124 | + "hyperedges": [], |
| 125 | + } |
| 126 | + merged2 = build_merge([doc_without_claim], graph_path=graph2) |
| 127 | + assert "pkg_test_x" in merged2 |
| 128 | + assert merged2.nodes["pkg_test_x"]["source_file"] == "pkg/test_x.py" |
0 commit comments