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])
def test_type_name(self): type_str = AttrTypeFactory.xs_string() self.assertEqual("str", generator.type_name(type_str)) type_foo_bar_bam = AttrTypeFactory.create(name="foo:bar_bam") self.assertEqual("BarBam", generator.type_name(type_foo_bar_bam))
def test_flatten_extension_with_native_type_and_target_not_enumeration( self, mock_create_default_attribute ): extension = ExtensionFactory.create(type=AttrTypeFactory.xs_string()) target = ClassFactory.elements(1, extensions=[extension]) self.analyzer.flatten_extension(target, extension) mock_create_default_attribute.assert_called_once_with(target, extension)
def test_flatten_extension_with_native_type(self, mock_flatten_extension_native): extension = ExtensionFactory.create(type=AttrTypeFactory.xs_string()) target = ClassFactory.elements(1, extensions=[extension]) self.analyzer.flatten_extension(target, extension) mock_flatten_extension_native.assert_called_once_with( target, extension)
def test_constant_value(self): attr = AttrFactory.create(types=[AttrTypeFactory.xs_string()], default="foo") self.assertEqual('"foo"', self.filters.constant_value(attr)) attr = AttrFactory.create(types=[AttrTypeFactory.create(qname="foo")]) self.assertEqual("Foo", self.filters.constant_value(attr)) attr = AttrFactory.create(types=[AttrTypeFactory.create(alias="alias")]) self.assertEqual("Alias", self.filters.constant_value(attr))
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()))
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_build_class_attribute_types_when_obj_has_no_types( self, mock_build_inner_class, mock_real_type): mock_real_type.return_value = None mock_build_inner_class.return_value = None item = ClassFactory.create() attribute = Attribute.create(default="false", index=66, name="attr") actual = self.builder.build_class_attribute_types(item, attribute) self.assertEqual(1, len(actual)) self.assertEqual(AttrTypeFactory.xs_string(), actual[0])
def test_build_class_attribute_types_when_obj_has_no_types( self, mock_build_inner_classes, mock_real_type, mock_default_type): mock_real_type.return_value = "" mock_build_inner_classes.return_value = [] mock_default_type.return_value = "xs:string" item = ClassFactory.create() attribute = Attribute(default="false", name="attr") actual = SchemaMapper.build_class_attribute_types(item, attribute) self.assertEqual(1, len(actual)) self.assertEqual(AttrTypeFactory.xs_string(), actual[0])
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_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)
def test_flatten_attribute_types_when_source_has_more_than_one_attribute( self, mock_find_class, mock_logger_warning ): type_a = AttrTypeFactory.create(name="a") type_str = AttrTypeFactory.xs_string() common = ClassFactory.create(name="bar", attrs=AttrFactory.list(2)) mock_find_class.return_value = common parent = ClassFactory.create() attr = AttrFactory.create(name="a", types=[type_a]) self.analyzer.flatten_attribute_types(parent, attr) self.assertEqual([type_str], attr.types) mock_logger_warning.assert_called_once_with( "Missing type implementation: %s", common.type.__name__ )
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_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_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):