예제 #1
0
def test_duplicate_edges(mutable_edge_set: MutableEdgeSet):
    node1 = Node('1')
    node2 = Node('2')
    edge = Edge('e1', (node1, node2))
    edge2 = Edge('e1', (node1, node2))

    mutable_edge_set.add_edge(edge, edge2)

    assert edge in mutable_edge_set
    assert len(mutable_edge_set) == 1
예제 #2
0
def test_mutable_graph_delete_node_conflict_delete_nothing(
        mutable_graph: MutableGraph):
    last_num = 10
    common_node = Node(f'{last_num}')
    mutable_graph.add_node(common_node)

    edge_list = []
    for i in range(last_num):
        node = mutable_graph.add_node(f'{i}')
        edge_list.append(
            mutable_graph.add_edge(edge=Edge(i, (common_node, node))))

    assert len(edge_list) == len(mutable_graph.E)
    assert len(mutable_graph.V) == last_num + 1
    assert common_node in mutable_graph.V
    assert all(edge in mutable_graph.E for edge in edge_list)
    assert all(
        all(node in mutable_graph.V for node in edge) for edge in edge_list)

    mutable_graph.remove_node(common_node)

    assert len(edge_list) == len(mutable_graph.E)
    assert len(mutable_graph.V) == last_num + 1
    assert all(edge in mutable_graph.E for edge in edge_list)
    assert all(
        all(node in mutable_graph.V for node in edge) for edge in edge_list)
    assert common_node in mutable_graph.V
