Skip to content

Commit

Permalink
refactor: make ModelBindingsMapper private
Browse files Browse the repository at this point in the history
As indicated in the docstring for ModelBindingsMapper, the class is
somewhat coupled to SPARQLModelAdapter, because ModelBindinsMapper
does not run model sanity by itself - sanity checking should happen in
SPARQLModelAdapter, i.e. as early as possible. See issue #108. Once
sanity checking is implemented, RDFProxy will make a public
ModelBindingsMapper class available which will run model sanity
checking itself.
  • Loading branch information
lu-pl committed Jan 22, 2025
1 parent 933da4a commit c58a959
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 11 deletions.
1 change: 0 additions & 1 deletion rdfproxy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from rdfproxy.adapter import SPARQLModelAdapter # noqa: F401
from rdfproxy.mapper import ModelBindingsMapper # noqa: F401
from rdfproxy.sparql_strategies import (
SPARQLStrategy, # noqa: F401
SPARQLWrapperStrategy, # noqa: F401
Expand Down
4 changes: 2 additions & 2 deletions rdfproxy/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Generic

from rdfproxy.constructor import QueryConstructor
from rdfproxy.mapper import ModelBindingsMapper
from rdfproxy.mapper import _ModelBindingsMapper
from rdfproxy.sparql_strategies import HttpxStrategy, SPARQLStrategy
from rdfproxy.utils._types import _TModelInstance
from rdfproxy.utils.models import Page, QueryParameters
Expand Down Expand Up @@ -70,7 +70,7 @@ def query(
logger.debug(f"Running items query: \n{items_query}")

items_query_bindings: Iterator[dict] = self.sparql_strategy.query(items_query)
mapper = ModelBindingsMapper(self._model, *items_query_bindings)
mapper = _ModelBindingsMapper(self._model, *items_query_bindings)
items: list[_TModelInstance] = mapper.get_models()

logger.debug(f"Running count query: \n{count_query}")
Expand Down
2 changes: 1 addition & 1 deletion rdfproxy/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from rdfproxy.utils.utils import CurryModel, FieldsBindingsMap


class ModelBindingsMapper(Generic[_TModelInstance]):
class _ModelBindingsMapper(Generic[_TModelInstance]):
"""Utility class for mapping bindings to nested/grouped Pydantic models.
RDFProxy utilizes Pydantic models also as a modelling grammar for grouping
Expand Down
4 changes: 2 additions & 2 deletions tests/tests_mapper/test_model_bindings_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from pydantic import BaseModel
from rdfproxy.mapper import ModelBindingsMapper
from rdfproxy.mapper import _ModelBindingsMapper
from tests.tests_mapper.params.model_bindings_mapper_parameters import (
author_array_collection_parameters,
author_work_title_parameters,
Expand Down Expand Up @@ -31,6 +31,6 @@ def test_basic_model_bindings_mapper(model, bindings, expected):
Given a model and a set of bindings, run the BindingsModelMapper logic
and compare the result against the expected shape.
"""
mapper: ModelBindingsMapper = ModelBindingsMapper(model, *bindings)
mapper: _ModelBindingsMapper = _ModelBindingsMapper(model, *bindings)
models: list[BaseModel] = mapper.get_models()
assert [model.model_dump() for model in models] == expected
4 changes: 2 additions & 2 deletions tests/tests_mapper/test_model_bindings_mapper_model_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from pydantic import BaseModel
from rdfproxy.mapper import ModelBindingsMapper
from rdfproxy.mapper import _ModelBindingsMapper
from tests.tests_mapper.params.model_bindings_mapper_model_bool_parameters import (
parent_child_parameters,
)
Expand All @@ -19,6 +19,6 @@ def test_basic_model_bindings_mapper(model, bindings, expected):
Given a model and a set of bindings, run the BindingsModelMapper logic
and compare the result against the expected shape.
"""
mapper: ModelBindingsMapper = ModelBindingsMapper(model, *bindings)
mapper: _ModelBindingsMapper = _ModelBindingsMapper(model, *bindings)
models: list[BaseModel] = mapper.get_models()
assert [model.model_dump() for model in models] == expected
7 changes: 4 additions & 3 deletions tests/tests_mapper/test_sad_path_mapper_grouped_models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import pytest

from pydantic import BaseModel
from rdfproxy import ConfigDict, ModelBindingsMapper
from rdfproxy.mapper import _ModelBindingsMapper
from rdfproxy.utils._exceptions import (
InvalidGroupingKeyException,
MissingModelConfigException,
)
from rdfproxy.utils._types import ConfigDict


class ModelMissingGroupByConfig(BaseModel):
Expand All @@ -21,10 +22,10 @@ class ModelMissingGroupByValue(BaseModel):
@pytest.mark.xfail(reason="Not yet implemented, checks will run in model checkers.")
def test_sad_path_adapter_missing_grouping_config():
with pytest.raises(MissingModelConfigException):
ModelBindingsMapper(ModelMissingGroupByConfig, {"x": 1}).get_models()
_ModelBindingsMapper(ModelMissingGroupByConfig, {"x": 1}).get_models()


@pytest.mark.xfail(reason="Not yet implemented, checks will run in model checkers.")
def test_sad_path_adapter_missing_grouping_value():
with pytest.raises(InvalidGroupingKeyException):
ModelBindingsMapper(ModelMissingGroupByValue, {"x": 1}).get_models()
_ModelBindingsMapper(ModelMissingGroupByValue, {"x": 1}).get_models()

0 comments on commit c58a959

Please sign in to comment.