Commit d552f1f2 authored by jobasto's avatar jobasto Committed by oroulet

Add tests for structs containing enums

parent db91a3be
This diff is collapsed.
......@@ -10,6 +10,8 @@ from tests_subscriptions import SubscriptionTests
from tests_common import CommonTests, add_server_methods
from tests_xml import XmlTests
from tests_enum_struct import add_server_custom_enum_struct
port_num1 = 48510
......@@ -26,6 +28,7 @@ class TestClient(unittest.TestCase, CommonTests, SubscriptionTests, XmlTests):
cls.srv = Server()
cls.srv.set_endpoint('opc.tcp://127.0.0.1:{0:d}'.format(port_num1))
add_server_methods(cls.srv)
add_server_custom_enum_struct(cls.srv)
cls.srv.start()
# start admin client
......@@ -110,7 +113,7 @@ class TestClient(unittest.TestCase, CommonTests, SubscriptionTests, XmlTests):
nenumstrings = self.clt.get_node(ua.ObjectIds.AxisScaleEnumeration_EnumStrings)
with self.assertNotRaises(Exception):
value = ua.Variant(nenumstrings.get_value())
def test_uasocketclient_connect_disconnect(self):
"""Initialize, connect, and disconnect a UaSocketClient
"""
......@@ -118,8 +121,15 @@ class TestClient(unittest.TestCase, CommonTests, SubscriptionTests, XmlTests):
uaclt.connect_socket('127.0.0.1', port_num1)
self.assertTrue(uaclt._thread.is_alive())
self.assertIsInstance(uaclt._socket, SocketWrapper)
# disconnect_socket() should shut down the receiving thread
uaclt.disconnect_socket()
self.assertFalse(uaclt._thread.is_alive())
def test_custom_enum_struct(self):
self.ro_clt.load_type_definitions()
ns = self.ro_clt.get_namespace_index('http://yourorganisation.org/struct_enum_example/')
myvar = self.ro_clt.get_node(ua.NodeId(6009, ns))
val = myvar.get_value()
self.assertEqual(val.IntVal1, 242)
self.assertEqual(val.EnumVal, ua.ExampleEnum.EnumVal2)
from opcua import ua
from opcua.ua import uatypes
from enum import IntEnum
class ExampleEnum(IntEnum):
EnumVal1 = 0
EnumVal2 = 1
EnumVal3 = 2
import opcua.ua
setattr(opcua.ua, 'ExampleEnum', ExampleEnum)
class ExampleStruct(uatypes.FrozenClass):
ua_types = [
('IntVal1', 'Int16'),
('EnumVal', 'ExampleEnum'),
]
def __init__(self):
self.IntVal1 = 0
self.EnumVal = ExampleEnum(0)
self._freeze = True
def __str__(self):
return 'ExampleStruct(' + 'IntVal1:' + str(self.IntVal1) + ', ' + \
'EnumVal:' + str(self.EnumVal) + ')'
__repr__ = __str__
def add_server_custom_enum_struct(server):
# import some nodes from xml
server.import_xml("tests/enum_struct_test_nodes.xml")
ns = server.get_namespace_index('http://yourorganisation.org/struct_enum_example/')
uatypes.register_extension_object('ExampleStruct', ua.NodeId(5001, ns), ExampleStruct)
val = ua.ExampleStruct()
val.IntVal1 = 242
val.EnumVal = ua.ExampleEnum.EnumVal2
myvar = server.get_node(ua.NodeId(6009, ns))
myvar.set_value(val)
\ No newline at end of file
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