Skip to content

Commit

Permalink
even more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cutoffthetop committed Dec 19, 2024
1 parent 782d36e commit 4bdafae
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 22 deletions.
4 changes: 2 additions & 2 deletions mex/editor/edit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class EditorPrimarySource(rx.Base):

name: EditorValue
identifier: MergedPrimarySourceIdentifier
editor_values: list[EditorValue]
editor_values: list[EditorValue] = []
enabled: bool = True


class EditorField(rx.Base):
"""Model for describing the editor state for a single field."""

name: str
primary_sources: list[EditorPrimarySource]
primary_sources: list[EditorPrimarySource] = []
45 changes: 34 additions & 11 deletions mex/editor/edit/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,16 @@ def transform_models_to_fields(
subtractive: AnySubtractiveModel | None = None,
preventive: AnyPreventiveModel | None = None,
) -> list[EditorField]:
"""Convert a list of models and optional rules into editor field models."""
"""Convert the given models and optional rules into editor field models.
Args:
models: A series of extracted, merged or additive models
subtractive: An optional subtractive rule model
preventive: An optional preventive rule model
Returns:
A list of editor field instances
"""
fields_by_name = {
field_name: EditorField(name=field_name, primary_sources=[])
for field_name in {f for m in models for f in m.get_all_fields()}
Expand All @@ -114,21 +123,25 @@ def transform_models_to_fields(


def _transform_field_to_preventive(
field: EditorField, preventive: AnyPreventiveModel
field: EditorField,
preventive: AnyPreventiveModel,
) -> None:
if (field.name in MERGEABLE_FIELDS_BY_CLASS_NAME[preventive.entityType]) and (
(prevented_sources := getattr(preventive, field.name)) is not None
):
"""Transform an editor field back to a preventive rule field."""
if field.name in MERGEABLE_FIELDS_BY_CLASS_NAME[preventive.entityType]:
prevented_sources = getattr(preventive, field.name)
for primary_source in field.primary_sources:
if not primary_source.enabled and (
primary_source.identifier not in prevented_sources
):
prevented_sources.append(primary_source.identifier)


def _transform_render_value_to_model_type(
value: EditorValue, field_name: str, class_name: str
def _transform_editor_value_to_model_value(
value: EditorValue,
field_name: str,
class_name: str,
) -> AnyNestedModel | AnyPrimitiveType | AnyTemporalEntity | AnyVocabularyEnum:
"""Transform an editor value back to a value to be used in mex.common.models."""
if field_name in LINK_FIELDS_BY_CLASS_NAME[class_name]:
return Link(url=value.href, language=value.badge, title=value.text)
if field_name in TEXT_FIELDS_BY_CLASS_NAME[class_name]:
Expand All @@ -143,15 +156,16 @@ def _transform_render_value_to_model_type(


def _transform_field_to_subtractive(
field: EditorField, subtractive: AnySubtractiveModel
field: EditorField,
subtractive: AnySubtractiveModel,
) -> None:
if (field.name in MERGEABLE_FIELDS_BY_CLASS_NAME[subtractive.entityType]) and (
(subtracted_values := getattr(subtractive, field.name)) is not None
):
for primary_source in field.primary_sources:
for value in primary_source.editor_values:
if not value.enabled:
subtracted_value = _transform_render_value_to_model_type(
subtracted_value = _transform_editor_value_to_model_value(
value,
field.name,
ensure_prefix(subtractive.stemType, "Merged"),
Expand All @@ -161,9 +175,18 @@ def _transform_field_to_subtractive(


def transform_fields_to_rule_set(
stem_type: str, fields: list[EditorField]
stem_type: str,
fields: list[EditorField],
) -> AnyRuleSetRequest:
"""Transform the given fields to a rule set of the given stem type."""
"""Transform the given fields to a rule set of the given stem type.
Args:
stem_type: The stemType the resulting rule set should have
fields: A list of editor fields to convert into rules
Returns:
Any rule set request model
"""
rule_set_class = RULE_SET_REQUEST_CLASSES_BY_NAME[
ensure_postfix(stem_type, "RuleSetRequest")
]
Expand Down
188 changes: 179 additions & 9 deletions tests/edit/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
MEX_PRIMARY_SOURCE_STABLE_TARGET_ID,
AdditiveActivity,
AdditiveContactPoint,
AdditivePerson,
AnyAdditiveModel,
AnyExtractedModel,
AnyMergedModel,
Expand All @@ -14,6 +15,7 @@
ExtractedPerson,
MergedConsent,
MergedContactPoint,
MergedPerson,
PreventivePerson,
SubtractiveActivity,
SubtractivePerson,
Expand All @@ -26,13 +28,21 @@
MergedContactPointIdentifier,
MergedPersonIdentifier,
MergedPrimarySourceIdentifier,
Text,
TextLanguage,
Year,
YearMonthDayTime,
)
from mex.editor.edit.models import EditorField, EditorPrimarySource
from mex.editor.edit.transform import (
_get_primary_source_id_from_model,
_transform_editor_value_to_model_value,
_transform_field_to_preventive,
_transform_field_to_subtractive,
_transform_model_to_editor_primary_source,
_transform_model_values_to_editor_values,
transform_fields_to_rule_set,
transform_models_to_fields,
)
from mex.editor.models import EditorValue

Expand Down Expand Up @@ -149,7 +159,6 @@ def test_get_primary_source_id_from_model(
text="http://pavyzdys",
href="http://pavyzdys",
external=True,
enabled=True,
),
],
),
Expand Down Expand Up @@ -200,7 +209,6 @@ def test_transform_model_values_to_editor_values(
text="primarySourceId", href="/item/primarySourceId"
),
identifier=MergedPrimarySourceIdentifier("primarySourceId"),
editor_values=[],
)
],
),
Expand Down Expand Up @@ -263,20 +271,182 @@ def test_transform_model_to_editor_primary_source(


def test_transform_models_to_fields() -> None:
pass
editor_fields = transform_models_to_fields(
MergedPerson(
identifier=MergedPersonIdentifier.generate(), email=["person@rki.de"]
),
AdditivePerson(givenName=["Good"]),
subtractive=SubtractivePerson(givenName=["Bad"]),
preventive=PreventivePerson(memberOf=[MEX_PRIMARY_SOURCE_STABLE_TARGET_ID]),
)

assert len(editor_fields) == len(MergedPerson.get_all_fields())
field_names_of_interest = ("entityType", "givenName", "memberOf")
fields_of_interest = [f for f in editor_fields if f.name in field_names_of_interest]
assert fields_of_interest == [
EditorField(
name="entityType",
primary_sources=[
EditorPrimarySource(
name=EditorValue(
text="00000000000000", href="/item/00000000000000"
),
identifier=MergedPrimarySourceIdentifier("00000000000000"),
editor_values=[EditorValue(text="MergedPerson")],
),
EditorPrimarySource(
name=EditorValue(
text="00000000000000", href="/item/00000000000000"
),
identifier=MergedPrimarySourceIdentifier("00000000000000"),
editor_values=[EditorValue(text="AdditivePerson")],
),
],
),
EditorField(
name="givenName",
primary_sources=[
EditorPrimarySource(
name=EditorValue(
text="00000000000000",
href="/item/00000000000000",
),
identifier=MergedPrimarySourceIdentifier("00000000000000"),
),
EditorPrimarySource(
name=EditorValue(
text="00000000000000",
href="/item/00000000000000",
),
identifier=MergedPrimarySourceIdentifier("00000000000000"),
editor_values=[EditorValue(text="Good")],
),
],
),
EditorField(
name="memberOf",
primary_sources=[
EditorPrimarySource(
name=EditorValue(
text="00000000000000",
href="/item/00000000000000",
),
identifier=MergedPrimarySourceIdentifier("00000000000000"),
enabled=False,
),
EditorPrimarySource(
name=EditorValue(
text="00000000000000",
href="/item/00000000000000",
),
identifier=MergedPrimarySourceIdentifier("00000000000000"),
enabled=False,
),
],
),
]


def test_transform_field_to_preventive() -> None:
pass
@pytest.mark.parametrize(
("field", "expected"),
[
(
EditorField(
name="unknownField",
primary_sources=[
EditorPrimarySource(
enabled=True,
name=EditorValue(text="Enabled Primary Source"),
identifier=MergedPrimarySourceIdentifier(
"enabledPrimarySourceId"
),
)
],
),
{},
),
(
EditorField(
name="familyName",
primary_sources=[
EditorPrimarySource(
enabled=True,
name=EditorValue(text="Enabled Primary Source"),
identifier=MergedPrimarySourceIdentifier(
"enabledPrimarySourceId"
),
),
EditorPrimarySource(
enabled=False,
name=EditorValue(text="Prevented Primary Source"),
identifier=MergedPrimarySourceIdentifier(
"preventedPrimarySourceId"
),
),
],
),
{"familyName": ["preventedPrimarySourceId"]},
),
],
)
def test_transform_field_to_preventive(
field: EditorField, expected: dict[str, object]
) -> None:
preventive = PreventivePerson()
_transform_field_to_preventive(field, preventive)
assert preventive.model_dump(exclude_defaults=True) == expected


def test_transform_render_value_to_model_type() -> None:
pass
@pytest.mark.parametrize(
("editor_value", "field_name", "class_name", "expected"),
[
(
EditorValue(text="Titel", badge="de", href="https://beispiel"),
"documentation",
"MergedResource",
Link(url="https://beispiel", language=LinkLanguage.DE, title="Titel"),
),
(
EditorValue(text="Beispiel Text", badge="de"),
"alternativeName",
"ExtractedOrganization",
Text(language=TextLanguage.DE, value="Beispiel Text"),
),
(
EditorValue(text="VALID_FOR_PROCESSING", badge="ConsentStatus"),
"hasConsentType",
"MergedConsent",
ConsentStatus["VALID_FOR_PROCESSING"],
),
(
EditorValue(text="2004", badge="year"),
"start",
"ExtractedActivity",
Year(2004),
),
(
EditorValue(text="Funds for Funding e.V."),
"fundingProgram",
"ExtractedActivity",
"Funds for Funding e.V.",
),
],
ids=["link", "text", "vocab", "temporal", "string"],
)
def test_transform_render_value_to_model_type(
editor_value: EditorValue, field_name: str, class_name: str, expected: object
) -> None:
model_value = _transform_editor_value_to_model_value(
editor_value,
field_name,
class_name,
)
assert model_value == expected


def test_transform_field_to_subtractive() -> None:
pass
assert _transform_field_to_subtractive()


def test_transform_fields_to_rule_set() -> None:
pass
assert transform_fields_to_rule_set()

0 comments on commit 4bdafae

Please sign in to comment.