Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions tests/unit/test_schema_generated_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
}


Expand All @@ -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 (
Expand Down
50 changes: 37 additions & 13 deletions tests/unit/test_schema_offline_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,49 +519,63 @@ 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",
schema=_extension_node_schema(
{"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",
schema=_extension_node_schema(
{"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'",
),
]

Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
Loading