Skip to content

Commit

Permalink
tests: added integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
doctrino committed Dec 5, 2023
1 parent bb8ebbc commit d40caa0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
2 changes: 2 additions & 0 deletions cognite/client/data_classes/data_modeling/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
DataModelingId,
EdgeId,
NodeId,
PropertyId,
VersionedDataModelingId,
ViewId,
ViewIdentifier,
Expand Down Expand Up @@ -98,6 +99,7 @@
"ViewIdentifier",
"ViewApply",
"ViewApplyList",
"PropertyId",
"MappedPropertyApply",
"VersionedDataModelingId",
"DataModelingId",
Expand Down
8 changes: 4 additions & 4 deletions cognite/client/data_classes/data_modeling/ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,20 @@ def as_property_ref(self, property: str) -> tuple[str, ...]:


@dataclass(frozen=True)
class ViewPropertyId(CogniteObject):
view_id: ViewId
class PropertyId(CogniteObject):
source: ViewId | ContainerId
property: str

@classmethod
def _load(cls, resource: dict[str, Any], cognite_client: CogniteClient | None = None) -> Self:
return cls(
view_id=ViewId.load(resource["source"]),
source=ViewId.load(resource["source"]),
property=resource["identifier"],
)

def dump(self, camel_case: bool = True) -> dict[str, Any]:
return {
"source": self.view_id.dump(camel_case=camel_case, include_type=True),
"source": self.source.dump(camel_case=camel_case, include_type=True),
"identifier": self.property,
}

Expand Down
14 changes: 7 additions & 7 deletions cognite/client/data_classes/data_modeling/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
DirectRelationReference,
PropertyType,
)
from cognite.client.data_classes.data_modeling.ids import ContainerId, ViewId, ViewPropertyId
from cognite.client.data_classes.data_modeling.ids import ContainerId, PropertyId, ViewId
from cognite.client.data_classes.filters import Filter
from cognite.client.utils._text import convert_all_keys_to_camel_case_recursive, to_snake_case

Expand Down Expand Up @@ -472,7 +472,7 @@ class ReverseSingleHopConnection(ConnectionDefinition):
Args:
source (ViewId): The node(s) containing the direct relation property can be read through
the view specified in 'source'.
through (ViewPropertyId): The view or container of the node containing the direct relation property.
through (PropertyId): The view or container of the node containing the direct relation property.
connection_type (Literal["single_reverse_direct_relation", "multi_reverse_direct_relation"]):
The type of connection. The single_reverse_direct_relation type is used to indicate that only a
single direct relation is expected to exist. The multi_reverse_direct_relation type is used to
Expand All @@ -483,7 +483,7 @@ class ReverseSingleHopConnection(ConnectionDefinition):
"""

source: ViewId
through: ViewPropertyId
through: PropertyId
connection_type: Literal["single_reverse_direct_relation", "multi_reverse_direct_relation"]
name: str | None = None
description: str | None = None
Expand All @@ -492,7 +492,7 @@ class ReverseSingleHopConnection(ConnectionDefinition):
def _load(cls, resource: dict[str, Any], cognite_client: CogniteClient | None = None) -> Self:
return cls(
source=ViewId.load(resource["source"]),
through=ViewPropertyId.load(resource["through"]),
through=PropertyId.load(resource["through"]),
connection_type=resource["connectionType"],
name=resource.get("name"),
description=resource.get("description"),
Expand Down Expand Up @@ -594,7 +594,7 @@ class ReverseSingleHopConnectionApply(ConnectionDefinitionApply):
Args:
source (ViewId): The node(s) containing the direct relation property can be read through
the view specified in 'source'.
through (ViewPropertyId): The view or container of the node containing the direct relation property.
through (PropertyId): The view or container of the node containing the direct relation property.
connection_type (Literal["single_reverse_direct_relation", "multi_reverse_direct_relation"]):
The type of connection. The single_reverse_direct_relation type is used to indicate that only a
single direct relation is expected to exist. The multi_reverse_direct_relation type is used to
Expand All @@ -605,7 +605,7 @@ class ReverseSingleHopConnectionApply(ConnectionDefinitionApply):
"""

source: ViewId
through: ViewPropertyId
through: PropertyId
connection_type: Literal["single_reverse_direct_relation", "multi_reverse_direct_relation"]
name: str | None = None
description: str | None = None
Expand All @@ -614,7 +614,7 @@ class ReverseSingleHopConnectionApply(ConnectionDefinitionApply):
def _load(cls, resource: dict[str, Any], cognite_client: CogniteClient | None = None) -> Self:
return cls(
source=ViewId.load(resource["source"]),
through=ViewPropertyId.load(resource["through"]),
through=PropertyId.load(resource["through"]),
connection_type=resource["connectionType"],
name=resource.get("name"),
description=resource.get("description"),
Expand Down
42 changes: 40 additions & 2 deletions tests/tests_integration/test_api/test_data_modeling/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@
DirectRelation,
DirectRelationReference,
MappedPropertyApply,
PropertyId,
Space,
Text,
View,
ViewApply,
ViewId,
ViewList,
)
from cognite.client.data_classes.data_modeling.views import SingleHopConnectionDefinitionApply
from cognite.client.data_classes.data_modeling.views import (
ReverseSingleHopConnectionApply,
SingleHopConnectionDefinitionApply,
)
from cognite.client.exceptions import CogniteAPIError


Expand All @@ -40,6 +44,11 @@ def movie_view(movie_views: ViewList) -> View:
return cast(View, movie_views.get(external_id="Movie"))


@pytest.fixture()
def actor_view(movie_views: ViewList) -> View:
return cast(View, movie_views.get(external_id="Actor"))


class TestViewsAPI:
def test_list(self, cognite_client: CogniteClient, movie_views: ViewList, integration_test_space: Space) -> None:
# Arrange
Expand Down Expand Up @@ -262,7 +271,7 @@ def test_apply_different_property_types(
space=integration_test_space.space,
external_id="Critic",
version="v1",
description="This i a test view, and should not persist.",
description="This is a test view, and should not persist.",
name="Critic",
properties={
"name": MappedPropertyApply(
Expand Down Expand Up @@ -308,3 +317,32 @@ def test_apply_different_property_types(
# Cleanup
cognite_client.data_modeling.views.delete(new_view.as_id())
cognite_client.data_modeling.containers.delete(new_container.as_id())

def test_apply_view_with_reverse_direct_relation(
self, cognite_client: CogniteClient, integration_test_space: Space, person_view: View, actor_view: View
) -> None:
new_view = ViewApply(
space=integration_test_space.space,
external_id="Critic",
version="v3",
description="This is a test view, and should not persist.",
name="Critic",
properties={
"persons": ReverseSingleHopConnectionApply(
source=person_view.as_id(),
name="Person",
through=PropertyId(source=actor_view.as_id(), property="person"),
connection_type="single_reverse_direct_relation",
)
},
)

try:
# Act
created_view = cognite_client.data_modeling.views.apply(new_view)

# Assert
assert created_view.created_time
finally:
# Cleanup
cognite_client.data_modeling.views.delete(new_view.as_id())

0 comments on commit d40caa0

Please sign in to comment.