Commit 5c35942a authored by oroulet's avatar oroulet Committed by oroulet

add test for structs of structs and list and fix bugs

parent 5946687c
......@@ -6,6 +6,7 @@ import logging
import re
from asyncua import ua
from asyncua import Node
from asyncua.common.manage_nodes import create_encoding, create_data_type
logger = logging.getLogger(__name__)
......@@ -26,12 +27,12 @@ def new_struct_field(name, dtype, array=False, optional=False, description=""):
field.DataType = ua.NodeId(dtype.value, 0)
elif isinstance(dtype, ua.NodeId):
field.DataType = dtype
elif isinstance(dtype, ua.Node):
elif isinstance(dtype, Node):
field.DataType = dtype.nodeid
else:
raise ValueError(f"Datatype of a field must be a NodeId, not {dtype} of type {type(dtype)}")
if array:
field.ValueRand = ua.ValueRank.OneOrMoreDimensions
field.ValueRank = ua.ValueRank.OneOrMoreDimensions
field.ArrayDimensions = [1]
return field
......@@ -149,7 +150,9 @@ class {name}:
code += " ('Encoding', 'Byte'),\n"
uatypes = []
for field in sdef.Fields:
prefix = 'ListOf' if field.ValueRank >= 1 else ''
prefix = ""
if field.ValueRank >= 1 or field.ArrayDimensions:
prefix = 'ListOf'
if field.DataType.NamespaceIndex == 0 and field.DataType.Identifier in ua.ObjectIdNames:
uatype = ua.ObjectIdNames[field.DataType.Identifier]
elif field.DataType in ua.extension_objects_by_datatype:
......
......@@ -1139,20 +1139,20 @@ async def test_custom_enum(opc):
assert val == 1
async def test_custom_struct(opc):
async def test_custom_struct_(opc):
idx = 4
await new_struct(opc.opc, idx, "MyStruct", [
new_struct_field("MyBool", ua.VariantType.Boolean),
new_struct_field("MyUInt32", ua.VariantType.UInt32),
new_struct_field("MyUInt32", ua.VariantType.UInt32, array=True),
])
await opc.opc.load_data_type_definitions()
mystruct = ua.MyStruct()
mystruct.MyUInt32 = 78
mystruct.MyUInt32 = [78, 79]
var = await opc.opc.nodes.objects.add_variable(idx, "my_struct", ua.Variant(mystruct, ua.VariantType.ExtensionObject))
val = await var.read_value()
assert val.MyUInt32 == 78
assert val.MyUInt32 == [78, 79]
async def test_custom_struct_with_optional_fields(opc):
......@@ -1176,3 +1176,47 @@ async def test_custom_struct_with_optional_fields(opc):
assert val.MyInt64 == -67
async def test_custom_struct_of_struct(opc):
idx = 4
dtype = await new_struct(opc.opc, idx, "MySubStruct", [
new_struct_field("MyBool", ua.VariantType.Boolean),
new_struct_field("MyUInt32", ua.VariantType.UInt32),
])
await new_struct(opc.opc, idx, "MyMotherStruct", [
new_struct_field("MyBool", ua.VariantType.Boolean),
new_struct_field("MySubStruct", dtype),
])
await opc.opc.load_data_type_definitions()
mystruct = ua.MyMotherStruct()
mystruct.MySubStruct = ua.MySubStruct()
mystruct.MySubStruct.MyUInt32 = 78
var = await opc.opc.nodes.objects.add_variable(idx, "my_mother_struct", ua.Variant(mystruct, ua.VariantType.ExtensionObject))
val = await var.read_value()
assert val.MySubStruct.MyUInt32 == 78
async def test_custom_list_of_struct(opc):
idx = 4
dtype = await new_struct(opc.opc, idx, "MySubStruct", [
new_struct_field("MyBool", ua.VariantType.Boolean),
new_struct_field("MyUInt32", ua.VariantType.UInt32),
])
await new_struct(opc.opc, idx, "MyMotherStruct", [
new_struct_field("MyBool", ua.VariantType.Boolean),
new_struct_field("MySubStruct", dtype, array=True),
])
await opc.opc.load_data_type_definitions()
mystruct = ua.MyMotherStruct()
mystruct.MySubStruct = [ua.MySubStruct()]
mystruct.MySubStruct[0].MyUInt32 = 78
var = await opc.opc.nodes.objects.add_variable(idx, "my_mother_struct", ua.Variant(mystruct, ua.VariantType.ExtensionObject))
val = await var.read_value()
assert val.MySubStruct[0].MyUInt32 == 78
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment