Esempio n. 1
0
def test_PathCollection_add_path():
    """Add path to the path collection."""
    a = Node('a')
    b = Node('b')
    c = Node('c')
    e = Edge(a, b, uid='e')
    f = Edge(b, c, uid='f')

    p1 = Path(e, f, uid='p1')
    p2 = Path(e, uid='p2')
    p3 = Path(a, uid='p3')

    paths = PathCollection()
    paths.add(p1)

    paths.add(p1)
    assert paths.counter['p1'] == 2

    assert len(paths.nodes) == 2
    # assert len(paths.edges) == 2
    assert len(paths) == 1
    assert p1 in paths

    paths = PathCollection()
    paths.add(p1, p2)

    assert p1 in paths
    assert p2 in paths
Esempio n. 2
0
def test_uid():
    """Test the uid assignment."""

    a = Node('a')
    b = Node('b')

    e = Edge(a, b, uid='e')

    assert isinstance(e, Edge)
    assert isinstance(e.uid, str)
    assert e.uid == 'e'

    a = Node('a')
    b = Node('b')

    e = Edge(a, b, 'e')

    assert isinstance(e, Edge)
    assert isinstance(e.uid, str)
    assert e.uid == 'e'

    a = Node()
    b = Node()

    e = Edge(a, b)

    assert isinstance(e, Edge)
    assert isinstance(e.uid, str)
    assert e.uid == hex(id(e))
Esempio n. 3
0
def test_PathCollection_remove_edges():
    """Remove edge path from the path collection."""
    a = Node('a')
    b = Node('b')
    c = Node('c')
    e = Edge(a, b, uid='e')
    f = Edge(b, c, uid='f')

    paths = PathCollection()
    paths.add(e, f, uid='p1')

    paths.remove(e, f)
    assert len(paths) == 0
    assert 'p1' not in paths

    paths.add(e, f, uid='p1')
    paths.remove('p1')
    assert len(paths) == 0

    paths.add(e, f, uid='p1')
    paths.remove(e, f)
    assert len(paths) == 0

    paths.add(e, f, uid='p1')
    paths.remove('e', 'f')
    assert len(paths) == 0
Esempio n. 4
0
def test_PathCollection_add_edges():
    """Add edge path to the path collection."""
    a = Node('a')
    b = Node('b')
    c = Node('c')
    e = Edge(a, b, uid='e')
    f = Edge(b, c, uid='f')

    paths = PathCollection()
    paths.add(e, f, uid='p1')

    assert len(paths.nodes) == 2
    #     assert len(paths.edges) == 2
    assert len(paths) == 1
    assert 'p1' in paths

    paths.add(e, f, uid='p1')
    assert paths.counter['p1'] == 2

    paths.add(e, f)
    assert paths.counter['p1'] == 3

    with pytest.raises(Exception):
        paths.add(e, f, uid='p2')

    assert paths.counter['p1'] == 3
def test_higher_order_node():
    """Test higher order nodes."""
    a = Node('a', color='azure')
    b = Node('b', color='blue')
    c = Node('c', color='cyan')

    ab = Edge(a, b, uid='a-b')
    bc = Edge(b, c, uid='b-c')

    abc = HigherOrderNode(ab, bc, uid='abc')

    nodes = HigherOrderNodeCollection()
    nodes.add(abc)

    assert nodes[abc] == abc
    assert nodes['abc'] == abc
    assert nodes[a, b, c] == abc
    assert nodes['a', 'b', 'c'] == abc
    assert nodes[ab, bc] == abc
    assert nodes['a-b', 'b-c'] == abc

    assert abc in nodes
    assert 'abc' in nodes
    assert (a, b, c) in nodes
    assert ('a', 'b', 'c') in nodes
    assert (ab, bc) in nodes
    assert ('a-b', 'b-c') in nodes
