tests_unit.py
Go to the documentation of this file.
00001 # encoding: utf-8
00002 #! /usr/bin/env python
00003 import logging
00004 import io
00005 from datetime import datetime
00006 import unittest
00007 from collections import namedtuple
00008 import uuid
00009 
00010 from opcua import ua
00011 from opcua.ua import extensionobject_from_binary
00012 from opcua.ua import extensionobject_to_binary
00013 from opcua.ua.uatypes import flatten, get_shape, reshape
00014 from opcua.server.internal_subscription import WhereClauseEvaluator
00015 from opcua.common.event_objects import BaseEvent
00016 from opcua.common.ua_utils import string_to_variant, variant_to_string, string_to_val, val_to_string
00017 from opcua.common.xmlimporter import XmlImporter
00018 from opcua.ua.uatypes import _MaskEnum
00019 
00020 
00021 class TestUnit(unittest.TestCase):
00022 
00023     '''
00024     Simple unit test that do not need to setup a server or a client
00025     '''
00026 
00027     def test_nodeid_ordering(self):
00028         a = ua.NodeId(2000, 1)
00029         b = ua.NodeId(3000, 1)
00030         c = ua.NodeId(20, 0)
00031         d = ua.NodeId("tititu", 1)
00032         e = ua.NodeId("aaaaa", 1)
00033         f = ua.NodeId("aaaaa", 2)
00034         g = ua.NodeId(uuid.uuid4(), 1)
00035         h = ua.TwoByteNodeId(2001)
00036         i = ua.NodeId(b"lkjkl", 1, ua.NodeIdType.ByteString)
00037         j = ua.NodeId(b"aaa", 5, ua.NodeIdType.ByteString)
00038 
00039         mylist = [a, b, c, d, e, f, g, h, i, j]
00040         mylist.sort()
00041         expected = [c, h, a, b, e, d, f, g, i, j]
00042         self.assertEqual(mylist, expected)
00043 
00044     def test_string_to_variant_int(self):
00045         s_arr_uint = "[1, 2, 3, 4]"
00046         arr_uint = [1, 2, 3, 4]
00047         s_uint = "1"
00048 
00049         self.assertEqual(string_to_val(s_arr_uint, ua.VariantType.UInt32), arr_uint)
00050         self.assertEqual(string_to_val(s_arr_uint, ua.VariantType.UInt16), arr_uint)
00051         self.assertEqual(val_to_string(arr_uint), s_arr_uint)
00052 
00053     def test_string_to_variant_float(self):
00054         s_arr_float = "[1.1, 2.1, 3, 4.0]"
00055         arr_float = [1.1, 2.1, 3, 4.0]
00056         s_float = "1.9"
00057         self.assertEqual(string_to_val(s_float, ua.VariantType.Float), 1.9)
00058         self.assertEqual(val_to_string(arr_float), s_arr_float)
00059 
00060     def test_string_to_variant_datetime_string(self):
00061         s_arr_datetime = "[2014-05-6, 2016-10-3]"
00062         arr_string = ['2014-05-6', '2016-10-3']
00063         arr_datetime = [datetime(2014, 5, 6), datetime(2016, 10, 3)]
00064         s_datetime = "2014-05-3"
00065         self.assertEqual(val_to_string(arr_string), s_arr_datetime)
00066         self.assertEqual(string_to_val(s_arr_datetime, ua.VariantType.String), arr_string)
00067         self.assertEqual(string_to_val(s_arr_datetime, ua.VariantType.DateTime), arr_datetime)
00068 
00069     def test_string_to_variant_nodeid(self):
00070         s_arr_nodeid = "[ns=2;i=56, i=45]"
00071         arr_nodeid = [ua.NodeId.from_string("ns=2;i=56"), ua.NodeId.from_string("i=45")]
00072         s_nodeid = "i=45"
00073         self.assertEqual(string_to_val(s_arr_nodeid, ua.VariantType.NodeId), arr_nodeid)
00074 
00075     def test_string_to_variant_status_code(self):
00076         s_statuscode = "Good"
00077         statuscode = ua.StatusCode(ua.StatusCodes.Good)
00078         s_statuscode2 = "Uncertain"
00079         statuscode2 = ua.StatusCode(ua.StatusCodes.Uncertain)
00080         self.assertEqual(string_to_val(s_statuscode, ua.VariantType.StatusCode), statuscode)
00081         self.assertEqual(string_to_val(s_statuscode2, ua.VariantType.StatusCode), statuscode2)
00082     def test_string_to_variant_qname(self):
00083         string = "2:name"
00084         obj = ua.QualifiedName("name", 2)
00085         self.assertEqual(string_to_val(string, ua.VariantType.QualifiedName), obj)
00086         self.assertEqual(val_to_string(obj), string)
00087 
00088     def test_string_to_variant_localized_text(self):
00089         string = "_This is my string"
00090         # string = "_This is my nøåæ"FIXME: does not work with python2 ?!?!
00091         obj = ua.LocalizedText(string)
00092         self.assertEqual(string_to_val(string, ua.VariantType.LocalizedText), obj)
00093         self.assertEqual(val_to_string(obj), string)
00094 
00095     def test_variant_dimensions(self):
00096         l = [[[1.0, 1.0, 1.0, 1.0], [2.0, 2.0, 2.0, 2.0], [3.0, 3.0, 3.0, 3.0]], [[5.0, 5.0, 5.0, 5.0], [7.0, 8.0, 9.0, 01.0], [1.0, 1.0, 1.0, 1.0]]]
00097         v = ua.Variant(l)
00098         self.assertEqual(v.Dimensions, [2, 3, 4])
00099         v2 = ua.Variant.from_binary(ua.utils.Buffer(v.to_binary()))
00100         self.assertEqual(v, v2)
00101         self.assertEqual(v.Dimensions, v2.Dimensions)
00102 
00103         # very special case
00104         l = [[[], [], []], [[], [], []]]
00105         v = ua.Variant(l, ua.VariantType.UInt32)
00106         self.assertEqual(v.Dimensions, [2, 3, 0])
00107         v2 = ua.Variant.from_binary(ua.utils.Buffer(v.to_binary()))
00108         self.assertEqual(v.Dimensions, v2.Dimensions)
00109         self.assertEqual(v, v2)
00110 
00111     def test_flatten(self):
00112         l = [[[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]], [[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]]]
00113         l2 = flatten(l)
00114         dims = get_shape(l)
00115         self.assertEqual(dims, [2, 3, 4])
00116         self.assertNotEqual(l, l2)
00117 
00118         l3 = reshape(l2, (2, 3, 4))
00119         self.assertEqual(l, l3)
00120 
00121 
00122         l = [[[], [], []], [[], [], []]]
00123         l2 = flatten(l)
00124         dims = get_shape(l)
00125         self.assertEqual(dims, [2, 3, 0])
00126 
00127         l = [1, 2, 3, 4]
00128         l2 = flatten(l)
00129         dims = get_shape(l)
00130         self.assertEqual(dims, [4])
00131         self.assertEqual(l, l2)
00132 
00133     def test_custom_variant(self):
00134         with self.assertRaises(ua.UaError):
00135             v = ua.Variant(b"ljsdfljds", ua.VariantTypeCustom(89))
00136         v = ua.Variant(b"ljsdfljds", ua.VariantTypeCustom(61))
00137         v2 = ua.Variant.from_binary(ua.utils.Buffer(v.to_binary()))
00138         self.assertEqual(v.VariantType, v2.VariantType)
00139         self.assertEqual(v, v2)
00140 
00141 
00142     def test_custom_variant_array(self):
00143         v = ua.Variant([b"ljsdfljds", b"lkjsdljksdf"], ua.VariantTypeCustom(40))
00144         v2 = ua.Variant.from_binary(ua.utils.Buffer(v.to_binary()))
00145         self.assertEqual(v.VariantType, v2.VariantType)
00146         self.assertEqual(v, v2)
00147 
00148     def test_guid(self):
00149         v = ua.Variant(uuid.uuid4(), ua.VariantType.Guid)
00150         v2 = ua.Variant.from_binary(ua.utils.Buffer(v.to_binary()))
00151         self.assertEqual(v.VariantType, v2.VariantType)
00152         self.assertEqual(v, v2)
00153 
00154     def test_nodeid(self):
00155         nid = ua.NodeId()
00156         self.assertEqual(nid.NodeIdType, ua.NodeIdType.TwoByte)
00157         nid = ua.NodeId(446, 3, ua.NodeIdType.FourByte)
00158         self.assertEqual(nid.NodeIdType, ua.NodeIdType.FourByte)
00159         d = nid.to_binary()
00160         new_nid = nid.from_binary(io.BytesIO(d))
00161         self.assertEqual(new_nid, nid)
00162         self.assertEqual(new_nid.NodeIdType, ua.NodeIdType.FourByte)
00163         self.assertEqual(new_nid.Identifier, 446)
00164         self.assertEqual(new_nid.NamespaceIndex, 3)
00165 
00166         tb = ua.TwoByteNodeId(53)
00167         fb = ua.FourByteNodeId(53)
00168         n = ua.NumericNodeId(53)
00169         n1 = ua.NumericNodeId(53, 0)
00170         s = ua.StringNodeId(53, 0)  # should we raise an exception???
00171         s1 = ua.StringNodeId("53", 0)
00172         bs = ua.ByteStringNodeId(b"53", 0)
00173         gid = uuid.uuid4()
00174         g = ua.ByteStringNodeId(str(gid), 0)
00175         guid = ua.GuidNodeId(gid)
00176         self.assertEqual(tb, fb)
00177         self.assertEqual(tb, n)
00178         self.assertEqual(tb, n1)
00179         self.assertEqual(n1, fb)
00180         self.assertNotEqual(n1, s)
00181         self.assertNotEqual(s, bs)
00182         self.assertNotEqual(s, g)
00183         self.assertNotEqual(g, guid)
00184         self.assertEqual(tb, ua.NodeId.from_binary(ua.utils.Buffer(tb.to_binary())))
00185         self.assertEqual(fb, ua.NodeId.from_binary(ua.utils.Buffer(fb.to_binary())))
00186         self.assertEqual(n, ua.NodeId.from_binary(ua.utils.Buffer(n.to_binary())))
00187         self.assertEqual(s1, ua.NodeId.from_binary(ua.utils.Buffer(s1.to_binary())))
00188         self.assertEqual(bs, ua.NodeId.from_binary(ua.utils.Buffer(bs.to_binary())))
00189         self.assertEqual(guid, ua.NodeId.from_binary(ua.utils.Buffer(guid.to_binary())))
00190 
00191     def test_nodeid_string(self):
00192         nid0 = ua.NodeId(45)
00193         self.assertEqual(nid0, ua.NodeId.from_string("i=45"))
00194         self.assertEqual(nid0, ua.NodeId.from_string("ns=0;i=45"))
00195         nid = ua.NodeId(45, 10)
00196         self.assertEqual(nid, ua.NodeId.from_string("i=45; ns=10"))
00197         self.assertNotEqual(nid, ua.NodeId.from_string("i=45; ns=11"))
00198         self.assertNotEqual(nid, ua.NodeId.from_string("i=5; ns=10"))
00199         # not sure the next one is correct...
00200         self.assertEqual(nid, ua.NodeId.from_string("i=45; ns=10; srv=serverid"))
00201 
00202         nid1 = ua.NodeId("myid.mynodeid", 7)
00203         self.assertEqual(nid1, ua.NodeId.from_string("ns=7; s=myid.mynodeid"))
00204         with self.assertRaises(ua.UaError):
00205             nid1 = ua.NodeId(7, "myid.mynodeid")
00206 
00207     def test_bad_string(self):
00208         with self.assertRaises(ua.UaStringParsingError):
00209             ua.NodeId.from_string("ns=r;s=yu")
00210         with self.assertRaises(ua.UaStringParsingError):
00211             ua.NodeId.from_string("i=r;ns=1")
00212         with self.assertRaises(ua.UaStringParsingError):
00213             ua.NodeId.from_string("ns=1")
00214         with self.assertRaises(ua.UaError):
00215             ua.QualifiedName.from_string("i:yu")
00216         with self.assertRaises(ua.UaError):
00217             ua.QualifiedName.from_string("i:::yu")
00218 
00219     def test_expandednodeid(self):
00220         nid = ua.ExpandedNodeId()
00221         self.assertEqual(nid.NodeIdType, ua.NodeIdType.TwoByte)
00222         nid2 = ua.ExpandedNodeId.from_binary(ua.utils.Buffer(nid.to_binary()))
00223         self.assertEqual(nid, nid2)
00224 
00225     def test_null_string(self):
00226         v = ua.Variant(None, ua.VariantType.String)
00227         b = v.to_binary()
00228         v2 = ua.Variant.from_binary(ua.utils.Buffer(b))
00229         self.assertEqual(v.Value, v2.Value)
00230         v = ua.Variant("", ua.VariantType.String)
00231         b = v.to_binary()
00232         v2 = ua.Variant.from_binary(ua.utils.Buffer(b))
00233         self.assertEqual(v.Value, v2.Value)
00234 
00235     def test_extension_object(self):
00236         obj = ua.UserNameIdentityToken()
00237         obj.UserName = "admin"
00238         obj.Password = b"pass"
00239         obj2 = ua.extensionobject_from_binary(ua.utils.Buffer(extensionobject_to_binary(obj)))
00240         self.assertEqual(type(obj), type(obj2))
00241         self.assertEqual(obj.UserName, obj2.UserName)
00242         self.assertEqual(obj.Password, obj2.Password)
00243         v1 = ua.Variant(obj)
00244         v2 = ua.Variant.from_binary(ua.utils.Buffer(v1.to_binary()))
00245         self.assertEqual(type(v1), type(v2))
00246         self.assertEqual(v1.VariantType, v2.VariantType)
00247 
00248     def test_unknown_extension_object(self):
00249         obj = ua.ExtensionObject()
00250         obj.Body = b'example of data in custom format'
00251         obj.TypeId = ua.NodeId.from_string('ns=3;i=42')
00252         data = ua.utils.Buffer(extensionobject_to_binary(obj))
00253         obj2 = ua.extensionobject_from_binary(data)
00254         self.assertEqual(type(obj2), ua.ExtensionObject)
00255         self.assertEqual(obj2.TypeId, obj.TypeId)
00256         self.assertEqual(obj2.Body, b'example of data in custom format')
00257 
00258     def test_datetime(self):
00259         now = datetime.utcnow()
00260         epch = ua.ua_binary.datetime_to_win_epoch(now)
00261         dt = ua.ua_binary.win_epoch_to_datetime(epch)
00262         self.assertEqual(now, dt)
00263 
00264         # python's datetime has a range from Jan 1, 0001 to the end of year 9999
00265         # windows' filetime has a range from Jan 1, 1601 to approx. year 30828
00266         # let's test an overlapping range [Jan 1, 1601 - Dec 31, 9999]
00267         dt = datetime(1601, 1, 1)
00268         self.assertEqual(ua.ua_binary.win_epoch_to_datetime(ua.ua_binary.datetime_to_win_epoch(dt)), dt)
00269         dt = datetime(9999, 12, 31, 23, 59, 59)
00270         self.assertEqual(ua.ua_binary.win_epoch_to_datetime(ua.ua_binary.datetime_to_win_epoch(dt)), dt)
00271 
00272         epch = 128930364000001000
00273         dt = ua.ua_binary.win_epoch_to_datetime(epch)
00274         epch2 = ua.ua_binary.datetime_to_win_epoch(dt)
00275         self.assertEqual(epch, epch2)
00276 
00277         epch = 0
00278         self.assertEqual(ua.ua_binary.datetime_to_win_epoch(ua.ua_binary.win_epoch_to_datetime(epch)), epch)
00279 
00280     def test_equal_nodeid(self):
00281         nid1 = ua.NodeId(999, 2)
00282         nid2 = ua.NodeId(999, 2)
00283         self.assertTrue(nid1 == nid2)
00284         self.assertTrue(id(nid1) != id(nid2))
00285 
00286     def test_zero_nodeid(self):
00287         self.assertEqual(ua.NodeId(), ua.NodeId(0, 0))
00288         self.assertEqual(ua.NodeId(), ua.NodeId.from_string('ns=0;i=0;'))
00289 
00290     def test_string_nodeid(self):
00291         nid = ua.NodeId('titi', 1)
00292         self.assertEqual(nid.NamespaceIndex, 1)
00293         self.assertEqual(nid.Identifier, 'titi')
00294         self.assertEqual(nid.NodeIdType, ua.NodeIdType.String)
00295 
00296     def test_unicode_string_nodeid(self):
00297         nid = ua.NodeId('hëllò', 1)
00298         self.assertEqual(nid.NamespaceIndex, 1)
00299         self.assertEqual(nid.Identifier, 'hëllò')
00300         self.assertEqual(nid.NodeIdType, ua.NodeIdType.String)
00301         d = nid.to_binary()
00302         new_nid = nid.from_binary(io.BytesIO(d))
00303         self.assertEqual(new_nid, nid)
00304         self.assertEqual(new_nid.Identifier, 'hëllò')
00305         self.assertEqual(new_nid.NodeIdType, ua.NodeIdType.String)
00306 
00307     def test_numeric_nodeid(self):
00308         nid = ua.NodeId(999, 2)
00309         self.assertEqual(nid.NamespaceIndex, 2)
00310         self.assertEqual(nid.Identifier, 999)
00311         self.assertEqual(nid.NodeIdType, ua.NodeIdType.Numeric)
00312 
00313     def test_qualifiedstring_nodeid(self):
00314         nid = ua.NodeId.from_string('ns=2;s=PLC1.Manufacturer;')
00315         self.assertEqual(nid.NamespaceIndex, 2)
00316         self.assertEqual(nid.Identifier, 'PLC1.Manufacturer')
00317 
00318     def test_strrepr_nodeid(self):
00319         nid = ua.NodeId.from_string('ns=2;s=PLC1.Manufacturer;')
00320         self.assertEqual(nid.to_string(), 'ns=2;s=PLC1.Manufacturer')
00321         # self.assertEqual(repr(nid), 'ns=2;s=PLC1.Manufacturer;')
00322 
00323     def test_qualified_name(self):
00324         qn = ua.QualifiedName('qname', 2)
00325         self.assertEqual(qn.NamespaceIndex, 2)
00326         self.assertEqual(qn.Name, 'qname')
00327         self.assertEqual(qn.to_string(), '2:qname')
00328 
00329     def test_datavalue(self):
00330         dv = ua.DataValue(123)
00331         self.assertEqual(dv.Value, ua.Variant(123))
00332         self.assertEqual(type(dv.Value), ua.Variant)
00333         dv = ua.DataValue('abc')
00334         self.assertEqual(dv.Value, ua.Variant('abc'))
00335         now = datetime.utcnow()
00336         dv.SourceTimestamp = now
00337 
00338     def test_variant(self):
00339         dv = ua.Variant(True, ua.VariantType.Boolean)
00340         self.assertEqual(dv.Value, True)
00341         self.assertEqual(type(dv.Value), bool)
00342         now = datetime.utcnow()
00343         v = ua.Variant(now)
00344         self.assertEqual(v.Value, now)
00345         self.assertEqual(v.VariantType, ua.VariantType.DateTime)
00346         v2 = ua.Variant.from_binary(ua.utils.Buffer(v.to_binary()))
00347         self.assertEqual(v.Value, v2.Value)
00348         self.assertEqual(v.VariantType, v2.VariantType)
00349         # commonity method:
00350         self.assertEqual(v, ua.Variant(v))
00351 
00352     def test_variant_array(self):
00353         v = ua.Variant([1, 2, 3, 4, 5])
00354         self.assertEqual(v.Value[1], 2)
00355         # self.assertEqual(v.VarianType, ua.VariantType.Int64) # we do not care, we should aonly test for sutff that matter
00356         v2 = ua.Variant.from_binary(ua.utils.Buffer(v.to_binary()))
00357         self.assertEqual(v.Value, v2.Value)
00358         self.assertEqual(v.VariantType, v2.VariantType)
00359 
00360         now = datetime.utcnow()
00361         v = ua.Variant([now])
00362         self.assertEqual(v.Value[0], now)
00363         self.assertEqual(v.VariantType, ua.VariantType.DateTime)
00364         v2 = ua.Variant.from_binary(ua.utils.Buffer(v.to_binary()))
00365         self.assertEqual(v.Value, v2.Value)
00366         self.assertEqual(v.VariantType, v2.VariantType)
00367 
00368     def test_variant_array_dim(self):
00369         v = ua.Variant([1, 2, 3, 4, 5, 6], dimensions=[2, 3])
00370         self.assertEqual(v.Value[1], 2)
00371         v2 = ua.Variant.from_binary(ua.utils.Buffer(v.to_binary()))
00372         self.assertEqual(reshape(v.Value, (2, 3)), v2.Value)
00373         self.assertEqual(v.VariantType, v2.VariantType)
00374         self.assertEqual(v.Dimensions, v2.Dimensions)
00375         self.assertEqual(v2.Dimensions, [2, 3])
00376 
00377     def test_text(self):
00378         t1 = ua.LocalizedText('Root')
00379         t2 = ua.LocalizedText('Root')
00380         t3 = ua.LocalizedText('root')
00381         self.assertEqual(t1, t2)
00382         self.assertNotEqual(t1, t3)
00383         t4 = ua.LocalizedText.from_binary(ua.utils.Buffer(t1.to_binary()))
00384         self.assertEqual(t1, t4)
00385 
00386     def test_message_chunk(self):
00387         pol = ua.SecurityPolicy()
00388         chunks = ua.MessageChunk.message_to_chunks(pol, b'123', 65536)
00389         self.assertEqual(len(chunks), 1)
00390         seq = 0
00391         for chunk in chunks:
00392             seq += 1
00393             chunk.SequenceHeader.SequenceNumber = seq
00394         chunk2 = ua.MessageChunk.from_binary(pol, ua.utils.Buffer(chunks[0].to_binary()))
00395         self.assertEqual(chunks[0].to_binary(), chunk2.to_binary())
00396 
00397         # for policy None, MessageChunk overhead is 12+4+8 = 24 bytes
00398         # Let's pack 11 bytes into 28-byte chunks. The message must be split as 4+4+3
00399         chunks = ua.MessageChunk.message_to_chunks(pol, b'12345678901', 28)
00400         self.assertEqual(len(chunks), 3)
00401         self.assertEqual(chunks[0].Body, b'1234')
00402         self.assertEqual(chunks[1].Body, b'5678')
00403         self.assertEqual(chunks[2].Body, b'901')
00404         for chunk in chunks:
00405             seq += 1
00406             chunk.SequenceHeader.SequenceNumber = seq
00407             self.assertTrue(len(chunk.to_binary()) <= 28)
00408 
00409     def test_null(self):
00410         n = ua.NodeId(b'000000', 0, nodeidtype=ua.NodeIdType.Guid)
00411         self.assertTrue(n.is_null())
00412         self.assertTrue(n.has_null_identifier())
00413 
00414         n = ua.NodeId(b'000000', 1, nodeidtype=ua.NodeIdType.Guid)
00415         self.assertFalse(n.is_null())
00416         self.assertTrue(n.has_null_identifier())
00417 
00418         n = ua.NodeId()
00419         self.assertTrue(n.is_null())
00420         self.assertTrue(n.has_null_identifier())
00421 
00422         n = ua.NodeId(0, 0)
00423         self.assertTrue(n.is_null())
00424         self.assertTrue(n.has_null_identifier())
00425 
00426         n = ua.NodeId("", 0)
00427         self.assertTrue(n.is_null())
00428         self.assertTrue(n.has_null_identifier())
00429 
00430         n = ua.TwoByteNodeId(0)
00431         self.assertTrue(n.is_null())
00432         self.assertTrue(n.has_null_identifier())
00433 
00434         n = ua.NodeId(0, 3)
00435         self.assertFalse(n.is_null())
00436         self.assertTrue(n.has_null_identifier())
00437 
00438     def test_where_clause(self):
00439         cf = ua.ContentFilter()
00440 
00441         el = ua.ContentFilterElement()
00442 
00443         op = ua.SimpleAttributeOperand()
00444         op.BrowsePath.append(ua.QualifiedName("property", 2))
00445         el.FilterOperands.append(op)
00446 
00447         for i in range(10):
00448             op = ua.LiteralOperand()
00449             op.Value = ua.Variant(i)
00450             el.FilterOperands.append(op)
00451 
00452         el.FilterOperator = ua.FilterOperator.InList
00453         cf.Elements.append(el)
00454 
00455         wce = WhereClauseEvaluator(logging.getLogger(__name__), None, cf)
00456 
00457         ev = BaseEvent()
00458         ev._freeze = False
00459         ev.property = 3
00460 
00461         self.assertTrue(wce.eval(ev))
00462 
00463 
00464 class TestMaskEnum(unittest.TestCase):
00465     class MyEnum(_MaskEnum):
00466         member1 = 0
00467         member2 = 1
00468 
00469     def test_invalid_input(self):
00470         with self.assertRaises(ValueError):
00471             self.MyEnum(12345)
00472 
00473     def test_parsing(self):
00474         self.assertEqual(self.MyEnum.parse_bitfield(0b0), set())
00475         self.assertEqual(self.MyEnum.parse_bitfield(0b1), {self.MyEnum.member1})
00476         self.assertEqual(self.MyEnum.parse_bitfield(0b10), {self.MyEnum.member2})
00477         self.assertEqual(self.MyEnum.parse_bitfield(0b11), {self.MyEnum.member1, self.MyEnum.member2})
00478 
00479     def test_identity(self):
00480         bitfields = [0b00, 0b01, 0b10, 0b11]
00481 
00482         for bitfield in bitfields:
00483             as_set = self.MyEnum.parse_bitfield(bitfield)
00484             back_to_bitfield = self.MyEnum.to_bitfield(as_set)
00485             self.assertEqual(back_to_bitfield, bitfield)
00486 
00487 
00488 if __name__ == '__main__':
00489     logging.basicConfig(level=logging.WARN)
00490 
00491     unittest.main(verbosity=3)


ros_opcua_impl_python_opcua
Author(s): Denis Štogl , Daniel Draper
autogenerated on Sat Jun 8 2019 18:26:23