From b84819f0954473caf779c4778a5b81dafcae5762 Mon Sep 17 00:00:00 2001 From: Lukas Plank Date: Fri, 18 Oct 2024 08:59:39 +0200 Subject: [PATCH] test: implement basic example test for construct_count_query --- tests/data/models/dummy_model.py | 11 ++++ .../data/parameters/count_query_parameters.py | 50 +++++++++++++++++++ tests/test_construct_count_query.py | 46 +++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 tests/data/models/dummy_model.py create mode 100644 tests/data/parameters/count_query_parameters.py create mode 100644 tests/test_construct_count_query.py diff --git a/tests/data/models/dummy_model.py b/tests/data/models/dummy_model.py new file mode 100644 index 0000000..2cab0d9 --- /dev/null +++ b/tests/data/models/dummy_model.py @@ -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") diff --git a/tests/data/parameters/count_query_parameters.py b/tests/data/parameters/count_query_parameters.py new file mode 100644 index 0000000..1dbf8af --- /dev/null +++ b/tests/data/parameters/count_query_parameters.py @@ -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, + ), +] diff --git a/tests/test_construct_count_query.py b/tests/test_construct_count_query.py new file mode 100644 index 0000000..94f6e2d --- /dev/null +++ b/tests/test_construct_count_query.py @@ -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