|
| 1 | +from collections.abc import MutableMapping |
| 2 | + |
| 3 | +from pytest import raises |
| 4 | + |
| 5 | +from igraph_ctypes.constructors import create_full_graph, create_famous_graph |
| 6 | + |
| 7 | + |
| 8 | +def test_new_graph_has_no_edge_attributes(): |
| 9 | + g = create_full_graph(4) |
| 10 | + assert isinstance(g.eattrs, MutableMapping) |
| 11 | + assert len(g.eattrs) == 0 |
| 12 | + |
| 13 | + |
| 14 | +def test_create_edge_attribute(): |
| 15 | + g = create_full_graph(4) |
| 16 | + |
| 17 | + g.eattrs.set("name", "test") |
| 18 | + assert list(g.eattrs["name"]) == ["test"] * 6 |
| 19 | + |
| 20 | + g.eattrs.set("weight", (5, 10, 15, 20, 25, 30)) |
| 21 | + assert list(g.eattrs["weight"]) == [5, 10, 15, 20, 25, 30] |
| 22 | + |
| 23 | + g.eattrs.set("cost", 1) |
| 24 | + assert list(g.eattrs["cost"]) == [1] * 6 |
| 25 | + |
| 26 | + with raises(RuntimeError, match="length must be"): |
| 27 | + g.eattrs.set("city", ("London", "Stockholm")) |
| 28 | + |
| 29 | + |
| 30 | +def test_copying_graph_copies_edge_attributes(): |
| 31 | + g = create_famous_graph("zachary") |
| 32 | + n = g.ecount() |
| 33 | + |
| 34 | + g.eattrs.set("index", list(range(n))) |
| 35 | + g.eattrs.set("name", [f"E{i}" for i in range(n)]) |
| 36 | + |
| 37 | + g2 = g.copy() |
| 38 | + assert sorted(g2.eattrs.keys()) == ["index", "name"] |
| 39 | + assert list(g2.eattrs["index"]) == list(range(n)) |
| 40 | + assert list(g2.eattrs["name"]) == [f"E{i}" for i in range(n)] |
| 41 | + |
| 42 | + del g2.eattrs["index"] |
| 43 | + assert "index" not in g2.eattrs |
| 44 | + assert list(g.eattrs["index"]) == list(range(n)) |
| 45 | + |
| 46 | + |
| 47 | +def test_adding_edges_extends_edge_attribute_vectors(): |
| 48 | + g = create_full_graph(4) |
| 49 | + |
| 50 | + g.eattrs.set("name", "test") |
| 51 | + g.eattrs.set("age", (5, 10, 15, 20, 25, 30)) |
| 52 | + |
| 53 | + g.add_vertices(1) |
| 54 | + g.add_edges([(0, 4), (2, 4), (3, 4)]) |
| 55 | + assert list(g.eattrs["name"]) == ["test"] * 6 + [""] * 3 |
| 56 | + assert list(g.eattrs["age"]) == [5, 10, 15, 20, 25, 30] + [0] * 3 |
| 57 | + |
| 58 | + |
| 59 | +def test_assigning_edge_attributes_between_graphs(): |
| 60 | + g = create_full_graph(4) |
| 61 | + g.eattrs.set("age", (5, 10, 15, 20, 25, 30)) |
| 62 | + |
| 63 | + g2 = create_full_graph(4) |
| 64 | + g2.eattrs["age"] = g.eattrs["age"] |
| 65 | + |
| 66 | + assert g2.eattrs["age"] == g.eattrs["age"] |
| 67 | + assert g2.eattrs["age"] is not g.eattrs["age"] |
| 68 | + |
| 69 | + g2 = create_full_graph(8) |
| 70 | + with raises(RuntimeError, match="length must be"): |
| 71 | + g2.eattrs["age"] = g.eattrs["age"] |
| 72 | + |
| 73 | + |
| 74 | +def test_shallow_copy(): |
| 75 | + g = create_full_graph(4) |
| 76 | + |
| 77 | + g.eattrs.set("name", "test") |
| 78 | + g.eattrs.set("age", (5, 10, 15, 20, 25, 30)) |
| 79 | + |
| 80 | + map = g.eattrs.copy() |
| 81 | + assert map["name"] == g.eattrs["name"] and map["name"] is not g.eattrs["name"] |
| 82 | + assert map["age"] == g.eattrs["age"] and map["age"] is not g.eattrs["age"] |
| 83 | + |
| 84 | + |
| 85 | +def test_attempt_to_change_attribute_type(): |
| 86 | + g = create_full_graph(4) |
| 87 | + g.eattrs.set("age", (5, 10, 15, 20, 25, 30)) |
| 88 | + |
| 89 | + with raises(ValueError, match="could not convert"): |
| 90 | + g.eattrs["age"][2] = "test" |
0 commit comments