Commit ea611693 authored by oroulet's avatar oroulet Committed by oroulet

fix tests

parent 2ef024b3
......@@ -9,7 +9,8 @@ import logging
# The next two imports are for generated code
from datetime import datetime
from enum import IntEnum, EnumMeta
from dataclasses import dataclass
from dataclasses import dataclass, field
from typing import List
from xml.etree import ElementTree as ET
......@@ -152,7 +153,7 @@ class StructGenerator(object):
field.value = get_default_value(field.uatype, enums)
if array:
field.array = True
field.value = []
field.value = "field(default_factory=list)"
array = False
struct.fields.append(field)
self.model.append(struct)
......@@ -184,6 +185,8 @@ THIS FILE IS AUTOGENERATED, DO NOT EDIT!!!
from datetime import datetime
import uuid
from dataclasses import dataclass, field
from typing import List, Union
from asyncua import ua
""")
......@@ -268,6 +271,10 @@ def _generate_python_class(model, env=None):
env['IntEnum'] = IntEnum
if "dataclass" not in env:
env['dataclass'] = dataclass
if "field" not in env:
env['field'] = field
if "List" not in env:
env['List'] = List
# generate classes one by one and add them to dict
for element in model:
code = element.get_code()
......
......@@ -203,7 +203,6 @@ class {struct_name}(ua.FrozenClass):
code += f" '{fname}': ('Encoding', {counter}),\n"
counter += 1
code += " }\n\n"
print("CODE", code)
return code
......
......@@ -649,10 +649,10 @@ class LocalizedText:
@staticmethod
def from_string(string):
m = re.match(r"^LocalizedText\(Encoding:(.*), Locale:(.*), Text:(.*)\)$", string)
m = re.match(r"^LocalizedText\(Locale='(.*)', Text='(.*)'\)$", string)
if m:
text = m.group(3) if m.group(3) != str(None) else None
locale = m.group(2) if m.group(2) != str(None) else None
text = m.group(2) if m.group(2) != str(None) else None
locale = m.group(1) if m.group(1) != str(None) else None
return LocalizedText(Text=text, Locale=locale)
return LocalizedText(string)
......
......@@ -148,8 +148,8 @@ async def test_opc_type_dict_get_dict_value(srv):
async def test_reference_generator_1(srv):
id1 = ua.NodeId(1, namespaceidx=2, nodeidtype=ua.NodeIdType.Numeric)
id2 = ua.NodeId(2, namespaceidx=2, nodeidtype=ua.NodeIdType.Numeric)
id1 = ua.NodeId(1, NamespaceIndex=2, NodeIdType=ua.NodeIdType.Numeric)
id2 = ua.NodeId(2, NamespaceIndex=2, NodeIdType=ua.NodeIdType.Numeric)
ref = ua.NodeId(ua.ObjectIds.HasEncoding, 0)
result = reference_generator(id1, id2, ref)
assert result.IsForward
......@@ -160,8 +160,8 @@ async def test_reference_generator_1(srv):
async def test_reference_generator_2(srv):
id1 = ua.NodeId(1, namespaceidx=2, nodeidtype=ua.NodeIdType.Numeric)
id2 = ua.NodeId(2, namespaceidx=2, nodeidtype=ua.NodeIdType.Numeric)
id1 = ua.NodeId(1, NamespaceIndex=2, NodeIdType=ua.NodeIdType.Numeric)
id2 = ua.NodeId(2, NamespaceIndex=2, NodeIdType=ua.NodeIdType.Numeric)
ref = ua.NodeId(ua.ObjectIds.HasEncoding, 0)
result = reference_generator(id1, id2, ref, False)
assert not result.IsForward
......@@ -264,8 +264,6 @@ async def test_data_type_dict_add_field_1(srv):
await srv.dict_builder.set_dict_byte_string()
await srv.srv.load_type_definitions()
struct = get_ua_class(struct_name)
assert struct.ua_types[0][0] == 'id'
assert struct.ua_types[0][1] == 'Int32'
struct_instance = struct()
assert struct_instance.id == 0
......@@ -277,8 +275,6 @@ async def test_data_type_dict_add_field_2(srv):
await srv.dict_builder.set_dict_byte_string()
await srv.srv.load_type_definitions()
struct = get_ua_class(struct_name)
assert struct.ua_types[0][0] == 'id'
assert struct.ua_types[0][1] == 'ListOfInt32'
struct_instance = struct()
assert isinstance(struct_instance.id, list)
......@@ -298,8 +294,6 @@ async def test_struct_node_add_field(srv):
await srv.dict_builder.set_dict_byte_string()
await srv.srv.load_type_definitions()
struct = get_ua_class(struct_name)
assert struct.ua_types[0][0] == 'id'
assert struct.ua_types[0][1] == 'Int32'
struct_instance = struct()
assert struct_instance.id == 0
......@@ -339,7 +333,7 @@ async def test_functional_basic(srv):
await srv.dict_builder.set_dict_byte_string()
await srv.srv.load_type_definitions()
basic_var = await srv.srv.nodes.objects.add_variable(ua.NodeId(namespaceidx=srv.idx), 'BasicStruct',
basic_var = await srv.srv.nodes.objects.add_variable(ua.NodeId(NamespaceIndex=srv.idx), 'BasicStruct',
ua.Variant(None, ua.VariantType.Null),
datatype=basic_struct.data_type)
......@@ -370,7 +364,7 @@ async def test_functional_advance(srv):
await srv.dict_builder.set_dict_byte_string()
await srv.srv.load_type_definitions()
basic_var = await srv.srv.nodes.objects.add_variable(ua.NodeId(namespaceidx=srv.idx), 'BasicStruct',
basic_var = await srv.srv.nodes.objects.add_variable(ua.NodeId(NamespaceIndex=srv.idx), 'BasicStruct',
ua.Variant(None, ua.VariantType.Null),
datatype=basic_struct.data_type)
......@@ -380,7 +374,7 @@ async def test_functional_advance(srv):
basic_msg.Comments = 'Test string'
await basic_var.write_value(basic_msg)
nested_var = await srv.srv.nodes.objects.add_variable(ua.NodeId(namespaceidx=srv.idx), 'NestedStruct',
nested_var = await srv.srv.nodes.objects.add_variable(ua.NodeId(NamespaceIndex=srv.idx), 'NestedStruct',
ua.Variant(None, ua.VariantType.Null),
datatype=nested_struct.data_type)
......
......@@ -225,14 +225,15 @@ def test_string_to_variant_qname():
def test_string_to_variant_localized_text():
string = "_This is my nøåæ"
obj = ua.LocalizedText(string)
string_repr = f"LocalizedText(Locale=None, Text='{string}')"
assert obj == string_to_val(string, ua.VariantType.LocalizedText)
assert string == val_to_string(obj)
assert string_repr == val_to_string(obj)
def test_string_to_variant_localized_text_with_locale():
locale = "cs-CZ"
string = "Moje jméno"
string_repr = f'LocalizedText(Encoding:3, Locale:{locale}, Text:{string})'
string_repr = f"LocalizedText(Locale='{locale}', Text='{string}')"
obj = ua.LocalizedText(string, locale)
assert obj == string_to_val(string_repr, ua.VariantType.LocalizedText)
assert string_repr == val_to_string(obj)
......@@ -241,22 +242,20 @@ def test_string_to_variant_localized_text_with_locale():
def test_string_to_variant_localized_text_with_none1():
locale = "en-US"
string = ""
string_repr = f"LocalizedText(Encoding:1, Locale:{locale}, Text:{string})"
string_repr = f"LocalizedText(Locale='{locale}', Text='{string}')"
obj = ua.LocalizedText(string, locale)
obj2 = ua.LocalizedText(string)
assert obj == string_to_val(string_repr, ua.VariantType.LocalizedText)
assert obj2 == string_to_val(string, ua.VariantType.LocalizedText)
assert "" == val_to_string(obj)
def test_string_to_variant_localized_text_with_none2():
locale = None
string = "my name is ..."
string_repr = f"LocalizedText(Encoding:2, Locale:{locale}, Text:{string})"
string_repr = f"LocalizedText(Locale='{locale}', Text='{string}')"
obj = ua.LocalizedText(string, locale)
assert obj == string_to_val(string_repr, ua.VariantType.LocalizedText)
assert obj == string_to_val(string, ua.VariantType.LocalizedText)
assert string == val_to_string(obj)
def test_string_to_val_xml_element():
......
import os
from dataclasses import dataclass
from asyncua import ua
from asyncua.ua import uatypes
from enum import IntEnum
......@@ -18,21 +20,10 @@ import asyncua.ua
setattr(asyncua.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 f'ExampleStruct(IntVal1:{self.IntVal1}, EnumVal:{self.EnumVal})'
__repr__ = __str__
@dataclass
class ExampleStruct:
IntVal1: uatypes.Int16 = 0
EnumVal: ExampleEnum = ExampleEnum(0)
async def add_server_custom_enum_struct(server: Server):
......@@ -44,4 +35,4 @@ async def add_server_custom_enum_struct(server: Server):
val.IntVal1 = 242
val.EnumVal = ua.ExampleEnum.EnumVal2
myvar = server.get_node(ua.NodeId(6009, ns))
await myvar.write_value(val)
\ No newline at end of file
await myvar.write_value(val)
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