Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix group counting #99

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rdfproxy/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def query(
size: int = 100,
) -> Page[_TModelInstance]:
"""Run a query against an endpoint and return a Page model object."""
count_query: str = construct_count_query(self._query)
count_query: str = construct_count_query(query=self._query, model=self._model)
items_query: str = ungrouped_pagination_base_query.substitute(
query=self._query, offset=calculate_offset(page, size), limit=size
)
Expand Down
10 changes: 8 additions & 2 deletions rdfproxy/utils/sparql_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import cast

from SPARQLWrapper import QueryResult, SPARQLWrapper
from rdfproxy.utils._types import _TModelInstance


ungrouped_pagination_base_query: Annotated[
Expand Down Expand Up @@ -35,9 +36,14 @@ def replace_query_select_clause(query: str, repl: str) -> str:
return count_query


def construct_count_query(query: str) -> str:
def construct_count_query(query: str, model: type[_TModelInstance]) -> str:
"""Construct a generic count query from a SELECT query."""
count_query = replace_query_select_clause(query, "select (count(*) as ?cnt)")
try:
group_by: str = model.model_config["group_by"]
count_query = construct_grouped_count_query(query, group_by)
except KeyError:
count_query = replace_query_select_clause(query, "select (count(*) as ?cnt)")

return count_query


Expand Down
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
Loading