Example #1
0
    def test_dependencies(self):
        obj = ClassFactory.create(
            attrs=[
                AttrFactory.create(
                    types=[AttrTypeFactory.native(DataType.DECIMAL)]),
                AttrFactory.create(
                    types=[
                        AttrTypeFactory.create(
                            qname=build_qname(Namespace.XS.uri, "annotated"),
                            forward=True,
                        )
                    ],
                    choices=[
                        AttrFactory.create(
                            name="x",
                            types=[
                                AttrTypeFactory.create(qname="choiceAttr"),
                                AttrTypeFactory.native(DataType.STRING),
                            ],
                        ),
                        AttrFactory.create(
                            name="x",
                            types=[
                                AttrTypeFactory.create(qname="choiceAttrTwo"),
                                AttrTypeFactory.create(qname="choiceAttrEnum"),
                            ],
                        ),
                    ],
                ),
                AttrFactory.create(types=[
                    AttrTypeFactory.create(
                        qname=build_qname(Namespace.XS.uri, "openAttrs")),
                    AttrTypeFactory.create(
                        qname=build_qname(Namespace.XS.uri, "localAttribute")),
                ]),
            ],
            extensions=[
                ExtensionFactory.reference(
                    build_qname(Namespace.XS.uri, "foobar")),
                ExtensionFactory.reference(
                    build_qname(Namespace.XS.uri, "foobar")),
            ],
            inner=[
                ClassFactory.create(attrs=AttrFactory.list(
                    2, types=AttrTypeFactory.list(1, qname="{xsdata}foo")))
            ],
        )

        expected = [
            "choiceAttr",
            "choiceAttrTwo",
            "choiceAttrEnum",
            "{http://www.w3.org/2001/XMLSchema}openAttrs",
            "{http://www.w3.org/2001/XMLSchema}localAttribute",
            "{http://www.w3.org/2001/XMLSchema}foobar",
            "{xsdata}foo",
        ]
        self.assertCountEqual(expected, list(obj.dependencies()))
Example #2
0
    def test_remove_inherited_fields_with_lists_type(self):
        target = ClassFactory.elements(2)
        target.attrs[0].restrictions.min_occurs = 1
        target.attrs[0].restrictions.max_occurs = 3

        source = target.clone()
        source.qname = "BaseClass"
        target.attrs[0].restrictions.min_occurs = 0
        target.attrs[1].restrictions.max_occurs = 10

        target.extensions.append(ExtensionFactory.reference(source.qname))
        self.processor.container.add(source)

        self.processor.process(target)
        self.assertEqual(2, len(target.attrs))  # min/max occurs didn't match

        self.processor.container.config.output.compound_fields = True
        self.processor.process(target)
        self.assertEqual(2, len(
            target.attrs))  # The are not part of a choice group

        target.attrs[0].restrictions.choice = "123"
        target.attrs[1].restrictions.choice = "123"
        source.attrs[0].restrictions.choice = "456"
        source.attrs[1].restrictions.choice = "456"
        self.processor.process(target)
        self.assertEqual(0, len(target.attrs))
Example #3
0
    def test_process_enum_extension_with_complex_source(self):
        source = ClassFactory.create(
            tag=Tag.COMPLEX_TYPE,
            attrs=[
                AttrFactory.create(tag=Tag.ATTRIBUTE),
                AttrFactory.create(tag=Tag.RESTRICTION),
            ],
            extensions=ExtensionFactory.list(2),
            status=Status.PROCESSED,
        )
        target = ClassFactory.enumeration(1)
        target.attrs[0].default = "Yes"
        extension = ExtensionFactory.reference(source.qname)
        target.extensions.append(extension)
        expected = target.clone()

        self.processor.container.add(source)
        self.processor.container.add(target)
        self.processor.process_dependency_extension(target, extension)

        expected.attrs = [attr.clone() for attr in source.attrs]
        expected.extensions = [ext.clone() for ext in source.extensions]
        expected.attrs[1].default = "Yes"
        expected.attrs[1].fixed = True

        self.assertEqual(expected, target)

        self.assertIsNone(target.attrs[0].default)
        self.assertFalse(target.attrs[0].fixed)
        self.assertEqual("Yes", target.attrs[1].default)
        self.assertTrue(target.attrs[1].fixed)
