File: test_reset_attribute_sequence_numbers.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 (81 lines) | stat: -rw-r--r-- 2,975 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
from xsdata.codegen.container import ClassContainer, Steps
from xsdata.codegen.handlers import ResetAttributeSequenceNumbers
from xsdata.codegen.models import Restrictions, Status
from xsdata.models.config import GeneratorConfig
from xsdata.utils.testing import (
    AttrFactory,
    ClassFactory,
    ExtensionFactory,
    FactoryTestCase,
)


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

        self.config = GeneratorConfig()
        self.container = ClassContainer(config=self.config)
        self.container.step = Steps.FINALIZE
        self.processor = ResetAttributeSequenceNumbers(container=self.container)

    def test_process_without_sequence_fields(self):
        target = ClassFactory.elements(2)
        self.processor.process(target)

    def test_process_without_parent_class(self):
        target = ClassFactory.create(
            attrs=[
                AttrFactory.create(restrictions=Restrictions(sequence=100)),
                AttrFactory.create(restrictions=Restrictions(sequence=100)),
                AttrFactory.create(),
                AttrFactory.create(restrictions=Restrictions(sequence=101)),
                AttrFactory.create(restrictions=Restrictions(sequence=101)),
            ]
        )
        self.processor.process(target)

        actual = [x.restrictions.sequence for x in target.attrs]
        expected = [1, 1, None, 2, 2]
        self.assertEqual(expected, actual)

    def test_process_with_parent_classes(self):
        target = ClassFactory.create(
            status=Status.FINALIZING,
            attrs=[
                AttrFactory.create(restrictions=Restrictions(sequence=100)),
                AttrFactory.create(restrictions=Restrictions(sequence=100)),
                AttrFactory.create(),
                AttrFactory.create(restrictions=Restrictions(sequence=101)),
                AttrFactory.create(restrictions=Restrictions(sequence=101)),
            ],
        )

        parent_a = ClassFactory.create(
            status=Status.RESOLVED,
            attrs=[
                AttrFactory.create(restrictions=Restrictions(sequence=102)),
            ],
        )

        parent_b = ClassFactory.create(
            status=Status.RESOLVED,
            attrs=[
                AttrFactory.create(restrictions=Restrictions(sequence=103)),
            ],
        )

        parent_a.extensions.append(ExtensionFactory.reference(parent_b.qname))
        target.extensions.append(ExtensionFactory.reference(parent_a.qname))

        self.container.add(target)
        self.container.add(parent_a)
        self.container.add(parent_b)
        self.processor.process(target)

        actual = [x.restrictions.sequence for x in target.attrs]
        expected = [3, 3, None, 4, 4]
        self.assertEqual(expected, actual)

        self.assertEqual(2, parent_a.attrs[0].restrictions.sequence)
        self.assertEqual(1, parent_b.attrs[0].restrictions.sequence)