Esempio n. 6
0
def test_repr(nodes):
    """Test printing the node."""

    v, w = nodes

    vw = Edge(v, w, 'vw')

    assert vw.__repr__() == 'Edge vw'
Esempio n. 7
0
def test_path():
    """Test basic path"""
    a = Node('a')
    b = Node('b')
    c = Node('c')
    e = Edge(a, b)
    f = Edge(b, c)

    p = Path(e, f)
Esempio n. 8
0
def test_copy(nodes):
    """Test to make a copy of a node."""

    v, w = nodes
    vw = Edge(v, w, 'vw')
    ab = vw.copy()

    assert ab.uid == vw.uid == 'vw'

    # different objects
    assert ab != vw
Esempio n. 9
0
def test_update(nodes):
    """Test update node attributes."""
    v, w = nodes
    vw = Edge(v, w, length=5)

    assert vw['length'] == 5

    vw.update(length=10, capacity=6)

    assert vw['length'] == 10
    assert vw['capacity'] == 6
Esempio n. 10
0
def test_hash():
    """Test the hash of an edge"""
    a = Node('a')
    b = Node('b')
    c = Node('c')

    e1 = Edge(a, b)
    e2 = Edge(b, c)
    e3 = Edge(a, b)

    # different objects
    assert e1.__hash__() != e2.__hash__()

    # different objects but same uid
    assert e1.__hash__() != e3.__hash__()
def test_higher_order_edge_collection():
    """Test HigherOrderEdgeCollection."""

    a = Node('a', color='azure')
    b = Node('b', color='blue')
    c = Node('c', color='cyan')
    d = Node('d', color='desert')
    e = Node('e', color='desert')

    ab = Edge(a, b, uid='a-b')
    bc = Edge(b, c, uid='b-c')
    cd = Edge(c, d, uid='c-d')
    de = Edge(d, e, uid='d-e')

    abc = HigherOrderNode(ab, bc, uid='abc')
    bcd = HigherOrderNode(bc, cd, uid='bcd')
    cde = HigherOrderNode(cd, de, uid='cde')

    abc_bcd = HigherOrderEdge(abc, bcd, uid='abc-bcd')
    bcd_cde = HigherOrderEdge(bcd, cde, uid='bcd-cde')

    edges = HigherOrderEdgeCollection()

    edges.add(abc_bcd)

    assert edges[abc_bcd] == abc_bcd
    assert edges['abc-bcd'] == abc_bcd
    assert edges[abc, bcd] == abc_bcd
    assert edges['abc', 'bcd'] == abc_bcd
    assert edges[(a, b, c), (b, c, d)] == abc_bcd
    assert edges[('a', 'b', 'c'), ('b', 'c', 'd')] == abc_bcd
    assert edges[(ab, bc), (bc, cd)] == abc_bcd
    assert edges[('a-b', 'b-c'), ('b-c', 'c-d')] == abc_bcd

    # NotImplemented
    # edges['a','b','c','d'] # path node uids
    # edges['a-b','b-c','c-d'] # path edge uids
    # edges[a,b,c,d] # node objects
    # edges[ab,bc,cd] # edge objects

    assert abc_bcd in edges
    assert 'abc-bcd' in edges
    assert (abc, bcd) in edges
    assert ('abc', 'bcd') in edges
    assert ((a, b, c), (b, c, d)) in edges
    assert (('a', 'b', 'c'), ('b', 'c', 'd')) in edges
    assert ((ab, bc), (bc, cd)) in edges
    assert (('a-b', 'b-c'), ('b-c', 'c-d')) in edges
Esempio n. 12
0
def test_add_edges():
    """Test assigning edges form a list."""
    net = Network()
    ab = Edge(Node('a'), Node('b'))

    net.add_edges(ab, ('b', 'c'))

    assert net.number_of_edges() == 2

    net = Network()
    edges = [("A", "B"), ("B", "C")]
    net.add_edges(edges)

    assert net.number_of_edges() == 2
    assert net.number_of_nodes() == 3

    net = Network()
    edges = [("a", "b"),
             ("b", "c"),
             ("c", "d"),
             ("c", "e")]
    edges = [tuple(Node(x) for x in e) for e in edges]
    # with pytest.raises(Exception):
    #     net.add_edges(edges)

    net = Network()

    net.add_edges(('a', 'b'), ('b', 'c'))
