示例#1
0
def test_default_node_attributes():
    graph = Graph(name='test', default_node_attributes={'a': 1, 'b': 2})
    for node in graph.nodes():
        assert graph.node_attribute(node, name='a') == 1
        assert graph.node_attribute(node, name='b') == 2
        graph.node_attribute(node, name='a', value=3)
        assert graph.node_attribute(node, name='a') == 3
示例#2
0
def test_graph_networkx_conversion():
    if compas.IPY:
        return

    g = Graph()
    g.attributes['name'] = 'DiGraph'
    g.attributes['val'] = (0, 0, 0)
    g.add_node(0)
    g.add_node(1, weight=1.2, height='test')
    g.add_node(2, x=1, y=1, z=0)

    g.add_edge(0, 1, attr_value=10)
    g.add_edge(1, 2)

    nxg = g.to_networkx()

    assert nxg.graph['name'] == 'DiGraph', "Graph attributes must be preserved"
    assert nxg.graph['val'] == (0, 0, 0), "Graph attributes must be preserved"
    assert set(nxg.nodes()) == set(g.nodes()), "Node sets must match"
    assert nxg.nodes[1]['weight'] == 1.2, "Node attributes must be preserved"
    assert nxg.nodes[1]['height'] == "test", "Node attributes must be preserved"
    assert nxg.nodes[2]['x'] == 1, "Node attributes must be preserved"

    assert set(nxg.edges()) == set(((0, 1), (1, 2))), "Edge sets must match"
    assert nxg.edges[0, 1]['attr_value'] == 10, "Edge attributes must be preserved"

    g2 = Graph.from_networkx(nxg)

    assert g.number_of_nodes() == g2.number_of_nodes()
    assert g.number_of_edges() == g2.number_of_edges()
    assert g2.edge_attribute((0, 1), 'attr_value') == 10
    assert g2.attributes['name'] == 'DiGraph', "Graph attributes must be preserved"
    assert g2.attributes['val'] == (0, 0, 0), "Graph attributes must be preserved"