def test_too_many_neighbours_triggers_bad_edge_exception(caplog): with pytest.raises(InvalidEdgeError) as e: graph_from_ascii(""" 1---------------3 | 2""") assert str(e.value) == """Too many nodes:
def test_bad_label_triggers_exception(caplog): with pytest.raises(InvalidEdgeError) as e: graph_from_ascii(""" n1 | n2--(label) | n3""") assert str(e.value) == """Too many nodes:
def test_converts_other_kind_of_escaped_down_obtuse_angle(): graph = graph_from_ascii(""" 1-------\\ \\ 2 """) assert set(graph.nodes()) == {"1", "2"} assert set(graph.edges()) == {("1", "2")}
def test_converts_the_other_kind_of_up_right_angle(): graph = graph_from_ascii(""" 1 | 2------- """) assert set(graph.nodes()) == {"1", "2"} assert set(graph.edges()) == {("1", "2")}
def test_converts_down_right_angle(): graph = graph_from_ascii(""" 1-------- | 2 """) assert set(graph.nodes()) == {"1", "2"} assert set(graph.edges()) == {("1", "2")}
def test_line_labels(): graph = graph_from_ascii(""" A---(nuts)----B----(string)---C | | | D---(string)----E """) assert set(graph.nodes()) == {"A", "B", "C", "D", "E"} assert set(graph.edges()) == {("A", "B"), ("B", "C"), ("B", "D"), ("D", "E")} assert graph.get_edge_data("A", "B")["label"] == "nuts" assert graph.get_edge_data("A", "B")["length"] == 13 assert graph.get_edge_data("B", "C")["label"] == "string" assert graph.get_edge_data("B", "C")["length"] == 15 with pytest.raises(KeyError): assert graph.get_edge_data("B", "D")["label"] == "" assert graph.get_edge_data("B", "D")["length"] == 3 assert graph.get_edge_data("D", "E")["label"] == "string" assert graph.get_edge_data("D", "E")["length"] == 15
def test_converts_other_kind_of_down_left_angle(): graph = graph_from_ascii(""" 1 | ---2 """) assert set(graph.nodes()) == {"1", "2"} assert set(graph.edges()) == {("1", "2")}
def test_S_bend(): graph = graph_from_ascii(""" 0----- | |---2 """) assert set(graph.nodes()) == {"0", "2"} assert set(graph.edges()) == {("0", "2")}
def test_converts_meshed_network(): graph = graph_from_ascii(""" 0----1-------2 \ / 3---4 """) assert set(graph.nodes()) == {"0", "1", "2", "3", "4"} assert set(graph.edges()) == {("0", "1"), ("1", "2"), ("1", "3"), ("2", "4"), ("3", "4")}
def test_node_ordering(): graph = graph_from_ascii(""" 0----3-------2 \ / 1---4 """) assert set(graph.nodes()) == {"0", "1", "2", "3", "4"} assert set(graph.edges()) == {("0", "3"), ("3", "2"), ("3", "1"), ("2", "4"), ("1", "4")}
def test_ascii_positions(): graph = graph_from_ascii(""" Node_1---Node2----- / Nald33 """) assert graph.node["Node_1"]["position"] == Point(x=16, y=1) assert graph.node["Node2"]["position"] == Point(x=25, y=1) assert graph.node["Nald33"]["position"] == Point(x=31, y=3)
def test_converts_left_down_angle(): graph = graph_from_ascii(""" ------2 | 1 """) assert set(graph.nodes()) == {"2", "1"} assert set(graph.edges()) == {("2", "1")}
def test_u_bend(): graph = graph_from_ascii(""" ------| | | 1 | 2------| """) assert set(graph.nodes()) == {"1", "2"} assert set(graph.edges()) == {("1", "2")}
def test_node_positions(): graph = graph_from_ascii( """ Node_1---Node2----- / Nald33 """) assert networkx.get_node_attributes(graph, "position")["Node_1"] == (16, 1) assert networkx.get_node_attributes(graph, "position")["Node2"] == (25, 1) assert networkx.get_node_attributes(graph, "position")["Nald33"] == (31, 3)
def test_doubl_down_left_angle(): graph = graph_from_ascii(""" ------2 | | -- 1---| """) assert set(graph.nodes()) == {"2", "1"} assert set(graph.edges()) == {("2", "1")}
def test_adjacent_edges(): graph = graph_from_ascii(""" a------b c----------d """) assert set(graph.nodes()) == {"a", "b", "c", "d"} assert set(graph.edges()) == { ("a", "b"), ("c", "d"), }
def test_node_position_attributes(): graph = graph_from_ascii(""" n0-------------n1----------n2 | | | n4 """) node_positions = networkx.get_node_attributes(graph, "position") assert node_positions == { "n0": (8, 2), "n1": (23, 2), "n2": (35, 2), "n4": (23, 6), }
def test_line_lengths(): edge_data = graph_from_ascii(""" <13> <10> n0-------------n1----------n2 | | <3> | n4 """).edges(data=True) assert list(edge_data) == [ ("n0", "n1", OrderedDict([("length", 13)])), ("n1", "n2", OrderedDict([("length", 10)])), ("n1", "n4", OrderedDict([("length", 3)])), ]
def test_vertical_line_adjacent_labels(): graph = graph_from_ascii(""" C A---D | (Vertical) | B """) assert set(graph.nodes()) == {"A", "B", "C", "D"} assert set(graph.edges()) == {("A", "D"), ("C", "B")} assert graph.get_edge_data("C", "B")["label"] == "Vertical" assert graph.get_edge_data("C", "B")["length"] == 3
def test_vertical_line_labels(label): graph = graph_from_ascii(""" A | ({label}) | B """.format(label=label)) assert set(graph.nodes()) == {"A", "B"} assert set(graph.edges()) == {("A", "B")} assert graph.get_edge_data("A", "B")["label"] == label assert graph.get_edge_data("A", "B")["length"] == 3
def test_line_lengths(): graph = graph_from_ascii(""" <13> <10> n0-------------n1----------n2 | | <3> | n4 """) lengths = networkx.get_edge_attributes(graph, "length") assert lengths == { ("n0", "n1"): 13, ("n1", "n2"): 10, ("n1", "n4"): 3, }
def test_line_positions_with_horizontal_label(): graph = graph_from_ascii(" n1---(label)--n2 ") points = networkx.get_edge_attributes(graph, 'points') assert points == { ("n1", "n2"): [ # positions of the first three dashes right of `n1` (4, 0), (5, 0), (6, 0), # positions of the characters in `(label)` (7, 0), (8, 0), (9, 0), (10, 0), (11, 0), (12, 0), (13, 0), # positions of the last 2 dashes left of `n2` (14, 0), (15, 0) ] }
def test_some_more_node_names(): graph = graph_from_ascii(""" s---p----1---nx / | | / | 0---f 6l-a c-- / | \--k / ua | 9e q \ | / \-r7z jud \ | m y \ | v-ow """) assert len(graph.nodes()) == 19 assert len(graph.edges()) == 19
def test_line_positions_with_vertical_label(): graph = graph_from_ascii(""" n1 | (label) | n2 """) assert networkx.get_edge_attributes(graph, 'points') == { ("n1", "n2"): [ (12, 3), # position of the '|' under `n1` (12, 4), # position of the 'b' in `label` (12, 5), # position of the '|' above `n2` ] }
def test_line_positions(): graph = graph_from_ascii(""" n1------ | | | n2 """) points = networkx.get_edge_attributes(graph, 'points') assert points == { ("n1", "n2"): [ (10, 2), (11, 2), (12, 2), (13, 2), (14, 2), (15, 2), (15, 3), (15, 4), (15, 5), ] }
def test_line_positions_when_line_is_split(): graph = graph_from_ascii(""" -------n1 | | | n2 """) points = networkx.get_edge_attributes(graph, 'points') assert points == { ("n1", "n2"): [ (21, 2), (20, 2), (19, 2), (18, 2), (17, 2), (16, 2), (15, 2), (15, 3), (15, 4), (15, 5), ] }
def test_line_positions_when_order_is_reversed(): graph = graph_from_ascii(""" ----- | | n2 n1""") points = networkx.get_edge_attributes(graph, 'points') assert points == { ("n2", "n1"): [ (15, 3), (15, 2), (16, 2), (17, 2), (18, 2), (19, 2), (19, 3), ] }
def test_converts_linear_network(): graph = graph_from_ascii(" A---B----C----D") assert set(graph.nodes()) == {"A", "B", "C", "D"} assert set(graph.edges()) == {("A", "B"), ("B", "C"), ("C", "D")}
def test_missing_end_node_raises_missing_end_node_exception(): with pytest.raises(TooFewNodesOnEdge): graph_from_ascii('1---')
def test_too_many_neighbours_triggers_bad_edge_exception(): with pytest.raises(TooManyNodesOnEdge): graph_from_ascii(""" 1---------------3 | 2 """)