Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions changelog/624.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed nested object template range expansion.
3 changes: 2 additions & 1 deletion infrahub_sdk/spec/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ async def process(self, client: InfrahubClient, branch: str | None = None) -> No
position=[idx + 1],
branch=branch,
default_schema_kind=self.kind,
parameters=self.parameters,
)

@classmethod
Expand Down Expand Up @@ -509,7 +510,7 @@ async def create_node(
context=context,
branch=branch,
default_schema_kind=default_schema_kind,
parameters=parameters,
parameters=InfrahubObjectParameters(**data[rel].get("parameters")) if "parameters" in data[rel] else None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the None be parameters here? I'm not sure how parameters passed into create_node are expected to behave in combination with parameters defined inside of data

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If none is passed to the function it will create a new instance of InfrahubObjectParameters with parameters = parameters or InfrahubObjectParameters()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the other calls to create_related_nodes in this method do something similar with overriding parameters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct. There is one other instance where range expansion is possible. I have added that but in the case where RelationshipDataFormat.ONE_OBJ is the relation there is no need for parameters as you cant expand into a cardinality of ONE.

)

return node
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/sdk/spec/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ def location_expansion_multiple_ranges_bad_syntax(root_location: dict) -> dict:
return location


@pytest.fixture
def location_with_non_dict_parameters(root_location: dict) -> dict:
data = [{"name": "Mexico", "type": "Country"}]
location = root_location.copy()
location["spec"]["data"] = data
location["spec"]["parameters"] = "not_a_dict"
return location


@pytest.fixture
def location_with_empty_parameters(root_location: dict) -> dict:
data = [{"name": "Mexico", "type": "Country"}]
location = root_location.copy()
location["spec"]["data"] = data
location["spec"]["parameters"] = {}
return location


async def test_validate_object(client: InfrahubClient, schema_query_01_data: dict, location_mexico_01) -> None:
client.schema.set_cache(schema=schema_query_01_data, branch="main")
obj = ObjectFile(location="some/path", content=location_mexico_01)
Expand Down Expand Up @@ -221,3 +239,27 @@ async def test_get_relationship_info_tags(
rel_info = await get_relationship_info(client_with_schema_01, location_schema, "tags", data)
assert rel_info.is_valid == is_valid
assert rel_info.format == format


async def test_parameters_top_level(client_with_schema_01: InfrahubClient, location_expansion) -> None:
obj = ObjectFile(location="some/path", content=location_expansion)
await obj.validate_format(client=client_with_schema_01)
assert obj.spec.parameters.expand_range is True


async def test_parameters_missing(client_with_schema_01: InfrahubClient, location_mexico_01) -> None:
obj = ObjectFile(location="some/path", content=location_mexico_01)
await obj.validate_format(client=client_with_schema_01)
assert hasattr(obj.spec.parameters, "expand_range")


async def test_parameters_empty_dict(client_with_schema_01: InfrahubClient, location_with_empty_parameters) -> None:
obj = ObjectFile(location="some/path", content=location_with_empty_parameters)
await obj.validate_format(client=client_with_schema_01)
assert hasattr(obj.spec.parameters, "expand_range")


async def test_parameters_non_dict(client_with_schema_01: InfrahubClient, location_with_non_dict_parameters) -> None:
obj = ObjectFile(location="some/path", content=location_with_non_dict_parameters)
with pytest.raises(ValidationError):
await obj.validate_format(client=client_with_schema_01)
Loading