def test_process_dependency_type_with_simple_type( self, mock_find_dependency, mock_copy_attribute_properties ): simple = ClassFactory.simple_type() target = ClassFactory.create() attr = AttrFactory.create() attr_type = attr.types[0] mock_find_dependency.return_value = simple self.processor.process_dependency_type(target, attr, attr_type) mock_copy_attribute_properties.assert_called_once_with( simple, target, attr, attr_type )
def test_set_circular_flag(self, mock_is_circular_dependency): source = ClassFactory.create() target = ClassFactory.create() attr = AttrFactory.create() attr_type = attr.types[0] mock_is_circular_dependency.return_value = True self.processor.set_circular_flag(source, target, attr_type) self.assertTrue(attr_type.circular) self.assertEqual(id(source), attr_type.reference) mock_is_circular_dependency.assert_called_once_with(source, target, set())
def test_field_choices(self): attr = AttrFactory.create(choices=[ AttrFactory.element( namespace="foo", types=[type_float], restrictions=Restrictions(max_exclusive="10"), ), AttrFactory.element(namespace="bar"), AttrFactory.any(namespace="##other"), AttrFactory.element(name="bar", default="aa"), AttrFactory.element(name="tok", restrictions=Restrictions(tokens=True)), ]) actual = self.filters.field_choices(attr, "foo", ["a", "b"]) expected = ( { "name": "attr_B", "type": "Type[float]", "max_exclusive": 10.0 }, { "name": "attr_C", "namespace": "bar", "type": "Type[str]" }, { "namespace": "##other", "wildcard": True, "type": "Type[object]", }, { "default": '"aa"', "name": "bar", "type": "Type[str]" }, { "default_factory": "list", "name": "tok", "tokens": True, "type": "Type[List[str]]", }, ) self.assertEqual(expected, actual) self.filters.docstring_style = DocstringStyle.ACCESSIBLE attr.choices[0].help = "help" actual = self.filters.field_choices(attr, None, []) self.assertEqual(attr.choices[0].help, actual[0]["doc"]) self.assertNotIn("doc", actual[1])
def test_copy_inner_classes(self, mock_copy_inner_class): source = ClassFactory.create() target = ClassFactory.create() attr = AttrFactory.create(types=AttrTypeFactory.list(3)) ClassUtils.copy_inner_classes(source, target, attr) mock_copy_inner_class.assert_has_calls( [ mock.call(source, target, attr, attr.types[0]), mock.call(source, target, attr, attr.types[1]), mock.call(source, target, attr, attr.types[2]), ] )
def test_process_inner_type_with_simple_type( self, mock_copy_attribute_properties, mock_update_restrictions): attr = AttrFactory.create( types=[AttrTypeFactory.create(qname="{bar}a")]) inner = ClassFactory.simple_type(qname="{bar}a", status=Status.PROCESSED) target = ClassFactory.create(inner=[inner]) self.processor.process_inner_type(target, attr, attr.types[0]) self.assertNotIn(inner, target.inner) self.assertEqual(0, mock_update_restrictions.call_count) mock_copy_attribute_properties.assert_called_once_with( inner, target, attr, attr.types[0])
def test_rename_attr_dependencies_with_default_enum(self): attr_type = AttrTypeFactory.create(qname="{foo}bar", reference=1) target = ClassFactory.create(attrs=[ AttrFactory.create( types=[attr_type], default=f"@enum@{attr_type.qname}::member", ), ]) self.sanitizer.rename_class_dependencies(target, 1, "thug") dependencies = set(target.dependencies()) self.assertEqual("@enum@thug::member", target.attrs[0].default) self.assertNotIn("{foo}bar", dependencies) self.assertIn("thug", dependencies)
def test_field_metadata_choices(self): attr = AttrFactory.create(choices=AttrFactory.list(2, tag=Tag.ELEMENT)) actual = self.filters.field_metadata(attr, "foo", ["cls"]) expected = ( { "name": "attr_B", "type": "Type[str]" }, { "name": "attr_C", "type": "Type[str]" }, ) self.assertEqual(expected, actual["choices"])
def test_clone_attribute(self): attr = AttrFactory.create( restrictions=Restrictions(length=1), types=[ AttrTypeFactory.create(qname="x"), AttrTypeFactory.create(qname="y"), AttrTypeFactory.native(DataType.INT), ], ) restrictions = Restrictions(length=2) clone = ClassUtils.clone_attribute(attr, restrictions) self.assertEqual(2, clone.restrictions.length) self.assertIsNot(attr, clone)
def test_copy_inner_class(self): source = ClassFactory.create() inner = ClassFactory.create(qname="a", module="b", package="c") target = ClassFactory.create() attr = AttrFactory.create() attr_type = AttrTypeFactory.create(forward=True, qname=inner.qname) source.inner.append(inner) ClassUtils.copy_inner_class(source, target, attr, attr_type) self.assertEqual(1, len(target.inner)) self.assertIsNot(inner, target.inner[0]) self.assertEqual(target.package, target.inner[0].package) self.assertEqual(target.module, target.inner[0].module) self.assertEqual(inner.qname, target.inner[0].qname)
def test_field_metadata_name(self): attr = AttrFactory.element(name="bar") attr.local_name = "foo" actual = self.filters.field_metadata(attr, None, ["cls"]) self.assertEqual("foo", actual["name"]) attr = AttrFactory.element(name="Foo") attr.local_name = "foo" actual = self.filters.field_metadata(attr, None, ["cls"]) self.assertNotIn("name", actual) attr = AttrFactory.create(tag=Tag.ANY, name="bar") attr.local_name = "foo" actual = self.filters.field_metadata(attr, None, ["cls"]) self.assertNotIn("name", actual)
def test_field_default_value_with_type_float(self): attr = AttrFactory.create(types=[type_float], default="1.5") self.assertEqual("1.5", self.filters.field_default_value(attr)) attr.default = "inf" attr.types = [type_int, type_float] self.assertEqual('float("inf")', self.filters.field_default_value(attr)) attr.default = "-inf" self.assertEqual('float("-inf")', self.filters.field_default_value(attr)) attr.default = "NaN" self.assertEqual('float("nan")', self.filters.field_default_value(attr))
def test_field_default_value_with_type_tokens(self): attr = AttrFactory.create(types=[type_int, type_str], default="1 \n bar") attr.restrictions.tokens = True expected = """lambda: [ 1, "bar", ]""" self.assertEqual(expected, self.filters.field_default_value(attr)) attr.tag = Tag.ENUMERATION expected = """( 1, "bar", )""" self.assertEqual(expected, self.filters.field_default_value(attr))
def test_build_class_attribute( self, mock_real_name, mock_display_help, mock_prefix, mock_default_value, mock_is_fixed, mock_get_restrictions, mock_element_namespace, mock_build_class_attribute_types, ): item = ClassFactory.create(ns_map={"bar": "foo"}) mock_build_class_attribute_types.return_value = AttrTypeFactory.list( 1, qname="int") mock_real_name.return_value = item.name mock_display_help.return_value = "sos" mock_prefix.return_value = "com" mock_default_value.return_value = "default" mock_is_fixed.return_value = True mock_element_namespace.return_value = "http://something/common" mock_get_restrictions.return_value = {"required": True} attribute = Attribute(default="false") attribute.index = 66 attribute.ns_map["foo"] = "bar" SchemaMapper.build_class_attribute(item, attribute, Restrictions()) expected = AttrFactory.create( name=mock_real_name.return_value, types=mock_build_class_attribute_types.return_value, tag=Tag.ATTRIBUTE, namespace=mock_element_namespace.return_value, help=mock_display_help.return_value, default=mock_default_value.return_value, fixed=mock_is_fixed.return_value, index=66, restrictions=Restrictions(required=True), ) self.assertEqual(expected, item.attrs[0]) self.assertEqual({"bar": "foo", "foo": "bar"}, item.ns_map) mock_build_class_attribute_types.assert_called_once_with( item, attribute) mock_element_namespace.assert_called_once_with(attribute, item.target_namespace)
def test_build_class_attribute_from_list(self): target = ClassFactory.create() data = [1, True, 1.1] DictMapper.build_class_attribute(target, "a", data) expected = AttrFactory.create( name="a", tag=Tag.ELEMENT, types=[ AttrTypeFactory.native(DataType.SHORT, tag=Tag.ELEMENT), AttrTypeFactory.native(DataType.BOOLEAN, tag=Tag.ELEMENT), AttrTypeFactory.native(DataType.FLOAT, tag=Tag.ELEMENT), ], ) restrictions = Restrictions(max_occurs=sys.maxsize) self.assertEqual(expected, target.attrs[0]) self.assertEqual(restrictions, target.attrs[0].restrictions)
def test_build_attr_choice(self): attr = AttrFactory.create(name="a", namespace="xsdata", default="123", help="help", fixed=True) attr.local_name = "aaa" attr.restrictions = Restrictions( required=True, prohibited=None, min_occurs=1, max_occurs=1, min_exclusive="1.1", min_inclusive="1", min_length=1, max_exclusive="1", max_inclusive="1.1", max_length=10, total_digits=333, fraction_digits=2, length=5, white_space="collapse", pattern=r"[A-Z]", explicit_timezone="+1", nillable=True, choice="abc", sequential=True, ) expected_res = attr.restrictions.clone() expected_res.min_occurs = None expected_res.max_occurs = None expected_res.sequential = None actual = self.sanitizer.build_attr_choice(attr) self.assertEqual(attr.local_name, actual.name) self.assertEqual(attr.namespace, actual.namespace) self.assertEqual(attr.default, actual.default) self.assertEqual(attr.tag, actual.tag) self.assertEqual(attr.types, actual.types) self.assertEqual(expected_res, actual.restrictions) self.assertEqual(attr.help, actual.help) self.assertFalse(actual.fixed)
def test_add_attribute(self): target = ClassFactory.elements(2) attr = target.attrs[0].clone() attr.index += 1 ElementMapper.add_attribute(target, attr) self.assertEqual(2, len(target.attrs)) self.assertEqual(sys.maxsize, target.attrs[0].restrictions.max_occurs) self.assertEqual(2, len(target.attrs[0].types)) self.assertFalse(target.attrs[0].restrictions.sequential) attr = target.attrs[1].clone() attr.restrictions.sequential = True ElementMapper.add_attribute(target, attr) self.assertTrue(target.attrs[1].restrictions.sequential) attr = AttrFactory.create() ElementMapper.add_attribute(target, attr) self.assertEqual(3, len(target.attrs))
def test_process_enum_extension_with_simple_source(self): qname_type = AttrTypeFactory.native(DataType.QNAME) source = ClassFactory.create( tag=Tag.SIMPLE_TYPE, attrs=[ AttrFactory.create(types=[qname_type], restrictions=Restrictions(length=10)) ], ) target = ClassFactory.enumeration(2) extension = ExtensionFactory.reference(source.qname) target.extensions.append(extension) self.processor.container.add(source) self.processor.container.add(target) self.processor.process_dependency_extension(target, extension) for attr in target.attrs: self.assertIn(qname_type, attr.types) self.assertEqual(10, attr.restrictions.length)
def test_add_default_attribute_with_any_type(self): extension = ExtensionFactory.create( AttrTypeFactory.native(DataType.ANY_TYPE), Restrictions(min_occurs=1, max_occurs=1, required=True), ) item = ClassFactory.create(extensions=[extension]) ClassExtensionHandler.add_default_attribute(item, extension) expected = AttrFactory.create( name="@any_element", default=None, types=[extension.type.clone()], tag=Tag.ANY, namespace="##any", restrictions=Restrictions(min_occurs=1, max_occurs=1, required=True), ) self.assertEqual(1, len(item.attrs)) self.assertEqual(0, len(item.extensions)) self.assertEqual(expected, item.attrs[0])
def test_property_should_generate(self): obj = ClassFactory.create(tag=Tag.ELEMENT) self.assertTrue(obj.should_generate) obj = ClassFactory.create(tag=Tag.COMPLEX_TYPE) self.assertTrue(obj.should_generate) obj.attrs.append(AttrFactory.create(tag=Tag.EXTENSION)) self.assertFalse(obj.should_generate) obj = ClassFactory.create(tag=Tag.BINDING_OPERATION) self.assertTrue(obj.should_generate) obj = ClassFactory.create(tag=Tag.BINDING_MESSAGE) self.assertTrue(obj.should_generate) obj = ClassFactory.enumeration(2) self.assertTrue(obj.should_generate) obj = ClassFactory.create(tag=Tag.SIMPLE_TYPE) self.assertFalse(obj.should_generate)
def setUp(self): super().setUp() self.root_enum = ClassFactory.enumeration(2) self.inner_enum = ClassFactory.enumeration(2) self.target = ClassFactory.create(attrs=[ AttrFactory.create( name="value", tag=Tag.UNION, types=[ AttrTypeFactory.create(qname=self.root_enum.qname), AttrTypeFactory.create(qname=self.inner_enum.qname, forward=True), ], ), ]) self.target.inner.append(self.inner_enum) self.container = ClassContainer() self.container.add(self.target) self.container.add(self.root_enum) self.processor = ClassEnumerationHandler(container=self.container)
def test_remove_inherited_fields(self): target = ClassFactory.elements(2) source = target.clone() source.qname = "BaseClass" target.extensions.append(ExtensionFactory.reference(source.qname)) original = target.clone() self.processor.container.add(source) source.attrs[1].restrictions.choice = "1234" source.attrs[1].restrictions.max_length = 1 source.attrs[1].default = "1" source.attrs[1].mixed = True source.attrs[1].fixed = True self.processor.process(target) self.assertEqual(1, len(target.attrs)) target = original.clone() source.attrs[1].fixed = False self.processor.process(target) self.assertEqual(1, len(target.attrs)) target = original.clone() source.attrs[1].mixed = False self.processor.process(target) self.assertEqual(1, len(target.attrs)) target = original.clone() source.attrs[1].default = None self.processor.process(target) self.assertEqual(1, len(target.attrs)) target = original.clone() target.attrs.append(AttrFactory.create()) source.attrs[1].restrictions.max_length = None self.processor.process(target) self.assertEqual(1, len(target.attrs))
def test_property_is_list(self): attr = AttrFactory.create(restrictions=Restrictions(max_occurs=2)) self.assertTrue(attr.is_list) attr.restrictions.max_occurs = 1 self.assertFalse(attr.is_list)
def test_sanitize_duplicate_attribute_names(self): attrs = [ AttrFactory.create(name="a", tag=Tag.ELEMENT), AttrFactory.create(name="a", tag=Tag.ATTRIBUTE), AttrFactory.create(name="b", tag=Tag.ATTRIBUTE), AttrFactory.create(name="c", tag=Tag.ATTRIBUTE), AttrFactory.create(name="c", tag=Tag.ELEMENT), AttrFactory.create(name="d", tag=Tag.ELEMENT), AttrFactory.create(name="d", tag=Tag.ELEMENT), AttrFactory.create(name="e", tag=Tag.ELEMENT, namespace="b"), AttrFactory.create(name="e", tag=Tag.ELEMENT), AttrFactory.create(name="f", tag=Tag.ELEMENT), AttrFactory.create(name="f", tag=Tag.ELEMENT, namespace="a"), AttrFactory.create(name="gA", tag=Tag.ENUMERATION), AttrFactory.create(name="g[A]", tag=Tag.ENUMERATION), AttrFactory.create(name="g_a", tag=Tag.ENUMERATION), AttrFactory.create(name="g_a_1", tag=Tag.ENUMERATION), ] self.sanitizer.process_duplicate_attribute_names(attrs) expected = [ "a", "a_Attribute", "b", "c_Attribute", "c", "d_Element", "d", "b_e", "e", "f", "a_f", "gA", "g[A]_2", "g_a_3", "g_a_1", ] self.assertEqual(expected, [x.name for x in attrs])
def test_process_attribute_restrictions(self): required = Restrictions(min_occurs=1, max_occurs=1) attr = AttrFactory.attribute(restrictions=required.clone()) self.sanitizer.process_attribute_restrictions(attr) self.assertIsNone(attr.restrictions.min_occurs) self.assertIsNone(attr.restrictions.max_occurs) tokens = Restrictions(required=True, tokens=True, min_occurs=1, max_occurs=1) attr = AttrFactory.element(restrictions=tokens.clone()) self.sanitizer.process_attribute_restrictions(attr) self.assertFalse(attr.restrictions.required) self.assertIsNone(attr.restrictions.min_occurs) self.assertIsNone(attr.restrictions.max_occurs) attr = AttrFactory.element(restrictions=tokens.clone()) attr.restrictions.max_occurs = 2 self.sanitizer.process_attribute_restrictions(attr) self.assertFalse(attr.restrictions.required) self.assertIsNotNone(attr.restrictions.min_occurs) self.assertIsNotNone(attr.restrictions.max_occurs) multiple = Restrictions(min_occurs=0, max_occurs=2) attr = AttrFactory.create(tag=Tag.EXTENSION, restrictions=multiple) self.sanitizer.process_attribute_restrictions(attr) self.assertTrue(attr.restrictions.required) self.assertIsNone(attr.restrictions.min_occurs) self.assertIsNone(attr.restrictions.max_occurs) multiple = Restrictions(max_occurs=2, required=True) attr = AttrFactory.element(restrictions=multiple, fixed=True) self.sanitizer.process_attribute_restrictions(attr) self.assertIsNone(attr.restrictions.required) self.assertEqual(0, attr.restrictions.min_occurs) self.assertFalse(attr.fixed) attr = AttrFactory.element(restrictions=required.clone()) self.sanitizer.process_attribute_restrictions(attr) self.assertTrue(attr.restrictions.required) self.assertIsNone(attr.restrictions.min_occurs) self.assertIsNone(attr.restrictions.max_occurs) restrictions = Restrictions(required=True, min_occurs=0, max_occurs=1) attr = AttrFactory.element(restrictions=restrictions, default="A", fixed=True) self.sanitizer.process_attribute_restrictions(attr) self.assertIsNone(attr.restrictions.required) self.assertIsNone(attr.restrictions.min_occurs) self.assertIsNone(attr.restrictions.max_occurs) self.assertIsNone(attr.default) self.assertFalse(attr.fixed) attr = AttrFactory.element(restrictions=required.clone(), default="A") self.sanitizer.process_attribute_restrictions(attr) self.assertIsNone(attr.restrictions.required) attr = AttrFactory.element(restrictions=required.clone(), fixed=True) self.sanitizer.process_attribute_restrictions(attr) self.assertIsNone(attr.restrictions.required) attr = AttrFactory.element(restrictions=required.clone()) attr.restrictions.nillable = True self.sanitizer.process_attribute_restrictions(attr) self.assertIsNone(attr.restrictions.required)
def test_property_is_wild_attr(self): attr = AttrFactory.create() self.assertFalse(attr.is_wildcard) attr = AttrFactory.any() self.assertTrue(attr.is_wildcard)
def test_property_is_optional(self): attr = AttrFactory.create(restrictions=Restrictions(min_occurs=0)) self.assertTrue(attr.is_optional) attr.restrictions.min_occurs = 1 self.assertFalse(attr.is_optional)
def test_property_is_suffix(self): attr = AttrFactory.create() self.assertFalse(attr.is_suffix) attr.index = sys.maxsize self.assertTrue(attr.is_suffix)
def test_apply_aliases(self): self.resolver.aliases = { build_qname("xsdata", "d"): "IamD", build_qname("xsdata", "a"): "IamA", } type_a = AttrTypeFactory.create(qname="{xsdata}a") type_b = AttrTypeFactory.create(qname="{xsdata}b") type_c = AttrTypeFactory.create(qname="{xsdata}c") type_d = AttrTypeFactory.create(qname="{xsdata}d") obj = ClassFactory.create( qname="a", attrs=[ AttrFactory.create(name="a", types=[type_a]), AttrFactory.create(name="b", types=[type_b]), AttrFactory.create(name="c", types=[type_a, type_d]), ], inner=[ ClassFactory.create( qname="b", attrs=[ AttrFactory.create(name="c", types=[type_c]), AttrFactory.create(name="d", types=[type_d]), AttrFactory.create( name="compound", types=[AttrTypeFactory.native(DataType.ANY_TYPE)], choices=[ AttrFactory.create(name="a", types=[type_a, type_d]), ], ), ], ) ], extensions=[ExtensionFactory.create(type_a)], ) self.resolver.apply_aliases(obj) self.assertEqual(3, len(obj.attrs)) self.assertEqual(1, len(obj.attrs[0].types)) self.assertEqual(1, len(obj.attrs[1].types)) self.assertEqual(2, len(obj.attrs[2].types)) self.assertEqual("IamA", obj.attrs[0].types[0].alias) self.assertIsNone(obj.attrs[1].types[0].alias) self.assertEqual("IamA", obj.attrs[2].types[0].alias) self.assertEqual("IamD", obj.attrs[2].types[1].alias) self.assertEqual("IamA", obj.extensions[0].type.alias) self.assertEqual(1, len(obj.inner)) self.assertEqual(3, len(obj.inner[0].attrs)) self.assertEqual(1, len(obj.inner[0].attrs[0].types)) self.assertEqual(1, len(obj.inner[0].attrs[1].types)) self.assertEqual("IamA", obj.inner[0].attrs[2].choices[0].types[0].alias) self.assertEqual("IamD", obj.inner[0].attrs[2].choices[0].types[1].alias) self.assertIsNone(obj.inner[0].attrs[0].types[0].alias) self.assertEqual("IamD", obj.inner[0].attrs[1].types[0].alias)
def test_property_is_nameless(self): self.assertFalse(AttrFactory.create(tag=Tag.ELEMENT).is_nameless) self.assertFalse(AttrFactory.create(tag=Tag.ATTRIBUTE).is_nameless) self.assertTrue(AttrFactory.create(tag=Tag.ANY).is_nameless)
def test_process_attribute_default_with_valid_case(self): target = ClassFactory.create() attr = AttrFactory.create(fixed=True, default=2) self.sanitizer.process_attribute_default(target, attr) self.assertTrue(attr.fixed) self.assertEqual(2, attr.default)