Skip to content

Commit

Permalink
final cleanup (extracted from reverted PR)
Browse files Browse the repository at this point in the history
  • Loading branch information
haakonvt committed Sep 16, 2024
1 parent 573b1ba commit 11b6c45
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 69 deletions.
7 changes: 2 additions & 5 deletions cognite/client/data_classes/data_modeling/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
26 changes: 11 additions & 15 deletions cognite/client/data_classes/data_modeling/instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
"""
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
"[email protected]",
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),
"[email protected]",
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",
Expand All @@ -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"},
Expand All @@ -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),
"[email protected]",
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",
Expand All @@ -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),
"[email protected]",
type=DirectRelationReference("sp_model_space", "person"),
)
]
)

persons = NodeList[PersonRead]([person_read])
df = persons.to_pandas(expand_properties=True)

pd.testing.assert_frame_equal(
Expand Down

0 comments on commit 11b6c45

Please sign in to comment.