def test_higher_order_edge():
    """Test higher order edges."""

    a = Node('a', color='azure')
    b = Node('b', color='blue')
    c = Node('c', color='cyan')
    d = Node('d', color='desert')

    ab = Edge(a, b, uid='a-b')
    bc = Edge(b, c, uid='b-c')
    cd = Edge(c, d, uid='c-d')

    abc = HigherOrderNode(ab, bc, uid='abc')
    bcd = HigherOrderNode(bc, cd, uid='bcd')

    abc_bcd = HigherOrderEdge(abc, bcd, uid='abc-bcd')
Esempio n. 14
0
def create_edges(pathpy=True, iterations=1000):
    """ Create nodes without attributes. """
    a = Node('a')
    b = Node('b')

    if pathpy:
        for i in range(iterations):
            e = Edge(a, b)
    else:
        for i in range(iterations):
            e = {}
            e.update(uid=None, v=a, w=b, nodes=set())
            e['nodes'].add(a)
            e['nodes'].add(b)

    return True
Esempio n. 15
0
def test_getitem(nodes):
    """Test the extraction of attributes."""

    v, w = nodes

    vw = Edge(v, w, length=10)

    assert vw['length'] == 10
    assert vw['attribute not in dict'] is None
Esempio n. 16
0
def test_setitem(nodes):
    """Test the assignment of attributes."""

    v, w = nodes

    vw = Edge(v, w)
    vw['capacity'] = 5.5

    assert vw['capacity'] == 5.5
Esempio n. 17
0
def test_multiedges():
    a = Node('a')
    b = Node('b')
    c = Node('c')
    d = Node('d')

    e1 = Edge(a, b, uid='a-b')
    e2 = Edge(a, b, uid='e2')
    e3 = Edge(c, d, uid='a-b')

    edges = EdgeCollection()
    edges.add(e1)

    # with pytest.raises(Exception):
    #     edges.add(e2)
    # with pytest.raises(Exception):
    #     edges.add(e3)

    edges = EdgeCollection(multiedges=True)
    edges.add(e1)
    edges.add(e2)
Esempio n. 18
0
def test_PathCollection_remove_path():
    """Remove path from the path collection."""
    a = Node('a')
    b = Node('b')
    c = Node('c')
    e = Edge(a, b, uid='e')
    f = Edge(b, c, uid='f')

    p1 = Path(e, f, uid='p1')
    p2 = Path(e, uid='p2')
    p3 = Path(a, uid='p3')

    paths = PathCollection()
    paths.add(p1)

    paths.remove(p1)

    assert len(paths.nodes) == 0
    # assert len(paths.edges) == 2
    assert len(paths) == 0
    assert p1 not in paths
Esempio n. 19
0
def test_EdgeSet():
    """Test edge sets"""
    edge = EdgeSet()

    a = Node('a')
    b = Node('b')
    e1 = Edge(a, b, uid='e1')
    e2 = Edge(a, b, uid='e2')
    e3 = Edge(a, b, uid='e3')

    edge.add(e1)
    edge.add(e2)
    edge.add(e3)

    assert len(edge) == 3
    assert e1 and e2 and e2 in edge
    assert 'e1' and 'e2' and 'e3' in edge
    assert edge[e1] == e1
    assert edge['e1'] == e1
    assert edge[0] == e1
    assert edge[-1] == e3
    assert edge[1:] == [e2, e3]

    edge['color'] = 'green'

    assert e1['color'] == 'green'
    assert e2['color'] == 'green'
    assert e3['color'] == 'green'

    edge['e1']['color'] = 'blue'
    edge[e2]['color'] = 'red'
    e3['color'] = 'orange'

    assert e1['color'] == 'blue'
    assert e2['color'] == 'red'
    assert edge[-1]['color'] == 'orange'
