Skip to content

Commit

Permalink
🐛 Container requires other container (#1027)
Browse files Browse the repository at this point in the history
# Description

We fail to look-up required containers.
### Bug

![image](https://github.com/user-attachments/assets/0e2f554f-74dc-4140-b1d6-6d74d1abbad5)


## Bump

- [x] Patch
- [ ] Minor
- [ ] Skip

## Changelog
### Fixed

- The `neat.read.cdf()` and `neat.read.yaml(..., format="toolkit")` no
longer give a `ResourceNotFoundError` for containers that are required
by the containers in the model and exists in CDF.
  • Loading branch information
doctrino authored Mar 4, 2025
1 parent 133fc63 commit a3a4785
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
10 changes: 5 additions & 5 deletions cognite/neat/_rules/models/dms/_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from collections import Counter, defaultdict
from collections.abc import Mapping
from functools import lru_cache
from typing import ClassVar

from cognite.client import data_modeling as dm
from cognite.client.data_classes.data_modeling import ContainerList, ViewId, ViewList
Expand Down Expand Up @@ -59,10 +58,6 @@ class DMSValidation:
"""This class does all the validation of the DMS rules that have dependencies between
components."""

# When checking for changes extension=addition, we need to check if the new view has changed.
# For example, changing the filter is allowed, but changing the properties is not.
changeable_view_attributes: ClassVar[set[str]] = {"filter"}

def __init__(
self,
rules: DMSRules,
Expand Down Expand Up @@ -96,6 +91,11 @@ def imported_views_and_containers_ids(
imported_views.add(prop.view)
view_with_properties.add(prop.view)

for container in self._containers or []:
for required in container.constraint or []:
if required not in existing_containers:
imported_containers.add(required)

if include_views_with_no_properties:
extra_views = existing_views - view_with_properties
imported_views.update({view for view in extra_views})
Expand Down
31 changes: 30 additions & 1 deletion tests/tests_unit/rules/test_models/test_dms_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
DMSValidation,
)
from cognite.neat._rules.models.dms._exporter import _DMSExporter
from cognite.neat._rules.models.entities._single_value import UnknownEntity, ViewEntity
from cognite.neat._rules.models.entities._single_value import ContainerEntity, UnknownEntity, ViewEntity
from cognite.neat._rules.transformers import (
DMSToInformation,
InformationToDMS,
Expand Down Expand Up @@ -1779,3 +1779,32 @@ def test_edge_types_by_view_property_id(
actual = exporter._edge_types_by_view_property_id(properties_by_view_id, view_by_id)

assert actual == expected_edge_types_by_view_property_id


class TestDMSValidation:
@pytest.mark.parametrize(
"input_rules, expected_views, expected_containers",
[
pytest.param(
DMSInputRules(
DMSInputMetadata("my_space", "MyModel", "Me", "v1"),
properties=[
DMSInputProperty("MyView", "name", "text", container="MyContainer", container_property="name"),
],
views=[DMSInputView("MyView")],
containers=[DMSInputContainer("MyContainer", constraint="cdf_cdm:CogniteDescribable")],
),
set(),
{ContainerEntity(space="cdf_cdm", externalId="CogniteDescribable")},
id="Container requiring other container",
)
],
)
def test_imported_views_and_containers_ids(
self, input_rules: DMSInputRules, expected_views: set[ViewEntity], expected_containers: set[ContainerEntity]
) -> None:
validation = DMSValidation(input_rules.as_verified_rules())
actual_views, actual_containers = validation.imported_views_and_containers_ids()

assert actual_views == expected_views
assert actual_containers == expected_containers

0 comments on commit a3a4785

Please sign in to comment.