Example #1
0
    def test_dependencies(self):
        obj = ClassFactory.create(
            attrs=[
                AttrFactory.create(types=[AttrTypeFactory.xs_decimal()]),
                AttrFactory.create(
                    types=[
                        AttrTypeFactory.create(name="xs:annotated", forward_ref=True)
                    ]
                ),
                AttrFactory.create(
                    types=[
                        AttrTypeFactory.create(name="xs:openAttrs"),
                        AttrTypeFactory.create(name="xs:localAttribute"),
                    ]
                ),
            ],
            extensions=ExtensionFactory.list(
                1, type=AttrTypeFactory.create(name="xs:localElement")
            ),
            inner=[
                ClassFactory.create(
                    attrs=AttrFactory.list(2, types=AttrTypeFactory.list(1, name="foo"))
                )
            ],
        )

        expected = {
            QName("{http://www.w3.org/2001/XMLSchema}localAttribute"),
            QName("{http://www.w3.org/2001/XMLSchema}localElement"),
            QName("{http://www.w3.org/2001/XMLSchema}openAttrs"),
            QName("{xsdata}foo"),
        }
        self.assertEqual(expected, obj.dependencies())
Example #2
0
    def test_dependencies(self):
        obj = ClassFactory.create(
            attrs=[
                AttrFactory.create(types=[AttrTypeFactory.xs_decimal()]),
                AttrFactory.create(types=[
                    AttrTypeFactory.create(qname=QName(Namespace.XS.uri,
                                                       "annotated"),
                                           forward=True)
                ]),
                AttrFactory.create(types=[
                    AttrTypeFactory.create(
                        qname=QName(Namespace.XS.uri, "openAttrs")),
                    AttrTypeFactory.create(
                        qname=QName(Namespace.XS.uri, "localAttribute")),
                ]),
            ],
            extensions=[
                ExtensionFactory.create(type=AttrTypeFactory.create(
                    qname=QName(Namespace.XS.uri, "foobar"))),
                ExtensionFactory.create(type=AttrTypeFactory.create(
                    qname=QName(Namespace.XS.uri, "foobar"))),
            ],
            inner=[
                ClassFactory.create(attrs=AttrFactory.list(
                    2, types=AttrTypeFactory.list(1, qname="foo")))
            ],
        )

        expected = [
            QName("{http://www.w3.org/2001/XMLSchema}openAttrs"),
            QName("{http://www.w3.org/2001/XMLSchema}localAttribute"),
            QName("{http://www.w3.org/2001/XMLSchema}foobar"),
            QName("{xsdata}foo"),
        ]
        self.assertEqual(expected, list(obj.dependencies()))
