6
6
from xsdata .exceptions import ParserError
7
7
from xsdata .formats .dataclass .context import XmlContext
8
8
from xsdata .formats .dataclass .models .elements import XmlType
9
+ from xsdata .formats .dataclass .parsers .config import ParserConfig
9
10
from xsdata .formats .dataclass .parsers .nodes import UnionNode
10
11
from xsdata .models .mixins import attribute
11
12
from xsdata .utils .testing import XmlVarFactory
12
13
13
14
14
15
class UnionNodeTests (TestCase ):
16
+ def setUp (self ) -> None :
17
+ super ().setUp ()
18
+
19
+ self .context = XmlContext ()
20
+ self .config = ParserConfig ()
21
+
15
22
def test_child (self ):
16
23
attrs = {"id" : "1" }
17
24
ns_map = {"ns0" : "xsdata" }
18
- ctx = XmlContext ()
19
25
var = XmlVarFactory .create (xml_type = XmlType .TEXT , name = "foo" , qname = "foo" )
20
- node = UnionNode (position = 0 , var = var , context = ctx , attrs = {}, ns_map = {})
26
+ node = UnionNode (
27
+ position = 0 ,
28
+ var = var ,
29
+ config = self .config ,
30
+ context = self .context ,
31
+ attrs = {},
32
+ ns_map = {},
33
+ )
21
34
self .assertEqual (node , node .child ("foo" , attrs , ns_map , 10 ))
22
35
23
36
self .assertEqual (1 , node .level )
24
37
self .assertEqual ([("start" , "foo" , attrs , ns_map )], node .events )
25
38
self .assertIsNot (attrs , node .events [0 ][2 ])
26
39
27
40
def test_bind_appends_end_event_when_level_not_zero (self ):
28
- ctx = XmlContext ()
29
41
var = XmlVarFactory .create (xml_type = XmlType .TEXT , name = "foo" , qname = "foo" )
30
- node = UnionNode (position = 0 , var = var , context = ctx , attrs = {}, ns_map = {})
42
+ node = UnionNode (
43
+ position = 0 ,
44
+ var = var ,
45
+ config = self .config ,
46
+ context = self .context ,
47
+ attrs = {},
48
+ ns_map = {},
49
+ )
31
50
node .level = 1
32
51
objects = []
33
52
@@ -43,12 +62,18 @@ def test_bind_returns_best_matching_object(self):
43
62
item2 = make_dataclass ("Item2" , [("a" , int , attribute ())])
44
63
root = make_dataclass ("Root" , [("item" , Union [str , int , item2 , item ])])
45
64
46
- ctx = XmlContext ()
47
- meta = ctx .build (root )
65
+ meta = self .context .build (root )
48
66
var = next (meta .find_children ("item" ))
49
67
attrs = {"a" : "1" , "b" : 2 }
50
68
ns_map = {}
51
- node = UnionNode (position = 0 , var = var , context = ctx , attrs = attrs , ns_map = ns_map )
69
+ node = UnionNode (
70
+ position = 0 ,
71
+ var = var ,
72
+ config = self .config ,
73
+ context = self .context ,
74
+ attrs = attrs ,
75
+ ns_map = ns_map ,
76
+ )
52
77
objects = []
53
78
54
79
self .assertTrue (node .bind ("item" , "1" , None , objects ))
@@ -73,11 +98,17 @@ def test_bind_returns_best_matching_object(self):
73
98
self .assertEqual ("a" , objects [- 1 ][1 ])
74
99
75
100
def test_bind_raises_parser_error_on_failure (self ):
76
- ctx = XmlContext ()
77
- meta = ctx .build (UnionType )
101
+ meta = self .context .build (UnionType )
78
102
var = next (meta .find_children ("element" ))
79
103
80
- node = UnionNode (position = 0 , var = var , context = ctx , attrs = {}, ns_map = {})
104
+ node = UnionNode (
105
+ position = 0 ,
106
+ var = var ,
107
+ config = self .config ,
108
+ context = self .context ,
109
+ attrs = {},
110
+ ns_map = {},
111
+ )
81
112
82
113
with self .assertRaises (ParserError ) as cm :
83
114
node .bind ("element" , None , None , [])
0 commit comments