File: test_validator.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 (181 lines) | stat: -rw-r--r-- 6,657 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
from unittest import mock

from xsdata.codegen.container import ClassContainer
from xsdata.codegen.utils import ClassUtils
from xsdata.codegen.validator import ClassValidator
from xsdata.models.config import GeneratorConfig
from xsdata.models.enums import DataType, Tag
from xsdata.utils.testing import (
    AttrFactory,
    AttrTypeFactory,
    ClassFactory,
    ExtensionFactory,
    FactoryTestCase,
)


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

        self.container = ClassContainer(config=GeneratorConfig())
        self.validator = ClassValidator(container=self.container)

    @mock.patch.object(ClassValidator, "merge_global_types")
    @mock.patch.object(ClassValidator, "handle_duplicate_types")
    @mock.patch.object(ClassValidator, "remove_invalid_classes")
    def test_process(
        self,
        mock_remove_invalid_classes,
        mock_handle_duplicate_types,
        mock_merge_global_types,
    ):
        first = ClassFactory.create()
        second = first.clone()
        third = ClassFactory.create()

        self.container.extend([first, second, third])
        self.validator.process()

        mock_remove_invalid_classes.assert_called_once_with([first, second])
        mock_handle_duplicate_types.assert_called_once_with([first, second])
        mock_merge_global_types.assert_called_once_with([first, second])

    def test_remove_invalid_classes(self):
        first = ClassFactory.create(
            extensions=[
                ExtensionFactory.create(AttrTypeFactory.native(DataType.BOOLEAN)),
                ExtensionFactory.create(AttrTypeFactory.create(qname="foo")),
            ]
        )
        second = ClassFactory.create(
            extensions=[
                ExtensionFactory.create(AttrTypeFactory.native(DataType.BOOLEAN))
            ]
        )
        third = ClassFactory.create()

        self.validator.container.extend([first, second, third])

        classes = [first, second, third]
        self.validator.remove_invalid_classes(classes)
        self.assertEqual([second, third], classes)

    @mock.patch("xsdata.codegen.mappers.definitions.logger.warning")
    def test_handle_duplicate_types(self, mock_warning):
        one = ClassFactory.create(tag=Tag.ELEMENT)
        two = one.clone()
        three = one.clone()
        four = ClassFactory.create(tag=Tag.ATTRIBUTE)
        classes = [one, two, three, four]

        self.validator.handle_duplicate_types(classes)
        self.assertEqual([three, four], classes)
        mock_warning.assert_called_once_with(
            "Duplicate type %s, will keep the last defined",
            "{xsdata}class_B",
        )

    @mock.patch.object(ClassValidator, "merge_redefined_type")
    def test_handle_duplicate_types_with_redefined_type(
        self, mock_merge_redefined_type
    ):
        one = ClassFactory.create(tag=Tag.ELEMENT)
        two = one.clone()
        three = one.clone()
        four = ClassFactory.create(tag=Tag.ATTRIBUTE)
        one.container = Tag.REDEFINE
        classes = [one, two, three, four]

        self.validator.handle_duplicate_types(classes)
        self.assertEqual([one, four], classes)
        mock_merge_redefined_type.assert_has_calls(
            [mock.call(two, one), mock.call(three, one)]
        )

    def test_merge_global_types(self):
        one = ClassFactory.create(
            qname="foo",
            tag=Tag.ELEMENT,
            namespace="a",
            help="b",
            substitutions=["a", "b"],
        )
        two = ClassFactory.create(qname="foo", tag=Tag.COMPLEX_TYPE, substitutions=[])
        three = ClassFactory.create(qname="foo", tag=Tag.SIMPLE_TYPE)

        classes = [one, two, three]
        self.validator.merge_global_types(classes)
        self.assertEqual(3, len(classes))

        classes = [one, three]
        self.validator.merge_global_types(classes)
        self.assertEqual(2, len(classes))

        classes = [two, three]
        self.validator.merge_global_types(classes)
        self.assertEqual(2, len(classes))

        classes = [one, two, three]
        one.attrs.append(AttrFactory.create)
        one.extensions.append(ExtensionFactory.reference(two.qname))

        self.validator.merge_global_types(classes)
        self.assertEqual(3, len(classes))

        one.attrs.clear()
        one.extensions.append(ExtensionFactory.reference("foo"))
        self.validator.merge_global_types(classes)
        self.assertEqual(3, len(classes))

        one.extensions.pop()
        self.validator.merge_global_types(classes)
        self.assertEqual(2, len(classes))
        self.assertIn(two, classes)
        self.assertIn(three, classes)
        self.assertEqual(one.namespace, two.namespace)
        self.assertEqual(one.help, two.help)
        self.assertEqual(one.substitutions, two.substitutions)
        self.assertEqual(2, len(one.substitutions))

    @mock.patch.object(ClassUtils, "copy_extensions")
    @mock.patch.object(ClassUtils, "copy_attributes")
    def test_merge_redefined_type_with_circular_extension(
        self, mock_copy_attributes, mock_copy_extensions
    ):
        source = ClassFactory.create()
        target = source.clone()

        ext_a = ExtensionFactory.create(AttrTypeFactory.create(qname=source.name))
        ext_str = ExtensionFactory.create(AttrTypeFactory.create(qname="foo"))
        target.extensions.append(ext_str)
        target.extensions.append(ext_a)

        self.validator.merge_redefined_type(source, target)

        mock_copy_attributes.assert_called_once_with(source, target, ext_a)
        mock_copy_extensions.assert_called_once_with(source, target, ext_a)

    @mock.patch.object(ClassUtils, "copy_group_attributes")
    def test_merge_redefined_type_with_circular_group(self, mock_copy_group_attributes):
        source = ClassFactory.create()
        target = source.clone()
        target.container = Tag.REDEFINE
        first_attr = AttrFactory.create()
        second_attr = AttrFactory.create(name=source.name)
        target.attrs.extend((first_attr, second_attr))

        self.validator.merge_redefined_type(source, target)

        mock_copy_group_attributes.assert_called_once_with(source, target, second_attr)

    def test_select_winner(self):
        classes = ClassFactory.list(2)
        self.assertEqual(-1, self.validator.select_winner(classes))

        classes[0].container = Tag.OVERRIDE
        self.assertEqual(0, self.validator.select_winner(classes))

        classes[0].container = Tag.SCHEMA
        classes[1].container = Tag.REDEFINE
        self.assertEqual(1, self.validator.select_winner(classes))