File: test_process_mixed_content_class.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 (48 lines) | stat: -rw-r--r-- 1,746 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
import sys

from xsdata.codegen.handlers import ProcessMixedContentClass
from xsdata.codegen.models import Restrictions
from xsdata.models.enums import DataType, NamespaceType
from xsdata.utils.testing import AttrFactory, ClassFactory, FactoryTestCase


class ProcessMixedContentClassTests(FactoryTestCase):
    def setUp(self):
        super().setUp()
        self.processor = ProcessMixedContentClass()

    def test_process(self):
        res = Restrictions(min_occurs=1, max_occurs=1, sequence=1)
        attrs = [
            AttrFactory.attribute(),  # keep
            AttrFactory.reference("foo", restrictions=res.clone()),  # choice
            AttrFactory.native(DataType.INT, restrictions=res.clone()),  # choice
            AttrFactory.native(DataType.ANY_TYPE, restrictions=res.clone()),  # drop
            AttrFactory.any(),  # drop
        ]
        item = ClassFactory.create(attrs=attrs)

        self.processor.process(item)
        self.assertEqual(5, len(item.attrs))

        item.mixed = True
        self.processor.process(item)

        self.assertEqual(2, len(item.attrs))
        self.assertEqual(attrs[0], item.attrs[0])

        expected = AttrFactory.any(
            mixed=True, name="content", namespace=NamespaceType.ANY_NS
        )
        expected.restrictions.min_occurs = 0
        expected.restrictions.max_occurs = sys.maxsize
        choices = []
        for attr in attrs[1:-2]:
            choice = attr.clone()
            choice.restrictions.min_occurs = None
            choice.restrictions.max_occurs = None
            choice.restrictions.sequence = None
            choices.append(choice)

        self.assertEqual(expected, item.attrs[1])
        self.assertEqual(choices, item.attrs[1].choices)