Example #3
0
    def test_dependencies(self):
        obj = ClassFactory.create(
            attrs=[
                AttrFactory.create(types=[AttrTypeFactory.xs_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.xs_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.create(type=AttrTypeFactory.create(
                    qname=build_qname(Namespace.XS.uri, "foobar"))),
                ExtensionFactory.create(type=AttrTypeFactory.create(
                    qname=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 #4
0
    def test_flatten_attribute_types_when_type_is_native(self):
        xs_bool = AttrTypeFactory.xs_bool()
        xs_decimal = AttrTypeFactory.xs_decimal()
        attr = AttrFactory.create(types=[xs_bool, xs_decimal])
        target = ClassFactory.create()
        self.analyzer.flatten_attribute_types(target, attr)

        self.assertEqual([xs_bool, xs_decimal], attr.types)

        self.analyzer.flatten_attribute_types(target, attr)
        self.assertEqual([xs_bool, xs_decimal], attr.types)
Example #5
0
    def test_flatten_attribute_types_with_restriction_pattern(self):
        xs_bool = AttrTypeFactory.xs_bool()
        xs_decimal = AttrTypeFactory.xs_decimal()
        attr = AttrFactory.create(types=[xs_bool, xs_decimal])
        target = ClassFactory.create()
        self.analyzer.flatten_attribute_types(target, attr)

        self.assertEqual([xs_bool, xs_decimal], attr.types)

        attr.restrictions.pattern = r"[0-1]"
        self.analyzer.flatten_attribute_types(target, attr)
        self.assertEqual([AttrTypeFactory.xs_string()], attr.types)
Example #6
0
    def test_attribute_default(self):
        type_str = AttrTypeFactory.xs_string()
        type_int = AttrTypeFactory.xs_int()
        type_float = AttrTypeFactory.xs_float()
        type_decimal = AttrTypeFactory.xs_decimal()
        type_bool = AttrTypeFactory.xs_bool()
        type_qname = AttrTypeFactory.xs_qname()

        attr = AttrFactory.create(name="foo", types=[type_str])
        self.assertEqual(None, generator.attribute_default(attr))

        attr.default = "foo"
        self.assertEqual('"foo"', generator.attribute_default(attr))

        attr.default = "1.5"
        attr.types[0] = type_float
        self.assertEqual(1.5, generator.attribute_default(attr))

        attr.default = "1"
        attr.types[0] = type_int
        self.assertEqual(1, generator.attribute_default(attr))

        attr.default = "true"
        attr.types[0] = type_bool
        self.assertTrue(generator.attribute_default(attr))

        attr.restrictions.max_occurs = 2
        self.assertEqual("list", generator.attribute_default(attr))

        attr.default = "1"
        attr.restrictions.max_occurs = 1
        attr.types = [type_bool, type_int, type_float]
        self.assertEqual(1, generator.attribute_default(attr))

        attr.default = "1.0"
        self.assertEqual(1.0, generator.attribute_default(attr))

        attr.default = "true"
        self.assertTrue(generator.attribute_default(attr))

        attr.default = "inf"
        attr.types = [type_int, type_float]
        self.assertEqual("float('inf')", generator.attribute_default(attr))

        attr.default = "-inf"
        self.assertEqual("float('-inf')", generator.attribute_default(attr))

        attr.types = [type_decimal]
        self.assertEqual("Decimal('-Infinity')",
                         generator.attribute_default(attr))

        attr.default = "inf"
        self.assertEqual("Decimal('Infinity')",
                         generator.attribute_default(attr))

        ns_map = {"xs": Namespace.XS.uri}
        attr.default = "xs:anyType"
        attr.types = [type_qname]
        self.assertEqual(
            'QName("http://www.w3.org/2001/XMLSchema", "anyType")',
            generator.attribute_default(attr, ns_map),
        )
Example #7
0
from unittest import TestCase

from tests.factories import AttrFactory
from tests.factories import AttrTypeFactory
from xsdata.formats.dataclass.filters import attribute_default
from xsdata.models.enums import Namespace

type_str = AttrTypeFactory.xs_string()
type_int = AttrTypeFactory.xs_int()
type_float = AttrTypeFactory.xs_float()
type_decimal = AttrTypeFactory.xs_decimal()
type_bool = AttrTypeFactory.xs_bool()
type_qname = AttrTypeFactory.xs_qname()
type_qmap = AttrTypeFactory.xs_qmap()
type_tokens = AttrTypeFactory.xs_tokens()


class AttributeDefaultTests(TestCase):
    def test_attribute_default_with_value_none(self):
        attr = AttrFactory.create(types=[type_str])
        self.assertEqual(None, attribute_default(attr))

    def test_attribute_default_with_type_str(self):
        attr = AttrFactory.create(types=[type_str], default="foo")
        self.assertEqual('"foo"', attribute_default(attr))

    def test_attribute_default_with_type_tokens(self):
        attr = AttrFactory.create(types=[type_tokens], default="foo  bar  \n")
        self.assertEqual('"foo bar"', attribute_default(attr))

    def test_attribute_default_with_type_float(self):