Skip to content

Commit

Permalink
Support more direct relation types when ingesting instance data (#1410)
Browse files Browse the repository at this point in the history
  • Loading branch information
erlendvollset authored Oct 13, 2023
1 parent 01e28a6 commit 7ec0e90
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Changes are grouped as follows
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [6.33.0] - 2023-10-13
### Added
- Support for providing `DirectRelationReference` and `NodeId` as direct relation values when
ingesting node and edge data.

## [6.32.4] - 2023-10-12
### Fixed
- Filters using e.g. metadata keys no longer dumps the key in camel case.
Expand Down
2 changes: 1 addition & 1 deletion cognite/client/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import annotations

__version__ = "6.32.4"
__version__ = "6.33.0"
__api_subversion__ = "V20220125"
26 changes: 24 additions & 2 deletions cognite/client/data_classes/data_modeling/instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,20 @@

if TYPE_CHECKING:
from cognite.client import CogniteClient
PropertyValue = Union[str, int, float, bool, dict, List[str], List[int], List[float], List[bool], List[dict]]
PropertyValue = Union[
str,
int,
float,
bool,
dict,
List[str],
List[int],
List[float],
List[bool],
List[dict],
NodeId,
DirectRelationReference,
]
Space = str
PropertyIdentifier = str

Expand All @@ -67,7 +80,16 @@ def load(cls, data: dict) -> NodeOrEdgeData:
return cls(**convert_all_keys_to_snake_case(data))

def dump(self, camel_case: bool = False) -> dict:
output: dict[str, Any] = {"properties": dict(self.properties.items())}
properties: dict[str, PropertyValue] = {}
for key, value in self.properties.items():
if isinstance(value, NodeId):
# We don't want to dump the instance_type field when serializing NodeId in this context
properties[key] = value.dump(camel_case, include_instance_type=False)
elif isinstance(value, DirectRelationReference):
properties[key] = value.dump(camel_case)
else:
properties[key] = value
output: dict[str, Any] = {"properties": properties}
if self.source:
if isinstance(self.source, (ContainerId, ViewId)):
output["source"] = self.source.dump(camel_case)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "cognite-sdk"

version = "6.32.4"
version = "6.33.0"
description = "Cognite Python SDK"
readme = "README.md"
documentation = "https://cognite-sdk-python.readthedocs-hosted.com"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
DirectRelationReference,
EdgeApply,
NodeApply,
NodeId,
NodeOrEdgeData,
)

Expand Down Expand Up @@ -33,6 +34,26 @@ def test_dump(self) -> None:
}


class TestNodeOrEdgeData:
def test_direct_relation_serialization(self) -> None:
data = NodeOrEdgeData(
source=ContainerId("IntegrationTestsImmutable", "Case"),
properties=dict(
name="Integration test",
some_direct_relation=DirectRelationReference("space", "external_id"),
another_direct_relation_type=NodeId("space", "external_id"),
),
)
assert {
"properties": {
"another_direct_relation_type": {"external_id": "external_id", "space": "space"},
"name": "Integration test",
"some_direct_relation": {"external_id": "external_id", "space": "space"},
},
"source": {"external_id": "Case", "space": "IntegrationTestsImmutable", "type": "container"},
} == data.dump()


class TestNodeApply:
def test_dump_with_snake_case_fields(self) -> None:
# Arrange
Expand Down

0 comments on commit 7ec0e90

Please sign in to comment.