Skip to content

Commit

Permalink
add test for structures
Browse files Browse the repository at this point in the history
  • Loading branch information
oroulet committed Aug 18, 2018
1 parent 2d5a379 commit b9152c7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
41 changes: 40 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,40 @@ def test_save_open_ua_model(self):
self.assertEqual(node.get_value(), val)
self.mgr.close_model()

def test_structs(self):
self.mgr.new_model()

urns = self.modeler.get_current_server().get_namespace_array()
ns_node = self.mgr.server_mgr.get_node(ua.ObjectIds.Server_NamespaceArray)
urns = ns_node.get_value()
urns.append("urn://modeller/testing")
ns_node.set_value(urns)

path = "test_save_structs.xml"

struct_node = self.mgr.server_mgr.get_node(ua.ObjectIds.Structure)
self.modeler.tree_ui.expand_to_node(struct_node)
mystruct = self.mgr.add_data_type(1, "MyStruct")
var1 = mystruct.add_variable(1, "MyFloat", 0.1, varianttype=ua.VariantType.Float)
var2 = mystruct.add_variable(1, "MyBytes", b'lkjlk', varianttype=ua.VariantType.ByteString)
self.mgr._save_structs()
self.assertEqual(len(self.mgr.new_nodes), 2) # 2 since we created one struct + TypeDictionary node

# FIXME: test for presence of nodes under typedict for every new struct

self.mgr.save_xml(path)
self.mgr.close_model()
self.mgr.open_xml(path)

opc_binary = self.mgr.server_mgr.get_node(ua.ObjectIds.OPCBinarySchema_TypeSystem)
typedict = opc_binary.get_child("1:TypeDictionary")
xml = typedict.get_value()
self.assertIn(b"MyFloat", xml)
self.assertIn(b"MyStruct", xml)

struct_node = self.mgr.server_mgr.get_node(ua.ObjectIds.Structure)
struct_node.get_child("1:MyStruct")



class TestModeler(unittest.TestCase):
Expand All @@ -89,6 +123,11 @@ def test_set_current_node(self):
self.modeler.tree_ui.expand_to_node("Objects")
self.assertEqual(objects, self.modeler.tree_ui.get_current_node())

def test_set_current_node_nodeid(self):
struct_node = self.mgr.server_mgr.get_node(ua.ObjectIds.Structure)
self.modeler.tree_ui.expand_to_node(struct_node)
self.assertEqual(struct_node, self.modeler.tree_ui.get_current_node())

def test_add_folder(self):
self.modeler.tree_ui.expand_to_node("Objects")
dia = NewNodeBaseDialog(self.modeler, "Add Folder", self.modeler.get_current_server())
Expand Down Expand Up @@ -135,7 +174,7 @@ def test_add_variable_bytes(self):

def test_add_variable_float_fail(self):
self.modeler.tree_ui.expand_to_node("Objects")
dia = NewUaVariableDialog(self.modeler, "Add Variable", self.modeler.get_current_server(), default_value=b"lkjkl", dtype=ua.ObjectIds.Float)
dia = NewUaVariableDialog(self.modeler, "Add Variable", self.modeler.get_current_server(), default_value="lkjkl", dtype=ua.ObjectIds.Float)
with self.assertRaises(ValueError):
args = dia.get_args()
new_node = self.mgr.add_variable(*args)
Expand Down
9 changes: 5 additions & 4 deletions uamodeler/model_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(self, name, typename):
self.typename = typename
self.fields = []


class ModelManager(QObject):
"""
Manage our model. loads xml, start and close, add nodes
Expand Down Expand Up @@ -147,7 +148,7 @@ def _show_structs(self):
# we only care about structs, ignoring enums
if isinstance(el, Struct):
try:
struct_node = base_struct.get_child("0:" + el.name)
struct_node = base_struct.get_child(f"{idx}:{el.name}")
except ua.UaError:
logger.warning("Could not find struct %s under %s", el.name, base_struct)
continue
Expand All @@ -160,7 +161,7 @@ def _show_structs(self):
logger.warning("Could not find datatype of name %s %s", field.uatype, type(field.uatype))
continue
vtype = data_type_to_variant_type(dtype)
new = struct_node.add_variable(1, field.name, field.value, varianttype=vtype, datatype=dtype.nodeid)
new = struct_node.add_variable(idx, field.name, field.value, varianttype=vtype, datatype=dtype.nodeid)

def _get_datatype_from_string(self, idx, name):
#FIXME: this is very heavy and missing recusion, what is the correct way to do that?
Expand Down Expand Up @@ -356,7 +357,7 @@ def _save_bsd(self, structs):
for struct in structs:
struct_el = Et.SubElement(root_el, 'opc:StructuredType', {'Name': struct.name, 'BaseType': 'ua' + struct.typename})
for name, typename in struct.fields:
if typename in ua.ObjectIdNames:
if hasattr(ua.ObjectIds, typename):
prefix = "opc"
else:
prefix = "tns"
Expand All @@ -368,7 +369,7 @@ def _save_bsd(self, structs):
dict_node = opc_binary.get_child(f'{idx}:{dict_name}')
dict_node.set_value(val)
except ua.UaError:
dict_node = opc_binary.add_variable(1, dict_name, val, ua.VariantType.ByteString)
dict_node = opc_binary.add_variable(idx, dict_name, val, ua.VariantType.ByteString)
self._after_add(dict_node)
#FIXME: add struct node under dict_node
# for debug
Expand Down

0 comments on commit b9152c7

Please sign in to comment.