示例#1
0
def test_input_json_from_string_property_graph():
    class StringSupplier:
        def __init__(self, prefix):
            self._count = 0
            self._prefix = prefix

        def __call__(self):
            value = "{}{}".format(self._prefix, self._count)
            self._count += 1
            return value

    g = create_graph(
        directed=False,
        allowing_self_loops=False,
        allowing_multiple_edges=False,
        weighted=True,
        any_hashable=True,
        vertex_supplier=StringSupplier("v"),
        edge_supplier=StringSupplier("e"),
    )
    input_string = r'{"version":"1","nodes":[{"id":"5"},{"id":"7"}],"edges":[{"source":"5","target":"7"}]}'

    def import_id_cb(file_id):
        return "vertex-{}".format(file_id)

    parse_json(g, input_string, import_id_cb=import_id_cb)
    assert g.vertices == set(["vertex-5", "vertex-7"])
    assert g.edges == set(["e0"])
示例#2
0
def test_input_from_string_with_labels():
    g = create_graph(
        directed=False,
        allowing_self_loops=False,
        allowing_multiple_edges=False,
        weighted=True,
    )

    def import_id(file_id):
        return int(file_id)

    v_attrs = dict()
    e_attrs = dict()

    # test that you read back unescaped
    def va_cb(vertex, attribute_name, attribute_value):
        if vertex not in v_attrs:
            v_attrs[vertex] = {}
        v_attrs[vertex][attribute_name] = attribute_value

    def ea_cb(edge, attribute_name, attribute_value):
        if edge not in e_attrs:
            e_attrs[edge] = {}
        e_attrs[edge][attribute_name] = attribute_value

    parse_json(
        g, expected1_unescaped, import_id_cb=import_id, vertex_attribute_cb=va_cb
    )
    assert v_attrs[0]["label"] == "κόμβος 0"
    assert g.vertices == set([0])
    assert len(g.edges) == 0
示例#3
0
def test_input_json_from_string_create_new_vertices():
    g = create_graph(directed=False,
                     allowing_self_loops=False,
                     allowing_multiple_edges=False,
                     weighted=True)
    input_string = r'{"version":"1","nodes":[{"id":"5"},{"id":"7"}],"edges":[{"source":"5","target":"7"}]}'
    parse_json(g, input_string)
    assert g.vertices() == set([0, 1])
示例#4
0
def test_input_json_from_string_nocallbacks(tmpdir):

    g = create_graph(directed=False,
                     allowing_self_loops=False,
                     allowing_multiple_edges=False,
                     weighted=True)

    parse_json(g, expected)
    assert g.number_of_vertices() == 10
    assert g.number_of_edges() == 18
示例#5
0
def test_input_json_from_string_preserve_ids():
    g = create_graph(directed=False,
                     allowing_self_loops=False,
                     allowing_multiple_edges=False,
                     weighted=True)
    input_string = r'{"version":"1","nodes":[{"id":"5"},{"id":"7"}],"edges":[{"source":"5","target":"7"}]}'

    def import_id(file_id):
        return int(file_id)

    parse_json(g, input_string, import_id_cb=import_id)
    assert g.vertices() == set([5, 7])