Ejemplo n.º 1
0
def test_NodeCollection():
    """Test node collection"""
    nodes = NodeCollection()

    assert len(nodes) == 0

    a = Node('a')
    nodes.add(a)

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

    nodes.add(('b', 'c'))

    print(nodes)
    assert len(nodes) == 3

    with pytest.raises(Exception):
        nodes.add('a')

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

    d = Node('d', color='blue')
    nodes.add(d)

    assert nodes['d']['color'] == 'blue'

    d['color'] = 'red'
    assert nodes['d']['color'] == 'red'

    nodes.add(['e', ('f', 'g'), ['h', 'i']])

    assert len(nodes) == 9

    nodes.remove(a)

    assert len(nodes) == 8
    assert a not in nodes

    nodes.remove('b')

    assert len(nodes) == 7
    assert 'b' not in nodes

    nodes.remove([('e', 'f', 'g'), 'h', ['i']])

    assert len(nodes) == 2
Ejemplo n.º 2
0
def test_NodeCollection_iter():
    """Test iter trough the node collection"""
    nodes = NodeCollection()
    nodes.add(['a', 'b', 'c', 'd'])

    for node in nodes:
        assert isinstance(node.uid, str)

    for uid, node in nodes.items():
        assert isinstance(uid, str)
        assert isinstance(node, Node)

    for uid in nodes.keys():
        assert isinstance(uid, str)

    for node in nodes.values():
        assert isinstance(node, Node)