예제 #3
0
def test_mutable_graph_delete_node_no_conflict(mutable_graph: MutableGraph):
    node_num = 20

    edge_list = []
    for i in range(node_num // 2):
        node = mutable_graph.add_node(f'{i}')
        node_ = mutable_graph.add_node(f'{i}_{i}')
        edge_list.append(mutable_graph.add_edge(edge=Edge(i, (node_, node))))

    common_node = mutable_graph.add_node('11')

    assert all(edge in mutable_graph.E for edge in edge_list)
    assert all(
        all(node in mutable_graph.V for node in edge) for edge in edge_list)
    assert common_node in mutable_graph.V
    assert len(edge_list) == len(mutable_graph.E)
    assert len(mutable_graph.V) == node_num + 1

    mutable_graph.remove_node(common_node)

    assert len(edge_list) == len(mutable_graph.E)
    assert all(edge in mutable_graph.E for edge in edge_list)
    assert all(
        all(node in mutable_graph.V for node in edge) for edge in edge_list)
    assert common_node not in mutable_graph.V
    assert len(mutable_graph.V) == node_num
예제 #4
0
def test_mutable_graph_add_edge(mutable_graph: MutableGraph):
    n1 = mutable_graph.add_node('n1')
    n2 = mutable_graph.add_node('n2')

    mutable_graph.add_edge('1', ('n1', 'n2'))
    assert mutable_graph.has_edge('1')
    recorded_edge = mutable_graph.get_edge('1')
    assert recorded_edge.node_pair[0].identity == 'n1'
    assert recorded_edge.node_pair[1].identity == 'n2'
    # assert Edge('1', (Node('n1'), Node('n2'))) in mutable_graph
    assert len(mutable_graph.E) == 1

    mutable_graph.add_edge('2', (n1, n2))
    assert mutable_graph.has_edge('2')
    recorded_edge = mutable_graph.get_edge('2')
    assert recorded_edge.node_pair[0].identity == 'n1'
    assert recorded_edge.node_pair[1].identity == 'n2'
    # assert Edge('1', (Node('n1'), Node('n2'))) in mutable_graph
    assert len(mutable_graph.E) == 2

    mutable_graph.add_edge(edge=Edge('3', (n1, n2)))
    assert mutable_graph.has_edge('3')
    recorded_edge = mutable_graph.get_edge('3')
    assert recorded_edge.node_pair[0].identity == 'n1'
    assert recorded_edge.node_pair[1].identity == 'n2'
    # assert Edge('1', (Node('n1'), Node('n2'))) in mutable_graph
    assert len(mutable_graph.E) == 3
예제 #5
0
def test_mutable_edge_set_add_edge(mutable_edge_set):
    node1 = Node('1')
    node2 = Node('2')
    edge = Edge('e1', (node1, node2))
    mutable_edge_set.add_edge(edge)

    assert edge in mutable_edge_set
    assert not mutable_edge_set.is_empty()
예제 #6
0
def test_cy_id():
    start_cy_id = Edge._comparable_counter + 1
    edge_list = [
        Edge(str(i), (Node(str(i**i)), Node(str((i + 1) * (i - 1)))))
        for i in range(10)
    ]
    assert all(edge_list[i].cy_id == f'{Edge._PREFIX}_{start_cy_id + i }'
               for i in range(10))
예제 #7
0
def test_mutable_edge_set_remove_edge(mutable_edge_set: MutableEdgeSet):
    node1 = Node('1')
    node2 = Node('2')
    edge = Edge('e1', (node1, node2))
    mutable_edge_set.add_edge(edge)
    mutable_edge_set.remove_edge(edge)

    assert edge not in mutable_edge_set
    assert mutable_edge_set.is_empty()
예제 #8
0
def test_mutable_edge_set_add_edges(mutable_edge_set: MutableEdgeSet):
    edge_list = []

    for i in range(10):
        node1 = Node(f'{i}_{i}')
        node2 = Node(f'{i}')
        edge_list.append(Edge(f'e{i}', (node1, node2)))

    mutable_edge_set.add_edge(*edge_list)

    assert all(edge in mutable_edge_set for edge in edge_list)
    assert len(edge_list) == len(mutable_edge_set)
예제 #9
0
def test_mutable_edge_set_remove_edges(mutable_edge_set: MutableEdgeSet):
    edge_list = []

    for i in range(10):
        node2 = Node(f'{i}')
        node1 = Node(f'{i}_{i}')
        edge_list.append(Edge(f'e{i}', (node1, node2)))

    mutable_edge_set.add_edge(*edge_list)
    mutable_edge_set.remove_edge(*edge_list)

    assert all(edge not in mutable_edge_set for edge in edge_list)
    assert mutable_edge_set.is_empty()
예제 #10
0
def test_unresolvable_edge(mutable_graph: MutableGraph):
    n1 = Node('n1')
    n2 = Node('n2')
    edge = Edge('n1->n2', (n1, n2))
    with pytest.raises(ValueError):
        mutable_graph.add_edge(edge=edge)

    with pytest.raises(ValueError):
        mutable_graph.add_edge('n1->n2', (n1, n2))

    with pytest.raises(Exception):
        mutable_graph.add_edge('n1->n2', ('n1', 'n2'))

    with pytest.raises(TypeError):
        mutable_graph.add_edge('n1->n2', (True, False))
예제 #11
0
def test_mutable_graph_add_edge(mutable_graph: MutableGraph):
    mutable_graph.add_edge('1', ('n1', 'n2'))
    assert Edge('1', (Node('n1'), Node('n2'))) in mutable_graph
    assert len(mutable_graph.E) == 1
예제 #12
0
def test_simple_graph_parsing(simple_graph_js):
    simple_graph = Graph.graph_generator(simple_graph_js)

    assert len(simple_graph.edges) == 16

    assert Edge(0, (Node('n0'), Node('n1'))) in simple_graph
    assert Edge(1, (Node('n1'), Node('n2'))) in simple_graph
    assert Edge(2, (Node('n1'), Node('n3'))) in simple_graph
    assert Edge(3, (Node('n2'), Node('n7'))) in simple_graph
    assert Edge(4, (Node('n2'), Node('n11'))) in simple_graph
    assert Edge(5, (Node('n3'), Node('n4'))) in simple_graph
    assert Edge(6, (Node('n3'), Node('n16'))) in simple_graph
    assert Edge(7, (Node('n4'), Node('n5'))) in simple_graph
    assert Edge(8, (Node('n4'), Node('n6'))) in simple_graph
    assert Edge(9, (Node('n6'), Node('n8'))) in simple_graph
    assert Edge(10, (Node('n8'), Node('n9'))) in simple_graph
    assert Edge(11, (Node('n8'), Node('n10'))) in simple_graph
    assert Edge(12, (Node('n11'), Node('n12'))) in simple_graph
    assert Edge(13, (Node('n12'), Node('n13'))) in simple_graph
    assert Edge(14, (Node('n13'), Node('n14'))) in simple_graph
    assert Edge(15, (Node('n13'), Node('n15'))) in simple_graph

    assert len(simple_graph.nodes) == 17

    for i in range(17):
        assert Node('n%d' % i) in simple_graph
예제 #13
0
def test_process_variable_state(empty_recorder):
    var_id_string = empty_recorder.register_variable(('namespace', 'var'))

    class A:
        pass

    test_mapping = [
        # simple individuals
        1,
        Number,
        1.2,
        Number,
        'abc',
        str,
        Node('id'),
        Node,
        Edge('ide', (Node('id1'), Node('id2'))),
        Edge,
        # simple containers
        [1, 2],
        List,
        (3, 4),
        Tuple,
        deque([5, 6]),
        Deque,
        Counter([1, 2, 2, 3, 4, 5, 5, 5]),
        Counter,
        None,
        type(None),
        {6, 7, 8},
        Set,  # which includes Set, set, KeyView(dict_keys), ItemView(dict_items),
        # frozenset, MutableSet
        {
            6: 7,
            8: 9
        }.items(),
        Set,
        {
            6: 7,
            8: 9
        }.keys(),
        Set,
        {
            10: 11,
            12: 13
        },
        Mapping,  # which includes mappingproxy (not sure what that is), MutableMapping, dict
        range(1, 10),
        Sequence,  # which includes tuple, str, range, memoryview, MutableSequence, list, bytearray
        # wildcard
        A(),
        object
    ]

    for i in range(0, len(test_mapping), 2):
        state_info_mapping = empty_recorder.process_variable_state(
            var_id_string, test_mapping[i])

        assert state_info_mapping[
            empty_recorder._TYPE_HEADER] == empty_recorder._TYPE_MAPPING[
                test_mapping[i + 1]]
예제 #14
0
def gen_edge(uid, id1, id2):
    return Edge(uid, (Node(id1), Node(id2)))