-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add ids to triples for easier manipulation (#21)
* add internal namespaces * add id to graph * add unit tests * format * save triple ids to vector * format * remove print
- Loading branch information
Showing
6 changed files
with
140 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from rdflib import Namespace | ||
|
||
TRIPLE_PROP = Namespace("triple:property:") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import pytest | ||
from rdflib import Graph, URIRef, Literal, BNode | ||
|
||
from memonto.utils.namespaces import TRIPLE_PROP | ||
from memonto.utils.rdf import serialize_graph_without_ids, hydrate_graph_with_ids | ||
|
||
|
||
@pytest.fixture | ||
def graph(): | ||
g = Graph() | ||
|
||
g.add( | ||
( | ||
URIRef("http://example.org/s1"), | ||
URIRef("http://example.org/p1"), | ||
URIRef("http://example.org/o1"), | ||
) | ||
) | ||
g.add( | ||
( | ||
URIRef("http://example.org/s2"), | ||
URIRef("http://example.org/p2"), | ||
URIRef("http://example.org/o2"), | ||
) | ||
) | ||
|
||
return g | ||
|
||
|
||
@pytest.fixture | ||
def bnode_graph(): | ||
g = Graph() | ||
|
||
g.add( | ||
( | ||
URIRef("http://example.org/s"), | ||
URIRef("http://example.org/p"), | ||
URIRef("http://example.org/o"), | ||
) | ||
) | ||
g.add((BNode(), TRIPLE_PROP.uuid, Literal("12345"))) | ||
|
||
return g | ||
|
||
|
||
def test_serialize_graph(bnode_graph): | ||
g = serialize_graph_without_ids(bnode_graph) | ||
|
||
assert "12345" not in g | ||
assert "s" in g | ||
|
||
|
||
def test_hydrate_graph_with_ids(graph): | ||
g = hydrate_graph_with_ids(graph) | ||
|
||
uuid_triples = [t for t in g if t[1] == TRIPLE_PROP.uuid] | ||
assert len(uuid_triples) == 2 | ||
assert all(isinstance(t[2], Literal) for t in uuid_triples) |