-
Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathtest_common.py
2061 lines (1706 loc) · 67.5 KB
/
test_common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Tests that will be run twice. Once on server side and once on
client side since we have been carefull to have the exact
same api on server and client side
"""
import asyncio
import contextlib
import math
import tempfile
from datetime import datetime, timedelta, timezone
from pathlib import Path
from base64 import b64encode
import pytest
from asyncua.ua import NodeId, String, NodeIdType, Int16
from asyncua import Node, ua, uamethod
from asyncua.common import ua_utils
from asyncua.common.copy_node_util import copy_node
from asyncua.common.instantiate_util import instantiate
from asyncua.common.methods import call_method_full
from asyncua.common.sql_injection import SqlInjectionError, validate_table_name
from asyncua.common.structures104 import new_enum, new_struct, new_struct_field
from asyncua.ua.ua_binary import struct_from_binary, struct_to_binary
pytestmark = pytest.mark.asyncio
async def add_server_methods(srv):
@uamethod
def func(parent, value):
return value * 2
o = srv.nodes.objects
await o.add_method(
ua.NodeId("ServerMethod", 2),
ua.QualifiedName("ServerMethod", 2),
func,
[ua.Int64],
[ua.Int64],
)
@uamethod
def func_no_arg(parent):
return 13
o = srv.nodes.objects
await o.add_method(
ua.NodeId("ServerMethodNoArg", 2),
ua.QualifiedName("ServerMethodNoArg", 2),
func_no_arg,
)
@uamethod
def func2(parent, methodname, value):
if methodname == "panic":
return ua.StatusCode(ua.StatusCodes.BadOutOfMemory)
if methodname != "sin":
res = ua.CallMethodResult()
res.StatusCode = ua.StatusCode(ua.StatusCodes.BadInvalidArgument)
res.InputArgumentResults = [
ua.StatusCode(ua.StatusCodes.BadNotSupported),
ua.StatusCode(),
]
return res
return math.sin(value)
o = srv.nodes.objects
await o.add_method(
ua.NodeId("ServerMethodArray", 2),
ua.QualifiedName("ServerMethodArray", 2),
func2,
[ua.VariantType.String, ua.VariantType.Int64],
[ua.VariantType.Int64],
)
@uamethod
def func3(parent, mylist):
return [i * 2 for i in mylist]
o = srv.nodes.objects
await o.add_method(
ua.NodeId("ServerMethodArray2", 2),
ua.QualifiedName("ServerMethodArray2", 2),
func3,
[ua.VariantType.Int64],
[ua.Int64],
)
@uamethod
def func4(parent):
return None
base_otype = srv.get_node(ua.ObjectIds.BaseObjectType)
custom_otype = await base_otype.add_object_type(2, "ObjectWithMethodsType")
await custom_otype.add_method(2, "ServerMethodDefault", func4)
await (await custom_otype.add_method(2, "ServerMethodMandatory", func4)).set_modelling_rule(True)
await (await custom_otype.add_method(2, "ServerMethodOptional", func4)).set_modelling_rule(False)
await (await custom_otype.add_method(2, "ServerMethodNone", func4)).set_modelling_rule(None)
await o.add_object(2, "ObjectWithMethods", custom_otype)
@uamethod
def func5(parent):
return 1, 2, 3
o = srv.nodes.objects
await o.add_method(
ua.NodeId("ServerMethodTuple", 2),
ua.QualifiedName("ServerMethodTuple", 2),
func5,
[],
[ua.VariantType.Int64, ua.VariantType.Int64, ua.VariantType.Int64],
)
@uamethod
async def func6(parent):
await asyncio.sleep(0)
o = srv.nodes.objects
await o.add_method(
ua.NodeId("ServerMethodAsync", 2),
ua.QualifiedName("ServerMethodAsync", 2),
func6,
[],
[],
)
@uamethod
async def func7(parent, val, mybytes):
return (val, [mybytes[0], mybytes[1]])
o = srv.nodes.objects
await o.add_method(
ua.NodeId("ServerMethodByteArray", 2),
ua.QualifiedName("ServerMethodByteArray", 2),
func7,
[ua.VariantType.Byte, [ua.VariantType.Byte, ua.VariantType.Byte, ua.VariantType.Byte]],
[ua.VariantType.Boolean, [ua.VariantType.Byte, ua.VariantType.Byte]],
)
async def test_find_servers(opc):
await opc.opc.find_servers()
# FIXME : finish
async def test_add_node_bad_args(opc):
obj = opc.opc.nodes.objects
with pytest.raises(TypeError):
await obj.add_folder(1.2, "kk")
with pytest.raises(TypeError):
await obj.add_folder(ua.UaError, "khjh")
with pytest.raises(ua.UaError):
await obj.add_folder("kjk", 1.2)
with pytest.raises(TypeError):
await obj.add_folder("i=0;s='oooo'", 1.2)
with pytest.raises(ua.UaError):
await obj.add_folder("i=0;s='oooo'", "tt:oioi")
async def test_delete_nodes(opc):
obj = opc.opc.nodes.objects
fold = await obj.add_folder(2, "FolderToDelete")
var = await fold.add_variable(2, "VarToDelete", 9.1)
childs = await fold.get_children()
assert var in childs
await opc.opc.delete_nodes([var])
with pytest.raises(ua.UaStatusCodeError):
await var.write_value(7.8)
with pytest.raises(ua.UaStatusCodeError):
await obj.get_child(["2:FolderToDelete", "2:VarToDelete"])
childs = await fold.get_children()
assert var not in childs
await opc.opc.delete_nodes([fold])
async def test_node_bytestring(opc):
obj = opc.opc.nodes.objects
var = await obj.add_variable(
ua.ByteStringNodeId(b"VarByteString", 2),
ua.QualifiedName("toto", 2),
ua.UInt16(9),
)
node = opc.opc.get_node("ns=2;b=VarByteString")
assert node == var
node = opc.opc.get_node(f"ns=2;b={b64encode(b'VarByteString').decode()}")
assert node == var
node = opc.opc.get_node(f"ns=2;b=0x{b'VarByteString'.hex()}")
assert node == var
async def test_add_node_using_builtin(opc):
obj = opc.opc.nodes.objects
fold = await obj.add_folder(2, "FolderBuiltin")
var = await fold.add_variable(2, "VarBuiltin", ua.UInt16(9))
dv = await var.read_data_value()
assert dv.Value.VariantType == ua.VariantType.UInt16
data_type = await var.read_data_type()
assert data_type.Identifier == ua.VariantType.UInt16.value == ua.ObjectIds.UInt16
assert data_type.NamespaceIndex == 0
await var.write_value(ua.UInt16(6))
dv = await var.read_data_value()
assert dv.Value.VariantType == ua.VariantType.UInt16
assert dv.Value.Value == 6
assert (await var.read_value()) == 6
async def test_delete_nodes_with_inverse_references(opc):
obj = opc.opc.nodes.objects
fold = await obj.add_folder(2, "FolderToDelete")
var = await fold.add_variable(2, "VarToDelete", 9.1)
var2 = await fold.add_variable(2, "VarWithReference", 9.2)
childs = await fold.get_children()
assert var in childs
assert var2 in childs
# add two references to var, this includes adding the inverse references to var2
await var.add_reference(
var2.nodeid,
reftype=ua.ObjectIds.HasDescription,
forward=True,
bidirectional=True,
)
await var.add_reference(var2.nodeid, reftype=ua.ObjectIds.HasEffect, forward=True, bidirectional=True)
await opc.opc.delete_nodes([var])
childs = await fold.get_children()
assert var not in childs
has_desc_refs = await var2.get_referenced_nodes(
refs=ua.ObjectIds.HasDescription, direction=ua.BrowseDirection.Inverse
)
assert len(has_desc_refs) == 0
has_effect_refs = await var2.get_referenced_nodes(refs=ua.ObjectIds.HasEffect, direction=ua.BrowseDirection.Inverse)
assert len(has_effect_refs) == 0
await opc.opc.delete_nodes([fold])
async def test_delete_nodes_recursive(opc):
obj = opc.opc.nodes.objects
fold = await obj.add_folder(2, "FolderToDeleteR")
var = await fold.add_variable(2, "VarToDeleteR", 9.1)
await opc.opc.delete_nodes([fold, var])
with pytest.raises(ua.UaStatusCodeError):
await var.write_value(7.8)
with pytest.raises(ua.UaStatusCodeError):
await obj.get_child(["2:FolderToDelete", "2:VarToDelete"])
async def test_delete_nodes_recursive2(opc):
obj = opc.opc.nodes.objects
fold = await obj.add_folder(2, "FolderToDeleteRoot")
mynodes = []
for i in range(7):
nfold = await fold.add_folder(2, f"FolderToDeleteRoot{i}")
var = await nfold.add_variable(2, "VarToDeleteR", 9.1)
var = await nfold.add_property(2, "ProToDeleteR", 9.1)
prop = await nfold.add_property(2, "ProToDeleteR2", 9.1)
o = await nfold.add_object(3, "ObjToDeleteR")
mynodes.append(nfold)
mynodes.append(var)
mynodes.append(prop)
mynodes.append(o)
await opc.opc.delete_nodes([fold], recursive=True)
for node in mynodes:
with pytest.raises(ua.UaStatusCodeError):
await node.read_browse_name()
await opc.opc.delete_nodes([fold])
async def test_delete_references(opc):
newtype = await opc.opc.get_node(ua.ObjectIds.HierarchicalReferences).add_reference_type(
0, "HasSuperSecretVariable"
)
obj = opc.opc.nodes.objects
fold = await obj.add_folder(2, "FolderToRef")
var = await fold.add_variable(2, "VarToRef", 42)
await fold.add_reference(var, newtype)
assert [fold] == await var.get_referenced_nodes(newtype)
assert [var] == await fold.get_referenced_nodes(newtype)
await fold.delete_reference(var, newtype)
assert [] == await var.get_referenced_nodes(newtype)
assert [] == await fold.get_referenced_nodes(newtype)
await fold.add_reference(var, newtype, bidirectional=False)
assert [] == await var.get_referenced_nodes(newtype)
assert [var] == await fold.get_referenced_nodes(newtype)
await fold.delete_reference(var, newtype)
assert [] == await var.get_referenced_nodes(newtype)
assert [] == await fold.get_referenced_nodes(newtype)
await var.add_reference(fold, newtype, forward=False, bidirectional=False)
assert [fold] == await var.get_referenced_nodes(newtype)
assert [] == await fold.get_referenced_nodes(newtype)
with pytest.raises(ua.UaStatusCodeError):
await fold.delete_reference(var, newtype)
assert [fold] == await var.get_referenced_nodes(newtype)
assert [] == await fold.get_referenced_nodes(newtype)
with pytest.raises(ua.UaStatusCodeError):
await var.delete_reference(fold, newtype)
assert [fold] == await var.get_referenced_nodes(newtype)
assert [] == await fold.get_referenced_nodes(newtype)
await var.delete_reference(fold, newtype, forward=False)
assert [] == await var.get_referenced_nodes(newtype)
assert [] == await fold.get_referenced_nodes(newtype)
# clean-up
await opc.opc.delete_nodes([fold, newtype], recursive=True)
async def test_server_node(opc):
node = opc.opc.nodes.server
assert ua.QualifiedName("Server", 0) == await node.read_browse_name()
async def test_root(opc):
root = opc.opc.nodes.root
assert ua.QualifiedName("Root", 0) == await root.read_browse_name()
assert ua.LocalizedText("Root") == await root.read_display_name()
nid = ua.NodeId(84, 0)
assert nid == root.nodeid
async def test_objects(opc):
objects = opc.opc.nodes.objects
assert ua.QualifiedName("Objects", 0) == await objects.read_browse_name()
nid = ua.NodeId(85, 0)
assert nid == objects.nodeid
async def test_browse(opc):
objects = opc.opc.nodes.objects
obj = await objects.add_object(4, "browsetest")
folder = await obj.add_folder(4, "folder")
prop = await obj.add_property(4, "property", 1)
prop2 = await obj.add_property(4, "property2", 2)
var = await obj.add_variable(4, "variable", 3)
obj2 = await obj.add_object(4, "obj")
alle = await obj.get_children()
assert prop in alle
assert prop2 in alle
assert var in alle
assert folder in alle
assert obj not in alle
props = await obj.get_children(refs=ua.ObjectIds.HasProperty)
assert prop in props
assert prop2 in props
assert var not in props
assert folder not in props
assert obj2 not in props
all_vars = await obj.get_children(nodeclassmask=ua.NodeClass.Variable)
assert prop in all_vars
assert var in all_vars
assert folder not in props
assert obj2 not in props
all_objs = await obj.get_children(nodeclassmask=ua.NodeClass.Object)
assert folder in all_objs
assert obj2 in all_objs
assert var not in all_objs
await opc.opc.delete_nodes([folder, prop, prop2, var, obj2, obj])
await opc.opc.delete_nodes(props)
await opc.opc.delete_nodes(all_vars)
await opc.opc.delete_nodes(all_objs)
async def test_browse_references(opc):
objects = opc.opc.nodes.objects
folder = await objects.add_folder(4, "folder")
childs = await objects.get_referenced_nodes(
refs=ua.ObjectIds.Organizes,
direction=ua.BrowseDirection.Forward,
includesubtypes=False,
)
assert folder in childs
childs = await objects.get_referenced_nodes(
refs=ua.ObjectIds.Organizes,
direction=ua.BrowseDirection.Both,
includesubtypes=False,
)
assert folder in childs
childs = await objects.get_referenced_nodes(
refs=ua.ObjectIds.Organizes,
direction=ua.BrowseDirection.Inverse,
includesubtypes=False,
)
assert folder not in childs
parents = await folder.get_referenced_nodes(
refs=ua.ObjectIds.Organizes,
direction=ua.BrowseDirection.Inverse,
includesubtypes=False,
)
assert objects in parents
parents = await folder.get_referenced_nodes(
refs=ua.ObjectIds.HierarchicalReferences,
direction=ua.BrowseDirection.Inverse,
includesubtypes=True,
)
assert objects in parents
parents = await folder.get_referenced_nodes(
refs=ua.ObjectIds.HierarchicalReferences,
direction=ua.BrowseDirection.Inverse,
includesubtypes=False,
)
assert objects not in parents
assert await folder.get_parent() == objects
async def test_browsename_with_spaces(opc):
o = opc.opc.nodes.objects
v = await o.add_variable(3, "BNVariable with spaces and %+?", 1.3)
v2 = await o.get_child("/3:BNVariable with spaces and %+?")
assert v == v2
await opc.opc.delete_nodes([v])
async def test_non_existing_path(opc):
root = opc.opc.nodes.root
with pytest.raises(ua.UaStatusCodeError):
await root.get_child("0:Objects/0:Server/0:nonexistingnode")
async def test_bad_attribute(opc):
root = opc.opc.nodes.root
with pytest.raises(ua.UaStatusCodeError):
await root.write_value(99)
async def test_get_node_by_nodeid(opc):
root = opc.opc.nodes.root
server_time_node = await root.get_child("/Objects/Server/ServerStatus.CurrentTime")
correct = opc.opc.get_node(ua.NodeId(ua.ObjectIds.Server_ServerStatus_CurrentTime))
assert server_time_node == correct
async def test_get_children_by_path(opc):
root = opc.opc.nodes.root
[server_time_nodes] = await root.get_children_by_path(
[["0:Objects", "0:Server", "0:ServerStatus", "0:CurrentTime"]]
)
correct = opc.opc.get_node(ua.NodeId(ua.ObjectIds.Server_ServerStatus_CurrentTime))
assert len(server_time_nodes) == 1
assert server_time_nodes[0] == correct
with pytest.raises(ua.UaStatusCodeError) as e:
await root.get_children_by_path(
[
["0:Objects", "0:Server", "0:ServerStatus", "0:CurrentTime"],
["0:Objects", "0:Unknown"],
]
)
assert e.type == ua.UaStatusCodeErrors
assert e.value.codes == [ua.StatusCodes.Good, ua.StatusCodes.BadNoMatch]
assert e.value.code == ua.StatusCodes.BadNoMatch
[server_time_nodes, unknown] = await root.get_children_by_path(
[
["0:Objects", "0:Server", "0:ServerStatus", "0:CurrentTime"],
["0:Objects", "0:Unknown"],
],
raise_on_partial_error=False,
)
assert len(server_time_nodes) == 1
assert server_time_nodes[0] == correct
assert unknown is None
async def test_datetime_read_value(opc):
time_node = opc.opc.get_node(ua.NodeId(ua.ObjectIds.Server_ServerStatus_CurrentTime))
dt = await time_node.read_value()
utcnow = datetime.now(timezone.utc)
delta = utcnow - dt
assert delta < timedelta(seconds=2)
async def test_datetime_write_value(opc):
now = datetime.now(timezone.utc)
objects = opc.opc.nodes.objects
v1 = await objects.add_variable(4, "test_datetime", now)
tid = await v1.read_value()
assert now == tid
await opc.opc.delete_nodes([v1])
async def test_variant_array_dim(opc):
objects = opc.opc.nodes.objects
arry = [
[[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, 1.0], [1.0, 1.0, 1.0, 1.0]],
]
v = await objects.add_variable(3, "variableWithDims", arry)
await v.write_array_dimensions([0, 0, 0])
dim = await v.read_array_dimensions()
assert [0, 0, 0] == dim
await v.write_value_rank(0)
rank = await v.read_value_rank()
assert 0 == rank
v2 = await v.read_value()
assert arry == v2
dv = await v.read_data_value()
assert [2, 3, 4] == dv.Value.Dimensions
arry = [[[], [], []], [[], [], []]]
variant = ua.Variant(arry, ua.VariantType.UInt32)
v1 = await objects.add_variable(3, "variableWithDimsEmpty", variant)
v2 = await v1.read_value()
assert arry == v2
dv = await v1.read_data_value()
assert [2, 3, 0] == dv.Value.Dimensions
await opc.opc.delete_nodes([v, v1])
async def test_add_numeric_variable(opc):
objects = opc.opc.nodes.objects
v = await objects.add_variable("ns=3;i=888;", "3:numericnodefromstring", 99)
nid = ua.NodeId(888, 3)
qn = ua.QualifiedName("numericnodefromstring", 3)
assert nid == v.nodeid
assert qn == await v.read_browse_name()
await opc.opc.delete_nodes([v])
async def test_add_string_variable(opc):
objects = opc.opc.nodes.objects
v = await objects.add_variable("ns=3;s=stringid", "3:stringnodefromstring", [68])
nid = ua.NodeId("stringid", 3)
qn = ua.QualifiedName("stringnodefromstring", 3)
assert nid == v.nodeid
assert qn == await v.read_browse_name()
await opc.opc.delete_nodes([v])
async def test_utf8(opc):
objects = opc.opc.nodes.objects
utf_string = "æøå@%&"
bn = ua.QualifiedName(utf_string, 3)
nid = ua.NodeId("æølå", 3)
val = "æøå"
v = await objects.add_variable(nid, bn, val)
assert nid == v.nodeid
val2 = await v.read_value()
assert val == val2
bn2 = await v.read_browse_name()
assert bn == bn2
await opc.opc.delete_nodes([v])
async def test_null_variable(opc):
objects = opc.opc.nodes.objects
var = await objects.add_variable(3, "nullstring", "a string")
await var.write_value(ua.Variant(None, ua.VariantType.String))
val = await var.read_value()
assert val is None
await var.write_value("")
val = await var.read_value()
assert val is not None
assert "" == val
await opc.opc.delete_nodes([var])
async def test_variable_data_type(opc):
objects = opc.opc.nodes.objects
var = await objects.add_variable(3, "stringfordatatype", "a string")
val = await var.read_data_type_as_variant_type()
assert ua.VariantType.String == val
await opc.opc.delete_nodes([var])
var = await objects.add_variable(3, "stringarrayfordatatype", ["a", "b"])
val = await var.read_data_type_as_variant_type()
assert ua.VariantType.String == val
await opc.opc.delete_nodes([var])
async def test_add_string_array_variable(opc):
objects = opc.opc.nodes.objects
v = await objects.add_variable("ns=3;s=stringarrayid", "9:stringarray", ["l", "b"])
nid = ua.NodeId("stringarrayid", 3)
qn = ua.QualifiedName("stringarray", 9)
assert nid == v.nodeid
assert qn == await v.read_browse_name()
val = await v.read_value()
assert ["l", "b"] == val
await opc.opc.delete_nodes([v])
async def test_add_numeric_node(opc):
objects = opc.opc.nodes.objects
nid = ua.NodeId(9999, 3)
qn = ua.QualifiedName("AddNodeVar1", 3)
v1 = await objects.add_variable(nid, qn, 0)
assert nid == v1.nodeid
assert qn == await v1.read_browse_name()
await opc.opc.delete_nodes([v1])
async def test_add_string_node(opc):
objects = opc.opc.nodes.objects
qn = ua.QualifiedName("AddNodeVar2", 3)
nid = ua.NodeId("AddNodeVar2Id", 3)
v2 = await objects.add_variable(nid, qn, 0)
assert nid == v2.nodeid
assert qn == await v2.read_browse_name()
await opc.opc.delete_nodes([v2])
async def test_add_find_node_(opc):
objects = opc.opc.nodes.objects
o = await objects.add_object("ns=2;i=101;", "2:AddFindObject")
o2 = await objects.get_child("2:AddFindObject")
assert o == o2
await opc.opc.delete_nodes([o, o2])
async def test_same_browse_name(opc):
objects = opc.opc.nodes.objects
f = await objects.add_folder("ns=2;i=201;", "2:MyBNameFolder")
o = await f.add_object("ns=2;i=202;", "2:MyBName")
v = await o.add_variable("ns=2;i=203;", "2:MyBNameTarget", 2.0)
o2 = await f.add_object("ns=2;i=204;", "2:MyBName")
v2 = await o2.add_variable("ns=2;i=205;", "2:MyBNameTarget", 2.0)
nodes = await objects.get_child("/2:MyBNameFolder/2:MyBName/2:MyBNameTarget", return_all=True)
assert len(nodes) == 2
assert nodes[0] == v
assert nodes[1] == v2
[nodes] = await objects.get_children_by_path(["/2:MyBNameFolder/2:MyBName/2:MyBNameTarget"])
assert len(nodes) == 2
assert nodes[0] == v
assert nodes[1] == v2
await opc.opc.delete_nodes([f, o, o2, v, v2])
async def test_node_path(opc):
objects = opc.opc.nodes.objects
o = await objects.add_object("ns=2;i=105;", "2:NodePathObject")
root = opc.opc.nodes.root
o2 = await root.get_child(["0:Objects", "2:NodePathObject"])
assert o == o2
await opc.opc.delete_nodes([o, o2])
async def test_add_read_node(opc):
objects = opc.opc.nodes.objects
o = await objects.add_object("ns=2;i=102;", "2:AddReadObject")
nid = ua.NodeId(102, 2)
assert nid == o.nodeid
qn = ua.QualifiedName("AddReadObject", 2)
assert qn == await o.read_browse_name()
await opc.opc.delete_nodes([o])
async def test_simple_value(opc):
o = opc.opc.nodes.objects
v = await o.add_variable(3, "VariableTestValue", 4.32)
val = await v.read_value()
assert 4.32 == val
await opc.opc.delete_nodes([v])
async def test_add_exception(opc):
objects = opc.opc.nodes.objects
v = await objects.add_object("ns=2;i=103;", "2:AddReadObject")
with pytest.raises(ua.UaStatusCodeError):
await objects.add_object("ns=2;i=103;", "2:AddReadObject")
await opc.opc.delete_nodes([v])
async def test_negative_value(opc):
o = opc.opc.nodes.objects
v = await o.add_variable(3, "VariableNegativeValue", 4.0)
await v.write_value(-4.54)
assert -4.54 == await v.read_value()
await opc.opc.delete_nodes([v])
async def test_read_server_state(opc):
statenode = opc.opc.get_node(ua.NodeId(ua.ObjectIds.Server_ServerStatus_State))
assert 0 == await statenode.read_value()
async def test_bad_node(opc):
bad = opc.opc.get_node(ua.NodeId(999, 999))
with pytest.raises(ua.UaStatusCodeError):
await bad.read_browse_name()
with pytest.raises(ua.UaStatusCodeError):
await bad.write_value(89)
with pytest.raises(ua.UaStatusCodeError):
await bad.add_object(0, "0:myobj")
with pytest.raises(ua.UaStatusCodeError):
await bad.get_child("0:myobj")
await opc.opc.delete_nodes([bad])
async def test_value(opc):
o = opc.opc.nodes.objects
var = ua.Variant(ua.Double(1.98))
v = await o.add_variable(3, "VariableValue", var)
assert 1.98 == await v.read_value()
dvar = ua.DataValue(var)
dv = await v.read_data_value()
assert ua.DataValue is type(dv)
assert dvar.Value == dv.Value
assert dvar.Value == var
await opc.opc.delete_nodes([v])
async def test_write_value(opc):
o = opc.opc.nodes.objects
var = ua.Variant(1.98, ua.VariantType.Double)
dvar = ua.DataValue(var)
v = await o.add_variable(3, "VariableValue", var)
await v.write_value(var.Value)
v1 = await v.read_value()
assert v1 == var.Value
await v.write_value(var)
v2 = await v.read_value()
assert v2 == var.Value
await v.write_value(dvar)
v3 = await v.read_data_value()
assert v3.Value == dvar.Value
await opc.opc.delete_nodes([v])
async def test_write_value_statuscode_bad(opc):
o = opc.opc.nodes.objects
var = ua.Variant("Some value that should not be set!")
dvar = ua.DataValue(None, StatusCode_=ua.StatusCode(ua.StatusCodes.BadDeviceFailure))
v = await o.add_variable(3, "VariableValueBad", var)
await v.write_value(dvar)
with pytest.raises(ua.UaStatusCodeError) as error_read:
await v.read_data_value()
assert error_read.type.code == dvar.StatusCode.value
await opc.opc.delete_nodes([v])
async def test_array_value(opc):
o = opc.opc.nodes.objects
v = await o.add_variable(3, "VariableArrayValue", [1, 2, 3])
assert [1, 2, 3] == await v.read_value()
await opc.opc.delete_nodes([v])
async def test_bool_variable(opc):
o = opc.opc.nodes.objects
v = await o.add_variable(3, "BoolVariable", True)
dt = await v.read_data_type_as_variant_type()
assert ua.VariantType.Boolean == dt
val = await v.read_value()
assert val is True
await v.write_value(False)
val = await v.read_value()
assert val is False
await opc.opc.delete_nodes([v])
async def test_array_size_one_value(opc):
o = opc.opc.nodes.objects
v = await o.add_variable(3, "VariableArrayValue2", [1, 2, 3])
await v.write_value([1])
assert [1] == await v.read_value()
await opc.opc.delete_nodes([v])
async def test_use_namespace(opc):
idx = await opc.opc.get_namespace_index("urn:freeopcua:python:server")
assert 1 == idx
root = opc.opc.nodes.root
myvar = await root.add_variable(idx, "var_in_custom_namespace", [5])
myid = myvar.nodeid
assert idx == myid.NamespaceIndex
await opc.opc.delete_nodes([myvar])
async def test_method(opc):
o = opc.opc.nodes.objects
await o.get_child("2:ServerMethod")
result = await o.call_method("2:ServerMethod", 2.1)
assert 4.2 == result
with pytest.raises(ua.UaStatusCodeError):
# FIXME: we should raise a more precise exception
await o.call_method("2:ServerMethod", 2.1, 89, 9)
with pytest.raises(ua.UaStatusCodeError):
await o.call_method(ua.NodeId(999), 2.1) # non existing method
async def test_method_no_arg(opc):
o = opc.opc.nodes.objects
await o.get_child("2:ServerMethodNoArg")
result = await o.call_method("2:ServerMethodNoArg")
assert 13 == result
with pytest.raises(ua.UaStatusCodeError):
# FIXME: we should raise a more precise exception
await o.call_method("2:ServerMethodNoArg", 2.1, 89, 9)
async def test_method_array(opc):
o = opc.opc.nodes.objects
m = await o.get_child("2:ServerMethodArray")
result = await o.call_method(m, "sin", ua.Variant(math.pi))
assert result < 0.01
with pytest.raises(ua.UaStatusCodeError) as exc_info:
await o.call_method(m, "cos", ua.Variant(math.pi))
assert ua.StatusCodes.BadInvalidArgument == exc_info.type.code
with pytest.raises(ua.UaStatusCodeError) as exc_info:
await o.call_method(m, "panic", ua.Variant(math.pi))
assert ua.StatusCodes.BadOutOfMemory == exc_info.type.code
async def test_method_array2(opc):
o = opc.opc.nodes.objects
m = await o.get_child("2:ServerMethodArray2")
result = await o.call_method(m, [1.1, 3.4, 9])
assert [2.2, 6.8, 18] == result
result = await call_method_full(o, m, [1.1, 3.4, 9])
assert [[2.2, 6.8, 18]] == result.OutputArguments
async def test_method_tuple(opc):
o = opc.opc.nodes.objects
m = await o.get_child("2:ServerMethodTuple")
result = await o.call_method(m)
assert [1, 2, 3] == result
result = await call_method_full(o, m)
assert [1, 2, 3] == result.OutputArguments
async def test_method_none(opc):
# this test calls the function linked to the type's method..
o = await opc.opc.get_node(ua.ObjectIds.BaseObjectType).get_child("2:ObjectWithMethodsType")
m = await o.get_child("2:ServerMethodDefault")
result = await o.call_method(m)
assert result is None
result = await call_method_full(o, m)
assert [] == result.OutputArguments
async def test_method_async(opc):
o = opc.opc.nodes.objects
m = await o.get_child("2:ServerMethodAsync")
await o.call_method(m)
await call_method_full(o, m)
async def test_method_byte_array(opc):
o = opc.opc.nodes.objects
m = await o.get_child("2:ServerMethodByteArray")
val, mybytes = await o.call_method(m, 1, (2, 3, 4))
assert val == 1
assert mybytes == [2, 3]
async def test_method_byte_array_better(opc):
o = opc.opc.nodes.objects
m = await o.get_child("2:ServerMethodByteArray")
val, mybytes = await o.call_method(m, 1, (ua.Variant((2, 3, 4), ua.VariantType.Byte)))
assert val == 1
assert mybytes == [2, 3]
async def test_add_nodes(opc):
objects = opc.opc.nodes.objects
f = await objects.add_folder(3, "MyFolder")
child = await objects.get_child("3:MyFolder")
assert child == f
o = await f.add_object(3, "MyObject")
child = await f.get_child("3:MyObject")
assert child == o
v = await f.add_variable(3, "MyVariable", 6)
child = await f.get_child("3:MyVariable")
assert child == v
p = await f.add_property(3, "MyProperty", 10)
child = await f.get_child("3:MyProperty")
assert child == p
childs = await f.get_children()
assert o in childs
assert v in childs
assert p in childs
await opc.opc.delete_nodes([f, o, v, p, child])
async def test_modelling_rules(opc):
obj = await opc.opc.nodes.base_object_type.add_object_type(2, "MyFooObjectType")
v = await obj.add_variable(2, "myvar", 1.1)
await v.set_modelling_rule(True)
p = await obj.add_property(2, "myvar2", 1.1)
await p.set_modelling_rule(False)
refs = await obj.get_referenced_nodes(refs=ua.ObjectIds.HasModellingRule)
assert 0 == len(refs)
refs = await v.get_referenced_nodes(refs=ua.ObjectIds.HasModellingRule)
assert opc.opc.get_node(ua.ObjectIds.ModellingRule_Mandatory) == refs[0]
refs = await p.get_referenced_nodes(refs=ua.ObjectIds.HasModellingRule)
assert opc.opc.get_node(ua.ObjectIds.ModellingRule_Optional) == refs[0]
await p.set_modelling_rule(None)
refs = await p.get_referenced_nodes(refs=ua.ObjectIds.HasModellingRule)
assert 0 == len(refs)
await opc.opc.delete_nodes([v, p])
async def test_incl_subtypes(opc):
base_type = await opc.opc.nodes.root.get_child(["0:Types", "0:ObjectTypes", "0:BaseObjectType"])
descs = await base_type.get_children_descriptions(includesubtypes=True)
assert len(descs) > 10
descs = await base_type.get_children_descriptions(includesubtypes=False)
assert 0 == len(descs)
async def test_add_node_with_type(opc):
objects = opc.opc.nodes.objects
f = await objects.add_folder(3, "MyFolder_TypeTest")
o = await f.add_object(3, "MyObject1", ua.ObjectIds.BaseObjectType)
assert ua.NodeId(ua.ObjectIds.BaseObjectType) == await o.read_type_definition()
o = await f.add_object(3, "MyObject2", ua.NodeId(ua.ObjectIds.BaseObjectType, 0))
assert ua.NodeId(ua.ObjectIds.BaseObjectType) == await o.read_type_definition()
base_otype = opc.opc.get_node(ua.ObjectIds.BaseObjectType)
custom_otype = await base_otype.add_object_type(2, "MyFooObjectType2")
o = await f.add_object(3, "MyObject3", custom_otype.nodeid)
assert custom_otype.nodeid == await o.read_type_definition()
references = await o.get_references(refs=ua.ObjectIds.HasTypeDefinition, direction=ua.BrowseDirection.Forward)
assert 1 == len(references)
assert custom_otype.nodeid == references[0].NodeId
await opc.opc.delete_nodes([f, o])
async def test_references_for_added_nodes(opc):
objects = opc.opc.nodes.objects
o = await objects.add_object(3, "MyObject4")
nodes = await objects.get_referenced_nodes(
refs=ua.ObjectIds.Organizes,
direction=ua.BrowseDirection.Forward,
includesubtypes=False,
)
assert o in nodes
nodes = await o.get_referenced_nodes(
refs=ua.ObjectIds.Organizes,
direction=ua.BrowseDirection.Inverse,
includesubtypes=False,
)
assert objects in nodes
assert objects == await o.get_parent()
assert ua.NodeId(ua.ObjectIds.BaseObjectType) == await o.read_type_definition()
assert [] == await o.get_references(ua.ObjectIds.HasModellingRule)
o2 = await o.add_object(3, "MySecondObject")
nodes = await o.get_referenced_nodes(
refs=ua.ObjectIds.HasComponent,
direction=ua.BrowseDirection.Forward,
includesubtypes=False,
)
assert o2 in nodes
nodes = await o2.get_referenced_nodes(
refs=ua.ObjectIds.HasComponent,
direction=ua.BrowseDirection.Inverse,
includesubtypes=False,
)
assert o in nodes
assert o == await o2.get_parent()
assert ua.NodeId(ua.ObjectIds.BaseObjectType) == await o2.read_type_definition()
assert [] == await o2.get_references(ua.ObjectIds.HasModellingRule)
v = await o.add_variable(3, "MyVariable", 6)
nodes = await o.get_referenced_nodes(
refs=ua.ObjectIds.HasComponent,
direction=ua.BrowseDirection.Forward,
includesubtypes=False,
)
assert v in nodes
nodes = await v.get_referenced_nodes(
refs=ua.ObjectIds.HasComponent,
direction=ua.BrowseDirection.Inverse,