Skip to content

Commit

Permalink
test: adapt/add adapter tests for group_by alias resolution
Browse files Browse the repository at this point in the history
The change adapts/adds tests for group_by configs that require rdfproxy.SPARQLBinding resolution.
  • Loading branch information
lu-pl committed Dec 5, 2024
1 parent 1afacaa commit 1a1a649
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 8 deletions.
2 changes: 2 additions & 0 deletions tests/data/models/dummy_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ class Dummy(BaseModel):

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

x: int
92 changes: 84 additions & 8 deletions tests/tests_adapter/test_adapter_grouped_pagination.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
"""Basic tests for rdfproxy.SPARQLModelAdapter pagination with grouped models."""

from typing import Any, NamedTuple
from typing import Annotated, Any, NamedTuple

import pytest

from pydantic import BaseModel, ConfigDict
from rdfproxy import Page, QueryParameters, SPARQLModelAdapter
from rdfproxy import Page, QueryParameters, SPARQLBinding, SPARQLModelAdapter


binding_query = """
select ?parentBinding ?child ?name
where {
values (?parentBinding ?child ?name) {
('x' 'c' 'foo')
('y' 'd' UNDEF)
('y' 'e' UNDEF)
('z' UNDEF UNDEF)
}
}
"""

query = """
select ?parent ?child ?name
where {
Expand All @@ -25,14 +37,27 @@ class Child(BaseModel):
name: str | None = None


class BindingParent(BaseModel):
model_config = ConfigDict(group_by="parent")

parent: Annotated[str, SPARQLBinding("parentBinding")]
children: list[Child]


class Parent(BaseModel):
model_config = ConfigDict(group_by="parent")

parent: str
children: list[Child]


parent_adapter = SPARQLModelAdapter(
binding_adapter = SPARQLModelAdapter(
target="https://graphdb.r11.eu/repositories/RELEVEN",
query=binding_query,
model=BindingParent,
)

adapter = SPARQLModelAdapter(
target="https://graphdb.r11.eu/repositories/RELEVEN",
query=query,
model=Parent,
Expand All @@ -47,7 +72,58 @@ class AdapterParameter(NamedTuple):

adapter_parameters = [
AdapterParameter(
adapter=parent_adapter,
adapter=binding_adapter,
query_parameters={"page": 1, "size": 2},
expected=Page[BindingParent](
items=[
{"parent": "x", "children": [{"name": "foo"}]},
{"parent": "y", "children": []},
],
page=1,
size=2,
total=3,
pages=2,
),
),
AdapterParameter(
adapter=binding_adapter,
query_parameters={"page": 2, "size": 2},
expected=Page[BindingParent](
items=[{"parent": "z", "children": []}],
page=2,
size=2,
total=3,
pages=2,
),
),
AdapterParameter(
adapter=binding_adapter,
query_parameters={"page": 1, "size": 1},
expected=Page[BindingParent](
items=[{"parent": "x", "children": [{"name": "foo"}]}],
page=1,
size=1,
total=3,
pages=3,
),
),
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
),
),
#
AdapterParameter(
adapter=adapter,
query_parameters={"page": 1, "size": 2},
expected=Page[Parent](
items=[
Expand All @@ -61,7 +137,7 @@ class AdapterParameter(NamedTuple):
),
),
AdapterParameter(
adapter=parent_adapter,
adapter=adapter,
query_parameters={"page": 2, "size": 2},
expected=Page[Parent](
items=[{"parent": "z", "children": []}],
Expand All @@ -72,7 +148,7 @@ class AdapterParameter(NamedTuple):
),
),
AdapterParameter(
adapter=parent_adapter,
adapter=adapter,
query_parameters={"page": 1, "size": 1},
expected=Page[Parent](
items=[{"parent": "x", "children": [{"name": "foo"}]}],
Expand All @@ -83,14 +159,14 @@ class AdapterParameter(NamedTuple):
),
),
AdapterParameter(
adapter=parent_adapter,
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=parent_adapter,
adapter=adapter,
query_parameters={"page": 3, "size": 1},
expected=Page[Parent](
items=[{"parent": "z", "children": []}], page=3, size=1, total=3, pages=3
Expand Down

0 comments on commit 1a1a649

Please sign in to comment.