Ejemplo n.º 1
0
    def test_remove_invalid_classes(self):
        first = ClassFactory.create(extensions=[
            ExtensionFactory.create(type=AttrTypeFactory.xs_bool()),
            ExtensionFactory.create(type=AttrTypeFactory.create(name="foo")),
        ])
        second = ClassFactory.create(extensions=[
            ExtensionFactory.create(type=AttrTypeFactory.xs_bool()),
        ])
        third = ClassFactory.create()

        self.validator.container.extend([first, second, third])

        classes = [first, second, third]
        self.validator.remove_invalid_classes(classes)
        self.assertEqual([second, third], classes)
Ejemplo n.º 2
0
 def test_flatten_attribute_types_filters_duplicate_types(self):
     target = ClassFactory.create(attrs=[
         AttrFactory.create(types=[
             AttrTypeFactory.xs_string(),
             AttrTypeFactory.xs_string(),
             AttrTypeFactory.xs_bool(),
         ])
     ])
     self.analyzer.flatten_attribute_types(target, target.attrs[0])
     self.assertEqual(["string", "boolean"],
                      [x.name for x in target.attrs[0].types])
Ejemplo n.º 3
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)
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
    def test_process(self, mock_process_type):
        xs_int = AttrTypeFactory.xs_int()
        xs_bool = AttrTypeFactory.xs_bool()
        xs_string = AttrTypeFactory.xs_string()

        target = ClassFactory.create(attrs=[
            AttrFactory.create(types=[xs_int, xs_bool]),
            AttrFactory.create(types=[xs_string, xs_string]),
        ])

        self.processor.process(target)
        self.assertEqual(2, len(target.attrs[0].types))
        self.assertEqual(1, len(target.attrs[1].types))  # remove duplicate

        mock_process_type.assert_has_calls([
            mock.call(target, target.attrs[0], xs_int),
            mock.call(target, target.attrs[0], xs_bool),
            mock.call(target, target.attrs[1], xs_string),
            mock.call(target, target.attrs[1], xs_string),
        ])
Ejemplo n.º 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),
        )
Ejemplo n.º 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):