From 41291bea37d1a977c1d3a1b0b6c4030bd1ce6058 Mon Sep 17 00:00:00 2001 From: Lukas Plank Date: Wed, 6 Nov 2024 12:50:39 +0100 Subject: [PATCH] test: implement tests for ModelBindingsMapper with model_bool config The tests cover all cases discussed in #110. --- ...l_bindings_mapper_model_bool_parameters.py | 114 ++++++++++++++++++ .../test_model_bindings_mapper_model_bool.py | 24 ++++ 2 files changed, 138 insertions(+) create mode 100644 tests/data/parameters/model_bindings_mapper_model_bool_parameters.py create mode 100644 tests/tests_mapper/test_model_bindings_mapper_model_bool.py diff --git a/tests/data/parameters/model_bindings_mapper_model_bool_parameters.py b/tests/data/parameters/model_bindings_mapper_model_bool_parameters.py new file mode 100644 index 0000000..2b31d5a --- /dev/null +++ b/tests/data/parameters/model_bindings_mapper_model_bool_parameters.py @@ -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": []}, + ], + ), +] diff --git a/tests/tests_mapper/test_model_bindings_mapper_model_bool.py b/tests/tests_mapper/test_model_bindings_mapper_model_bool.py new file mode 100644 index 0000000..f9bde75 --- /dev/null +++ b/tests/tests_mapper/test_model_bindings_mapper_model_bool.py @@ -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