Example #4
0
    def test_process_enum_extension_raises_exception(self):
        source = ClassFactory.elements(2)
        target = ClassFactory.enumeration(2)
        extension = ExtensionFactory.reference(source.qname)
        target.extensions.append(extension)
        self.processor.container.add(source)
        self.processor.container.add(target)

        with self.assertRaises(CodeGenerationError):
            self.processor.process_dependency_extension(target, extension)
    def test_should_remove_extension(self):
        source = ClassFactory.create()
        target = ClassFactory.create()

        # source is target
        self.assertTrue(self.processor.should_remove_extension(source, source))
        self.assertFalse(self.processor.should_remove_extension(source, target))

        # Source is parent class
        source.inner.append(target)
        self.assertTrue(self.processor.should_remove_extension(target, target))

        # MRO Violation
        source.inner.clear()
        target.extensions.append(ExtensionFactory.reference("foo"))
        target.extensions.append(ExtensionFactory.reference("bar"))
        self.assertFalse(self.processor.should_remove_extension(source, target))

        source.extensions.append(ExtensionFactory.reference("bar"))
        self.assertTrue(self.processor.should_remove_extension(source, target))
Example #6
0
    def test_process_enum_extension_with_enum_source(self):
        source = ClassFactory.enumeration(3)
        target = ClassFactory.enumeration(2)
        target.attrs[1].name = source.attrs[2].name
        extension = ExtensionFactory.reference(source.qname)
        target.extensions.append(extension)

        self.processor.container.add(source)
        self.processor.container.add(target)
        self.processor.process_dependency_extension(target, extension)

        self.assertEqual(2, len(target.attrs))
        self.assertEqual(0, len(target.extensions))
        self.assertEqual(source.attrs[2], target.attrs[1])
Example #7
0
    def test_process_enum_extension_with_simple_source(self):
        qname_type = AttrTypeFactory.native(DataType.QNAME)
        source = ClassFactory.create(
            tag=Tag.SIMPLE_TYPE,
            attrs=[
                AttrFactory.create(types=[qname_type],
                                   restrictions=Restrictions(length=10))
            ],
        )
        target = ClassFactory.enumeration(2)
        extension = ExtensionFactory.reference(source.qname)
        target.extensions.append(extension)

        self.processor.container.add(source)
        self.processor.container.add(target)
        self.processor.process_dependency_extension(target, extension)

        for attr in target.attrs:
            self.assertIn(qname_type, attr.types)
            self.assertEqual(10, attr.restrictions.length)
Example #8
0
    def test_build_class_extensions(self, mock_children_extensions):
        bar_type = AttrTypeFactory.create(qname="bar")
        foo_type = AttrTypeFactory.create(qname="foo")

        bar = ExtensionFactory.create(bar_type)
        double = ExtensionFactory.create(bar_type)
        foo = ExtensionFactory.create(foo_type)

        mock_children_extensions.return_value = [bar, double, foo]
        self_ext = ExtensionFactory.reference(
            qname="{xsdata}something",
            restrictions=Restrictions(min_occurs=1, max_occurs=1),
        )

        item = ClassFactory.create()
        element = Element(type="something")
        SchemaMapper.build_class_extensions(element, item)

        self.assertEqual(3, len(item.extensions))
        self.assertCountEqual([bar, self_ext, foo], item.extensions)
Example #9
0
    def test_remove_inherited_fields(self):
        target = ClassFactory.elements(2)
        source = target.clone()
        source.qname = "BaseClass"
        target.extensions.append(ExtensionFactory.reference(source.qname))
        original = target.clone()

        self.processor.container.add(source)
        source.attrs[1].restrictions.choice = "1234"
        source.attrs[1].restrictions.max_length = 1
        source.attrs[1].default = "1"
        source.attrs[1].mixed = True
        source.attrs[1].fixed = True

        self.processor.process(target)
        self.assertEqual(1, len(target.attrs))

        target = original.clone()
        source.attrs[1].fixed = False
        self.processor.process(target)
        self.assertEqual(1, len(target.attrs))

        target = original.clone()
        source.attrs[1].mixed = False
        self.processor.process(target)
        self.assertEqual(1, len(target.attrs))

        target = original.clone()
        source.attrs[1].default = None
        self.processor.process(target)
        self.assertEqual(1, len(target.attrs))

        target = original.clone()
        target.attrs.append(AttrFactory.create())
        source.attrs[1].restrictions.max_length = None
        self.processor.process(target)
        self.assertEqual(1, len(target.attrs))