Skip to content

Change behavior of include to only include the requested fields #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions changelog/192.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
When using include as part of a query, the default behavior has changed.
Only the fields defined in `include` will be returned now, where previously the Python SDK would add the fields provided to the default one.
20 changes: 6 additions & 14 deletions infrahub_sdk/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,11 @@ def __getitem__(self, item: int) -> RelatedNode:

async def fetch(self) -> None:
if not self.initialized:
exclude = self.node._schema.relationship_names + self.node._schema.attribute_names
exclude.remove(self.schema.name)
node = await self.client.get(
kind=self.node._schema.kind,
id=self.node.id,
branch=self.branch,
include=[self.schema.name],
exclude=exclude,
include=[self.schema.name]
)
rm = getattr(node, self.schema.name)
self.peers = rm.peers
Expand Down Expand Up @@ -614,14 +611,11 @@ def __getitem__(self, item: int) -> RelatedNodeSync:

def fetch(self) -> None:
if not self.initialized:
exclude = self.node._schema.relationship_names + self.node._schema.attribute_names
exclude.remove(self.schema.name)
node = self.client.get(
kind=self.node._schema.kind,
id=self.node.id,
branch=self.branch,
include=[self.schema.name],
exclude=exclude,
)
rm = getattr(node, self.schema.name)
self.peers = rm.peers
Expand Down Expand Up @@ -992,9 +986,7 @@ def generate_query_data_init(
data["@filters"]["limit"] = limit

if include and exclude:
in_both, _, _ = compare_lists(include, exclude)
if in_both:
raise ValueError(f"{in_both} are part of both include and exclude")
raise ValueError("include and exclude are exclusive, they shouldn't be used together")

if partial_match:
data["@filters"]["partial_match"] = True
Expand Down Expand Up @@ -1244,7 +1236,7 @@ async def generate_query_data_node(
data: dict[str, Any] = {}

for attr_name in self._attributes:
if exclude and attr_name in exclude:
if (exclude and attr_name in exclude) or (include and attr_name not in include):
continue

attr: Attribute = getattr(self, attr_name)
Expand All @@ -1262,7 +1254,7 @@ async def generate_query_data_node(
data[attr_name] = {"@alias": f"__alias__{self._schema.kind}__{attr_name}"}

for rel_name in self._relationships:
if exclude and rel_name in exclude:
if (exclude and rel_name in exclude) or (include and rel_name not in include):
continue

rel_schema = self._schema.get_relationship(name=rel_name)
Expand Down Expand Up @@ -1749,7 +1741,7 @@ def generate_query_data_node(
data: dict[str, Any] = {}

for attr_name in self._attributes:
if exclude and attr_name in exclude:
if (exclude and attr_name in exclude) or (include and attr_name not in include):
continue

attr: Attribute = getattr(self, attr_name)
Expand All @@ -1767,7 +1759,7 @@ def generate_query_data_node(
data[attr_name] = {"@alias": f"__alias__{self._schema.kind}__{attr_name}"}

for rel_name in self._relationships:
if exclude and rel_name in exclude:
if (exclude and rel_name in exclude) or (include and rel_name not in include):
continue

rel_schema = self._schema.get_relationship(name=rel_name)
Expand Down
73 changes: 19 additions & 54 deletions tests/unit/sdk/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,10 +991,10 @@ async def test_query_data_generic_fragment(clients, mock_schema_query_02, client
async def test_query_data_include_property(client, location_schema: NodeSchemaAPI, client_type):
if client_type == "standard":
node = InfrahubNode(client=client, schema=location_schema)
data = await node.generate_query_data(include=["tags"], property=True)
data = await node.generate_query_data(include=["name", "type", "tags"], property=True)
else:
node = InfrahubNodeSync(client=client, schema=location_schema)
data = node.generate_query_data(include=["tags"], property=True)
data = node.generate_query_data(include=["name", "type", "tags"], property=True)

assert data == {
"BuiltinLocation": {
Expand Down Expand Up @@ -1023,23 +1023,6 @@ async def test_query_data_include_property(client, location_schema: NodeSchemaAP
},
"value": None,
},
"description": {
"is_default": None,
"is_from_profile": None,
"is_protected": None,
"is_visible": None,
"owner": {
"__typename": None,
"display_label": None,
"id": None,
},
"source": {
"__typename": None,
"display_label": None,
"id": None,
},
"value": None,
},
"type": {
"is_default": None,
"is_from_profile": None,
Expand All @@ -1057,28 +1040,6 @@ async def test_query_data_include_property(client, location_schema: NodeSchemaAP
},
"value": None,
},
"primary_tag": {
"properties": {
"is_protected": None,
"is_visible": None,
"owner": {
"__typename": None,
"display_label": None,
"id": None,
},
"source": {
"__typename": None,
"display_label": None,
"id": None,
},
},
"node": {
"id": None,
"hfid": None,
"display_label": None,
"__typename": None,
},
},
"tags": {
"count": None,
"edges": {
Expand Down Expand Up @@ -1113,10 +1074,10 @@ async def test_query_data_include_property(client, location_schema: NodeSchemaAP
async def test_query_data_include(client, location_schema: NodeSchemaAPI, client_type):
if client_type == "standard":
node = InfrahubNode(client=client, schema=location_schema)
data = await node.generate_query_data(include=["tags"])
data = await node.generate_query_data(include=["name", "type", "tags"])
else:
node = InfrahubNodeSync(client=client, schema=location_schema)
data = node.generate_query_data(include=["tags"])
data = node.generate_query_data(include=["name", "type", "tags"])

assert data == {
"BuiltinLocation": {
Expand All @@ -1131,20 +1092,9 @@ async def test_query_data_include(client, location_schema: NodeSchemaAPI, client
"name": {
"value": None,
},
"description": {
"value": None,
},
"type": {
"value": None,
},
"primary_tag": {
"node": {
"id": None,
"hfid": None,
"display_label": None,
"__typename": None,
},
},
"tags": {
"count": None,
"edges": {
Expand Down Expand Up @@ -1251,6 +1201,21 @@ async def test_query_data_exclude(client, location_schema: NodeSchemaAPI, client
}


@pytest.mark.parametrize("client_type", client_types)
async def test_query_data_include_exclude(client, location_schema: NodeSchemaAPI, client_type):
if client_type == "standard":
node = InfrahubNode(client=client, schema=location_schema)

with pytest.raises(ValueError) as exc:
await node.generate_query_data(include=["name", "type"], exclude=["description"], property=True)
assert "include and exclude are exclusive" in str(exc.value)
else:
node = InfrahubNodeSync(client=client, schema=location_schema)
with pytest.raises(ValueError) as exc:
node.generate_query_data(include=["name", "type", "tags"], exclude=["description"], property=True)
assert "include and exclude are exclusive" in str(exc.value)


@pytest.mark.parametrize("client_type", client_types)
async def test_create_input_data(client, location_schema: NodeSchemaAPI, client_type):
data = {"name": {"value": "JFK1"}, "description": {"value": "JFK Airport"}, "type": {"value": "SITE"}}
Expand Down
Loading