Esempio n. 20
0
def test_remove_edge():
    """Test to remove an edge from the network."""

    net = Network()
    a = Node('a')
    b = Node('b')
    c = Node('c')
    e = Edge(a, b, uid='e')
    f = Edge(b, a, uid='f')
    g = Edge(b, c, uid='g')
    net.add_edge(e)
    net.add_edge(f)

    net.remove_edge(g)

    assert net.number_of_edges() == 2
    assert isinstance(net.edges['e'], Edge)
    assert g not in net.edges
    assert net.edges['a', 'b'] in net.edges

    assert net.successors['a'] == {b}
    assert net.outgoing['a'] == {e}
    assert net.incident_edges['a'] == {e, f}

    net.remove_edge(e)

    assert net.number_of_edges() == 1
    assert net.successors['a'] == set()
    assert net.outgoing['a'] == set()
    assert net.incident_edges['a'] == {f}

    net.remove_edge('f')

    assert net.number_of_edges() == 0
    assert net.incident_edges['a'] == set()

    a = Node('a')
    b = Node('b')
    e = Edge(a, b, uid='e')
    f = Edge(a, b, uid='f')
    g = Edge(a, b, uid='g')

    net = Network(multiedges=True)
    net.add_edges(e, f, g)

    assert net.number_of_edges() == 3
    assert e and f and g in net.edges['a', 'b']

    net.remove_edge('a', 'b', uid='g')
    assert net.number_of_edges() == 2
    assert g not in net.edges['a', 'b']

    net.remove_edge('a', 'b')
    assert net.number_of_edges() == 0
    assert len(net.edges['a', 'b']) == 0
Esempio n. 21
0
def test_EdgeCollection_multiedges():
    """Test the EdgeCollection"""
    edges = EdgeCollection(multiedges=True)

    assert len(edges) == 0

    a = Node('a')
    b = Node('b')
    ab = Edge(a, b, uid='a-b')

    edges.add(ab)
    edges.add(a, b, uid='new')

    assert len(edges) == 2
    assert edges['a-b'] == ab
    assert len(edges['a', 'b']) == 2
    assert len(edges[a, b]) == 2
Esempio n. 22
0
def test_get_edge():
    """Test to get edges."""
    net = Network(directed=False)
    net.add_edge('a', 'b')
    assert (('a', 'b') in net.edges) is True
    assert (('b', 'a') in net.edges) is True
    assert (('a', 'c') in net.edges) is False

    a = Node('a')
    b = Node('b')
    e = Edge(a, b)
    net = Network(directed=True)
    net.add_edge(e)
    assert ((a, b) in net.edges) is True
    assert (e in net.edges) is True
    assert (('a', b) in net.edges) is True
    assert ((b, a) in net.edges) is False
Esempio n. 23
0
def test_repr(nodes):
    """Test printing the node."""

    v, w = nodes

    vw = Edge(v, w, 'vw')

    assert vw.__repr__() == 'Edge vw'

    vw = Edge(v, w)

    assert vw.__repr__().replace('>', '').split(' ')[-1] == vw.uid
Esempio n. 24
0
def test_weight(nodes):
    """Test the weight assigment."""

    v, w = nodes

    vw = Edge(v, w)

    assert vw.weight() == 1.0

    vw['weight'] = 4

    assert vw.weight() == 4.0
    assert vw.weight(weight=None) == 1.0
    assert vw.weight(weight=False) == 1.0

    vw['length'] = 5
    assert vw.weight('length') == 5.0
