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)
def test_process_type_with_native_type(self, mock_process_native_type, mock_process_dependency_type): attr = AttrFactory.create() target = ClassFactory.create() xs_int = AttrTypeFactory.xs_int() self.processor.process_type(target, attr, xs_int) self.assertEqual(0, mock_process_dependency_type.call_count) mock_process_native_type.assert_called_once_with(attr, xs_int)
def test_field_metadata_restrictions(self): attr = AttrFactory.create(tag=Tag.RESTRICTION) attr.types.append(AttrTypeFactory.xs_int()) attr.restrictions.min_occurs = 1 attr.restrictions.max_occurs = 2 attr.restrictions.max_inclusive = "2" attr.restrictions.required = False expected = {"min_occurs": 1, "max_occurs": 2, "max_inclusive": 2} self.assertEqual(expected, self.filters.field_metadata(attr, None, []))
def test_process_native_type(self): attr = AttrFactory.create() xs_int = AttrTypeFactory.xs_int() xs_int_clone = xs_int.clone() self.processor.process_native_type(attr, xs_int) self.assertEqual(xs_int_clone, xs_int) attr.restrictions.pattern = "[a-z]" self.processor.process_native_type(attr, xs_int) self.assertEqual(AttrTypeFactory.xs_string(), xs_int)
def test_process_enumerations_with_mixed_types(self): obj = ClassFactory.create(attrs=[ AttrFactory.create(default="aaBB"), AttrFactory.create(default=1, types=[AttrTypeFactory.xs_int()]), ]) generator.process_enumerations(obj) actual = [(attr.name, attr.default) for attr in obj.attrs] expected = [("VALUE_1", 1), ("AA_BB", '"aaBB"')] self.assertEqual(expected, actual)
def test_build_class_attribute_types(self, mock_build_inner_class, mock_real_type): mock_real_type.return_value = " xs:integer xs:string " mock_build_inner_class.return_value = None item = ClassFactory.create() attribute = Attribute.create(default="false", index=66) actual = self.builder.build_class_attribute_types(item, attribute) expected = [AttrTypeFactory.xs_int(), AttrTypeFactory.xs_string()] self.assertEqual(expected, actual)
def test_attribute_type_with_multiple_types(self): attr = AttrFactory.create(types=[ AttrTypeFactory.create( qname="life", alias="Boss:Life", forward=True), AttrTypeFactory.xs_int(), ]) attr.restrictions.max_occurs = 2 self.assertEqual( 'List[Union["A.Parent.BossLife", int]]', attribute_type(attr, ["A", "Parent"]), )
def test_clone_attribute(self): attr = AttrFactory.create( restrictions=Restrictions(length=1), types=[ AttrTypeFactory.create(qname="x"), AttrTypeFactory.create(qname="y"), AttrTypeFactory.xs_int(), ], ) restrictions = Restrictions(length=2) clone = ClassUtils.clone_attribute(attr, restrictions) self.assertEqual(2, clone.restrictions.length) self.assertIsNot(attr, clone)
def test_build_class_attribute_types_when_obj_has_inner_class( self, mock_build_inner_class, mock_real_type): inner_class = ClassFactory.create(name="foo") mock_real_type.return_value = " xs:integer xs:string " mock_build_inner_class.return_value = inner_class item = ClassFactory.create() attribute = Attribute.create(default="false", index=66) actual = self.builder.build_class_attribute_types(item, attribute) expected = [ AttrTypeFactory.xs_int(), AttrTypeFactory.xs_string(), AttrTypeFactory.create(name="foo", forward_ref=True), ] self.assertEqual(expected, actual) self.assertEqual([inner_class], item.inner)
def test_clone_attribute(self): attr = AttrFactory.create( restrictions=RestrictionsFactory.create(length=1), types=[ AttrTypeFactory.create(name="foo:x"), AttrTypeFactory.create(name="y"), AttrTypeFactory.xs_int(), ], ) restrictions = RestrictionsFactory.create(length=2) prefix = "foo" clone = ClassUtils.clone_attribute(attr, restrictions, prefix) self.assertEqual(["foo:x", "foo:y", "integer"], [x.name for x in clone.types]) self.assertEqual(2, clone.restrictions.length) self.assertIsNot(attr, clone)
def test_build_class_attribute_types_when_obj_has_inner_class( self, mock_build_inner_classes, mock_real_type): inner_class = ClassFactory.create(qname="foo") mock_real_type.return_value = " xs:integer xs:string " mock_build_inner_classes.return_value = [inner_class] item = ClassFactory.create() attribute = Attribute(default="false", index=66) actual = SchemaMapper.build_class_attribute_types(item, attribute) expected = [ AttrTypeFactory.xs_int(), AttrTypeFactory.xs_string(), AttrTypeFactory.create(qname=QName(item.qname.namespace, "foo"), forward=True), ] self.assertEqual(expected, actual) self.assertEqual([inner_class], item.inner)
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), ])
def test_merge_redefined_classes_copies_extensions(self): class_a = ClassFactory.create() class_c = class_a.clone() type_int = AttrTypeFactory.xs_int() ext_a = ExtensionFactory.create( type=type_int, restrictions=Restrictions(max_inclusive=10, min_inclusive=1, required=True), ) ext_c = ExtensionFactory.create( type=AttrTypeFactory.create(name=class_a.name), restrictions=Restrictions(max_inclusive=0, min_inclusive=-10), ) class_a.extensions.append(ext_a) class_c.extensions.append(ext_c) classes = [class_a, class_c] expected = {"max_inclusive": 0, "min_inclusive": -10, "required": True} self.analyzer.merge_redefined_classes(classes) self.assertEqual(1, len(classes)) self.assertEqual(1, len(classes[0].extensions)) self.assertEqual(expected, classes[0].extensions[0].restrictions.asdict())
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), )
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):
def test_process_skip_when_types_is_not_enumeration_union(self): self.target.attrs[0].types.append(AttrTypeFactory.xs_int()) self.processor.process(self.target) self.assertFalse(self.target.is_enumeration)
def test_native_property(self): attr_type = AttrTypeFactory.xs_int() self.assertEqual("int", attr_type.native_name) self.assertEqual("integer", attr_type.native_code) self.assertEqual(int, attr_type.native_type)
def test_field_type_with_native_type(self): attr = AttrFactory.create( types=[AttrTypeFactory.xs_int(), AttrTypeFactory.xs_positive_int()] ) self.assertEqual("Optional[int]", self.filters.field_type(attr, ["a", "b"]))