Skip to content

Commit

Permalink
test: test adaptions for QueryConstructor rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-pl committed Dec 19, 2024
1 parent 088509b commit 2aa9604
Show file tree
Hide file tree
Showing 24 changed files with 436 additions and 106 deletions.
14 changes: 0 additions & 14 deletions tests/data/models/dummy_model.py

This file was deleted.

80 changes: 53 additions & 27 deletions tests/tests_adapter/test_adapter_grouped_pagination.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
"""Basic tests for rdfproxy.SPARQLModelAdapter pagination with grouped models."""

from functools import partial
from typing import Annotated, Any, NamedTuple

import pytest

from pydantic import BaseModel
from rdfproxy import (
ConfigDict,
HttpxStrategy,
Page,
QueryParameters,
SPARQLBinding,
SPARQLModelAdapter,
SPARQLWrapperStrategy,
)


Expand Down Expand Up @@ -57,28 +60,43 @@ class Parent(BaseModel):
children: list[Child]


binding_adapter = SPARQLModelAdapter(
target="https://graphdb.r11.eu/repositories/RELEVEN",
query=binding_query,
model=BindingParent,
)
@pytest.fixture()
def adapters():
_adapter_base = partial(
SPARQLModelAdapter,
target="https://graphdb.r11.eu/repositories/RELEVEN",
query=query,
model=Parent,
)

adapter = SPARQLModelAdapter(
target="https://graphdb.r11.eu/repositories/RELEVEN",
query=query,
model=Parent,
)
return [
_adapter_base(sparql_strategy=HttpxStrategy),
_adapter_base(sparql_strategy=SPARQLWrapperStrategy),
]


@pytest.fixture()
def binding_adapters():
_binding_adapter_base = partial(
SPARQLModelAdapter,
target="https://graphdb.r11.eu/repositories/RELEVEN",
query=binding_query,
model=BindingParent,
)

return [
_binding_adapter_base(sparql_strategy=HttpxStrategy),
_binding_adapter_base(sparql_strategy=SPARQLWrapperStrategy),
]


class AdapterParameter(NamedTuple):
adapter: SPARQLModelAdapter
query_parameters: dict[str, Any]
expected: Page


