def test_custom_structs_array(tmpdir): c = StructGenerator() c.make_model_from_file(EXAMPLE_BSD_PATH) ns = {} output_path = tmpdir.join("test_custom_structs_array.py").strpath c.save_to_file(output_path) with open(output_path) as s: exec(s.read(), ns) # test with default values v = ns["ArrayValueDataType"]() data = struct_to_binary(v) v2 = struct_from_binary(ns["ArrayValueDataType"], ua.utils.Buffer(data)) # set some values v = ns["ArrayValueDataType"]() v.SbyteValue = [1] v.ByteValue = [2] v.Int16Value = [3] v.UInt16Value = [4] v.Int32Value = [5] v.UInt32Value = [6] v.Int64Value = [7] v.UInt64Value = [8] v.FloatValue = [9.0] v.DoubleValue = [10.0] v.StringValue = ["elleven"] v.DateTimeValue = [datetime.utcnow()] # self.GuidValue = uuid.uudib"14" v.ByteStringValue = [b"fifteen", b"sixteen"] v.XmlElementValue = [ua.XmlElement("<toto>titi</toto>")] v.NodeIdValue = [ua.NodeId.from_string("ns=4;i=9999"), ua.NodeId.from_string("i=6")] data = struct_to_binary(v) v2 = struct_from_binary(ns["ArrayValueDataType"], ua.utils.Buffer(data)) assert v.NodeIdValue == v2.NodeIdValue
def test_bin_datattributes(): dta = ua.DataTypeAttributes() dta.DisplayName = ua.LocalizedText("titi") data = struct_to_binary(dta) dta2 = struct_from_binary(ua.DataTypeAttributes, ua.utils.Buffer(data)) assert dta.DisplayName == dta2.DisplayName
def test_string_to_val_xml_element(): string = "<p> titi toto </p>" obj = ua.XmlElement(string) assert obj == string_to_val(string, ua.VariantType.XmlElement) assert string == val_to_string(obj) b = struct_to_binary(obj) obj2 = struct_from_binary(ua.XmlElement, ua.utils.Buffer(b)) assert obj == obj2
def test_text(): t1 = ua.LocalizedText('Root') t2 = ua.LocalizedText('Root') t3 = ua.LocalizedText('root') assert t1 == t2 assert t1 != t3 t4 = struct_from_binary(ua.LocalizedText, ua.utils.Buffer(struct_to_binary(t1))) assert t1 == t4
def test_custom_structs(tmpdir): c = StructGenerator() c.make_model_from_file(EXAMPLE_BSD_PATH) output_path = tmpdir.join("test_custom_structs.py").strpath c.save_to_file(output_path) ns = {} with open(output_path) as s: exec(s.read(), ns) # test with default values v = ns["ScalarValueDataType"]() data = struct_to_binary(v) v2 = struct_from_binary(ns["ScalarValueDataType"], ua.utils.Buffer(data)) # set some values v = ns["ScalarValueDataType"]() v.SbyteValue = 1 v.ByteValue = 2 v.Int16Value = 3 v.UInt16Value = 4 v.Int32Value = 5 v.UInt32Value = 6 v.Int64Value = 7 v.UInt64Value = 8 v.FloatValue = 9.0 v.DoubleValue = 10.0 v.StringValue = "elleven" v.DateTimeValue = datetime.utcnow() # self.GuidValue = uuid.uudib"14" v.ByteStringValue = b"fifteen" v.XmlElementValue = ua.XmlElement("<toto>titi</toto>") v.NodeIdValue = ua.NodeId.from_string("ns=4;i=9999") # self.ExpandedNodeIdValue = # self.QualifiedNameValue = # self.LocalizedTextValue = # self.StatusCodeValue = # self.VariantValue = # self.EnumerationValue = # self.StructureValue = # self.Number = # self.Integer = # self.UInteger = data = struct_to_binary(v) v2 = struct_from_binary(ns["ScalarValueDataType"], ua.utils.Buffer(data)) assert v.NodeIdValue == v2.NodeIdValue
def test_struct_104(): @dataclass class MyStruct: Encoding: ua.Byte = field(default=0, repr=False, init=False) a: ua.Int32 = 1 b: Optional[ua.Int32] = None c: Optional[ua.String] = None l: List[ua.String] = None m = MyStruct() data = struct_to_binary(m) m2 = struct_from_binary(MyStruct, ua.utils.Buffer(data)) assert m == m2 m = MyStruct(a=4, b=5, c="lkjkæl", l=["a", "b", "c"]) data = struct_to_binary(m) m2 = struct_from_binary(MyStruct, ua.utils.Buffer(data)) assert m == m2
def test_bin_data_type_def(): ad = ua.AddNodesItem() ad.ParentNodeId = ua.NodeId(22) dta = ua.DataTypeAttributes() dta.DisplayName = ua.LocalizedText("titi") ad.NodeAttributes = dta data = struct_to_binary(ad) ad2 = struct_from_binary(ua.AddNodesItem, ua.utils.Buffer(data)) assert ad.ParentNodeId == ad2.ParentNodeId assert ad.NodeAttributes.DisplayName == ad2.NodeAttributes.DisplayName
def test_status_code_to_string(): # serialize a status code and deserialize it, name and doc resolution should work just fine statuscode = ua.StatusCode(ua.StatusCodes.BadNotConnected) statuscode2 = struct_from_binary(ua.StatusCode, io.BytesIO(struct_to_binary(ua.StatusCode(ua.StatusCodes.BadNotConnected)))) assert statuscode == statuscode2 assert statuscode.value == statuscode2.value # properties that are not serialized should still translate properly assert statuscode.name == statuscode2.name assert statuscode.doc == statuscode2.doc
def test_text_with_locale(): t0 = ua.LocalizedText('Root') t1 = ua.LocalizedText('Root', 'de-AT') t2 = ua.LocalizedText('Root', 'de-AT') t3 = ua.LocalizedText('Root', 'de-DE') t4 = ua.LocalizedText(locale='de-DE') t5 = ua.LocalizedText(locale='de-DE') assert t0 != t1 assert t1 == t2 assert t1 != t3 assert t3 != t4 assert t4 == t5 t6 = struct_from_binary(ua.LocalizedText, ua.utils.Buffer(struct_to_binary(t1))) assert t1 == t6
def test_bname(): qn = ua.QualifiedName("TOTO", 2) d = struct_to_binary(qn) qn2 = struct_from_binary(ua.QualifiedName, ua.utils.Buffer(d)) assert qn == qn2
def test_text_simple(): t = ua.LocalizedText('Root') b = struct_to_binary(t) buf = ua.utils.Buffer(b) t2 = struct_from_binary(ua.LocalizedText, buf) assert t == t2