-
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 tests for ModelBindingsMapper with model_bool config
The tests cover all cases discussed in #110.
- Loading branch information
Showing
2 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
tests/data/parameters/model_bindings_mapper_model_bool_parameters.py
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,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
24
tests/tests_mapper/test_model_bindings_mapper_model_bool.py
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,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 |