Python: Fixed declarative samples (#4051)

* Updated declarative kind mapping

* Fixed required property handling

* Updated inline yaml sample

* Fixed remaining declarative samples

* Added lazy initialization for PowerFx engine

* Small fix
This commit is contained in:
Dmytro Struk
2026-02-18 15:08:31 -08:00
committed by GitHub
Unverified
parent 1b87a07377
commit 57da1bcfeb
9 changed files with 173 additions and 16 deletions
@@ -103,6 +103,50 @@ class TestProperty:
assert prop.description == "A test property"
assert prop.required is True
def test_property_from_dict_type_maps_to_kind(self):
"""Test that 'type' field in YAML is mapped to 'kind' internally."""
data = {
"name": "test_prop",
"type": "string",
"description": "A test property",
"required": True,
}
prop = Property.from_dict(data)
assert prop.name == "test_prop"
assert prop.kind == "string"
def test_property_from_dict_kind_takes_precedence_over_type(self):
"""Test that 'kind' takes precedence when both 'type' and 'kind' are present."""
data = {
"name": "test_prop",
"type": "integer",
"kind": "string",
}
prop = Property.from_dict(data)
assert prop.kind == "string"
def test_property_from_dict_type_dispatches_to_array(self):
"""Test that 'type: array' correctly dispatches to ArrayProperty."""
data = {
"name": "test_array",
"type": "array",
"items": {"type": "string"},
}
prop = Property.from_dict(data)
assert isinstance(prop, ArrayProperty)
assert prop.kind == "array"
def test_property_from_dict_type_dispatches_to_object(self):
"""Test that 'type: object' correctly dispatches to ObjectProperty."""
data = {
"name": "test_object",
"type": "object",
"properties": {"field": {"type": "string"}},
}
prop = Property.from_dict(data)
assert isinstance(prop, ObjectProperty)
assert prop.kind == "object"
class TestArrayProperty:
"""Tests for ArrayProperty class."""
@@ -230,6 +274,29 @@ class TestPropertySchema:
assert age_prop.kind == "integer"
assert age_prop.required is True
def test_property_schema_with_type_field_produces_correct_json_schema(self):
"""Test that PropertySchema with 'type' fields (YAML spec format) produces valid JSON schema."""
data = {
"properties": {
"language": {"type": "string", "required": True, "description": "The language."},
"answer": {"type": "string", "required": False, "description": "The answer."},
},
}
schema = PropertySchema.from_dict(data)
assert len(schema.properties) == 2
lang_prop = next(p for p in schema.properties if p.name == "language")
assert lang_prop.kind == "string"
json_schema = schema.to_json_schema()
assert json_schema["type"] == "object"
assert json_schema["properties"]["language"]["type"] == "string"
assert json_schema["properties"]["answer"]["type"] == "string"
# required is a top-level array, not a per-property boolean
assert json_schema["required"] == ["language"]
assert "required" not in json_schema["properties"]["language"]
assert "required" not in json_schema["properties"]["answer"]
class TestConnection:
"""Tests for Connection base class."""