Skip to content

Commit 657dfe9

Browse files
committed
Fix HIF roundtrip test expectation
1 parent a166042 commit 657dfe9

1 file changed

Lines changed: 57 additions & 35 deletions

File tree

easygraph/tests/test_hif.py

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
1-
import unittest
21
import json
3-
import os
42
import tempfile
5-
import easygraph as eg
3+
import unittest
4+
65
from pathlib import Path
76

7+
import easygraph as eg
8+
9+
810
MOCK_HIF_DATA = {
9-
"metadata": {
10-
"name": "test_organism",
11-
"description": "Simulation for unit test"
12-
},
11+
"metadata": {"name": "test_organism", "description": "Simulation for unit test"},
1312
"network-type": "directed",
1413
"nodes": [
1514
{"node": "n1", "weight": 1.0, "attrs": {"name": "Node A"}},
16-
{"node": "n2", "weight": 1.0, "attrs": {"name": "Node B"}}
17-
],
18-
"edges": [
19-
{"edge": "e1", "weight": 1.0, "attrs": {"name": "Edge Alpha"}}
15+
{"node": "n2", "weight": 1.0, "attrs": {"name": "Node B"}},
2016
],
17+
"edges": [{"edge": "e1", "weight": 1.0, "attrs": {"name": "Edge Alpha"}}],
2118
"incidences": [
2219
{"edge": "e1", "node": "n1", "weight": 1.0, "direction": "tail"},
23-
{"edge": "e1", "node": "n2", "weight": 1.0, "direction": "head"}
24-
]
20+
{"edge": "e1", "node": "n2", "weight": 1.0, "direction": "head"},
21+
],
2522
}
2623

24+
2725
class HIFTest(unittest.TestCase):
2826
def setUp(self):
2927
self.temp_dir = tempfile.TemporaryDirectory()
3028
self.temp_dir_path = Path(self.temp_dir.name)
31-
29+
3230
self.input_file = self.temp_dir_path / "input_mock.hif.json"
3331
self.output_file = self.temp_dir_path / "output_result.hif.json"
3432

@@ -44,52 +42,76 @@ def test_hif_roundtrip_preservation(self):
4442
EasyGraph object is structurally valid.
4543
"""
4644
hg = eg.hif_to_hypergraph(filename=self.input_file)
47-
45+
4846
self.assertEqual(hg.num_v, 2, "Loaded graph should have 2 nodes")
4947
self.assertEqual(hg.num_e, 1, "Loaded graph should have 1 edge")
50-
51-
node_names = [props.get('name') for props in hg.v_property]
52-
self.assertIn("n1", node_names, "Node ID 'n1' should be in v_property")
53-
48+
49+
node_names = [props.get("name") for props in hg.v_property]
50+
self.assertEqual(
51+
node_names,
52+
["Node A", "Node B"],
53+
"HIF attrs.name should be preserved in v_property",
54+
)
55+
56+
hif_node_ids = [node["node"] for node in hg.custom_hif_nodes]
57+
self.assertEqual(
58+
hif_node_ids,
59+
["n1", "n2"],
60+
"Original HIF node IDs should be preserved separately",
61+
)
62+
5463
edges = hg.e[0]
5564
self.assertEqual(len(edges), 1, "Should have 1 edge group")
5665
self.assertEqual(len(edges[0]), 2, "Edge e1 should connect 2 nodes")
57-
58-
self.assertTrue(hasattr(hg, "custom_hif_incidences"), "Failed to attach custom incidences")
66+
67+
self.assertTrue(
68+
hasattr(hg, "custom_hif_incidences"), "Failed to attach custom incidences"
69+
)
5970
self.assertTrue(hasattr(hg, "metadata"), "Failed to attach metadata")
6071

6172
eg.hypergraph_to_hif(hg, filename=self.output_file)
62-
63-
with open(self.output_file, 'r', encoding="utf-8") as f:
73+
74+
with open(self.output_file, "r", encoding="utf-8") as f:
6475
res = json.load(f)
65-
76+
77+
output_node_ids = [node["node"] for node in res["nodes"]]
78+
self.assertEqual(
79+
output_node_ids,
80+
["n1", "n2"],
81+
"Original HIF node IDs should survive roundtrip export",
82+
)
83+
6684
first_incidence = res["incidences"][0]
67-
self.assertIn("direction", first_incidence, "'direction' field lost in roundtrip")
85+
self.assertIn(
86+
"direction", first_incidence, "'direction' field lost in roundtrip"
87+
)
6888
self.assertIn(first_incidence["direction"], ["tail", "head"])
69-
70-
self.assertNotIn("default_attrs", res["metadata"], "'default_attrs' was forced into metadata")
89+
90+
self.assertNotIn(
91+
"default_attrs",
92+
res["metadata"],
93+
"'default_attrs' was forced into metadata",
94+
)
7195
self.assertEqual(res["metadata"]["name"], "test_organism")
7296

7397
def test_manual_graph_export(self):
7498
"""Test exporting a manually created Hypergraph (not loaded from file)."""
7599
hg = eg.Hypergraph(
76-
num_v=5,
77-
e_list=[(0, 1, 2), (2, 3), (2, 3), (0, 4)],
78-
merge_op="sum"
100+
num_v=5, e_list=[(0, 1, 2), (2, 3), (2, 3), (0, 4)], merge_op="sum"
79101
)
80102
hg.metadata = {"created_by": "manual_test"}
81103

82104
eg.hypergraph_to_hif(hg, filename=self.output_file)
83-
84-
with open(self.output_file, 'r', encoding="utf-8") as f:
105+
106+
with open(self.output_file, "r", encoding="utf-8") as f:
85107
data = json.load(f)
86108
self.assertEqual(len(data["nodes"]), 5)
87-
self.assertEqual(len(data["edges"]), 3)
109+
self.assertEqual(len(data["edges"]), 3)
88110
self.assertEqual(data["metadata"]["created_by"], "manual_test")
89-
90111

91112
weights = [e["weight"] for e in data["edges"]]
92113
self.assertIn(2.0, weights, "Merged edge weight should be 2.0")
93114

115+
94116
if __name__ == "__main__":
95-
unittest.main()
117+
unittest.main()

0 commit comments

Comments
 (0)