Esempio n. 25
0
def test_EdgeCollection():
    """Test the EdgeCollection"""
    edges = EdgeCollection(color='green')

    assert len(edges) == 0

    a = Node('a')
    b = Node('b')
    ab = Edge(a, b, uid='a-b')

    edges.add(ab)

    # with pytest.raises(Exception):
    #     edges.add(ab)

    assert len(edges) == 1
    assert edges['a-b'] == ab
    assert edges[ab] == ab
    assert 'a-b' in edges
    assert ab in edges
    assert 'a-b' in edges.uids
    assert 'a-b' in edges.keys()
    assert ab in edges.values()
    assert ('a-b', ab) in edges.items()

    assert len(edges.nodes) == 2
    assert edges.nodes['a'] == a
    assert edges.nodes[a.uid] == a
    assert 'a' in edges.nodes
    assert a in edges.nodes.values()
    # assert 'a' in edges.nodes.uids
    assert 'a' in edges.nodes.keys()
    assert a in edges.nodes.values()
    # assert ('a', a) in edges.nodes.items()

    # with pytest.raises(Exception):
    #     edges.add((a))

    c = Node('c')
    d = Node('d')

    edges.add(c, d, uid='c-d')

    assert len(edges) == 2
    assert edges['c-d'].v.uid == 'c'

    edges.add('e', 'f', uid='e-f')

    assert len(edges) == 3
    assert 'e' and 'f' in edges.nodes

    for _e in [('f', 'g'), ('g', 'h')]:
        edges.add(_e)

    assert len(edges) == 5

    # edges.add('e', nodes=False)

    #     assert len(edges) == 6
    #     assert 'e' in edges
    #     assert isinstance(edges['e'].v, Node)
    #     assert isinstance(edges['e'].w, Node)
    #     assert len(edges.nodes) == 10

    #     _v = edges['e'].v.uid
    #     _w = edges['e'].w.uid

    #     edges.remove('e')
    #     assert len(edges) == 5
    #     assert 'e' not in edges

    #     # edges._remove_node(_v)
    #     # edges._remove_node(_w)
    #     # assert len(edges.nodes) == 8

    edges.remove('g', 'h')
    edges.remove(('f', 'g'))

    assert len(edges) == 3

    edges.remove(ab)
    edges.remove('c-d')
    assert len(edges) == 1
    # assert len(edges.nodes) == 10

    edges = EdgeCollection()
    edges.add('a', 'b')

    # with pytest.raises(Exception):
    #     edges.add('a', 'b')

    edges = EdgeCollection()
    edges.add('a', 'b', uid='e1')
    edges.add('b', 'c', uid='e2')
    edges.add('c', 'd', uid='e3')
    edges.add('d', 'e', uid='e4')

    assert len(edges) == 4

    edges.remove('e1')
    assert len(edges) == 3

    for _e in ['e2', 'e3']:
        edges.remove(_e)

    assert len(edges) == 1
