Example #1
0
    def test_attribute_display_type(self):
        parents = []
        type_foo_bar = AttrTypeFactory.create(name="foo_bar")

        attr = AttrFactory.create(name="foo",
                                  default="foo",
                                  types=[type_foo_bar])

        actual = generator.attribute_display_type(attr, parents)
        self.assertEqual("FooBar", actual)

        attr.default = None
        actual = generator.attribute_display_type(attr, parents)
        self.assertEqual("Optional[FooBar]", actual)

        parents = ["Parent"]
        attr.types[0].self_ref = True
        actual = generator.attribute_display_type(attr, parents)
        self.assertEqual('Optional["FooBar"]', actual)

        attr.types[0].forward_ref = True
        actual = generator.attribute_display_type(attr, parents)
        self.assertEqual('Optional["Parent.FooBar"]', actual)

        parents = ["A", "Parent"]
        attr.restrictions.max_occurs = 2
        actual = generator.attribute_display_type(attr, parents)
        self.assertEqual('List["A.Parent.FooBar"]', actual)

        attr.types[0].alias = "Boss:Life"
        actual = generator.attribute_display_type(attr, parents)
        self.assertEqual('List["A.Parent.BossLife"]', actual)

        attr.types = [
            AttrTypeFactory.create(name="thug:life",
                                   alias="Boss:Life",
                                   forward_ref=True),
            AttrTypeFactory.xs_int(),
        ]
        actual = generator.attribute_display_type(attr, parents)
        self.assertEqual('List[Union["A.Parent.BossLife", int]]', actual)

        attr.restrictions.max_occurs = 1
        attr.types = [AttrTypeFactory.xs_qmap()]
        actual = generator.attribute_display_type(attr, parents)
        self.assertEqual("Dict[QName, str]", actual)

        attr.types = [
            AttrTypeFactory.xs_int(),
            AttrTypeFactory.xs_positive_int()
        ]
        actual = generator.attribute_display_type(attr, parents)
        self.assertEqual("Optional[int]", actual)
Example #2
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):
Example #3
0
    def test_attribute_type_with_type_qmap(self):
        attr = AttrFactory.create(types=[AttrTypeFactory.xs_qmap()])

        self.assertEqual("Dict[QName, str]", attribute_type(attr, ["a", "b"]))