Skip to content

Commit f321523

Browse files
authored
Add support for clearing optional attributes (#548)
* add support for clearing optional dropdown attributes and enhance tests for dropdown handling * add changelog entry for clearing optional dropdown attributes * refactor: update branch handling in changelog and allow clearing optional attributes if mutated
1 parent 10618ee commit f321523

File tree

5 files changed

+74
-1
lines changed

5 files changed

+74
-1
lines changed

changelog/535.fixed.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Fix branch handling in `_run_transform` and `execute_graphql_query` functions in Infrahubctl to use environment variables for branch management.
1+
Fix branch handling in `_run_transform` and `execute_graphql_query` functions in Infrahubctl to use environment variables for branch management.

changelog/549.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow the ability to clear optional attributes by setting them to None if they have been mutated by the user.

infrahub_sdk/node/attribute.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ def _generate_input_data(self) -> dict | None:
7676
variables: dict[str, Any] = {}
7777

7878
if self.value is None:
79+
if self._schema.optional and self.value_has_been_mutated:
80+
data["value"] = None
7981
return data
8082

8183
if isinstance(self.value, str):

tests/unit/sdk/conftest.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,48 @@ async def location_schema() -> NodeSchemaAPI:
177177
return NodeSchema(**data).convert_api() # type: ignore
178178

179179

180+
@pytest.fixture
181+
async def location_schema_with_dropdown() -> NodeSchemaAPI:
182+
data = {
183+
"name": "Location",
184+
"namespace": "Builtin",
185+
"default_filter": "name__value",
186+
"attributes": [
187+
{"name": "name", "kind": "String", "unique": True},
188+
{"name": "description", "kind": "String", "optional": True},
189+
{"name": "type", "kind": "String"},
190+
{
191+
"name": "status",
192+
"kind": "Dropdown",
193+
"optional": True,
194+
"choices": [{"name": "active", "label": "Active"}, {"name": "planning", "label": "Planning"}],
195+
},
196+
],
197+
"relationships": [
198+
{
199+
"name": "tags",
200+
"peer": "BuiltinTag",
201+
"optional": True,
202+
"cardinality": "many",
203+
},
204+
{
205+
"name": "primary_tag",
206+
"peer": "BuiltinTag",
207+
"optional": True,
208+
"cardinality": "one",
209+
},
210+
{
211+
"name": "member_of_groups",
212+
"peer": "CoreGroup",
213+
"optional": True,
214+
"cardinality": "many",
215+
"kind": "Group",
216+
},
217+
],
218+
}
219+
return NodeSchema(**data).convert_api() # type: ignore
220+
221+
180222
@pytest.fixture
181223
async def schema_with_hfid() -> dict[str, NodeSchemaAPI]:
182224
data = {

tests/unit/sdk/test_node.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,34 @@ async def test_create_input_data(client, location_schema: NodeSchemaAPI, client_
13701370
}
13711371

13721372

1373+
@pytest.mark.parametrize("client_type", client_types)
1374+
async def test_create_input_data_with_dropdown(client, location_schema_with_dropdown, client_type) -> None:
1375+
"""Validate input data including dropdown field"""
1376+
data = {
1377+
"name": {"value": "JFK1"},
1378+
"description": {"value": "JFK Airport"},
1379+
"type": {"value": "SITE"},
1380+
"status": {"value": "active"},
1381+
}
1382+
1383+
if client_type == "standard":
1384+
node = InfrahubNode(client=client, schema=location_schema_with_dropdown, data=data)
1385+
else:
1386+
node = InfrahubNodeSync(client=client, schema=location_schema_with_dropdown, data=data)
1387+
1388+
assert node.status.value == "active"
1389+
node.status = None
1390+
assert node._generate_input_data()["data"] == {
1391+
"data": {
1392+
"name": {"value": "JFK1"},
1393+
"description": {"value": "JFK Airport"},
1394+
"type": {"value": "SITE"},
1395+
"status": {"value": None},
1396+
"primary_tag": None,
1397+
}
1398+
}
1399+
1400+
13731401
@pytest.mark.parametrize("client_type", client_types)
13741402
async def test_create_input_data__with_relationships_02(client, location_schema, client_type) -> None:
13751403
"""Validate input data with variables that needs replacements"""

0 commit comments

Comments
 (0)