From 11b6c4592ee47ff19171302bbb9c48b0fb9b5218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20V=2E=20Treider?= Date: Mon, 16 Sep 2024 15:54:46 +0200 Subject: [PATCH] final cleanup (extracted from reverted PR) --- .../data_classes/data_modeling/data_types.py | 7 +- .../data_classes/data_modeling/instances.py | 26 +++--- .../test_data_models/test_typed_instances.py | 79 +++++++------------ 3 files changed, 43 insertions(+), 69 deletions(-) diff --git a/cognite/client/data_classes/data_modeling/data_types.py b/cognite/client/data_classes/data_modeling/data_types.py index f49558f4c..53e91e746 100644 --- a/cognite/client/data_classes/data_modeling/data_types.py +++ b/cognite/client/data_classes/data_modeling/data_types.py @@ -34,12 +34,9 @@ def dump(self, camel_case: bool = True) -> dict[str, str | dict]: @classmethod def load(cls, data: dict | tuple[str, str] | DirectRelationReference) -> DirectRelationReference: if isinstance(data, dict): - return cls( - space=data["space"], - external_id=data["externalId"], - ) + return cls(space=data["space"], external_id=data["externalId"]) elif isinstance(data, tuple) and len(data) == 2: - return cls(data[0], data[1]) + return cls(*data) elif isinstance(data, cls): return data else: diff --git a/cognite/client/data_classes/data_modeling/instances.py b/cognite/client/data_classes/data_modeling/instances.py index 0b890d887..1642b214c 100644 --- a/cognite/client/data_classes/data_modeling/instances.py +++ b/cognite/client/data_classes/data_modeling/instances.py @@ -158,7 +158,7 @@ class InstanceCore(DataModelingResource, ABC): Args: space (str): The workspace for the instance, a unique identifier for the space. external_id (str): Combined with the space is the unique identifier of the instance. - instance_type (Literal["node", "edge"]): No description. + instance_type (Literal["node", "edge"]): The type of instance. """ def __init__(self, space: str, external_id: str, instance_type: Literal["node", "edge"]) -> None: @@ -180,7 +180,7 @@ class InstanceApply(WritableInstanceCore[T_CogniteResource], ABC): Args: space (str): The workspace for the instance, a unique identifier for the space. external_id (str): Combined with the space is the unique identifier of the instance. - instance_type (Literal["node", "edge"]): No description. + instance_type (Literal["node", "edge"]): The type of instance. existing_version (int | None): Fail the ingestion request if the instance's version is greater than or equal to this value. If no existingVersion is specified, the ingestion will always overwrite any existing data for the instance (for the specified container or instance). If existingVersion is set to 0, the upsert will behave as an insert, so it will fail the bulk if the instance already exists. If skipOnVersionConflict is set on the ingestion request, then the instance will be skipped instead of failing the ingestion request. sources (list[NodeOrEdgeData] | None): List of source properties to write. The properties are from the instance and/or container the container(s) making up this node. """ @@ -325,7 +325,7 @@ class Instance(WritableInstanceCore[T_CogniteResource], ABC): Args: space (str): The workspace for the instance, a unique identifier for the space. external_id (str): Combined with the space is the unique identifier of the instance. - version (int): DMS version. + version (int): Current version of the instance. last_updated_time (int): The number of milliseconds since 00:00:00 Thursday, 1 January 1970, Coordinated Universal Time (UTC), minus leap seconds. created_time (int): The number of milliseconds since 00:00:00 Thursday, 1 January 1970, Coordinated Universal Time (UTC), minus leap seconds. instance_type (Literal["node", "edge"]): The type of instance. @@ -607,13 +607,13 @@ def as_write(self) -> Self: return self -class Node(Instance["NodeApply"]): +class Node(Instance[NodeApply]): """A node. This is the read version of the node. Args: space (str): The workspace for the node, a unique identifier for the space. external_id (str): Combined with the space is the unique identifier of the node. - version (int): DMS version. + version (int): Current version of the node. last_updated_time (int): The number of milliseconds since 00:00:00 Thursday, 1 January 1970, Coordinated Universal Time (UTC), minus leap seconds. created_time (int): The number of milliseconds since 00:00:00 Thursday, 1 January 1970, Coordinated Universal Time (UTC), minus leap seconds. deleted_time (int | None): The number of milliseconds since 00:00:00 Thursday, 1 January 1970, Coordinated Universal Time (UTC), minus leap seconds. Timestamp when the instance was soft deleted. Note that deleted instances are filtered out of query results, but present in sync results @@ -689,7 +689,7 @@ class NodeApplyResult(InstanceApplyResult): Args: space (str): The workspace for the node, a unique identifier for the space. external_id (str): Combined with the space is the unique identifier of the node. - version (int): DMS version of the node. + version (int): Current version of the node. was_modified (bool): Whether the node was modified by the ingestion. last_updated_time (int): The number of milliseconds since 00:00:00 Thursday, 1 January 1970, Coordinated Universal Time (UTC), minus leap seconds. created_time (int): The number of milliseconds since 00:00:00 Thursday, 1 January 1970, Coordinated Universal Time (UTC), minus leap seconds. @@ -747,13 +747,9 @@ def __init__( sources: list[NodeOrEdgeData] | None = None, ) -> None: super().__init__(space, external_id, "edge", existing_version, sources) - self.type = type if isinstance(type, DirectRelationReference) else DirectRelationReference.load(type) - self.start_node = ( - start_node if isinstance(start_node, DirectRelationReference) else DirectRelationReference.load(start_node) - ) - self.end_node = ( - end_node if isinstance(end_node, DirectRelationReference) else DirectRelationReference.load(end_node) - ) + self.type = DirectRelationReference.load(type) + self.start_node = DirectRelationReference.load(start_node) + self.end_node = DirectRelationReference.load(end_node) def as_id(self) -> EdgeId: return EdgeId(space=self.space, external_id=self.external_id) @@ -793,7 +789,7 @@ class Edge(Instance[EdgeApply]): Args: space (str): The workspace for the edge, a unique identifier for the space. external_id (str): Combined with the space is the unique identifier of the edge. - version (int): DMS version. + version (int): Current version of the edge. type (DirectRelationReference | tuple[str, str]): The type of edge. last_updated_time (int): The number of milliseconds since 00:00:00 Thursday, 1 January 1970, Coordinated Universal Time (UTC), minus leap seconds. created_time (int): The number of milliseconds since 00:00:00 Thursday, 1 January 1970, Coordinated Universal Time (UTC), minus leap seconds. @@ -882,7 +878,7 @@ class EdgeApplyResult(InstanceApplyResult): Args: space (str): The workspace for the edge, a unique identifier for the space. external_id (str): Combined with the space is the unique identifier of the edge. - version (int): DMS version. + version (int): Current version of the edge. was_modified (bool): Whether the edge was modified by the ingestion. last_updated_time (int): The number of milliseconds since 00:00:00 Thursday, 1 January 1970, Coordinated Universal Time (UTC), minus leap seconds. created_time (int): The number of milliseconds since 00:00:00 Thursday, 1 January 1970, Coordinated Universal Time (UTC), minus leap seconds. diff --git a/tests/tests_unit/test_data_classes/test_data_models/test_typed_instances.py b/tests/tests_unit/test_data_classes/test_data_models/test_typed_instances.py index ee85f4a61..4c61ca1e5 100644 --- a/tests/tests_unit/test_data_classes/test_data_models/test_typed_instances.py +++ b/tests/tests_unit/test_data_classes/test_data_models/test_typed_instances.py @@ -215,22 +215,27 @@ def test_dump_load_flow_write(self) -> None: assert flow.dump() == loaded.dump() +@pytest.fixture +def person_read() -> PersonRead: + return PersonRead( + "sp_my_fixed_space", + "my_external_id", + 1, + 0, + 0, + "John Doe", + date(1990, 1, 1), + "john@doe.com", + type=DirectRelationReference("sp_model_space", "person"), + ) + + class TestTypedNode: - def test_dump_load_person(self) -> None: - person = PersonRead( - "sp_my_fixed_space", - "my_external_id", - 1, - 0, - 0, - "John Doe", - date(1990, 1, 1), - "example@email.com", - siblings=[ - DirectRelationReference("sp_data_space", "brother"), - DirectRelationReference("sp_data_space", "sister"), - ], - ) + def test_dump_load_person(self, person_read: PersonRead) -> None: + person_read.siblings = [ + DirectRelationReference("sp_data_space", "brother"), + DirectRelationReference("sp_data_space", "sister"), + ] expected = { "space": "sp_my_fixed_space", "externalId": "my_external_id", @@ -243,7 +248,7 @@ def test_dump_load_person(self) -> None: "view_id/1": { "name": "John Doe", "birthDate": "1990-01-01", - "email": "example@email.com", + "email": "john@doe.com", "siblings": [ {"space": "sp_data_space", "externalId": "brother"}, {"space": "sp_data_space", "externalId": "sister"}, @@ -252,31 +257,22 @@ def test_dump_load_person(self) -> None: }, } }, + "type": {"space": "sp_model_space", "externalId": "person"}, } - actual = person.dump() + actual = person_read.dump() assert actual == expected loaded = PersonRead.load(expected) - assert person == loaded + assert person_read == loaded assert isinstance(loaded.birth_date, date) - assert all(isinstance(sibling, DirectRelationReference) for sibling in loaded.siblings or []) + assert loaded.siblings is not None + assert all(isinstance(sibling, DirectRelationReference) for sibling in loaded.siblings) @pytest.mark.dsl - def test_to_pandas(self) -> None: + def test_to_pandas(self, person_read: PersonRead) -> None: import pandas as pd - person = PersonRead( - "sp_my_fixed_space", - "my_external_id", - 1, - 0, - 0, - "John Doe", - date(1990, 1, 1), - "john@doe.com", - type=DirectRelationReference("sp_model_space", "person"), - ) - df = person.to_pandas(expand_properties=True) + df = person_read.to_pandas(expand_properties=True) expected_df = pd.Series( { "space": "sp_my_fixed_space", @@ -296,25 +292,10 @@ def test_to_pandas(self) -> None: pd.testing.assert_frame_equal(df, expected_df) @pytest.mark.dsl - def test_to_pandas_list(self) -> None: + def test_to_pandas_list(self, person_read: PersonRead) -> None: import pandas as pd - persons = NodeList[PersonRead]( - [ - PersonRead( - "sp_my_fixed_space", - "my_external_id", - 1, - 0, - 0, - "John Doe", - date(1990, 1, 1), - "john@doe.com", - type=DirectRelationReference("sp_model_space", "person"), - ) - ] - ) - + persons = NodeList[PersonRead]([person_read]) df = persons.to_pandas(expand_properties=True) pd.testing.assert_frame_equal(