|
| 1 | +import pytest |
| 2 | + |
| 3 | +from neo4j_viz import Node, VisualizationGraph |
| 4 | + |
| 5 | + |
| 6 | +def test_set_node_captions_from_property() -> None: |
| 7 | + """Test setting captions from a node property.""" |
| 8 | + nodes = [ |
| 9 | + Node(id="1", properties={"name": "Alice", "age": 30}), |
| 10 | + Node(id="2", properties={"name": "Bob", "age": 25}), |
| 11 | + Node(id="3", properties={"name": "Charlie", "age": 35}), |
| 12 | + ] |
| 13 | + VG = VisualizationGraph(nodes=nodes, relationships=[]) |
| 14 | + |
| 15 | + VG.set_node_captions(property="name") |
| 16 | + |
| 17 | + assert VG.nodes[0].caption == "Alice" |
| 18 | + assert VG.nodes[1].caption == "Bob" |
| 19 | + assert VG.nodes[2].caption == "Charlie" |
| 20 | + |
| 21 | + |
| 22 | +def test_set_node_captions_from_field() -> None: |
| 23 | + """Test setting captions from a node field.""" |
| 24 | + nodes = [ |
| 25 | + Node(id="node-1", properties={"name": "Alice"}), |
| 26 | + Node(id="node-2", properties={"name": "Bob"}), |
| 27 | + Node(id="node-3", properties={"name": "Charlie"}), |
| 28 | + ] |
| 29 | + VG = VisualizationGraph(nodes=nodes, relationships=[]) |
| 30 | + |
| 31 | + VG.set_node_captions(field="id") |
| 32 | + |
| 33 | + assert VG.nodes[0].caption == "node-1" |
| 34 | + assert VG.nodes[1].caption == "node-2" |
| 35 | + assert VG.nodes[2].caption == "node-3" |
| 36 | + |
| 37 | + |
| 38 | +def test_set_node_captions_override_true() -> None: |
| 39 | + """Test that override=True replaces existing captions.""" |
| 40 | + nodes = [ |
| 41 | + Node(id="1", caption="OldCaption1", properties={"name": "Alice"}), |
| 42 | + Node(id="2", caption="OldCaption2", properties={"name": "Bob"}), |
| 43 | + Node(id="3", properties={"name": "Charlie"}), |
| 44 | + ] |
| 45 | + VG = VisualizationGraph(nodes=nodes, relationships=[]) |
| 46 | + |
| 47 | + VG.set_node_captions(property="name", override=True) |
| 48 | + |
| 49 | + assert VG.nodes[0].caption == "Alice" |
| 50 | + assert VG.nodes[1].caption == "Bob" |
| 51 | + assert VG.nodes[2].caption == "Charlie" |
| 52 | + |
| 53 | + |
| 54 | +def test_set_node_captions_override_false() -> None: |
| 55 | + """Test that override=False preserves existing captions.""" |
| 56 | + nodes = [ |
| 57 | + Node(id="1", caption="ExistingCaption", properties={"name": "Alice"}), |
| 58 | + Node(id="2", properties={"name": "Bob"}), |
| 59 | + Node(id="3", caption="AnotherCaption", properties={"name": "Charlie"}), |
| 60 | + ] |
| 61 | + VG = VisualizationGraph(nodes=nodes, relationships=[]) |
| 62 | + |
| 63 | + VG.set_node_captions(property="name", override=False) |
| 64 | + |
| 65 | + assert VG.nodes[0].caption == "ExistingCaption" # Not overridden |
| 66 | + assert VG.nodes[1].caption == "Bob" # Set (was None) |
| 67 | + assert VG.nodes[2].caption == "AnotherCaption" # Not overridden |
| 68 | + |
| 69 | + |
| 70 | +def test_set_node_captions_missing_property() -> None: |
| 71 | + """Test behavior when property is missing from some nodes.""" |
| 72 | + nodes = [ |
| 73 | + Node(id="1", properties={"name": "Alice"}), |
| 74 | + Node(id="2", properties={"age": 25}), # No "name" property |
| 75 | + Node(id="3", properties={"name": "Charlie"}), |
| 76 | + ] |
| 77 | + VG = VisualizationGraph(nodes=nodes, relationships=[]) |
| 78 | + |
| 79 | + VG.set_node_captions(property="name") |
| 80 | + |
| 81 | + assert VG.nodes[0].caption == "Alice" |
| 82 | + assert VG.nodes[1].caption == "" # Empty string for missing property |
| 83 | + assert VG.nodes[2].caption == "Charlie" |
| 84 | + |
| 85 | + |
| 86 | +def test_set_node_captions_numeric_property() -> None: |
| 87 | + """Test setting captions from numeric properties.""" |
| 88 | + nodes = [ |
| 89 | + Node(id="1", properties={"score": 100}), |
| 90 | + Node(id="2", properties={"score": 200}), |
| 91 | + Node(id="3", properties={"score": 300}), |
| 92 | + ] |
| 93 | + VG = VisualizationGraph(nodes=nodes, relationships=[]) |
| 94 | + |
| 95 | + VG.set_node_captions(property="score") |
| 96 | + |
| 97 | + assert VG.nodes[0].caption == "100" |
| 98 | + assert VG.nodes[1].caption == "200" |
| 99 | + assert VG.nodes[2].caption == "300" |
| 100 | + |
| 101 | + |
| 102 | +def test_set_node_captions_field_and_property_both_provided() -> None: |
| 103 | + """Test that providing both field and property raises an error.""" |
| 104 | + nodes = [Node(id="1", properties={"name": "Alice"})] |
| 105 | + VG = VisualizationGraph(nodes=nodes, relationships=[]) |
| 106 | + |
| 107 | + with pytest.raises(ValueError, match="Exactly one of the arguments"): |
| 108 | + VG.set_node_captions(field="id", property="name") |
| 109 | + |
| 110 | + |
| 111 | +def test_set_node_captions_neither_field_nor_property() -> None: |
| 112 | + """Test that providing neither field nor property raises an error.""" |
| 113 | + nodes = [Node(id="1", properties={"name": "Alice"})] |
| 114 | + VG = VisualizationGraph(nodes=nodes, relationships=[]) |
| 115 | + |
| 116 | + with pytest.raises(ValueError, match="Exactly one of the arguments"): |
| 117 | + VG.set_node_captions() |
| 118 | + |
| 119 | + |
| 120 | +def test_set_node_captions_field_with_snake_case() -> None: |
| 121 | + """Test that field names are converted to snake_case.""" |
| 122 | + nodes = [ |
| 123 | + Node(id="1", caption_size=1, properties={"name": "Alice"}), |
| 124 | + Node(id="2", caption_size=2, properties={"name": "Bob"}), |
| 125 | + ] |
| 126 | + VG = VisualizationGraph(nodes=nodes, relationships=[]) |
| 127 | + |
| 128 | + VG.set_node_captions(field="captionSize") |
| 129 | + |
| 130 | + assert VG.nodes[0].caption == "1" |
| 131 | + assert VG.nodes[1].caption == "2" |
| 132 | + |
| 133 | + |
| 134 | +def test_set_node_captions_empty_graph() -> None: |
| 135 | + """Test setting captions on an empty graph.""" |
| 136 | + VG = VisualizationGraph(nodes=[], relationships=[]) |
| 137 | + |
| 138 | + # Should not raise an error |
| 139 | + VG.set_node_captions(property="name") |
| 140 | + |
| 141 | + assert len(VG.nodes) == 0 |
| 142 | + |
| 143 | + |
| 144 | +def test_set_node_captions_complex_property_values() -> None: |
| 145 | + """Test setting captions from properties with complex types.""" |
| 146 | + nodes = [ |
| 147 | + Node(id="1", properties={"tags": ["tag1", "tag2"]}), |
| 148 | + Node(id="2", properties={"metadata": {"key": "value"}}), |
| 149 | + Node(id="3", properties={"value": None}), |
| 150 | + ] |
| 151 | + VG = VisualizationGraph(nodes=nodes, relationships=[]) |
| 152 | + |
| 153 | + VG.set_node_captions(property="tags") |
| 154 | + assert VG.nodes[0].caption == "['tag1', 'tag2']" |
| 155 | + assert VG.nodes[1].caption == "" |
| 156 | + assert VG.nodes[2].caption == "" |
| 157 | + |
| 158 | + VG.set_node_captions(property="metadata") |
| 159 | + assert VG.nodes[0].caption == "" |
| 160 | + assert VG.nodes[1].caption == "{'key': 'value'}" |
| 161 | + assert VG.nodes[2].caption == "" |
0 commit comments