Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use deepcopy to copy data during expansion #306

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions tests/instances/resources/instance_extended_attribute.vspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Vehicle:
type: branch
instances: Test[1,3]
description: High-level vehicle data.

Vehicle.SomeThing:
type: sensor
description: "test"
datatype: string
dbc:
signal: bababa
transform:
mapping:
- from: 0
to: false
- from: 1
to: true
30 changes: 30 additions & 0 deletions tests/instances/test_instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# provisions of the license provided by the LICENSE file in this repository.
#

from typing import Dict
from vspec.model.vsstree import VSSNode
from vspec.model.constants import VSSType, VSSTreeType
import vspec
Expand Down Expand Up @@ -170,3 +171,32 @@ def test_exclusion_from_instance(request):

assert "Vehicle.ExcludeSomeThing" in name_list
assert "Vehicle.ExcludeNode" in name_list


def test_extended_attribute(request):
test_path = os.path.dirname(request.fspath)
# load the file
tree = vspec.load_tree(os.path.join(test_path, "resources/instance_extended_attribute.vspec"), [os.path.join(
test_path, "resources/")], VSSTreeType.SIGNAL_TREE)

# check if root node has 3 children
assert len(tree.children) == 3

# Programmatically change value of dbc/signal for instance in Test2

for child in tree.children:
if child.qualified_name() == "Vehicle.Test2":
assert len(child.children) == 1
assert child.children[0].name == "SomeThing" # <... SomeThing
assert len(child.children[0].extended_attributes.items()) == 1
assert isinstance(child.children[0].extended_attributes["dbc"], Dict)
assert isinstance(child.children[0].extended_attributes["dbc"]["signal"], str)
assert child.children[0].extended_attributes["dbc"]["signal"] == "bababa"
child.children[0].extended_attributes["dbc"]["signal"] = "lalala"

for child in tree.children:
if child.qualified_name() == "Vehicle.Test2":
assert child.children[0].extended_attributes["dbc"]["signal"] == "lalala"
else:
# Make sure that all other instances keeps same name
assert child.children[0].extended_attributes["dbc"]["signal"] == "bababa"
4 changes: 2 additions & 2 deletions tests/vspec/test_overlay_on_instance/test.vspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A:
description: Branch A.


# Instances defined a slist
# Instances defined as list

A.B:
type: branch
Expand Down Expand Up @@ -32,4 +32,4 @@ A.S.T:
type: sensor
unit: km
description: Signal A.S.T
comment: Orig comment.
comment: Orig comment.
4 changes: 3 additions & 1 deletion vspec/model/vsstree.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ class VSSNode(Node):
deprecation = ""

def __deepcopy__(self, memo):
return VSSNode(self.name, self.source_dict.copy(), self.available_types.copy(),
# Deep copy of source_dict and children needed as overlay or programmatic changes
# in exporters otherwise risk changing values not only for current instances but also for others
return VSSNode(self.name, copy.deepcopy(self.source_dict), self.available_types.copy(),
parent=None, children=copy.deepcopy(self.children, memo))

def __init__(self, name, source_dict: dict, available_types: Set[str], parent=None,
Expand Down