File: test_sanitize_attributes_default_value.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 (316 lines) | stat: -rw-r--r-- 11,991 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
from unittest import mock

from xsdata.codegen.container import ClassContainer
from xsdata.codegen.handlers import SanitizeAttributesDefaultValue
from xsdata.codegen.models import Restrictions
from xsdata.models.config import GeneratorConfig
from xsdata.models.enums import DataType, Namespace, Tag
from xsdata.utils.testing import (
    AttrFactory,
    AttrTypeFactory,
    ClassFactory,
    FactoryTestCase,
)


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

        container = ClassContainer(config=GeneratorConfig())
        self.processor = SanitizeAttributesDefaultValue(container=container)

    def test_process_attribute_with_enumeration(self):
        target = ClassFactory.create()
        attr = AttrFactory.enumeration()
        attr.restrictions.max_occurs = 2
        attr.fixed = True

        self.processor.process_attribute(target, attr)
        self.assertTrue(attr.fixed)

    @mock.patch.object(SanitizeAttributesDefaultValue, "process_attribute")
    def test_process_with_attr_choices(self, mock_process_attribute):
        choice = AttrFactory.create(
            name="attr_B_Or_attr_C",
            tag="Choice",
            index=0,
            types=[AttrTypeFactory.native(DataType.ANY_TYPE)],
            choices=[
                AttrFactory.reference("one"),
                AttrFactory.reference("two"),
                AttrFactory.reference("three"),
            ],
        )
        target = ClassFactory.create()
        target.attrs.append(choice)

        self.processor.process(target)

        self.assertEqual(4, mock_process_attribute.call_count)
        mock_process_attribute.assert_has_calls(
            [
                mock.call(target, target.attrs[0]),
                mock.call(target, target.attrs[0].choices[0]),
                mock.call(target, target.attrs[0].choices[1]),
                mock.call(target, target.attrs[0].choices[2]),
            ]
        )

    @mock.patch.object(SanitizeAttributesDefaultValue, "process_types")
    @mock.patch.object(SanitizeAttributesDefaultValue, "should_reset_default")
    @mock.patch.object(SanitizeAttributesDefaultValue, "should_reset_required")
    def test_process_attribute(
        self, mock_should_reset_required, mock_should_reset_default, mock_process_types
    ):
        target = ClassFactory.create()
        mock_should_reset_required.side_effect = [
            True,
            False,
            False,
            False,
            False,
            False,
        ]
        mock_should_reset_default.side_effect = [
            False,
            True,
            False,
            False,
            False,
            False,
        ]

        attr = AttrFactory.element(restrictions=Restrictions(min_occurs=2))
        self.processor.process_attribute(target, attr)
        self.assertEqual(0, attr.restrictions.min_occurs)

        attr = AttrFactory.element(fixed=True, default="abc")
        self.processor.process_attribute(target, attr)
        self.assertIsNone(attr.default)
        self.assertFalse(attr.fixed)

        attr = AttrFactory.element(default="abc")
        self.processor.process_attribute(target, attr)
        mock_process_types.assert_called_once_with(target, attr)

        attr = AttrFactory.extension()
        self.processor.process_attribute(target, attr)
        self.assertEqual("", attr.default)

        attr = AttrFactory.extension(default="abc")
        self.processor.process_attribute(target, attr)
        self.assertEqual("abc", attr.default)

        attr = AttrFactory.extension(types=[AttrTypeFactory.native(DataType.INTEGER)])
        self.processor.process_attribute(target, attr)
        self.assertIsNone(attr.default)

    def test_should_reset_required(self):
        attr = AttrFactory.create()
        self.assertFalse(self.processor.should_reset_required(attr))

        attr = AttrFactory.any(restrictions=Restrictions(max_occurs=2))
        self.assertFalse(self.processor.should_reset_required(attr))

        attr = AttrFactory.any(default="abc")
        self.assertFalse(self.processor.should_reset_required(attr))

        attr = AttrFactory.any()
        self.assertTrue(self.processor.should_reset_required(attr))

    def test_should_reset_default(self):
        attr = AttrFactory.create()
        self.assertFalse(self.processor.should_reset_default(attr))

        attr = AttrFactory.create(name="type", namespace=Namespace.XSI.uri)
        self.assertFalse(self.processor.should_reset_default(attr))

        attr.default = "abc"
        self.assertTrue(self.processor.should_reset_default(attr))

        attr = AttrFactory.element(
            default="abc", restrictions=Restrictions(min_occurs=1)
        )
        self.assertFalse(self.processor.should_reset_default(attr))
        attr.restrictions.max_occurs = 2
        self.assertTrue(self.processor.should_reset_default(attr))

        attr = AttrFactory.attribute(
            default="abc", restrictions=Restrictions(min_occurs=0)
        )
        self.assertFalse(self.processor.should_reset_default(attr))

        attr.tag = Tag.ELEMENT
        self.assertTrue(self.processor.should_reset_default(attr))

    @mock.patch(
        "xsdata.codegen.handlers.sanitize_attributes_default_value.logger.warning"
    )
    @mock.patch.object(SanitizeAttributesDefaultValue, "reset_attribute_types")
    @mock.patch.object(SanitizeAttributesDefaultValue, "is_valid_native_value")
    @mock.patch.object(SanitizeAttributesDefaultValue, "is_valid_external_value")
    def test_process_types(
        self,
        mock_is_valid_external_value,
        mock_is_valid_native_value,
        mock_reset_attribute_types,
        mock_warning,
    ):
        mock_is_valid_external_value.side_effect = [True, False, False]
        mock_is_valid_native_value.side_effect = [True, False]

        target = ClassFactory.create()
        attr = AttrFactory.native(DataType.INT)
        attr.restrictions.format = "foo"

        self.processor.process_types(target, attr)
        self.assertEqual(0, mock_is_valid_native_value.call_count)

        self.processor.process_types(target, attr)
        self.assertEqual(0, mock_reset_attribute_types.call_count)

        self.processor.process_types(target, attr)
        self.assertEqual(1, mock_reset_attribute_types.call_count)
        mock_warning.assert_called_once_with(
            "Failed to match %s.%s default value `%s` to one of %s",
            target.name,
            attr.local_name,
            attr.default,
            [str(DataType.INT)],
        )

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

        # Not native types
        attr = AttrFactory.create(types=[AttrTypeFactory.create("foo")], default="abc")
        self.assertFalse(self.processor.is_valid_native_value(target, attr))

        # Successful
        attr = AttrFactory.native(DataType.INT, default="2")
        self.assertTrue(self.processor.is_valid_native_value(target, attr))

        # Failed: mixed types
        attr = AttrFactory.native(
            DataType.INT, default="2 a 3", restrictions=Restrictions(tokens=True)
        )
        self.assertFalse(self.processor.is_valid_native_value(target, attr))

        # Successful: mixed types
        attr.types.append(AttrTypeFactory.native(DataType.STRING))
        self.assertTrue(self.processor.is_valid_native_value(target, attr))

        # Successful: not strict
        attr = AttrFactory.native(DataType.FLOAT, default="2.00")
        self.assertTrue(self.processor.is_valid_native_value(target, attr))

        # Failed: strict
        attr.fixed = True
        self.assertFalse(self.processor.is_valid_native_value(target, attr))

        # Successful qname
        attr = AttrFactory.enumeration()
        attr.types = [AttrTypeFactory.native(DataType.QNAME)]
        attr.restrictions.tokens = True
        attr.fixed = True
        attr.default = "a:b b:a"
        target.ns_map["a"] = "b"
        target.ns_map["b"] = "a"
        self.assertTrue(self.processor.is_valid_native_value(target, attr))

        # Filed qname: missing prefix / Bonus tokens downgrade
        attr.default = "nope:a"
        self.assertFalse(self.processor.is_valid_native_value(target, attr))
        self.assertFalse(attr.restrictions.tokens)

    @mock.patch.object(SanitizeAttributesDefaultValue, "is_valid_enum_type")
    @mock.patch.object(SanitizeAttributesDefaultValue, "is_valid_inner_type")
    def test_is_valid_external_value(
        self, mock_is_valid_inner_type, mock_is_valid_enum_type
    ):
        mock_is_valid_inner_type.side_effect = [False, False, False, True]
        mock_is_valid_enum_type.side_effect = [False, False, True]

        attr = AttrFactory.create()
        attr.types = [
            AttrTypeFactory.create("foo"),
            AttrTypeFactory.create("bar"),
        ]
        target = ClassFactory.create()
        foo = ClassFactory.create(qname="foo")
        bar = ClassFactory.create(qname="bar")
        self.processor.container.add(foo)
        self.processor.container.add(bar)

        self.assertFalse(self.processor.is_valid_external_value(target, attr))
        self.assertTrue(self.processor.is_valid_external_value(target, attr))
        self.assertTrue(self.processor.is_valid_external_value(target, attr))

    def test_is_valid_inner_type(self):
        source = ClassFactory.elements(2)

        attr_type = AttrTypeFactory.create()
        attr = AttrFactory.create()

        self.assertFalse(self.processor.is_valid_inner_type(source, attr, attr_type))

        attr_type.forward = True
        self.assertFalse(self.processor.is_valid_inner_type(source, attr, attr_type))

        source.attrs.append(AttrFactory.extension())
        attr.default = "abc"
        attr.fixed = True
        self.assertTrue(self.processor.is_valid_inner_type(source, attr, attr_type))

        self.assertFalse(attr.fixed)
        self.assertIsNone(attr.default)
        self.assertTrue(source.attrs[2].fixed)
        self.assertEqual("abc", source.attrs[2].default)

    def test_is_valid_enum_type(self):
        enumeration = ClassFactory.enumeration(2, qname="{a}root")
        enumeration.attrs[0].default = "1"
        enumeration.attrs[0].name = "one"
        enumeration.attrs[1].default = "2"
        enumeration.attrs[1].name = "two"

        attr = AttrFactory.create(default="1")
        self.assertTrue(self.processor.is_valid_enum_type(enumeration, attr))
        self.assertEqual("@enum@{a}root::one", attr.default)

        attr = AttrFactory.create(default="  1  2")
        self.assertTrue(self.processor.is_valid_enum_type(enumeration, attr))
        self.assertEqual("@enum@{a}root::one@two", attr.default)

        attr = AttrFactory.create(default="3")
        self.assertFalse(self.processor.is_valid_enum_type(enumeration, attr))
        self.assertEqual("3", attr.default)

    def test_find_type(self):
        target = ClassFactory.create()
        attr_type = AttrTypeFactory.create("foo")
        foo = ClassFactory.create(qname="foo")
        self.processor.container.add(foo)

        self.assertIs(foo, self.processor.find_type(target, attr_type))

        attr_type = AttrTypeFactory.create("bar", forward=True)
        bar = ClassFactory.create(qname="bar")
        target.inner.append(bar)
        self.assertIs(bar, self.processor.find_type(target, attr_type))

    def test_reset_attribute_types(self):
        attr = AttrFactory.create(
            types=[
                AttrTypeFactory.native(DataType.INT),
                AttrTypeFactory.native(DataType.FLOAT),
            ]
        )
        attr.restrictions.format = "foo"

        self.processor.reset_attribute_types(attr)
        self.assertIsNone(attr.restrictions.format)
        self.assertEqual(1, len(attr.types))
        self.assertEqual(str(DataType.STRING), attr.types[0].qname)
        self.assertTrue(attr.types[0].native)