adapter_parameters = [
binding_adapter_parameters = [
AdapterParameter(
adapter=binding_adapter,
query_parameters={"page": 1, "size": 2},
expected=Page[BindingParent](
items=[
Expand All @@ -92,7 +110,6 @@ class AdapterParameter(NamedTuple):
),
),
AdapterParameter(
adapter=binding_adapter,
query_parameters={"page": 2, "size": 2},
expected=Page[BindingParent](
items=[{"parent": "z", "children": []}],
Expand All @@ -103,7 +120,6 @@ class AdapterParameter(NamedTuple):
),
),
AdapterParameter(
adapter=binding_adapter,
query_parameters={"page": 1, "size": 1},
expected=Page[BindingParent](
items=[{"parent": "x", "children": [{"name": "foo"}]}],
Expand All @@ -114,22 +130,21 @@ class AdapterParameter(NamedTuple):
),
),
AdapterParameter(
adapter=binding_adapter,
query_parameters={"page": 2, "size": 1},
expected=Page[BindingParent](
items=[{"parent": "y", "children": []}], page=2, size=1, total=3, pages=3
),
),
AdapterParameter(
adapter=binding_adapter,
query_parameters={"page": 3, "size": 1},
expected=Page[BindingParent](
items=[{"parent": "z", "children": []}], page=3, size=1, total=3, pages=3
),
),
#
]
#
adapter_parameters = [
AdapterParameter(
adapter=adapter,
query_parameters={"page": 1, "size": 2},
expected=Page[Parent](
items=[
Expand All @@ -143,7 +158,6 @@ class AdapterParameter(NamedTuple):
),
),
AdapterParameter(
adapter=adapter,
query_parameters={"page": 2, "size": 2},
expected=Page[Parent](
items=[{"parent": "z", "children": []}],
Expand All @@ -154,7 +168,6 @@ class AdapterParameter(NamedTuple):
),
),
AdapterParameter(
adapter=adapter,
query_parameters={"page": 1, "size": 1},
expected=Page[Parent](
items=[{"parent": "x", "children": [{"name": "foo"}]}],
Expand All @@ -165,14 +178,12 @@ class AdapterParameter(NamedTuple):
),
),
AdapterParameter(
adapter=adapter,
query_parameters={"page": 2, "size": 1},
expected=Page[Parent](
items=[{"parent": "y", "children": []}], page=2, size=1, total=3, pages=3
),
),
AdapterParameter(
adapter=adapter,
query_parameters={"page": 3, "size": 1},
expected=Page[Parent](
items=[{"parent": "z", "children": []}], page=3, size=1, total=3, pages=3
Expand All @@ -183,8 +194,23 @@ class AdapterParameter(NamedTuple):

@pytest.mark.remote
@pytest.mark.parametrize(
["adapter", "query_parameters", "expected"], adapter_parameters
["query_parameters", "expected"],
adapter_parameters,
)
def test_basic_adapter_grouped_pagination(adapters, query_parameters, expected):
for adapter in adapters:
parameters = QueryParameters(**query_parameters)
assert adapter.query(parameters) == expected


@pytest.mark.remote
@pytest.mark.parametrize(
["query_parameters", "expected"],
binding_adapter_parameters,
)
def test_basic_adapter_grouped_pagination(adapter, query_parameters, expected):
parameters = QueryParameters(**query_parameters)
assert adapter.query(parameters) == expected
def test_basic_binding_adapter_grouped_pagination(
binding_adapters, query_parameters, expected
):
for adapter in binding_adapters:
parameters = QueryParameters(**query_parameters)
assert adapter.query(parameters) == expected
1 change: 1 addition & 0 deletions tests/tests_adapter/test_adapter_ungrouped_pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Basic tests for rdfproxy.SPARQLModelAdapter pagination with ungrouped models."""
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
from tests.data.models.dummy_model import Dummy, GroupedDummy
from pydantic import BaseModel
from rdfproxy import ConfigDict
from tests.utils._types import CountQueryParameter


class Dummy(BaseModel):
pass


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

x: int


construct_count_query_parameters = [
CountQueryParameter(
query="""
Expand Down
105 changes: 105 additions & 0 deletions tests/tests_constructor/test_query_constructor_items_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
"""Basic tests for the QueryConstructor class."""

from typing import NamedTuple

import pytest

from pydantic import BaseModel
from rdfproxy.constructor import QueryConstructor
from rdfproxy.utils._types import ConfigDict
from rdfproxy.utils.models import QueryParameters


class UngroupedModel(BaseModel):
x: int
y: int


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

x: int
y: list[int]


class Expected(NamedTuple):
count_query: str
items_query: str


class QueryConstructorParameters(NamedTuple):
query: str
query_parameters: QueryParameters
model: type[BaseModel]

expected: Expected


parameters = [
# ungrouped
QueryConstructorParameters(
query="select * where {?s ?p ?o}",
query_parameters=QueryParameters(),
model=UngroupedModel,
expected=Expected(
count_query="select (count(*) as ?cnt) where {?s ?p ?o}",
items_query="select * where {?s ?p ?o} order by ?s limit 100 offset 0",
),
),
QueryConstructorParameters(
query="select ?p ?o where {?s ?p ?o}",
query_parameters=QueryParameters(),
model=UngroupedModel,
expected=Expected(
count_query="select (count(*) as ?cnt) where {?s ?p ?o}",
items_query="select ?p ?o where {?s ?p ?o} order by ?p limit 100 offset 0",
),
),
QueryConstructorParameters(
query="select * where {?s ?p ?o}",
query_parameters=QueryParameters(page=2, size=2),
model=UngroupedModel,
expected=Expected(
count_query="select (count(*) as ?cnt) where {?s ?p ?o}",
items_query="select * where {?s ?p ?o} order by ?s limit 2 offset 2",
),
),
# grouped
QueryConstructorParameters(
query="select * where {?x a ?y}",
query_parameters=QueryParameters(),
model=GroupedModel,
expected=Expected(
count_query="select (count(distinct ?x) as ?cnt) where {?x a ?y}",
items_query="select * where {?x a ?y {select distinct ?x where {?x a ?y} order by ?x limit 100 offset 0} }",
),
),
QueryConstructorParameters(
query="select ?x ?y where {?x a ?y}",
query_parameters=QueryParameters(),
model=GroupedModel,
expected=Expected(
count_query="select (count(distinct ?x) as ?cnt) where {?x a ?y}",
items_query="select ?x ?y where {?x a ?y {select distinct ?x where {?x a ?y} order by ?x limit 100 offset 0} }",
),
),
QueryConstructorParameters(
query="select ?x ?y where {?x a ?y}",
query_parameters=QueryParameters(page=2, size=2),
model=GroupedModel,
expected=Expected(
count_query="select (count(distinct ?x) as ?cnt) where {?x a ?y}",
items_query="select ?x ?y where {?x a ?y {select distinct ?x where {?x a ?y} order by ?x limit 2 offset 2} }",
),
),
]


@pytest.mark.parametrize(["query", "query_parameters", "model", "expected"], parameters)
def test_query_constructor_items_query(query, query_parameters, model, expected):
constructor = QueryConstructor(
query=query, query_parameters=query_parameters, model=model
)

assert constructor.get_count_query() == expected.count_query
assert constructor.get_items_query() == expected.items_query
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class Child5(BaseModel):
child: str | None = Field(default=None, exclude=True)


class Child6(BaseModel):
model_config = ConfigDict(model_bool=["name", "child"])

name: str | None = None
child: str | None = None


def _create_parent_with_child(child: type[BaseModel]) -> type[BaseModel]:
model = create_model(
"Parent",
Expand Down Expand Up @@ -112,4 +119,13 @@ def _create_parent_with_child(child: type[BaseModel]) -> type[BaseModel]:
{"parent": "z", "children": []},
],
),
ModelBindingsMapperParameter(
model=_create_parent_with_child(Child6),
bindings=bindings,
expected=[
{"parent": "x", "children": [{"name": "foo", "child": "c"}]},
{"parent": "y", "children": []},
{"parent": "z", "children": []},
],
),
]
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from tests.data.models.author_array_collection_model import Author as ArrayAuthor
from tests.data.models.author_work_title_model import Author
from tests.data.models.basic_model import (
from tests.tests_mapper.params.models.author_array_collection_model import (
Author as ArrayAuthor,
)
from tests.tests_mapper.params.models.author_work_title_model import Author
from tests.tests_mapper.params.models.basic_model import (
BasicComplexModel,
BasicNestedModel,
BasicSimpleModel,
)
from tests.data.models.grouping_model import GroupingComplexModel
from tests.data.models.nested_grouping_model import NestedGroupingComplexModel
from tests.tests_mapper.params.models.grouping_model import GroupingComplexModel
from tests.tests_mapper.params.models.nested_grouping_model import (
NestedGroupingComplexModel,
)
from tests.utils._types import ModelBindingsMapperParameter


Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions tests/tests_mapper/test_model_bindings_mapper.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Pytest entry point for basic rdfproxy.mapper.ModelBindingsMapper."""

from pydantic import BaseModel
import pytest
from rdfproxy.mapper import ModelBindingsMapper

from tests.data.parameters.model_bindings_mapper_parameters import (
from pydantic import BaseModel
from rdfproxy.mapper import ModelBindingsMapper
from tests.tests_mapper.params.model_bindings_mapper_parameters import (
author_array_collection_parameters,
author_work_title_parameters,
basic_parameters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pydantic import BaseModel
from rdfproxy.mapper import ModelBindingsMapper
from tests.data.parameters.model_bindings_mapper_model_bool_parameters import (
from tests.tests_mapper.params.model_bindings_mapper_model_bool_parameters import (
parent_child_parameters,
)

Expand Down
Loading

0 comments on commit 2aa9604

Please sign in to comment.