Esempio n. 1
0
def test_write_json1():
    """
    Write a graph as JSON using JsonSink.
    """
    graph = get_graph()
    filename = os.path.join(TARGET_DIR, 'test_graph1.json')
    s = JsonSink(filename=filename)
    for n, data in graph.nodes(data=True):
        s.write_node(data)
    for u, v, k, data in graph.edges(data=True, keys=True):
        s.write_edge(data)
    s.finalize()
    assert os.path.exists(filename)
Esempio n. 2
0
def test_write_json2():
    """
    Write a graph as a compressed JSON using JsonSink.
    """
    graph = get_graph()
    filename = os.path.join(TARGET_DIR, 'test_graph2.json')
    s = JsonSink(filename=filename, compression='gz')
    for n, data in graph.nodes(data=True):
        s.write_node(data)
    for u, v, k, data in graph.edges(data=True, keys=True):
        s.write_edge(data)
    s.finalize()
    assert os.path.exists(f"{filename}.gz")
Esempio n. 3
0
def test_write_rdf3():
    """
    Write a graph as RDF N-Triples using RdfSink, where all edges are reified.
    """
    graph = get_graph()
    filename = os.path.join(TARGET_DIR, 'test_graph3.nt')

    s = RdfSink(filename=filename, reify_all_edges=True)
    for n, data in graph.nodes(data=True):
        s.write_node(data)
    for u, v, k, data in graph.edges(data=True, keys=True):
        s.write_edge(data)
    s.finalize()

    assert os.path.exists(filename)

    lines = open(filename, 'r').readlines()
    assert len(lines) == 42
Esempio n. 4
0
def test_write_rdf2():
    """
    Write a graph as a compressed RDF N-Triples using RdfSink.
    """
    graph = get_graph()
    filename = os.path.join(TARGET_DIR, 'test_graph2.nt.gz')

    s = RdfSink(filename=filename, compression=True)
    for n, data in graph.nodes(data=True):
        s.write_node(data)
    for u, v, k, data in graph.edges(data=True, keys=True):
        s.write_edge(data)
    s.finalize()

    assert os.path.exists(filename)

    lines = open(filename, 'r').readlines()
    assert len(lines) == 18
Esempio n. 5
0
def test_write_rdf1():
    """
    Write a graph as RDF N-Triples using RdfSink.
    """
    graph = get_graph()
    filename = os.path.join(TARGET_DIR, "test_graph1.nt")

    t = Transformer()
    s = RdfSink(owner=t, filename=filename)

    for n, data in graph.nodes(data=True):
        s.write_node(data)
    for u, v, k, data in graph.edges(data=True, keys=True):
        s.write_edge(data)
    s.finalize()

    assert os.path.exists(filename)

    lines = open(filename, "r").readlines()
    assert len(lines) == 18
Esempio n. 6
0
def test_write_jsonl1():
    """
    Write a graph as JSON Lines using JsonlSink.
    """
    graph = get_graph()
    filename = os.path.join(TARGET_DIR, 'test_graph1')
    s = JsonlSink(filename=filename)
    for n, data in graph.nodes(data=True):
        s.write_node(data)
    for u, v, k, data in graph.edges(data=True, keys=True):
        s.write_edge(data)
    s.finalize()
    del s
    assert os.path.exists(f"{filename}_nodes.jsonl")
    assert os.path.exists(f"{filename}_edges.jsonl")

    node_lines = open(f"{filename}_nodes.jsonl").readlines()
    edge_lines = open(f"{filename}_edges.jsonl").readlines()
    assert len(node_lines) == 6
    assert len(edge_lines) == 6
Esempio n. 7
0
def test_write_jsonl2():
    """
    Write a graph as compressed JSON Lines using JsonlSink.
    """
    graph = get_graph()
    filename = os.path.join(TARGET_DIR, 'test_graph2')
    s = JsonlSink(filename=filename, compression='gz')
    for n, data in graph.nodes(data=True):
        s.write_node(data)
    for u, v, k, data in graph.edges(data=True, keys=True):
        s.write_edge(data)
    s.finalize()
    del s
    assert os.path.exists(f"{filename}_nodes.jsonl.gz")
    assert os.path.exists(f"{filename}_edges.jsonl.gz")

    node_lines = gzip.open(f"{filename}_nodes.jsonl.gz", 'rb').readlines()
    edge_lines = gzip.open(f"{filename}_edges.jsonl.gz", 'rb').readlines()

    assert len(node_lines) == 6
    assert len(edge_lines) == 6