def test_merge_attributes(self): target = ClassFactory.create( attrs=[ AttrFactory.element(name="a", index=10), AttrFactory.element(name="b", index=1), AttrFactory.element(name="c", index=2), AttrFactory.attribute(name="id", index=0), ] ) source = target.clone() target.attrs[0].restrictions.min_occurs = 2 target.attrs[0].restrictions.max_occurs = 3 source.attrs[1].restrictions.min_occurs = 3 source.attrs[1].restrictions.max_occurs = 4 source.attrs[3].restrictions.min_occurs = 3 source.attrs[3].restrictions.max_occurs = 4 source.attrs.append(AttrFactory.enumeration(name="d", index=4)) ClassUtils.merge_attributes(target, source) names = ["id", "b", "c", "d", "a"] min_occurs = [0, 0, 0, None, 0] max_occurs = [4, 4, 1, None, 3] self.assertEqual(names, [x.name for x in target.attrs]) self.assertEqual(min_occurs, [x.restrictions.min_occurs for x in target.attrs]) self.assertEqual(max_occurs, [x.restrictions.max_occurs for x in target.attrs])
def test_property_is_factory(self): self.assertTrue(AttrFactory.any_attribute().is_factory) element = AttrFactory.element() self.assertFalse(element.is_factory) element.restrictions.max_occurs = 2 self.assertTrue(element.is_factory)
def test_constant_value(self): attr = AttrFactory.create( types=[AttrTypeFactory.native(DataType.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_property_has_suffix_attr(self): obj = ClassFactory.create() self.assertFalse(obj.has_suffix_attr) obj.attrs.append(AttrFactory.create()) obj.attrs.append(AttrFactory.create()) self.assertFalse(obj.has_suffix_attr) obj.attrs[1].index = sys.maxsize self.assertTrue(obj.has_suffix_attr)
def test_rename_attr_dependencies_with_choices(self): attr_type = AttrTypeFactory.create(qname="foo", reference=1) target = ClassFactory.create(attrs=[ AttrFactory.create(choices=[ AttrFactory.create(types=[attr_type.clone()]), ]) ]) self.sanitizer.rename_class_dependencies(target, 1, "bar") dependencies = set(target.dependencies()) self.assertNotIn("foo", dependencies) self.assertIn("bar", dependencies)
def test_rename_attr_dependencies_with_choices(self): attr_type = AttrTypeFactory.create("{foo}bar") target = ClassFactory.create(attrs=[ AttrFactory.create(choices=[ AttrFactory.create(types=[attr_type.clone()]), ]) ]) self.sanitizer.rename_class_dependencies(target, "{foo}bar", "thug") dependencies = set(target.dependencies()) self.assertNotIn("{foo}bar", dependencies) self.assertIn("thug", dependencies)
def test_process_attribute_default_enum(self, mock_find_enum, mock_logger_warning): enum_one = ClassFactory.enumeration(1, qname="{a}root") enum_one.attrs[0].default = "1" enum_one.attrs[0].name = "one" enum_two = ClassFactory.enumeration(1, qname="inner") enum_two.attrs[0].default = "2" enum_two.attrs[0].name = "two" enum_three = ClassFactory.enumeration(1, qname="missing_member") mock_find_enum.side_effect = [ None, enum_one, None, enum_two, enum_three, ] target = ClassFactory.create( qname="target", attrs=[ AttrFactory.create( types=[ AttrTypeFactory.create(), AttrTypeFactory.create(qname="foo"), ], default="1", ), AttrFactory.create( types=[ AttrTypeFactory.create(), AttrTypeFactory.create(qname="bar", forward=True), ], default="2", ), AttrFactory.create(default="3"), ], ) actual = [] for attr in target.attrs: self.sanitizer.process_attribute_default(target, attr) actual.append(attr.default) self.assertEqual(["@enum@{a}root::one", "@enum@inner::two", None], actual) mock_logger_warning.assert_called_once_with( "No enumeration member matched %s.%s default value `%s`", target.name, target.attrs[2].local_name, "3", )
def test_process(self): one = AttrFactory.attribute(fixed=True) one_clone = one.clone() restrictions = Restrictions(min_occurs=10, max_occurs=15) two = AttrFactory.element(restrictions=restrictions, fixed=True) two_clone = two.clone() two_clone.restrictions.min_occurs = 5 two_clone.restrictions.max_occurs = 5 two_clone_two = two.clone() two_clone_two.restrictions.min_occurs = 4 two_clone_two.restrictions.max_occurs = 4 three = AttrFactory.element() four = AttrFactory.enumeration() four_clone = four.clone() five = AttrFactory.element() five_clone = five.clone() five_clone_two = five.clone() target = ClassFactory.create( attrs=[ one, one_clone, two, two_clone, two_clone_two, three, four, four_clone, five, five_clone, five_clone_two, ] ) winners = [one, two, three, four, five] self.processor.process(target) self.assertEqual(winners, target.attrs) self.assertTrue(one.fixed) self.assertIsNone(one.restrictions.min_occurs) self.assertIsNone(one.restrictions.max_occurs) self.assertFalse(two.fixed) self.assertEqual(4, two.restrictions.min_occurs) self.assertEqual(24, two.restrictions.max_occurs) self.assertIsNone(three.restrictions.min_occurs) self.assertIsNone(three.restrictions.max_occurs) self.assertIsNone(four.restrictions.min_occurs) self.assertIsNone(four.restrictions.max_occurs) self.assertEqual(0, five.restrictions.min_occurs) self.assertEqual(3, five.restrictions.max_occurs)
def test_should_flatten_extension(self): source = ClassFactory.create() target = ClassFactory.create() self.assertFalse(self.processor.should_flatten_extension(source, target)) # Source has suffix attr and target has its own attrs source = ClassFactory.elements(1) source.attrs[0].index = sys.maxsize target.attrs.append(AttrFactory.create()) self.assertTrue(self.processor.should_flatten_extension(source, target)) # Target has suffix attr source = ClassFactory.create() target = ClassFactory.elements(1) target.attrs[0].index = sys.maxsize self.assertTrue(self.processor.should_flatten_extension(source, target)) # Source is a simple type source = ClassFactory.create(attrs=[AttrFactory.create(tag=Tag.SIMPLE_TYPE)]) target = ClassFactory.elements(1) self.assertTrue(self.processor.should_flatten_extension(source, target)) # Sequential violation source = ClassFactory.elements(3) target = source.clone() self.assertFalse(self.processor.should_flatten_extension(source, target)) for attr in target.attrs: attr.restrictions.sequential = True self.assertFalse(self.processor.should_flatten_extension(source, target)) target.attrs = [target.attrs[1], target.attrs[0], target.attrs[2]] self.assertTrue(self.processor.should_flatten_extension(source, target)) # Types violation target = source.clone() target.attrs[1].types = [ AttrTypeFactory.native(DataType.INT), AttrTypeFactory.native(DataType.FLOAT), ] source.attrs[1].types = [ AttrTypeFactory.native(DataType.INT), AttrTypeFactory.native(DataType.FLOAT), AttrTypeFactory.native(DataType.DECIMAL), ] self.assertFalse(self.processor.should_flatten_extension(source, target)) target.attrs[1].types.append(AttrTypeFactory.native(DataType.QNAME)) self.assertTrue(self.processor.should_flatten_extension(source, target))
def test_process(self, mock_create_substitutions, mock_process_attribute): def init_substitutions(): self.processor.substitutions = {} mock_create_substitutions.side_effect = init_substitutions target = ClassFactory.create( attrs=[AttrFactory.enumeration(), AttrFactory.any(), AttrFactory.element()] ) self.processor.process(target) self.processor.process(ClassFactory.create()) mock_process_attribute.assert_called_once_with(target, target.attrs[2]) mock_create_substitutions.assert_called_once()
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_property_xml_type(self): attr = AttrFactory.create(tag=Tag.ELEMENT) self.assertEqual("Element", attr.xml_type) attr = AttrFactory.create(tag=Tag.ATTRIBUTE) self.assertEqual("Attribute", attr.xml_type) attr = AttrFactory.create(tag=Tag.ANY_ATTRIBUTE) self.assertEqual("Attributes", attr.xml_type) attr = AttrFactory.create(tag=Tag.ANY) self.assertEqual("Wildcard", attr.xml_type) attr = AttrFactory.create(tag=Tag.RESTRICTION) self.assertIsNone(attr.xml_type)
def test_create_substitutions(self, mock_create_substitution): ns = "xsdata" classes = [ ClassFactory.create( substitutions=[build_qname(ns, "foo"), build_qname(ns, "bar")], abstract=True, ), ClassFactory.create(substitutions=[build_qname(ns, "foo")], abstract=True), ] reference_attrs = AttrFactory.list(3) mock_create_substitution.side_effect = reference_attrs self.processor.container.extend(classes) self.processor.create_substitutions() expected = { build_qname(ns, "foo"): [reference_attrs[0], reference_attrs[2]], build_qname(ns, "bar"): [reference_attrs[1]], } self.assertEqual(expected, self.processor.substitutions) mock_create_substitution.assert_has_calls( [mock.call(classes[0]), mock.call(classes[0]), mock.call(classes[1])] )
def test_field_type_with_forward_reference(self): attr = AttrFactory.create( types=AttrTypeFactory.list(1, qname="foo_bar", forward=True)) self.assertEqual( 'Optional["Parent.Inner.FooBar"]', self.filters.field_type(attr, ["Parent", "Inner"]), )
def test_field_metadata(self): attr = AttrFactory.element() expected = {"name": "attr_B", "type": "Element"} self.assertEqual(expected, self.filters.field_metadata(attr, None, ["cls"])) self.assertEqual(expected, self.filters.field_metadata(attr, "foo", ["cls"]))
def test_field_default_value_with_type_qname(self): attr = AttrFactory.create(types=[type_qname], default="xs:anyType") ns_map = {"xs": Namespace.XS.uri} self.assertEqual( 'QName("{http://www.w3.org/2001/XMLSchema}anyType")', self.filters.field_default_value(attr, ns_map), )
def test_map_binding_message_parts_with_original_message( self, mock_find_message, mock_create_message_attributes): definitions = Definitions message_name = "foo:bar" ns_map = {} message = Message( name="session", parts=[ Part(name="token", element="foo:token"), Part(name="messageId", type="id"), Part(name="another", type="id"), ], ) extended = AnyElement() mock_create_message_attributes.return_value = AttrFactory.list(2) mock_find_message.return_value = message actual = DefinitionsMapper.map_binding_message_parts( definitions, message_name, extended, ns_map) self.assertIsInstance(actual, Generator) self.assertEqual(2, len(list(actual))) mock_create_message_attributes.assert_called_once_with( message.parts, ns_map) mock_find_message.assert_called_once_with("bar")
def test_add_default_attribute(self): xs_string = AttrTypeFactory.native(DataType.STRING) extension = ExtensionFactory.create(xs_string, Restrictions(required=True)) item = ClassFactory.elements(1, extensions=[extension]) ClassExtensionHandler.add_default_attribute(item, extension) expected = AttrFactory.create(name="@value", default=None, types=[xs_string], tag=Tag.EXTENSION) self.assertEqual(2, len(item.attrs)) self.assertEqual(0, len(item.extensions)) self.assertEqual(expected, item.attrs[0]) xs_int = AttrTypeFactory.native(DataType.INT) extension = ExtensionFactory.create(xs_int, Restrictions(tokens=True)) item.extensions.append(extension) ClassExtensionHandler.add_default_attribute(item, extension) expected.types.append(xs_int) expected_restrictions = Restrictions(tokens=True, required=True, min_occurs=1, max_occurs=1) self.assertEqual(2, len(item.attrs)) self.assertEqual(0, len(item.extensions)) self.assertEqual(expected, item.attrs[0]) self.assertEqual(expected_restrictions, item.attrs[0].restrictions)
def test_field_type_with_native_type(self): attr = AttrFactory.create(types=[ AttrTypeFactory.native(DataType.INT), AttrTypeFactory.native(DataType.POSITIVE_INTEGER), ]) self.assertEqual("Optional[int]", self.filters.field_type(attr, ["a", "b"]))
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_process(self): item = ClassFactory.create() self.processor.process(item) self.assertEqual(0, len(item.attrs)) item = ClassFactory.elements(2, mixed=True) self.processor.process(item) expected = AttrFactory.create( name="content", types=[AttrTypeFactory.native(DataType.ANY_TYPE)], tag=Tag.ANY, namespace="##any", ) # Wildcard is not defined self.assertEqual(expected, item.attrs[0]) self.assertTrue(item.attrs[0].is_list) self.assertEqual(3, len(item.attrs)) self.assertTrue(item.attrs[0].mixed) # Wildcard is defined and is list item.attrs[0].restrictions.max_occurs = 3 item.attrs[0].mixed = False self.processor.process(item) self.assertEqual(3, len(item.attrs)) self.assertTrue(item.attrs[0].mixed) self.assertEqual(3, item.attrs[0].restrictions.max_occurs) # Wildcard is defined but not list item.attrs[0].restrictions.max_occurs = 1 self.processor.process(item) self.assertTrue(item.attrs[0].is_list) self.assertEqual(sys.maxsize, item.attrs[0].restrictions.max_occurs)
def test_process_type_with_forward_reference(self, mock_process_inner_type): attr = AttrFactory.create() target = ClassFactory.create() attr_type = AttrTypeFactory.create(forward=True) self.processor.process_type(target, attr, attr_type) mock_process_inner_type.assert_called_once_with(target, attr, attr_type)
def test_update_restrictions(self): attr = AttrFactory.create() self.processor.update_restrictions(attr, DataType.NMTOKENS) self.assertTrue(attr.restrictions.tokens) attr = AttrFactory.create() self.processor.update_restrictions(attr, DataType.IDREFS) self.assertTrue(attr.restrictions.tokens) attr = AttrFactory.create() self.processor.update_restrictions(attr, DataType.BASE64_BINARY) self.assertEqual("base64", attr.restrictions.format) attr = AttrFactory.create() self.processor.update_restrictions(attr, DataType.HEX_BINARY) self.assertEqual("base16", attr.restrictions.format)
def test_process_attribute_default_with_optional_field(self): target = ClassFactory.create() attr = AttrFactory.create(fixed=True, default=2) attr.restrictions.min_occurs = 0 self.sanitizer.process_attribute_default(target, attr) self.assertFalse(attr.fixed) self.assertIsNone(attr.default)
def test_find_enum(self): native_type = AttrTypeFactory.create() matching_external = AttrTypeFactory.create("foo") missing_external = AttrTypeFactory.create("bar") enumeration = ClassFactory.enumeration(1, qname="foo") inner = ClassFactory.enumeration(1, qname="foobar") target = ClassFactory.create( attrs=[ AttrFactory.create(types=[ native_type, matching_external, missing_external, ]) ], inner=[inner], ) self.sanitizer.container.extend([target, enumeration]) actual = self.sanitizer.find_enum(native_type) self.assertIsNone(actual) actual = self.sanitizer.find_enum(matching_external) self.assertEqual(enumeration, actual) actual = self.sanitizer.find_enum(missing_external) self.assertIsNone(actual)
def test_reduce(self, mock_merge_attributes): first = ClassFactory.elements(2) second = first.clone() second.attrs.append(AttrFactory.create()) third = second.clone() third.attrs.append(AttrFactory.create()) fourth = ClassFactory.create() actual = ClassUtils.reduce([first, second, third, fourth]) self.assertEqual([third, fourth], list(actual)) mock_merge_attributes.assert_has_calls( [ mock.call(third, first), mock.call(third, second), ] )
def test_reset_unsupported_types_ignore_user_types(self): attr_type = AttrTypeFactory.create(qname="foo") attr = AttrFactory.create(types=[attr_type], fixed=True, default="123") target = ClassFactory.create() target.attrs.append(attr) self.processor.process(target) self.assertEqual(attr_type, attr.types[0])
def test_process_attribute_default_with_enumeration(self): target = ClassFactory.create() attr = AttrFactory.enumeration() attr.restrictions.max_occurs = 2 attr.fixed = True self.sanitizer.process_attribute_default(target, attr) self.assertTrue(attr.fixed)
def test_field_metadata_namespace(self): attr = AttrFactory.element(namespace="foo") expected = {"name": "attr_B", "namespace": "foo", "type": "Element"} actual = self.filters.field_metadata(attr, None, ["cls"]) self.assertEqual(expected, actual) actual = self.filters.field_metadata(attr, "foo", ["cls"]) self.assertNotIn("namespace", actual) attr = AttrFactory.attribute(namespace="foo") expected = {"name": "attr_C", "namespace": "foo", "type": "Attribute"} actual = self.filters.field_metadata(attr, None, ["cls"]) self.assertEqual(expected, actual) actual = self.filters.field_metadata(attr, "foo", ["cls"]) self.assertIn("namespace", actual)
def test_field_type_with_alias(self): attr = AttrFactory.create(types=AttrTypeFactory.list( 1, qname="foo_bar", forward=True, alias="Boss:Life")) attr.restrictions.max_occurs = 2 self.assertEqual( 'List["A.Parent.BossLife"]', self.filters.field_type(attr, ["A", "Parent"]), )