Esempio n. 26
0
def test_PathCollection():
    """Test the paths object"""
    a = Node('a')
    b = Node('b')
    c = Node('c')
    e = Edge(a, b, uid='e')
    f = Edge(b, c, uid='f')

    p1 = Path(e, f, uid='p1')
    p2 = Path(e, uid='p2')
    p3 = Path(a, uid='p3')

    paths = PathCollection()
    paths.add(p1)
    paths.add(p2)
    paths.add(p3)

    paths.add(p1)
    assert paths.counter['p1'] == 2

    assert len(paths.nodes) == 3
    # assert len(paths.edges) == 2
    assert len(paths) == 3
    assert p1 in paths
    assert p2 in paths
    assert p3 in paths

    assert 'p1' in paths
    assert 'p2' in paths
    assert 'p3' in paths

    assert (e, f) in paths
    assert ('e', 'f') in paths
    assert (e, ) in paths
    assert ('e', ) in paths
    assert (a, ) in paths
    assert ('a', ) in paths

    a = Node('a')
    b = Node('b')
    c = Node('c')

    p1 = Path(a, b, c, uid='p1')
    p2 = Path(a, b, uid='p2')
    p3 = Path(a, uid='p3')

    paths = PathCollection()
    paths.add(p1)
    paths.add(p2)
    paths.add(p3)

    assert (a, b, c) in paths
    assert ('a', 'b', 'c') in paths
    assert (a, b) in paths
    assert ('a', 'b') in paths

    assert (a, ) in paths
    assert ('a', ) in paths
    # assert [a] in paths
    # assert ['a'] in paths

    assert paths['a', 'b', 'c'] == p1
    assert paths['a', 'b'] == p2

    with pytest.raises(Exception):
        p = paths['x', 'y']

    p4 = Path(b, c, uid='p4')
    # with pytest.raises(Exception):
    paths.add(p4)
    assert paths.counter['p4'] == 1

    paths = PathCollection()
    paths.add(a, b)

    # with pytest.raises(Exception):
    paths.add(a, b)
    assert paths.counter[paths['a', 'b'].uid] == 2

    paths = PathCollection()
    paths.add('a', 'b', 'c', uid='a-b-c')

    assert len(paths) == 1
    assert 'a-b-c' in paths
    assert 'a' and 'b' and 'c' in paths.nodes

    paths = PathCollection()
    paths.add(p1, p2)

    assert len(paths) == 2

    paths = PathCollection()
    paths.add(('a', 'b', 'c'), ('a', 'b'))

    assert len(paths.nodes) == 3
    #assert len(paths.edges) == 2
    assert len(paths) == 2

    paths = PathCollection()
    paths.add(e, f, uid='p1')

    assert len(paths) == 1
    #assert len(paths.edges) == 2
    assert len(paths.nodes) == 2

    assert (e, f) in paths
    #assert ('a', 'b', 'c') in paths

    # with pytest.raises(Exception):
    paths.add(f, e, uid='p2')

    #     paths = PathCollection()
    #     paths.add('e1', uid='p1', nodes=False)

    #     assert len(paths) == 1
    #     assert len(paths.edges) == 1
    #     assert len(paths.nodes) == 2
    #     assert 'p1' in paths
    #     assert 'e1' in paths.edges

    #     paths = PathCollection()
    #     paths.add('e1', 'e2', uid='p1', nodes=False)

    #     assert len(paths) == 1
    #     assert len(paths.edges) == 2
    #     assert len(paths.nodes) == 3
    #     assert 'p1' in paths
    #     assert 'e1' and 'e2' in paths.edges

    #     assert paths.edges['e1'].w == paths.edges['e2'].v

    #     paths = PathCollection()
    #     paths.add(('e1', 'e2'), ('e3', 'e4'), nodes=False)

    #     assert len(paths.nodes) == 6
    #     assert len(paths.edges) == 4
    #     assert len(paths) == 2

    paths = PathCollection()
    paths.add(p1, p2, p3)

    assert len(paths.nodes) == 3
    assert len(paths) == 3

    paths.remove(p3)
    assert len(paths.nodes) == 3
    #assert len(paths.edges) == 2
    assert len(paths) == 2
    assert p3 not in paths

    paths.remove('p1')
    assert len(paths.nodes) == 2
    #assert len(paths.edges) == 2
    assert len(paths) == 1
    assert p1 not in paths

    paths = PathCollection()
    paths.add(('a', 'b', 'c'), ('a', 'b'))

    assert len(paths) == 2

    paths.remove('a', 'b')

    assert len(paths) == 1

    paths = PathCollection()
    paths.add(('a', 'b'), ('b', 'c'), ('c', 'd'))
    paths.remove(('a', 'b'), ('b', 'c'))

    assert len(paths) == 1
    assert ('a', 'b') not in paths
    assert ('b', 'c') not in paths
    assert ('c', 'd') in paths

    #     paths = PathCollection()
    #     paths.add(('e1', 'e2'), ('e2', 'e3'), ('e3', 'e4'), nodes=False)

    #     assert len(paths) == 3
    #     assert len(paths.edges) == 4

    #     paths.remove('e1', 'e2')
    #     assert len(paths) == 2

    #     paths.remove(('e2', 'e3'), ('e3', 'e4'))
    #     assert len(paths) == 0

    paths = PathCollection()
    paths.add('a', 'b', uid='p1')
    paths.add('b', 'c', uid='p2')
    paths.add('c', 'd', uid='p3')
    paths.add('d', 'e', uid='p4')

    assert len(paths) == 4

    paths.remove('p1')

    assert len(paths) == 3

    paths.remove(('p2', 'p3'))
