-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: implement basic test for grouped pagination behavior
- Loading branch information
Showing
1 changed file
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
"""Basic tests for rdfproxy.SPARQLModelAdapter pagination with grouped models.""" | ||
|
||
from typing import Any, NamedTuple | ||
|
||
import pytest | ||
|
||
from pydantic import BaseModel, ConfigDict | ||
from rdfproxy import Page, SPARQLModelAdapter | ||
|
||
|
||
query = """ | ||
select ?parent ?child ?name | ||
where { | ||
values (?parent ?child ?name) { | ||
('x' 'c' 'foo') | ||
('y' 'd' UNDEF) | ||
('y' 'e' UNDEF) | ||
('z' UNDEF UNDEF) | ||
} | ||
} | ||
""" | ||
|
||
|
||
class Child(BaseModel): | ||
name: str | None = None | ||
|
||
|
||
class Parent(BaseModel): | ||
model_config = ConfigDict(group_by="parent") | ||
|
||
parent: str | ||
children: list[Child] | ||
|
||
|
||
parent_adapter = SPARQLModelAdapter( | ||
target="https://graphdb.r11.eu/repositories/RELEVEN", | ||
query=query, | ||
model=Parent, | ||
) | ||
|
||
|
||
class AdapterParameter(NamedTuple): | ||
adapter: SPARQLModelAdapter | ||
query_parameters: dict[str, Any] | ||
expected: Page | ||
|
||
|
||
adapter_parameters = [ | ||
AdapterParameter( | ||
adapter=parent_adapter, | ||
query_parameters={"page": 1, "size": 2}, | ||
expected=Page[Parent]( | ||
items=[ | ||
{"parent": "x", "children": [{"name": "foo"}]}, | ||
{"parent": "y", "children": []}, | ||
], | ||
page=1, | ||
size=2, | ||
total=3, | ||
pages=2, | ||
), | ||
), | ||
AdapterParameter( | ||
adapter=parent_adapter, | ||
query_parameters={"page": 2, "size": 2}, | ||
expected=Page[Parent]( | ||
items=[{"parent": "z", "children": []}], | ||
page=2, | ||
size=2, | ||
total=3, | ||
pages=2, | ||
), | ||
), | ||
AdapterParameter( | ||
adapter=parent_adapter, | ||
query_parameters={"page": 1, "size": 1}, | ||
expected=Page[Parent]( | ||
items=[{"parent": "x", "children": [{"name": "foo"}]}], | ||
page=1, | ||
size=1, | ||
total=3, | ||
pages=3, | ||
), | ||
), | ||
AdapterParameter( | ||
adapter=parent_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, | ||
query_parameters={"page": 3, "size": 1}, | ||
expected=Page[Parent]( | ||
items=[{"parent": "z", "children": []}], page=3, size=1, total=3, pages=3 | ||
), | ||
), | ||
] | ||
|
||
|
||
@pytest.mark.remote | ||
@pytest.mark.parametrize( | ||
["adapter", "query_parameters", "expected"], adapter_parameters | ||
) | ||
def test_basic_adapter_grouped_pagination(adapter, query_parameters, expected): | ||
assert adapter.query(**query_parameters) == expected |