Skip to content

Commit

Permalink
test: adapt tests to SPARQLModelAdapter redesign
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-pl authored and b1rger committed Aug 20, 2024
1 parent 48b3e27 commit ec97bf1
Showing 1 changed file with 68 additions and 5 deletions.
73 changes: 68 additions & 5 deletions tests/test_sparql_model_adapter.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
"""Pytest entry point for rdfproxy.SPARQLModelAdapter tests."""

import secrets
import string

import pytest

from rdfproxy import SPARQLModelAdapter
from rdfproxy.utils._exceptions import UndefinedBindingException
from tests.data.models import ComplexModel


@pytest.mark.remote
def test_sparql_model_adapter_basic(wikidata_wrapper):
def test_sparql_model_adapter_basic():
"""Simple base test for SPARQLModelAdapter."""
query = """
select ?x ?y ?a ?p
Expand All @@ -17,8 +21,67 @@ def test_sparql_model_adapter_basic(wikidata_wrapper):
}
}
"""
wikidata_wrapper.setQuery(query)
adapter = SPARQLModelAdapter(sparql_wrapper=wikidata_wrapper)
model, *_ = adapter(query=query, model_constructor=ComplexModel)
adapter = SPARQLModelAdapter(
target="https://query.wikidata.org/bigdata/namespace/wdq/sparql",
query=query,
model=ComplexModel,
)

assert all(isinstance(model, ComplexModel) for model in adapter.query())


@pytest.mark.remote
def test_sparql_model_adapter_grouping_basic():
query = """
select ?x ?y ?a ?p ?z
where {
values (?x ?y ?a ?p ?z) {
(1 2 "a value" "p value" 3)
(1 22 "other value" "yet another value" 3)
(2 22 "other value" "yet another value" 3)
}
}
"""

adapter = SPARQLModelAdapter(
target="https://query.wikidata.org/bigdata/namespace/wdq/sparql",
query=query,
model=ComplexModel,
)

group_x = adapter._query_group_by("x")
group_y = adapter._query_group_by("y")
group_z = adapter._query_group_by("z")

assert {1, 2} == set(len(v) for _, v in group_x.items())
assert {1, 2} == set(len(v) for _, v in group_y.items())
assert {3} == set(len(v) for _, v in group_z.items())


@pytest.mark.remote
@pytest.mark.parametrize(
"var",
["".join(secrets.choice(string.ascii_letters) for i in range(8)) for e in range(10)]
+ ["and", "as", "assert"],
)
def test_sparql_model_adapter_grouping_basic_fail(var):
query = """
select ?x ?y ?a ?p ?z
where {
values (?x ?y ?a ?p ?z) {
(1 2 "a value" "p value" 3)
(1 22 "other value" "yet another value" 3)
(2 22 "other value" "yet another value" 3)
}
}
"""

adapter = SPARQLModelAdapter(
target="https://query.wikidata.org/bigdata/namespace/wdq/sparql",
query=query,
model=ComplexModel,
)

assert isinstance(model, ComplexModel)
if var not in ["x", "y", "a", "p", "z"]:
with pytest.raises(UndefinedBindingException):
group = adapter._query_group_by(var) # noqa: F841

0 comments on commit ec97bf1

Please sign in to comment.