mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-04-25 08:26:21 +02:00
Updated test suite for explainability & provenance (#696)
* Provenance tests * Embeddings tests * Test librarian * Test triples stream * Test concurrency * Entity centric graph writes * Agent tool service tests * Structured data tests * RDF tests * Addition LLM tests * Reliability tests
This commit is contained in:
parent
e6623fc915
commit
29b4300808
36 changed files with 8799 additions and 0 deletions
1
tests/unit/test_rdf/__init__.py
Normal file
1
tests/unit/test_rdf/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
|
||||
309
tests/unit/test_rdf/test_rdf_primitives.py
Normal file
309
tests/unit/test_rdf/test_rdf_primitives.py
Normal file
|
|
@ -0,0 +1,309 @@
|
|||
"""
|
||||
Tests for RDF 1.2 type system primitives: Term dataclass (IRI, blank node,
|
||||
typed literal, language-tagged literal, quoted triple), Triple/Quad dataclass
|
||||
with named graph support, and the knowledge/defs helper types.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from trustgraph.schema import Term, Triple, IRI, BLANK, LITERAL, TRIPLE
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Type constants
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestTypeConstants:
|
||||
|
||||
def test_iri_constant(self):
|
||||
assert IRI == "i"
|
||||
|
||||
def test_blank_constant(self):
|
||||
assert BLANK == "b"
|
||||
|
||||
def test_literal_constant(self):
|
||||
assert LITERAL == "l"
|
||||
|
||||
def test_triple_constant(self):
|
||||
assert TRIPLE == "t"
|
||||
|
||||
def test_constants_are_distinct(self):
|
||||
vals = {IRI, BLANK, LITERAL, TRIPLE}
|
||||
assert len(vals) == 4
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# IRI terms
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestIriTerm:
|
||||
|
||||
def test_create_iri(self):
|
||||
t = Term(type=IRI, iri="http://example.org/Alice")
|
||||
assert t.type == IRI
|
||||
assert t.iri == "http://example.org/Alice"
|
||||
|
||||
def test_iri_defaults_empty(self):
|
||||
t = Term(type=IRI)
|
||||
assert t.iri == ""
|
||||
|
||||
def test_iri_with_fragment(self):
|
||||
t = Term(type=IRI, iri="http://example.org/ontology#Person")
|
||||
assert "#Person" in t.iri
|
||||
|
||||
def test_iri_with_unicode(self):
|
||||
t = Term(type=IRI, iri="http://example.org/概念")
|
||||
assert "概念" in t.iri
|
||||
|
||||
def test_iri_other_fields_default(self):
|
||||
t = Term(type=IRI, iri="http://example.org/x")
|
||||
assert t.id == ""
|
||||
assert t.value == ""
|
||||
assert t.datatype == ""
|
||||
assert t.language == ""
|
||||
assert t.triple is None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Blank node terms
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestBlankNodeTerm:
|
||||
|
||||
def test_create_blank_node(self):
|
||||
t = Term(type=BLANK, id="_:b0")
|
||||
assert t.type == BLANK
|
||||
assert t.id == "_:b0"
|
||||
|
||||
def test_blank_node_defaults_empty(self):
|
||||
t = Term(type=BLANK)
|
||||
assert t.id == ""
|
||||
|
||||
def test_blank_node_arbitrary_id(self):
|
||||
t = Term(type=BLANK, id="node-abc-123")
|
||||
assert t.id == "node-abc-123"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Typed literals (XSD datatypes)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestTypedLiteral:
|
||||
|
||||
def test_plain_literal(self):
|
||||
t = Term(type=LITERAL, value="hello")
|
||||
assert t.type == LITERAL
|
||||
assert t.value == "hello"
|
||||
assert t.datatype == ""
|
||||
assert t.language == ""
|
||||
|
||||
def test_xsd_integer(self):
|
||||
t = Term(
|
||||
type=LITERAL, value="42",
|
||||
datatype="http://www.w3.org/2001/XMLSchema#integer",
|
||||
)
|
||||
assert t.value == "42"
|
||||
assert "integer" in t.datatype
|
||||
|
||||
def test_xsd_boolean(self):
|
||||
t = Term(
|
||||
type=LITERAL, value="true",
|
||||
datatype="http://www.w3.org/2001/XMLSchema#boolean",
|
||||
)
|
||||
assert t.datatype.endswith("#boolean")
|
||||
|
||||
def test_xsd_date(self):
|
||||
t = Term(
|
||||
type=LITERAL, value="2026-03-13",
|
||||
datatype="http://www.w3.org/2001/XMLSchema#date",
|
||||
)
|
||||
assert t.value == "2026-03-13"
|
||||
assert t.datatype.endswith("#date")
|
||||
|
||||
def test_xsd_double(self):
|
||||
t = Term(
|
||||
type=LITERAL, value="3.14",
|
||||
datatype="http://www.w3.org/2001/XMLSchema#double",
|
||||
)
|
||||
assert t.datatype.endswith("#double")
|
||||
|
||||
def test_empty_value_literal(self):
|
||||
t = Term(type=LITERAL, value="")
|
||||
assert t.value == ""
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Language-tagged literals
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestLanguageTaggedLiteral:
|
||||
|
||||
def test_english_tag(self):
|
||||
t = Term(type=LITERAL, value="hello", language="en")
|
||||
assert t.language == "en"
|
||||
assert t.datatype == ""
|
||||
|
||||
def test_french_tag(self):
|
||||
t = Term(type=LITERAL, value="bonjour", language="fr")
|
||||
assert t.language == "fr"
|
||||
|
||||
def test_bcp47_subtag(self):
|
||||
t = Term(type=LITERAL, value="colour", language="en-GB")
|
||||
assert t.language == "en-GB"
|
||||
|
||||
def test_language_and_datatype_mutually_exclusive(self):
|
||||
"""Both can be set on the dataclass, but semantically only one should be used."""
|
||||
t = Term(type=LITERAL, value="x", language="en",
|
||||
datatype="http://www.w3.org/2001/XMLSchema#string")
|
||||
# Dataclass allows both — translators should respect mutual exclusivity
|
||||
assert t.language == "en"
|
||||
assert t.datatype != ""
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Quoted triples (RDF-star)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestQuotedTriple:
|
||||
|
||||
def test_term_with_nested_triple(self):
|
||||
inner = Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/Alice"),
|
||||
p=Term(type=IRI, iri="http://xmlns.com/foaf/0.1/knows"),
|
||||
o=Term(type=IRI, iri="http://example.org/Bob"),
|
||||
)
|
||||
qt = Term(type=TRIPLE, triple=inner)
|
||||
assert qt.type == TRIPLE
|
||||
assert qt.triple is inner
|
||||
assert qt.triple.s.iri == "http://example.org/Alice"
|
||||
|
||||
def test_quoted_triple_as_object(self):
|
||||
"""A triple whose object is a quoted triple (RDF-star)."""
|
||||
inner = Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/Hope"),
|
||||
p=Term(type=IRI, iri="http://www.w3.org/2004/02/skos/core#definition"),
|
||||
o=Term(type=LITERAL, value="A feeling of expectation"),
|
||||
)
|
||||
outer = Triple(
|
||||
s=Term(type=IRI, iri="urn:subgraph:123"),
|
||||
p=Term(type=IRI, iri="http://trustgraph.ai/tg/contains"),
|
||||
o=Term(type=TRIPLE, triple=inner),
|
||||
)
|
||||
assert outer.o.type == TRIPLE
|
||||
assert outer.o.triple.o.value == "A feeling of expectation"
|
||||
|
||||
def test_quoted_triple_none(self):
|
||||
t = Term(type=TRIPLE, triple=None)
|
||||
assert t.triple is None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Triple / Quad (named graph)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestTripleQuad:
|
||||
|
||||
def test_default_graph_is_none(self):
|
||||
t = Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/s"),
|
||||
p=Term(type=IRI, iri="http://example.org/p"),
|
||||
o=Term(type=LITERAL, value="val"),
|
||||
)
|
||||
assert t.g is None
|
||||
|
||||
def test_named_graph(self):
|
||||
t = Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/s"),
|
||||
p=Term(type=IRI, iri="http://example.org/p"),
|
||||
o=Term(type=LITERAL, value="val"),
|
||||
g="urn:graph:source",
|
||||
)
|
||||
assert t.g == "urn:graph:source"
|
||||
|
||||
def test_empty_string_graph(self):
|
||||
t = Triple(g="")
|
||||
assert t.g == ""
|
||||
|
||||
def test_triple_with_all_none_terms(self):
|
||||
t = Triple()
|
||||
assert t.s is None
|
||||
assert t.p is None
|
||||
assert t.o is None
|
||||
assert t.g is None
|
||||
|
||||
def test_triple_equality(self):
|
||||
"""Dataclass equality based on field values."""
|
||||
t1 = Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/A"),
|
||||
p=Term(type=IRI, iri="http://example.org/B"),
|
||||
o=Term(type=LITERAL, value="C"),
|
||||
)
|
||||
t2 = Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/A"),
|
||||
p=Term(type=IRI, iri="http://example.org/B"),
|
||||
o=Term(type=LITERAL, value="C"),
|
||||
)
|
||||
assert t1 == t2
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# knowledge/defs helper types
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestKnowledgeDefs:
|
||||
|
||||
def test_uri_type(self):
|
||||
from trustgraph.knowledge.defs import Uri
|
||||
u = Uri("http://example.org/x")
|
||||
assert u.is_uri() is True
|
||||
assert u.is_literal() is False
|
||||
assert u.is_triple() is False
|
||||
assert str(u) == "http://example.org/x"
|
||||
|
||||
def test_literal_type(self):
|
||||
from trustgraph.knowledge.defs import Literal
|
||||
l = Literal("hello world")
|
||||
assert l.is_uri() is False
|
||||
assert l.is_literal() is True
|
||||
assert l.is_triple() is False
|
||||
assert str(l) == "hello world"
|
||||
|
||||
def test_quoted_triple_type(self):
|
||||
from trustgraph.knowledge.defs import QuotedTriple, Uri, Literal
|
||||
qt = QuotedTriple(
|
||||
s=Uri("http://example.org/s"),
|
||||
p=Uri("http://example.org/p"),
|
||||
o=Literal("val"),
|
||||
)
|
||||
assert qt.is_uri() is False
|
||||
assert qt.is_literal() is False
|
||||
assert qt.is_triple() is True
|
||||
assert qt.s == "http://example.org/s"
|
||||
assert qt.o == "val"
|
||||
|
||||
def test_quoted_triple_repr(self):
|
||||
from trustgraph.knowledge.defs import QuotedTriple, Uri, Literal
|
||||
qt = QuotedTriple(
|
||||
s=Uri("http://example.org/A"),
|
||||
p=Uri("http://example.org/B"),
|
||||
o=Literal("C"),
|
||||
)
|
||||
r = repr(qt)
|
||||
assert "<<" in r
|
||||
assert ">>" in r
|
||||
assert "http://example.org/A" in r
|
||||
|
||||
def test_quoted_triple_nested(self):
|
||||
"""QuotedTriple can contain another QuotedTriple as object."""
|
||||
from trustgraph.knowledge.defs import QuotedTriple, Uri, Literal
|
||||
inner = QuotedTriple(
|
||||
s=Uri("http://example.org/s"),
|
||||
p=Uri("http://example.org/p"),
|
||||
o=Literal("v"),
|
||||
)
|
||||
outer = QuotedTriple(
|
||||
s=Uri("http://example.org/s2"),
|
||||
p=Uri("http://example.org/p2"),
|
||||
o=inner,
|
||||
)
|
||||
assert outer.o.is_triple() is True
|
||||
217
tests/unit/test_rdf/test_rdf_storage_helpers.py
Normal file
217
tests/unit/test_rdf/test_rdf_storage_helpers.py
Normal file
|
|
@ -0,0 +1,217 @@
|
|||
"""
|
||||
Tests for RDF storage helper functions used by the Cassandra triple writer:
|
||||
serialize_triple, get_term_value, get_term_otype, get_term_dtype, get_term_lang.
|
||||
"""
|
||||
|
||||
import json
|
||||
import pytest
|
||||
|
||||
from trustgraph.schema import Term, Triple, IRI, BLANK, LITERAL, TRIPLE
|
||||
from trustgraph.storage.triples.cassandra.write import (
|
||||
serialize_triple,
|
||||
get_term_value,
|
||||
get_term_otype,
|
||||
get_term_dtype,
|
||||
get_term_lang,
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# get_term_otype — maps Term.type to storage object type code
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestGetTermOtype:
|
||||
|
||||
def test_iri_maps_to_u(self):
|
||||
assert get_term_otype(Term(type=IRI, iri="http://x")) == "u"
|
||||
|
||||
def test_blank_maps_to_u(self):
|
||||
assert get_term_otype(Term(type=BLANK, id="_:b0")) == "u"
|
||||
|
||||
def test_literal_maps_to_l(self):
|
||||
assert get_term_otype(Term(type=LITERAL, value="x")) == "l"
|
||||
|
||||
def test_triple_maps_to_t(self):
|
||||
assert get_term_otype(Term(type=TRIPLE)) == "t"
|
||||
|
||||
def test_none_defaults_to_u(self):
|
||||
assert get_term_otype(None) == "u"
|
||||
|
||||
def test_unknown_type_defaults_to_u(self):
|
||||
assert get_term_otype(Term(type="z")) == "u"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# get_term_dtype — extracts XSD datatype from literals
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestGetTermDtype:
|
||||
|
||||
def test_literal_with_datatype(self):
|
||||
t = Term(type=LITERAL, value="42",
|
||||
datatype="http://www.w3.org/2001/XMLSchema#integer")
|
||||
assert get_term_dtype(t) == "http://www.w3.org/2001/XMLSchema#integer"
|
||||
|
||||
def test_literal_without_datatype(self):
|
||||
t = Term(type=LITERAL, value="hello")
|
||||
assert get_term_dtype(t) == ""
|
||||
|
||||
def test_iri_returns_empty(self):
|
||||
assert get_term_dtype(Term(type=IRI, iri="http://x")) == ""
|
||||
|
||||
def test_none_returns_empty(self):
|
||||
assert get_term_dtype(None) == ""
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# get_term_lang — extracts language tag from literals
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestGetTermLang:
|
||||
|
||||
def test_literal_with_language(self):
|
||||
t = Term(type=LITERAL, value="bonjour", language="fr")
|
||||
assert get_term_lang(t) == "fr"
|
||||
|
||||
def test_literal_without_language(self):
|
||||
t = Term(type=LITERAL, value="hello")
|
||||
assert get_term_lang(t) == ""
|
||||
|
||||
def test_iri_returns_empty(self):
|
||||
assert get_term_lang(Term(type=IRI, iri="http://x")) == ""
|
||||
|
||||
def test_none_returns_empty(self):
|
||||
assert get_term_lang(None) == ""
|
||||
|
||||
def test_bcp47_subtag_preserved(self):
|
||||
t = Term(type=LITERAL, value="colour", language="en-GB")
|
||||
assert get_term_lang(t) == "en-GB"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# get_term_value — extracts string value from any Term
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestGetTermValue:
|
||||
|
||||
def test_iri_returns_iri(self):
|
||||
t = Term(type=IRI, iri="http://example.org/Alice")
|
||||
assert get_term_value(t) == "http://example.org/Alice"
|
||||
|
||||
def test_literal_returns_value(self):
|
||||
t = Term(type=LITERAL, value="hello")
|
||||
assert get_term_value(t) == "hello"
|
||||
|
||||
def test_blank_returns_id(self):
|
||||
t = Term(type=BLANK, id="_:b0")
|
||||
assert get_term_value(t) == "_:b0"
|
||||
|
||||
def test_none_returns_none(self):
|
||||
assert get_term_value(None) is None
|
||||
|
||||
def test_triple_returns_serialized_json(self):
|
||||
inner = Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/s"),
|
||||
p=Term(type=IRI, iri="http://example.org/p"),
|
||||
o=Term(type=LITERAL, value="val"),
|
||||
)
|
||||
t = Term(type=TRIPLE, triple=inner)
|
||||
result = get_term_value(t)
|
||||
parsed = json.loads(result)
|
||||
assert parsed["s"]["type"] == "i"
|
||||
assert parsed["s"]["iri"] == "http://example.org/s"
|
||||
assert parsed["o"]["value"] == "val"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# serialize_triple — full Triple → JSON serialization
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestSerializeTriple:
|
||||
|
||||
def test_serialize_iri_triple(self):
|
||||
t = Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/A"),
|
||||
p=Term(type=IRI, iri="http://example.org/rel"),
|
||||
o=Term(type=IRI, iri="http://example.org/B"),
|
||||
)
|
||||
result = json.loads(serialize_triple(t))
|
||||
assert result["s"]["type"] == "i"
|
||||
assert result["s"]["iri"] == "http://example.org/A"
|
||||
assert result["p"]["iri"] == "http://example.org/rel"
|
||||
assert result["o"]["type"] == "i"
|
||||
|
||||
def test_serialize_literal_object(self):
|
||||
t = Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/s"),
|
||||
p=Term(type=IRI, iri="http://example.org/p"),
|
||||
o=Term(type=LITERAL, value="hello"),
|
||||
)
|
||||
result = json.loads(serialize_triple(t))
|
||||
assert result["o"]["type"] == "l"
|
||||
assert result["o"]["value"] == "hello"
|
||||
|
||||
def test_serialize_typed_literal(self):
|
||||
t = Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/s"),
|
||||
p=Term(type=IRI, iri="http://example.org/p"),
|
||||
o=Term(type=LITERAL, value="42",
|
||||
datatype="http://www.w3.org/2001/XMLSchema#integer"),
|
||||
)
|
||||
result = json.loads(serialize_triple(t))
|
||||
assert result["o"]["datatype"] == "http://www.w3.org/2001/XMLSchema#integer"
|
||||
|
||||
def test_serialize_language_tagged_literal(self):
|
||||
t = Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/s"),
|
||||
p=Term(type=IRI, iri="http://example.org/p"),
|
||||
o=Term(type=LITERAL, value="bonjour", language="fr"),
|
||||
)
|
||||
result = json.loads(serialize_triple(t))
|
||||
assert result["o"]["language"] == "fr"
|
||||
|
||||
def test_serialize_blank_node(self):
|
||||
t = Triple(
|
||||
s=Term(type=BLANK, id="_:b0"),
|
||||
p=Term(type=IRI, iri="http://example.org/p"),
|
||||
o=Term(type=LITERAL, value="v"),
|
||||
)
|
||||
result = json.loads(serialize_triple(t))
|
||||
assert result["s"]["type"] == "b"
|
||||
assert result["s"]["id"] == "_:b0"
|
||||
|
||||
def test_serialize_nested_quoted_triple(self):
|
||||
inner = Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/inner-s"),
|
||||
p=Term(type=IRI, iri="http://example.org/inner-p"),
|
||||
o=Term(type=LITERAL, value="inner-val"),
|
||||
)
|
||||
outer = Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/outer-s"),
|
||||
p=Term(type=IRI, iri="http://example.org/outer-p"),
|
||||
o=Term(type=TRIPLE, triple=inner),
|
||||
)
|
||||
result = json.loads(serialize_triple(outer))
|
||||
nested = json.loads(result["o"]["triple"])
|
||||
assert nested["s"]["iri"] == "http://example.org/inner-s"
|
||||
assert nested["o"]["value"] == "inner-val"
|
||||
|
||||
def test_serialize_none_returns_none(self):
|
||||
assert serialize_triple(None) is None
|
||||
|
||||
def test_serialize_none_terms(self):
|
||||
t = Triple(s=None, p=None, o=None)
|
||||
result = json.loads(serialize_triple(t))
|
||||
assert result["s"] is None
|
||||
assert result["p"] is None
|
||||
assert result["o"] is None
|
||||
|
||||
def test_serialize_plain_literal_omits_datatype_and_language(self):
|
||||
t = Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/s"),
|
||||
p=Term(type=IRI, iri="http://example.org/p"),
|
||||
o=Term(type=LITERAL, value="plain"),
|
||||
)
|
||||
result = json.loads(serialize_triple(t))
|
||||
assert "datatype" not in result["o"]
|
||||
assert "language" not in result["o"]
|
||||
357
tests/unit/test_rdf/test_rdf_wire_format.py
Normal file
357
tests/unit/test_rdf/test_rdf_wire_format.py
Normal file
|
|
@ -0,0 +1,357 @@
|
|||
"""
|
||||
Tests for RDF wire format translators: TermTranslator and TripleTranslator
|
||||
round-trip encoding for all RDF 1.2 term types (IRI, blank node, typed literal,
|
||||
language-tagged literal, quoted triple) and named graph quads.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from trustgraph.schema import Term, Triple, IRI, BLANK, LITERAL, TRIPLE
|
||||
from trustgraph.messaging.translators.primitives import (
|
||||
TermTranslator, TripleTranslator, SubgraphTranslator,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def term_tx():
|
||||
return TermTranslator()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def triple_tx():
|
||||
return TripleTranslator()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# TermTranslator — IRI
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestTermTranslatorIri:
|
||||
|
||||
def test_iri_to_pulsar(self, term_tx):
|
||||
data = {"t": "i", "i": "http://example.org/Alice"}
|
||||
term = term_tx.to_pulsar(data)
|
||||
assert term.type == IRI
|
||||
assert term.iri == "http://example.org/Alice"
|
||||
|
||||
def test_iri_from_pulsar(self, term_tx):
|
||||
term = Term(type=IRI, iri="http://example.org/Bob")
|
||||
wire = term_tx.from_pulsar(term)
|
||||
assert wire == {"t": "i", "i": "http://example.org/Bob"}
|
||||
|
||||
def test_iri_round_trip(self, term_tx):
|
||||
original = Term(type=IRI, iri="http://example.org/round")
|
||||
wire = term_tx.from_pulsar(original)
|
||||
restored = term_tx.to_pulsar(wire)
|
||||
assert restored == original
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# TermTranslator — Blank node
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestTermTranslatorBlank:
|
||||
|
||||
def test_blank_to_pulsar(self, term_tx):
|
||||
data = {"t": "b", "d": "_:b42"}
|
||||
term = term_tx.to_pulsar(data)
|
||||
assert term.type == BLANK
|
||||
assert term.id == "_:b42"
|
||||
|
||||
def test_blank_from_pulsar(self, term_tx):
|
||||
term = Term(type=BLANK, id="_:node1")
|
||||
wire = term_tx.from_pulsar(term)
|
||||
assert wire == {"t": "b", "d": "_:node1"}
|
||||
|
||||
def test_blank_round_trip(self, term_tx):
|
||||
original = Term(type=BLANK, id="_:x")
|
||||
wire = term_tx.from_pulsar(original)
|
||||
restored = term_tx.to_pulsar(wire)
|
||||
assert restored == original
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# TermTranslator — Typed literal (XSD)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestTermTranslatorTypedLiteral:
|
||||
|
||||
def test_plain_literal_to_pulsar(self, term_tx):
|
||||
data = {"t": "l", "v": "hello"}
|
||||
term = term_tx.to_pulsar(data)
|
||||
assert term.type == LITERAL
|
||||
assert term.value == "hello"
|
||||
assert term.datatype == ""
|
||||
assert term.language == ""
|
||||
|
||||
def test_xsd_integer_to_pulsar(self, term_tx):
|
||||
data = {
|
||||
"t": "l", "v": "42",
|
||||
"dt": "http://www.w3.org/2001/XMLSchema#integer",
|
||||
}
|
||||
term = term_tx.to_pulsar(data)
|
||||
assert term.value == "42"
|
||||
assert term.datatype.endswith("#integer")
|
||||
|
||||
def test_typed_literal_from_pulsar(self, term_tx):
|
||||
term = Term(
|
||||
type=LITERAL, value="3.14",
|
||||
datatype="http://www.w3.org/2001/XMLSchema#double",
|
||||
)
|
||||
wire = term_tx.from_pulsar(term)
|
||||
assert wire["t"] == "l"
|
||||
assert wire["v"] == "3.14"
|
||||
assert wire["dt"] == "http://www.w3.org/2001/XMLSchema#double"
|
||||
assert "ln" not in wire # No language tag
|
||||
|
||||
def test_typed_literal_round_trip(self, term_tx):
|
||||
original = Term(
|
||||
type=LITERAL, value="true",
|
||||
datatype="http://www.w3.org/2001/XMLSchema#boolean",
|
||||
)
|
||||
wire = term_tx.from_pulsar(original)
|
||||
restored = term_tx.to_pulsar(wire)
|
||||
assert restored == original
|
||||
|
||||
def test_plain_literal_omits_dt_and_ln(self, term_tx):
|
||||
term = Term(type=LITERAL, value="x")
|
||||
wire = term_tx.from_pulsar(term)
|
||||
assert "dt" not in wire
|
||||
assert "ln" not in wire
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# TermTranslator — Language-tagged literal
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestTermTranslatorLangLiteral:
|
||||
|
||||
def test_language_tag_to_pulsar(self, term_tx):
|
||||
data = {"t": "l", "v": "bonjour", "ln": "fr"}
|
||||
term = term_tx.to_pulsar(data)
|
||||
assert term.value == "bonjour"
|
||||
assert term.language == "fr"
|
||||
|
||||
def test_language_tag_from_pulsar(self, term_tx):
|
||||
term = Term(type=LITERAL, value="colour", language="en-GB")
|
||||
wire = term_tx.from_pulsar(term)
|
||||
assert wire["ln"] == "en-GB"
|
||||
assert "dt" not in wire # No datatype
|
||||
|
||||
def test_language_tag_round_trip(self, term_tx):
|
||||
original = Term(type=LITERAL, value="hola", language="es")
|
||||
wire = term_tx.from_pulsar(original)
|
||||
restored = term_tx.to_pulsar(wire)
|
||||
assert restored == original
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# TermTranslator — Quoted triple (RDF-star)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestTermTranslatorQuotedTriple:
|
||||
|
||||
def test_quoted_triple_to_pulsar(self, term_tx):
|
||||
data = {
|
||||
"t": "t",
|
||||
"tr": {
|
||||
"s": {"t": "i", "i": "http://example.org/Alice"},
|
||||
"p": {"t": "i", "i": "http://xmlns.com/foaf/0.1/knows"},
|
||||
"o": {"t": "i", "i": "http://example.org/Bob"},
|
||||
},
|
||||
}
|
||||
term = term_tx.to_pulsar(data)
|
||||
assert term.type == TRIPLE
|
||||
assert term.triple is not None
|
||||
assert term.triple.s.iri == "http://example.org/Alice"
|
||||
assert term.triple.o.iri == "http://example.org/Bob"
|
||||
|
||||
def test_quoted_triple_from_pulsar(self, term_tx):
|
||||
inner = Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/s"),
|
||||
p=Term(type=IRI, iri="http://example.org/p"),
|
||||
o=Term(type=LITERAL, value="val"),
|
||||
)
|
||||
term = Term(type=TRIPLE, triple=inner)
|
||||
wire = term_tx.from_pulsar(term)
|
||||
assert wire["t"] == "t"
|
||||
assert "tr" in wire
|
||||
assert wire["tr"]["s"]["i"] == "http://example.org/s"
|
||||
assert wire["tr"]["o"]["v"] == "val"
|
||||
|
||||
def test_quoted_triple_round_trip(self, term_tx):
|
||||
inner = Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/A"),
|
||||
p=Term(type=IRI, iri="http://example.org/B"),
|
||||
o=Term(type=LITERAL, value="C", language="en"),
|
||||
)
|
||||
original = Term(type=TRIPLE, triple=inner)
|
||||
wire = term_tx.from_pulsar(original)
|
||||
restored = term_tx.to_pulsar(wire)
|
||||
assert restored.type == TRIPLE
|
||||
assert restored.triple.s == original.triple.s
|
||||
assert restored.triple.o == original.triple.o
|
||||
|
||||
def test_quoted_triple_none_triple(self, term_tx):
|
||||
term = Term(type=TRIPLE, triple=None)
|
||||
wire = term_tx.from_pulsar(term)
|
||||
assert wire == {"t": "t"}
|
||||
# And back
|
||||
restored = term_tx.to_pulsar(wire)
|
||||
assert restored.type == TRIPLE
|
||||
assert restored.triple is None
|
||||
|
||||
def test_quoted_triple_with_literal_object(self, term_tx):
|
||||
data = {
|
||||
"t": "t",
|
||||
"tr": {
|
||||
"s": {"t": "i", "i": "http://example.org/Hope"},
|
||||
"p": {"t": "i", "i": "http://www.w3.org/2004/02/skos/core#definition"},
|
||||
"o": {"t": "l", "v": "A feeling of expectation"},
|
||||
},
|
||||
}
|
||||
term = term_tx.to_pulsar(data)
|
||||
assert term.triple.o.type == LITERAL
|
||||
assert term.triple.o.value == "A feeling of expectation"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# TermTranslator — Edge cases
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestTermTranslatorEdgeCases:
|
||||
|
||||
def test_unknown_type(self, term_tx):
|
||||
data = {"t": "z"}
|
||||
term = term_tx.to_pulsar(data)
|
||||
assert term.type == "z"
|
||||
|
||||
def test_empty_type(self, term_tx):
|
||||
data = {}
|
||||
term = term_tx.to_pulsar(data)
|
||||
assert term.type == ""
|
||||
|
||||
def test_missing_iri_field(self, term_tx):
|
||||
data = {"t": "i"}
|
||||
term = term_tx.to_pulsar(data)
|
||||
assert term.iri == ""
|
||||
|
||||
def test_missing_literal_fields(self, term_tx):
|
||||
data = {"t": "l"}
|
||||
term = term_tx.to_pulsar(data)
|
||||
assert term.value == ""
|
||||
assert term.datatype == ""
|
||||
assert term.language == ""
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# TripleTranslator
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestTripleTranslator:
|
||||
|
||||
def test_triple_to_pulsar(self, triple_tx):
|
||||
data = {
|
||||
"s": {"t": "i", "i": "http://example.org/s"},
|
||||
"p": {"t": "i", "i": "http://example.org/p"},
|
||||
"o": {"t": "l", "v": "object"},
|
||||
}
|
||||
triple = triple_tx.to_pulsar(data)
|
||||
assert triple.s.iri == "http://example.org/s"
|
||||
assert triple.o.value == "object"
|
||||
assert triple.g is None
|
||||
|
||||
def test_triple_from_pulsar(self, triple_tx):
|
||||
triple = Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/A"),
|
||||
p=Term(type=IRI, iri="http://example.org/B"),
|
||||
o=Term(type=LITERAL, value="C"),
|
||||
)
|
||||
wire = triple_tx.from_pulsar(triple)
|
||||
assert wire["s"]["t"] == "i"
|
||||
assert wire["o"]["v"] == "C"
|
||||
assert "g" not in wire
|
||||
|
||||
def test_quad_with_named_graph(self, triple_tx):
|
||||
data = {
|
||||
"s": {"t": "i", "i": "http://example.org/s"},
|
||||
"p": {"t": "i", "i": "http://example.org/p"},
|
||||
"o": {"t": "l", "v": "val"},
|
||||
"g": "urn:graph:source",
|
||||
}
|
||||
quad = triple_tx.to_pulsar(data)
|
||||
assert quad.g == "urn:graph:source"
|
||||
|
||||
def test_quad_from_pulsar_includes_graph(self, triple_tx):
|
||||
quad = Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/s"),
|
||||
p=Term(type=IRI, iri="http://example.org/p"),
|
||||
o=Term(type=LITERAL, value="v"),
|
||||
g="urn:graph:retrieval",
|
||||
)
|
||||
wire = triple_tx.from_pulsar(quad)
|
||||
assert wire["g"] == "urn:graph:retrieval"
|
||||
|
||||
def test_quad_round_trip(self, triple_tx):
|
||||
original = Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/s"),
|
||||
p=Term(type=IRI, iri="http://example.org/p"),
|
||||
o=Term(type=LITERAL, value="v"),
|
||||
g="urn:graph:source",
|
||||
)
|
||||
wire = triple_tx.from_pulsar(original)
|
||||
restored = triple_tx.to_pulsar(wire)
|
||||
assert restored == original
|
||||
|
||||
def test_none_graph_omitted_from_wire(self, triple_tx):
|
||||
triple = Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/s"),
|
||||
p=Term(type=IRI, iri="http://example.org/p"),
|
||||
o=Term(type=LITERAL, value="v"),
|
||||
g=None,
|
||||
)
|
||||
wire = triple_tx.from_pulsar(triple)
|
||||
assert "g" not in wire
|
||||
|
||||
def test_missing_terms_handled(self, triple_tx):
|
||||
data = {}
|
||||
triple = triple_tx.to_pulsar(data)
|
||||
assert triple.s is None
|
||||
assert triple.p is None
|
||||
assert triple.o is None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# SubgraphTranslator
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestSubgraphTranslator:
|
||||
|
||||
def test_subgraph_round_trip(self):
|
||||
tx = SubgraphTranslator()
|
||||
triples = [
|
||||
Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/A"),
|
||||
p=Term(type=IRI, iri="http://example.org/rel"),
|
||||
o=Term(type=LITERAL, value="v1"),
|
||||
),
|
||||
Triple(
|
||||
s=Term(type=IRI, iri="http://example.org/B"),
|
||||
p=Term(type=IRI, iri="http://example.org/rel"),
|
||||
o=Term(type=IRI, iri="http://example.org/C"),
|
||||
g="urn:graph:source",
|
||||
),
|
||||
]
|
||||
wire_list = tx.from_pulsar(triples)
|
||||
assert len(wire_list) == 2
|
||||
assert wire_list[1]["g"] == "urn:graph:source"
|
||||
|
||||
restored = tx.to_pulsar(wire_list)
|
||||
assert len(restored) == 2
|
||||
assert restored[0] == triples[0]
|
||||
assert restored[1] == triples[1]
|
||||
|
||||
def test_empty_subgraph(self):
|
||||
tx = SubgraphTranslator()
|
||||
assert tx.to_pulsar([]) == []
|
||||
assert tx.from_pulsar([]) == []
|
||||
Loading…
Add table
Add a link
Reference in a new issue