Esempio n. 27
0
def test_add_edge():
    """Test the edge assignment."""

    a = Node('a')
    b = Node('b')
    c = Node('c')

    # add edges with no uids
    e = Edge(a, b)
    f = Edge(b, c)
    g = Edge(a, b)

    net = Network()
    net.add_edge(e)
    net.add_edge(f)
    with pytest.raises(Exception):
        net.add_edge(g)

    assert len(net.edges) == 2
    assert len(net.nodes) == 3

    with pytest.raises(Exception):
        net.add_node(a)

    with pytest.raises(Exception):
        net.add_edge(e)

    # add edges with uids
    e = Edge(a, b, uid='a-b')
    f = Edge(b, c, uid='b-c')
    g = Edge(a, b, uid='a-b')
    h = Edge(a, b, uid='ab')

    net = Network()
    net.add_edge(e)
    net.add_edge(f)

    with pytest.raises(Exception):
        net.add_edge(h)

    assert len(net.edges) == 2
    assert len(net.nodes) == 3

    with pytest.raises(Exception):
        net.add_edge(g)

    with pytest.raises(Exception):
        net.add_edge(e)

    # add edges and nodes
    net = Network()
    net.add_edge(e)

    # add new node with same uid
    with pytest.raises(Exception):
        net.add_node('a')

    # add same node
    with pytest.raises(Exception):
        net.add_node(a)

    # add node and edge with the node
    a1 = Node('a')
    a2 = Node('a')
    b = Node('b')
    e1 = Edge(a2, b)
    net = Network()
    net.add_node(a1)

    with pytest.raises(Exception):
        net.add_edge(e1)

    e2 = Edge(net.nodes['a'], b)
    net.add_edge(e2)

    # net add edge via string and nodes
    net = Network()
    net.add_node('a')
    net.add_node('b')
    net.add_edge('a', 'b')

    assert len(net.nodes) == 2
    assert len(net.edges) == 1

    with pytest.raises(Exception):
        net.add_edge('a', 'b')

    net = Network(multiedges=True)
    net.add_node('a')
    net.add_node('b')
    net.add_edge('a', 'b')

    assert len(net.nodes) == 2
    assert len(net.edges) == 1

    net.add_edge('a', 'b')

    assert len(net.nodes) == 2
    assert len(net.edges) == 2

    c = Node('c')

    net.add_edge('b', c)

    assert len(net.nodes) == 3
    assert len(net.edges) == 3

    a = Node('a')

    with pytest.raises(Exception):
        net.add_edge(a, 'b')

    with pytest.raises(Exception):
        net.add_edge(None)

    net = Network()
    net.add_edge('a', 'b', uid='a-b', length=10)

    assert net.number_of_nodes() == 2
    assert net.number_of_edges() == 1
    assert isinstance(net.edges['a-b'], Edge)
    assert net.edges['a-b'].uid == 'a-b'
    assert net.edges['a-b']['length'] == 10
    assert net.nodes['a'].uid == 'a'
    assert net.nodes['b'].uid == 'b'

    b = net.nodes['b']
    c = Node('c')
    net.add_edge(b, c, uid='c-d', length=5)

    assert net.number_of_edges() == 2

    net.add_edge('c', 'd', uid='c-2-d')

    assert net.number_of_edges() == 3
    assert net.edges['c-2-d'].v.uid == 'c'

    net.add_edge('a', 'd', uid='a-d')
    assert net.edges['a-d'].uid == 'a-d'

    ab = Edge(Node('a'), Node('b'), uid='a-b')
    net = Network()
    net.add_edge(ab, color='blue')

    assert net.edges['a-b']['color'] == 'blue'

    net = Network()
    net.add_node("A")
    net.add_edge("A", "B")

    assert net.number_of_edges() == 1
    assert net.number_of_nodes() == 2

    net = Network()
    edges = [("A", "B"), ("B", "C")]
    for edge in edges:
        net.add_edge(edge)

    assert net.number_of_edges() == 2
    assert net.number_of_nodes() == 3
