Skip to content

Commit

Permalink
test: implement basic example test for construct_count_query
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-pl committed Oct 23, 2024
1 parent 03dcdb3 commit b84819f
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tests/data/models/dummy_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""Simple dummy models e.g. for count query constructor testing."""

from pydantic import BaseModel, ConfigDict


class Dummy(BaseModel):
pass


class GroupedDummy(BaseModel):
model_config = ConfigDict(group_by="x")
50 changes: 50 additions & 0 deletions tests/data/parameters/count_query_parameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from tests.data.models.dummy_model import Dummy, GroupedDummy
from tests.utils._types import CountQueryParameter


count_query_parameters = [
CountQueryParameter(
query="""
select ?x ?y ?z
where {
values (?x ?y ?z) {
(1 2 3)
(1 22 33)
(2 222 333)
}
}
""",
model=Dummy,
grouped_model=GroupedDummy,
),
CountQueryParameter(
query="""
select ?x ?y ?z
where {
values (?x ?y ?z) {
(1 2 3)
(1 22 33)
(1 22 33)
(2 222 333)
}
}
""",
model=Dummy,
grouped_model=GroupedDummy,
),
CountQueryParameter(
query="""
select ?x ?y ?z
where {
values (?x ?y ?z) {
(1 2 3)
(1 22 33)
(2 222 333)
(2 222 333)
}
}
""",
model=Dummy,
grouped_model=GroupedDummy,
),
]
46 changes: 46 additions & 0 deletions tests/test_construct_count_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Pytest entry point for rdfproxy.utils.sparql_utils.construct_count_query tests."""

from pydantic import BaseModel, ConfigDict
from rdflib import Graph
from rdflib.plugins.sparql.processor import SPARQLResult
from rdfproxy.utils.sparql_utils import construct_count_query


query = """
select ?x ?y ?z
where {
values (?x ?y ?z) {
(1 2 3)
(1 22 33)
(2 222 333)
}
}
"""

graph: Graph = Graph()


class Dummy(BaseModel):
pass


class GroupedDummy(BaseModel):
model_config = ConfigDict(group_by="x")


count_query_dummy = construct_count_query(query=query, model=Dummy)
count_query_grouped_dummy = construct_count_query(query=query, model=GroupedDummy)


def _get_cnt_value_from_sparql_result(
result: SPARQLResult, count_var: str = "cnt"
) -> int:
return int(result.bindings[0][count_var])


def test_basic_construct_count_query():
result_dummy: SPARQLResult = graph.query(count_query_dummy)
result_grouped_dummy: SPARQLResult = graph.query(count_query_grouped_dummy)

assert _get_cnt_value_from_sparql_result(result_dummy) == 3
assert _get_cnt_value_from_sparql_result(result_grouped_dummy) == 2

0 comments on commit b84819f

Please sign in to comment.