File: test_create_compound_fields.py

package info (click to toggle)
python-xsdata 24.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,936 kB
  • sloc: python: 29,257; xml: 404; makefile: 27; sh: 6
file content (299 lines) | stat: -rw-r--r-- 11,305 bytes parent folder | download
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
from unittest import mock

from xsdata.codegen.container import ClassContainer
from xsdata.codegen.handlers import CreateCompoundFields
from xsdata.codegen.models import Restrictions
from xsdata.models.config import GeneratorConfig
from xsdata.models.enums import Tag
from xsdata.utils import collections
from xsdata.utils.testing import (
    AttrFactory,
    ClassFactory,
    ExtensionFactory,
    FactoryTestCase,
)


class CreateCompoundFieldsTests(FactoryTestCase):
    def setUp(self):
        super().setUp()

        self.config = GeneratorConfig()
        self.config.output.compound_fields.enabled = True
        self.container = ClassContainer(config=self.config)
        self.processor = CreateCompoundFields(container=self.container)

    @mock.patch.object(CreateCompoundFields, "group_fields")
    def test_process(self, mock_group_fields):
        target = ClassFactory.elements(8)
        # First group repeating
        target.attrs[0].restrictions.choice = "1"
        target.attrs[1].restrictions.choice = "1"
        target.attrs[1].restrictions.max_occurs = 1
        # Second group repeating
        target.attrs[2].restrictions.choice = "2"
        target.attrs[3].restrictions.choice = "2"
        target.attrs[3].restrictions.max_occurs = 2
        # Third group optional
        target.attrs[4].restrictions.choice = "3"
        target.attrs[5].restrictions.choice = "3"

        self.processor.process(target)
        mock_group_fields.assert_has_calls(
            [
                mock.call(target, target.attrs[0:2]),
                mock.call(target, target.attrs[2:4]),
            ]
        )

    def test_process_with_config_enabled_false_calculate_min_occurs(self):
        self.processor.config.enabled = False
        target = ClassFactory.elements(5)
        target.attrs[0].restrictions.choice = 1
        target.attrs[1].restrictions.choice = 1
        target.attrs[2].restrictions.choice = 2
        target.attrs[3].restrictions.choice = 2

        for attr in target.attrs:
            attr.restrictions.min_occurs = 2
            attr.restrictions.max_occurs = 3

        target.attrs[0].restrictions.path = [("g", 0, 1, 1), ("c", 1, 2, 1)]
        target.attrs[1].restrictions.path = [("g", 0, 1, 1), ("c", 1, 2, 1)]
        target.attrs[2].restrictions.path = [("g", 0, 1, 1), ("c", 2, 1, 1)]
        target.attrs[3].restrictions.path = [("g", 0, 1, 1), ("c", 2, 1, 1)]
        self.processor.process(target)

        actual = [
            (attr.restrictions.min_occurs, attr.restrictions.max_occurs)
            for attr in target.attrs
        ]
        expected = [(2, 3), (2, 3), (0, 3), (0, 3), (2, 3)]
        self.assertEqual(expected, actual)

    def test_group_fields(self):
        target = ClassFactory.create(attrs=AttrFactory.list(4))
        target.attrs[0].restrictions.choice = 1
        target.attrs[1].restrictions.choice = 1
        target.attrs[0].restrictions.min_occurs = 10
        target.attrs[0].restrictions.max_occurs = 15
        target.attrs[1].restrictions.min_occurs = 5
        target.attrs[1].restrictions.max_occurs = 20
        target.attrs[2].restrictions.min_occurs = 2
        target.attrs[2].restrictions.max_occurs = 4
        target.attrs[3].restrictions.min_occurs = 1
        target.attrs[3].restrictions.max_occurs = 3

        target.attrs[0].restrictions.path = [("g", 0, 1, 1), ("c", 1, 1, 1)]
        target.attrs[1].restrictions.path = [("g", 0, 1, 1), ("c", 1, 1, 1)]
        target.attrs[2].restrictions.path = [("g", 0, 1, 1), ("c", 1, 1, 1)]
        target.attrs[3].restrictions.path = [("g", 0, 1, 1), ("c", 1, 1, 1)]

        expected = AttrFactory.create(
            name="choice",
            tag="Choice",
            index=0,
            types=collections.unique_sequence(
                t for attr in target.attrs for t in attr.types
            ),
            choices=[
                AttrFactory.create(
                    tag=target.attrs[0].tag,
                    name="attr_B",
                    types=target.attrs[0].types,
                ),
                AttrFactory.create(
                    tag=target.attrs[1].tag,
                    name="attr_C",
                    types=target.attrs[1].types,
                ),
                AttrFactory.create(
                    tag=target.attrs[2].tag,
                    name="attr_D",
                    types=target.attrs[2].types,
                ),
                AttrFactory.create(
                    tag=target.attrs[3].tag,
                    name="attr_E",
                    types=target.attrs[3].types,
                ),
            ],
        )
        expected_res = Restrictions(min_occurs=0, max_occurs=20)

        self.processor.group_fields(target, list(target.attrs))
        self.assertEqual(1, len(target.attrs))
        self.assertEqual(expected, target.attrs[0])
        self.assertEqual(expected_res, target.attrs[0].restrictions)

    def test_group_fields_with_effective_choices_sums_occurs(self):
        target = ClassFactory.create(attrs=AttrFactory.list(2))
        target.attrs[0].restrictions.choice = -1
        target.attrs[1].restrictions.choice = -1
        target.attrs[0].restrictions.min_occurs = 1
        target.attrs[0].restrictions.max_occurs = 2
        target.attrs[1].restrictions.min_occurs = 3
        target.attrs[1].restrictions.max_occurs = 4

        expected_res = Restrictions(min_occurs=4, max_occurs=6)

        self.processor.group_fields(target, list(target.attrs))
        self.assertEqual(1, len(target.attrs))
        self.assertEqual(expected_res, target.attrs[0].restrictions)

    def test_choose_name(self):
        target = ClassFactory.create()

        actual = self.processor.choose_name(target, ["a", "b", "c"], [])
        self.assertEqual("a_Or_b_Or_c", actual)

        actual = self.processor.choose_name(target, ["a", "b", "c", "d"], [])
        self.assertEqual("choice", actual)

        target.attrs.append(AttrFactory.create(name="choice"))
        actual = self.processor.choose_name(target, ["a", "b", "c", "d"], [])
        self.assertEqual("choice_1", actual)

        actual = self.processor.choose_name(target, ["a", "b", "c", "d"], [])
        self.assertEqual("choice_1", actual)

        self.processor.config.default_name = "ThisOrThat"
        actual = self.processor.choose_name(target, ["a", "b", "c", "d"], [])
        self.assertEqual("ThisOrThat", actual)

        self.processor.config.force_default_name = True
        actual = self.processor.choose_name(target, ["a", "b", "c"], [])
        self.assertEqual("ThisOrThat", actual)

        self.processor.config.force_default_name = False
        self.processor.config.use_substitution_groups = True
        actual = self.processor.choose_name(target, ["a", "b", "c"], ["d", "e", "f"])
        self.assertEqual("d_Or_e_Or_f", actual)

        actual = self.processor.choose_name(target, ["a", "b", "c"], ["d", "f"])
        self.assertEqual("a_Or_b_Or_c", actual)

    def test_build_reserved_names(self):
        base = ClassFactory.create(
            attrs=[
                AttrFactory.create("standalone"),
                AttrFactory.create(
                    name="first",
                    tag=Tag.CHOICE,
                    choices=[
                        AttrFactory.create(name="a"),
                        AttrFactory.create(name="b"),
                        AttrFactory.create(name="c"),
                    ],
                ),
                AttrFactory.create(
                    name="second",
                    tag=Tag.CHOICE,
                    choices=[
                        AttrFactory.create(name="b"),
                        AttrFactory.create(name="c"),
                    ],
                ),
            ]
        )

        target = ClassFactory.create()
        target.extensions.append(ExtensionFactory.reference(qname=base.qname))
        self.processor.container.extend([base, target])

        actual = self.processor.build_reserved_names(target, names=["b", "c"])
        expected = {"standalone", "first"}

        self.assertEqual(expected, actual)

    def test_build_attr_choice(self):
        attr = AttrFactory.create(
            name="a", namespace="xsdata", default="123", help="help", fixed=True
        )
        attr.local_name = "aaa"
        attr.restrictions = Restrictions(
            min_occurs=1,
            max_occurs=1,
            min_exclusive="1.1",
            min_inclusive="1",
            min_length=1,
            max_exclusive="1",
            max_inclusive="1.1",
            max_length=10,
            total_digits=333,
            fraction_digits=2,
            length=5,
            white_space="collapse",
            pattern=r"[A-Z]",
            explicit_timezone="+1",
            nillable=True,
            choice="abc",
            sequence=1,
        )
        expected_res = attr.restrictions.clone()
        expected_res.min_occurs = None
        expected_res.max_occurs = None
        expected_res.sequence = None

        actual = self.processor.build_attr_choice(attr)

        self.assertEqual(attr.local_name, actual.name)
        self.assertEqual(attr.namespace, actual.namespace)
        self.assertIsNone(actual.default)
        self.assertEqual(attr.tag, actual.tag)
        self.assertEqual(attr.types, actual.types)
        self.assertEqual(expected_res, actual.restrictions)
        self.assertEqual(attr.help, actual.help)
        self.assertFalse(actual.fixed)

    def test_sum_counters(self):
        counters = {
            ("g", 184, 1, 1): {
                "min": [],
                "max": [],
                ("s", 183, 1, 1): {
                    "min": [],
                    "max": [],
                    ("g", 185, 1, 1): {
                        "min": [],
                        "max": [],
                        ("s", 188, 1, 1): {
                            "min": [],
                            "max": [],
                            ("c", 192, 1, 1): {
                                "min": [0],
                                "max": [1],
                                ("s", 193, 1, 1): {
                                    "min": [0, 0],
                                    "max": [1, 1],
                                    ("c", 200, 1, 1): {"min": [0, 0], "max": [1, 1]},
                                },
                            },
                        },
                    },
                },
            }
        }

        result = self.processor.sum_counters(counters)
        self.assertEqual((0, 3), (sum(result[0]), sum(result[1])))

    def test_update_counters(self):
        attr = AttrFactory.create()
        attr.restrictions.min_occurs = 2
        attr.restrictions.max_occurs = 3
        attr.restrictions.path = [("c", 0, 1, 1)]

        counters = {}
        self.processor.update_counters(attr, counters)

        expected = {("c", 0, 1, 1): {"max": [3], "min": [0]}}
        self.assertEqual(expected, counters)

        attr.restrictions.min_occurs = 2
        attr.restrictions.path = [("c", 0, 2, 1)]

        counters = {}
        self.processor.update_counters(attr, counters)
        expected = {("c", 0, 2, 1): {"max": [3], "min": [2]}}
        self.assertEqual(expected, counters)