Esempio n. 28
0
def test_errors():
    """Test some errors user can make"""
    with pytest.raises(Exception):
        e = Edge('a', 'b')
Esempio n. 29
0
def test_self_loop():
    """Test self loop as an edge."""
    v = Node()

    vv = Edge(v, v)
    assert len(vv.nodes) == 1
Esempio n. 30
0
def test_EdgeCollection():
    """Test the EdgeCollection"""
    edges = EdgeCollection()

    assert len(edges) == 0

    a = Node('a')
    b = Node('b')
    ab = Edge(a, b, uid='a-b')

    edges.add(ab)
    with pytest.raises(Exception):
        edges.add(ab)

    assert len(edges) == 1
    assert edges['a-b'] == ab
    assert edges[ab] == ab
    assert 'a-b' in edges
    assert ab in edges
    assert 'a-b' in edges.uids
    assert 'a-b' in edges.keys()
    assert ab in edges.values()
    assert ('a-b', ab) in edges.items()
    assert {'a-b': ab} == edges.dict

    assert len(edges.nodes) == 2
    assert edges.nodes['a'] == a
    assert edges.nodes[a] == a
    assert 'a' in edges.nodes
    assert a in edges.nodes
    assert 'a' in edges.nodes.uids
    assert 'a' in edges.nodes.keys()
    assert a in edges.nodes.values()
    assert ('a', a) in edges.nodes.items()
    assert {'a': a, 'b': b} == edges.nodes.dict

    with pytest.raises(Exception):
        edges.add((a))

    c = Node('c')
    d = Node('d')

    edges.add(c, d, uid='c-d')

    assert len(edges) == 2
    assert edges['c-d'].v == c

    edges.add('e', 'f', uid='e-f')

    assert len(edges) == 3
    assert 'e' and 'f' in edges.nodes

    for _e in [('f', 'g'), ('g', 'h')]:
        edges.add(_e)

    assert len(edges) == 5

    edges.add('e', nodes=False)

    assert len(edges) == 6
    assert 'e' in edges
    assert isinstance(edges['e'].v, Node)
    assert isinstance(edges['e'].w, Node)
    assert len(edges.nodes) == 10

    _v = edges['e'].v.uid
    _w = edges['e'].w.uid

    edges.remove('e')
    assert len(edges) == 5
    assert 'e' not in edges

    # edges._remove_node(_v)
    # edges._remove_node(_w)
    # assert len(edges.nodes) == 8

    edges.remove('g', 'h')
    edges.remove(('f', 'g'))

    assert len(edges) == 3

    edges.remove(ab, 'c-d')
    assert len(edges) == 2
    assert len(edges.nodes) == 10

    edges = EdgeCollection()
    edges.add('a', 'b')
    with pytest.raises(Exception):
        edges.add('a', 'b')

    edges = EdgeCollection()
    edges.add('a', 'b', uid='e1')
    edges.add('b', 'c', uid='e2')
    edges.add('c', 'd', uid='e3')
    edges.add('d', 'e', uid='e4')

    assert len(edges) == 4

    edges.remove('e1')
    assert len(edges) == 3

    for _e in ['e2', 'e3']:
        edges.remove(_e)

    assert len(edges) == 1