From 745be3f848e3b91020b6052a2c861ff5b8a53b20 Mon Sep 17 00:00:00 2001 From: Saltaferis Dimitrios Date: Fri, 13 Nov 2026 03:05:23 +0000 Subject: [PATCH] test(schema): assert exact offline-validation errors and fold AttributeKind into the enum table The schema tests added by #1135 checked validator errors with `any("" in message ...)`. That passes as long as some message mentions the field, so a changed message text or an extra error does not fail the suite. Each of those tests now asserts exactly one error with its full message, or its field, reason and received value. The separate AttributeKind test checked only that "Text" is present and "String" is absent. AttributeKind is now an entry of the parametrized enum table, which also pins the member order. Co-Authored-By: Claude Opus 5.5 --- tests/unit/test_schema_generated_models.py | 35 ++++++++++---- tests/unit/test_schema_offline_validation.py | 50 +++++++++++++++----- 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/tests/unit/test_schema_generated_models.py b/tests/unit/test_schema_generated_models.py index 7e7eca521..b68f9e488 100644 --- a/tests/unit/test_schema_generated_models.py +++ b/tests/unit/test_schema_generated_models.py @@ -181,6 +181,32 @@ def test_extension_models_ignore_extra_fields(name: str) -> None: "SchemaState": ["present", "absent"], "SchemaAttributeDisplay": ["default", "extra"], "ComputedAttributeKind": ["User", "Jinja2", "TransformPython"], + # The deprecated "String" kind is dropped from the generated enum. + "AttributeKind": [ + "ID", + "Dropdown", + "Text", + "TextArea", + "DateTime", + "Email", + "Password", + "HashedPassword", + "URL", + "File", + "MacAddress", + "Color", + "Number", + "NumberPool", + "Bandwidth", + "IPHost", + "IPNetwork", + "IPAddress", + "Boolean", + "Checkbox", + "List", + "JSON", + "Any", + ], } @@ -192,15 +218,6 @@ def test_generated_enums_are_str_enum_classes_with_expected_values(enum_name: st assert [member.value for member in enum_cls] == _EXPECTED_ENUM_VALUES[enum_name] -def test_attribute_kind_enum_present_without_deprecated_string_member() -> None: - assert issubclass(enums_module.AttributeKind, Enum) - assert issubclass(enums_module.AttributeKind, str) - values = [member.value for member in enums_module.AttributeKind] - # The deprecated "String" kind is dropped from the generated enum. - assert "String" not in values - assert "Text" in values - - def test_constrained_fields_are_typed_with_generated_enums() -> None: # The constrained fields reference the dedicated enum classes, not inline Literals. assert ( diff --git a/tests/unit/test_schema_offline_validation.py b/tests/unit/test_schema_offline_validation.py index a673b8297..4d1f268ab 100644 --- a/tests/unit/test_schema_offline_validation.py +++ b/tests/unit/test_schema_offline_validation.py @@ -519,33 +519,45 @@ class OutOfEnumCase: # Exact dotted path expected among the reported error fields. For a discriminated union the # unknown discriminator is reported against the container (attribute), not a leaf field. expected_field: str - invalid_value: str + # Exact value reported as received. For a discriminated union this is the whole container. + expected_input: object + # Start of the reported reason. For a literal enum it is the whole reason. For a discriminated + # union pydantic then lists every accepted tag in the union's order, which is not pinned here. + expected_reason: str +_UNKNOWN_KIND_TAG = "Input tag 'NotARealKind' found using 'kind' does not match any of the expected tags: " + OUT_OF_ENUM_CASES = [ OutOfEnumCase( name="attribute-kind", schema=_attribute_out_of_enum_kind("NotARealKind"), expected_field="nodes[0].attributes[0]", - invalid_value="NotARealKind", + expected_input={"name": "hostname", "kind": "NotARealKind"}, + expected_reason=_UNKNOWN_KIND_TAG, ), OutOfEnumCase( name="relationship-kind", schema=_relationship_out_of_enum("kind", "NotARealKind"), expected_field="nodes[0].relationships[0].kind", - invalid_value="NotARealKind", + expected_input="NotARealKind", + expected_reason=( + "Input should be 'Generic', 'Attribute', 'Component', 'Parent', 'Group', 'Hierarchy', 'Profile' or 'Template'" + ), ), OutOfEnumCase( name="relationship-cardinality", schema=_relationship_out_of_enum("cardinality", "both"), expected_field="nodes[0].relationships[0].cardinality", - invalid_value="both", + expected_input="both", + expected_reason="Input should be 'one' or 'many'", ), OutOfEnumCase( name="computed-attribute-kind", schema=_schema_with_computed_attribute({"kind": "NotARealKind"}), expected_field="nodes[0].attributes[0].Text.computed_attribute", - invalid_value="NotARealKind", + expected_input={"kind": "NotARealKind"}, + expected_reason=_UNKNOWN_KIND_TAG, ), OutOfEnumCase( name="extension-attribute-kind", @@ -553,7 +565,8 @@ class OutOfEnumCase: {"kind": "InfraDevice", "attributes": [{"name": "extra", "kind": "NotARealKind"}]} ), expected_field="extensions.nodes[0].attributes[0]", - invalid_value="NotARealKind", + expected_input={"name": "extra", "kind": "NotARealKind"}, + expected_reason=_UNKNOWN_KIND_TAG, ), OutOfEnumCase( name="extension-relationship-cardinality", @@ -561,7 +574,8 @@ class OutOfEnumCase: {"kind": "InfraDevice", "relationships": [{"name": "peers", "peer": "InfraDevice", "cardinality": "both"}]} ), expected_field="extensions.nodes[0].relationships[0].cardinality", - invalid_value="both", + expected_input="both", + expected_reason="Input should be 'one' or 'many'", ), ] @@ -571,8 +585,12 @@ def test_out_of_enum_value_is_rejected_naming_field_and_value(case: OutOfEnumCas result = validate_schema(schema=case.schema) assert result.valid is False - assert case.expected_field in _fields_named(result), result.messages - assert any(case.invalid_value in message for message in result.messages), result.messages + assert len(result.errors) == 1, result.messages + error = result.errors[0] + assert error.field == case.expected_field + assert error.input == case.expected_input + assert error.reason.startswith(case.expected_reason), error.reason + assert error.message == f"{case.expected_field}: {error.reason} (received: {case.expected_input!r})" def test_enum_backed_relationship_cardinality_out_of_enum_value_is_rejected() -> None: @@ -582,7 +600,9 @@ def test_enum_backed_relationship_cardinality_out_of_enum_value_is_rejected() -> result = validate_schema(schema=schema) assert result.valid is False - assert any("cardinality" in message for message in result.messages), result.messages + assert result.messages == [ + "nodes[0].relationships[0].cardinality: Input should be 'one' or 'many' (received: 'both')" + ] def test_missing_version_is_rejected() -> None: @@ -675,14 +695,16 @@ def test_computed_attribute_jinja2_without_template_is_rejected() -> None: result = validate_schema(schema=_schema_with_computed_attribute({"kind": "Jinja2"})) assert result.valid is False - assert any("jinja2_template" in message for message in result.messages), result.messages + assert result.messages == ["nodes[0].attributes[0].Text.computed_attribute.Jinja2.jinja2_template: Field required"] def test_computed_attribute_transform_python_without_transform_is_rejected() -> None: result = validate_schema(schema=_schema_with_computed_attribute({"kind": "TransformPython"})) assert result.valid is False - assert any("transform" in message for message in result.messages), result.messages + assert result.messages == [ + "nodes[0].attributes[0].Text.computed_attribute.TransformPython.transform: Field required" + ] def test_valid_choice_passes() -> None: @@ -695,7 +717,9 @@ def test_choice_bad_color_is_rejected() -> None: result = validate_schema(schema=_schema_with_choices([{"name": "active", "color": "not-a-color"}])) assert result.valid is False - assert any("color" in message for message in result.messages), result.messages + assert result.messages == [ + r"nodes[0].attributes[0].Text.choices[0].color: String should match pattern '#[0-9a-fA-F]{6}\b' (received: 'not-a-color')" + ] def test_valid_text_parameters_pass() -> None: