Skip to content

Commit

Permalink
test: implement tests for ModelBindingsMapper with model_bool config
Browse files Browse the repository at this point in the history
The tests cover all cases discussed in #110.
  • Loading branch information
lu-pl committed Nov 15, 2024
1 parent 3e3730c commit 41291be
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
114 changes: 114 additions & 0 deletions tests/data/parameters/model_bindings_mapper_model_bool_parameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
"""Parameters for testing ModelBindingsMapper with the model_bool config option.
The test cover all cases discussed in https://github.com/acdh-oeaw/rdfproxy/issues/110.
"""

from pydantic import BaseModel, ConfigDict, Field, create_model
from tests.utils._types import ModelBindingsMapperParameter


bindings = [
{"parent": "x", "child": "c", "name": "foo"},
{"parent": "y", "child": "d", "name": None},
{"parent": "y", "child": "e", "name": None},
{"parent": "z", "child": None, "name": None},
]


class Child1(BaseModel):
name: str | None = None


class Child2(BaseModel):
model_config = ConfigDict(model_bool=lambda model: True)
name: str | None = None
child: str | None = None


class Child3(BaseModel):
model_config = ConfigDict(model_bool=lambda model: True)
name: str | None = None


class Child4(BaseModel):
model_config = ConfigDict(model_bool="child")
name: str | None = None
child: str | None = None


class Child5(BaseModel):
model_config = ConfigDict(model_bool="child")
name: str | None = None
child: str | None = Field(default=None, exclude=True)


def _create_parent_with_child(child: type[BaseModel]) -> type[BaseModel]:
model = create_model(
"Parent",
parent=(str, ...),
children=(list[child], ...),
__config__=ConfigDict(group_by="parent"),
)

return model


parent_child_parameters = [
ModelBindingsMapperParameter(
model=_create_parent_with_child(Child1),
bindings=bindings,
expected=[
{"parent": "x", "children": [{"name": "foo"}]},
{"parent": "y", "children": []},
{"parent": "z", "children": []},
],
),
ModelBindingsMapperParameter(
model=_create_parent_with_child(Child2),
bindings=bindings,
expected=[
{"parent": "x", "children": [{"name": "foo", "child": "c"}]},
{
"parent": "y",
"children": [
{"name": None, "child": "d"},
{"name": None, "child": "e"},
],
},
{"parent": "z", "children": [{"name": None, "child": None}]},
],
),
ModelBindingsMapperParameter(
model=_create_parent_with_child(Child3),
bindings=bindings,
expected=[
{"parent": "x", "children": [{"name": "foo"}]},
{"parent": "y", "children": [{"name": None}]},
{"parent": "z", "children": [{"name": None}]},
],
),
ModelBindingsMapperParameter(
model=_create_parent_with_child(Child4),
bindings=bindings,
expected=[
{"parent": "x", "children": [{"name": "foo", "child": "c"}]},
{
"parent": "y",
"children": [
{"name": None, "child": "d"},
{"name": None, "child": "e"},
],
},
{"parent": "z", "children": []},
],
),
ModelBindingsMapperParameter(
model=_create_parent_with_child(Child5),
bindings=bindings,
expected=[
{"parent": "x", "children": [{"name": "foo"}]},
{"parent": "y", "children": [{"name": None}, {"name": None}]},
{"parent": "z", "children": []},
],
),
]
24 changes: 24 additions & 0 deletions tests/tests_mapper/test_model_bindings_mapper_model_bool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Pytest entry point for testing rdfproxy.mapper.ModelBindingsMapper with model_flag config."""

import pytest

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


@pytest.mark.parametrize(
["model", "bindings", "expected"],
parent_child_parameters,
)
def test_basic_model_bindings_mapper(model, bindings, expected):
"""Test for rdfproxy.ModelBindingsMapper with model_bool config..
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)
models: list[BaseModel] = mapper.get_models()
assert [model.model_dump() for model in models] == expected

0 comments on commit 41291be

Please sign in to comment.