Skip to content

Commit

Permalink
[NEAT-515] 🐛 EdgeEntity (and ViewEntity) not dumped correctly (#709)
Browse files Browse the repository at this point in the history
* tests; extended to fail on case

* fix: dump correctly

* build: changelog

* fix: remove default space of type correctly
  • Loading branch information
doctrino authored Nov 5, 2024
1 parent 74a7786 commit 29f3f49
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
4 changes: 3 additions & 1 deletion cognite/neat/_rules/models/dms/_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ def remove_default_space(self, value: str, info: SerializationInfo) -> str:
@field_serializer("connection", when_used="unless-none")
def remove_defaults(self, value: Any, info: SerializationInfo) -> str:
if isinstance(value, Entity) and (metadata := _metadata(info.context)):
default_type = f"{metadata.space}{self.view.external_id}.{self.view_property}"
default_type = f"{self.view.external_id}.{self.view_property}"
if isinstance(value, EdgeEntity) and value.edge_type and value.edge_type.space != metadata.space:
default_type = f"{metadata.space}{default_type}"
return value.dump(space=metadata.space, version=metadata.version, type=default_type)
return str(value)

Expand Down
6 changes: 3 additions & 3 deletions cognite/neat/_rules/models/entities/_single_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ def _as_str(self, **defaults: Any) -> str:
}
if isinstance(defaults, dict):
for key, value in defaults.items():
if key in model_dump and value == defaults.get(key):
if key in model_dump and model_dump[key] == value:
del model_dump[key]

args = ",".join(f"{k}={v}" for k, v in model_dump.items())
# Sorting to ensure deterministic order
args = ",".join(f"{k}={v}" for k, v in sorted(model_dump.items(), key=lambda x: x[0]))
if self.prefix == Undefined or (isinstance(defaults, dict) and self.prefix == defaults.get("prefix")):
base_id = str(self.suffix)
else:
Expand Down
6 changes: 6 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ Changes are grouped as follows:
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## TBD
### Fixed
- `neat.to.excel` or `neat.to.yaml` now correctly writes `ViewTypes` and `Edge` that do not have the default
value. For example, if the `Connection` was `edge(direction=inwards)` it would not be written to the Excel or
YAML file as `edge` as `direction=inwards` was not the default value. This is now fixed.

## [0.96.3] - 05-11-**2024**
### Added
- Introduce `neat.inspect.outcome(...)` to check the outcome of `cdf.to.data_model`.
Expand Down
18 changes: 15 additions & 3 deletions tests/tests_unit/rules/test_models/test_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@
),
(
EdgeEntity,
"edge(properties=Owns, type=sp_my_space:ownership)",
"edge(properties=Owns,type=ownership)",
EdgeEntity(
properties=ViewEntity(space=DEFAULT_SPACE, version=DEFAULT_VERSION, externalId="Owns"),
type=DMSNodeEntity(space="sp_my_space", externalId="ownership"),
),
),
(
EdgeEntity,
"edge(properties=Owns(version=34), type=ownership)",
"edge(properties=Owns(version=34),type=ownership)",
EdgeEntity(
properties=ViewEntity(space=DEFAULT_SPACE, version="34", externalId="Owns"),
type=DMSNodeEntity(space=DEFAULT_SPACE, externalId="ownership"),
Expand All @@ -96,16 +96,28 @@
type=DMSNodeEntity(space=DEFAULT_SPACE, externalId="ownership"),
),
),
(
EdgeEntity,
"edge(direction=inwards,properties=StartEndTime)",
EdgeEntity(
properties=ViewEntity(space=DEFAULT_SPACE, version=DEFAULT_VERSION, externalId="StartEndTime"),
direction="inwards",
),
),
]


class TestEntities:
@pytest.mark.parametrize("cls_, raw, expected", TEST_CASES)
def test_load(self, cls_: type[Entity], raw: str, expected: Entity) -> None:
def test_load_dump(self, cls_: type[Entity], raw: str, expected: Entity) -> None:
loaded = cls_.load(raw, space=DEFAULT_SPACE, version=DEFAULT_VERSION)

assert loaded == expected

dumped = loaded.dump(space=DEFAULT_SPACE, version=DEFAULT_VERSION)

assert dumped == raw


class TestEntityPattern:
@pytest.mark.parametrize(
Expand Down

0 comments on commit 29f3f49

Please sign in to comment.