Skip to content

Commit

Permalink
ever more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cutoffthetop committed Dec 19, 2024
1 parent 4bdafae commit 9d4d5e6
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 11 deletions.
15 changes: 7 additions & 8 deletions mex/editor/edit/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,15 @@ def _transform_field_to_subtractive(
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
):
"""Transform an editor field back to subtractive rule values."""
if field.name in MERGEABLE_FIELDS_BY_CLASS_NAME[subtractive.entityType]:
subtracted_values = getattr(subtractive, field.name)
merged_class_name = ensure_prefix(subtractive.stemType, "Merged")
for primary_source in field.primary_sources:
for value in primary_source.editor_values:
if not value.enabled:
for editor_value in primary_source.editor_values:
if not editor_value.enabled:
subtracted_value = _transform_editor_value_to_model_value(
value,
field.name,
ensure_prefix(subtractive.stemType, "Merged"),
editor_value, field.name, merged_class_name
)
if subtracted_value not in subtracted_values:
subtracted_values.append(subtracted_value)
Expand Down
97 changes: 94 additions & 3 deletions tests/edit/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,100 @@ def test_transform_render_value_to_model_type(
assert model_value == expected


def test_transform_field_to_subtractive() -> None:
assert _transform_field_to_subtractive()
@pytest.mark.parametrize(
("field", "expected"),
[
(
EditorField(
name="unknownField",
primary_sources=[
EditorPrimarySource(
name=EditorValue(text="Primary Source 1"),
identifier=MergedPrimarySourceIdentifier("PrimarySource001"),
)
],
),
{},
),
(
EditorField(
name="familyName",
primary_sources=[
EditorPrimarySource(
name=EditorValue(text="Primary Source 1"),
identifier=MergedPrimarySourceIdentifier("PrimarySource001"),
editor_values=[
EditorValue(text="active", enabled=True),
EditorValue(text="inactive", enabled=False),
],
),
EditorPrimarySource(
name=EditorValue(text="Primary Source 2"),
identifier=MergedPrimarySourceIdentifier("PrimarySource002"),
editor_values=[
EditorValue(text="another inactive", enabled=False),
],
),
],
),
{"familyName": ["inactive", "another inactive"]},
),
],
)
def test_transform_field_to_subtractive(
field: EditorField, expected: dict[str, object]
) -> None:
subtractive = SubtractivePerson()
_transform_field_to_subtractive(field, subtractive)
assert subtractive.model_dump(exclude_defaults=True) == expected


def test_transform_fields_to_rule_set() -> None:
assert transform_fields_to_rule_set()
rule_set_request = transform_fields_to_rule_set(
"Person",
[
EditorField(
name="givenName",
primary_sources=[
EditorPrimarySource(
name=EditorValue(text="Enabled Primary Source"),
identifier=MergedPrimarySourceIdentifier("PrimarySource001"),
),
EditorPrimarySource(
name=EditorValue(text="Prevented Primary Source"),
identifier=MergedPrimarySourceIdentifier("PrimarySource002"),
enabled=False,
),
],
),
EditorField(
name="familyName",
primary_sources=[
EditorPrimarySource(
name=EditorValue(text="Primary Source 1"),
identifier=MergedPrimarySourceIdentifier("PrimarySource001"),
editor_values=[
EditorValue(text="active", enabled=True),
EditorValue(text="inactive", enabled=False),
],
),
EditorPrimarySource(
name=EditorValue(text="Primary Source 2"),
identifier=MergedPrimarySourceIdentifier("PrimarySource002"),
editor_values=[
EditorValue(text="another inactive", enabled=False),
],
),
],
),
],
)
assert rule_set_request.entityType == "PersonRuleSetRequest"
assert rule_set_request.model_dump(exclude_defaults=True) == {
"subtractive": {
"familyName": ["inactive", "another inactive"],
},
"preventive": {
"givenName": ["PrimarySource002"],
},
}

0 comments on commit 9d4d5e6

Please sign in to comment.