Ejemplo n.º 1
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):
Ejemplo n.º 2
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()
        type_tokens = AttrTypeFactory.xs_tokens()

        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),
        )

        attr.default = "foo  bar  \n"
        attr.types = [type_tokens]
        self.assertEqual('"foo bar"', generator.attribute_default(attr))