def test_property_default_type(self): obj = Element() self.assertEqual("anyType", obj.default_type) obj = Element() obj.ns_map["foo"] = Namespace.XS.uri self.assertEqual("foo:anyType", obj.default_type)
def test_end_schema( self, mock_set_schema_forms, mock_set_schema_namespaces, mock_add_default_imports, mock_resolve_schemas_locations, ): schema = Schema() schema.elements.append(Element()) schema.elements.append(Element()) schema.elements.append(Element()) for el in schema.elements: self.assertEqual(1, el.min_occurs) self.assertEqual(1, el.max_occurs) self.parser.end_schema(schema) for el in schema.elements: self.assertIsNone(el.min_occurs) self.assertIsNone(el.max_occurs) self.parser.end_schema(ComplexType()) mock_set_schema_forms.assert_called_once_with(schema) mock_set_schema_namespaces.assert_called_once_with(schema) mock_add_default_imports.assert_called_once_with(schema) mock_resolve_schemas_locations.assert_called_once_with(schema)
def test_element_children(self): sequence_one = Sequence(elements=[Element(), Element()]) sequence_two = Sequence(max_occurs=2, elements=[Element(), Element()]) restriction = Restriction( enumerations=[Enumeration(value=x) for x in "abc"], sequence=sequence_two, ) complex_type = ComplexType( attributes=[Attribute(), Attribute()], sequence=sequence_one, simple_content=SimpleContent(restriction=Restriction()), complex_content=ComplexContent(restriction=restriction,), ) restrictions = Restrictions.from_element(complex_type) children = self.builder.element_children(complex_type, restrictions) expected = [ (sequence_two.elements[0], Restrictions.from_element(sequence_two)), (sequence_two.elements[1], Restrictions.from_element(sequence_two)), (restriction.enumerations[0], Restrictions.from_element(restriction)), (restriction.enumerations[1], Restrictions.from_element(restriction)), (restriction.enumerations[2], Restrictions.from_element(restriction)), (sequence_one.elements[0], Restrictions.from_element(sequence_one)), (sequence_one.elements[1], Restrictions.from_element(sequence_one)), (complex_type.attributes[0], Restrictions.from_element(complex_type)), (complex_type.attributes[1], Restrictions.from_element(complex_type)), ] self.assertIsInstance(children, GeneratorType) self.assertEqual(expected, list(children))
def test_property_is_mixed(self): obj = Element() self.assertFalse(obj.is_mixed) obj.complex_type = ComplexType() self.assertFalse(obj.is_mixed) obj.complex_type.mixed = True self.assertTrue(obj.is_mixed)
def test_property_raw_type(self): obj = Element(ns_map={"xs": Namespace.XS.uri}) self.assertEqual("xs:anyType", obj.raw_type) obj.type = "foo" self.assertEqual("foo", obj.raw_type) obj.type = None obj.complex_type = ComplexType() self.assertIsNone(obj.raw_type)
def test_property_real_name(self): obj = Element(ref="bar") self.assertEqual("bar", obj.real_name) obj.name = "foo" self.assertEqual("foo", obj.real_name) with self.assertRaises(SchemaValueError): obj = Element() obj.real_name
def test_element_namespace(self): target_ns = "foobar" element = Element(ref="foo:something") element.ns_map["foo"] = "bar" self.assertEqual("bar", SchemaMapper.element_namespace(element, target_ns)) element = Element(form=FormType.QUALIFIED) self.assertEqual("foobar", SchemaMapper.element_namespace(element, target_ns)) element = Element() self.assertEqual("", SchemaMapper.element_namespace(element, target_ns)) element.target_namespace = "tns" self.assertEqual("tns", SchemaMapper.element_namespace(element, target_ns)) attribute = Attribute() self.assertIsNone(SchemaMapper.element_namespace(attribute, target_ns)) attribute.target_namespace = "tns" self.assertEqual("tns", SchemaMapper.element_namespace(attribute, target_ns))
def test_build_inner_classes(self, mock_build_class): inner_classes = ClassFactory.list(2) mock_build_class.side_effect = inner_classes simple_type = SimpleType() complex_type = ComplexType() enumeration = SimpleType(restriction=Restriction( enumerations=[Enumeration(value="a")])) element = Element(alternatives=[ Alternative(complex_type=complex_type, id="a"), Alternative(simple_type=simple_type, id="b"), Alternative(simple_type=enumeration, id="c"), ]) result = SchemaMapper.build_inner_classes(element, "module", "target_ns") self.assertIsInstance(result, Iterator) self.assertEqual(inner_classes, list(result)) self.assertEqual("a", complex_type.name) self.assertEqual("c", enumeration.name) mock_build_class.assert_has_calls([ mock.call(complex_type, Tag.ALTERNATIVE, "module", "target_ns"), mock.call(enumeration, Tag.ALTERNATIVE, "module", "target_ns"), ])
def test_root_elements(self): override = Override() redefine = Redefine() redefine.annotation = Annotation() redefine.complex_types.append(ComplexType()) override.annotation = Annotation() override.groups.append(Group()) override.simple_types.append(SimpleType()) schema = Schema() schema.simple_types.append(SimpleType()) schema.attribute_groups.append(AttributeGroup()) schema.groups.append(Group()) schema.attributes.append(Attribute()) schema.complex_types.append(ComplexType()) schema.elements.append(Element()) schema.redefines.append(redefine) schema.overrides.append(override) iterator = SchemaMapper.root_elements(schema) expected = [ ("Override", override.simple_types[0]), ("Override", override.groups[0]), ("Redefine", redefine.complex_types[0]), ("Schema", schema.simple_types[0]), ("Schema", schema.complex_types[0]), ("Schema", schema.groups[0]), ("Schema", schema.attribute_groups[0]), ("Schema", schema.elements[0]), ("Schema", schema.attributes[0]), ] self.assertEqual(expected, list(iterator))
def test_end_complex_type(self): complex_type = ComplexType() not_complex_type = Element() element = etree.Element("uno") self.parser.end_complex_type(not_complex_type, element) self.parser.end_complex_type(complex_type, element) self.assertEqual(0, len(complex_type.attribute_groups)) self.assertIsNone(complex_type.open_content) self.parser.default_attributes = "tns:attrs" self.parser.end_complex_type(complex_type, element) expected = AttributeGroup(ref="tns:attrs") self.assertEqual([expected], complex_type.attribute_groups) self.assertIsNone(complex_type.open_content) default_open_content = DefaultOpenContent() self.parser.default_attributes = None self.parser.default_open_content = default_open_content self.parser.end_complex_type(complex_type, element) self.assertIs(default_open_content, complex_type.open_content) open_content = OpenContent() complex_type.open_content = open_content self.parser.end_complex_type(complex_type, element) self.assertIs(open_content, complex_type.open_content)
def test_set_schema_forms_default(self): schema = Schema() schema.elements.append(Element()) schema.elements.append(Element()) schema.attributes.append(Element()) schema.attributes.append(Element()) self.parser.set_schema_forms(schema) self.assertEqual(FormType.UNQUALIFIED, schema.element_form_default) self.assertEqual(FormType.UNQUALIFIED, schema.attribute_form_default) for child_element in schema.elements: self.assertEqual(FormType.QUALIFIED, child_element.form) for child_attribute in schema.attributes: self.assertEqual(FormType.QUALIFIED, child_attribute.form)
def test_property_has_children(self): element = ElementBase() self.assertFalse(element.has_children) element = Element() self.assertFalse(element.has_children) element.complex_type = ComplexType() self.assertTrue(element.has_children)
def test_build_class( self, mock_real_name, mock_display_help, mock_is_nillable, mock_is_abstract, mock_substitutions, mock_build_class_extensions, mock_build_class_attributes, mock_element_namespace, ): mock_real_name.return_value = "name" mock_display_help.return_value = "sos" mock_is_abstract.return_value = True mock_is_nillable.return_value = True mock_substitutions.return_value = ["foo", "sm:bar"] mock_element_namespace.return_value = "foo:name" element = Element() element.ns_map["sm"] = "sm_ns" result = SchemaMapper.build_class(element, "container", "module", "target_ns") mock_build_class_attributes.assert_called_once_with(element, result) mock_build_class_extensions.assert_called_once_with(element, result) mock_element_namespace.assert_called_once_with(element, "target_ns") expected = ClassFactory.create( qname=build_qname("target_ns", "name"), type=Element, help="sos", abstract=True, nillable=True, namespace="foo:name", ns_map=element.ns_map, package=None, module="module", substitutions=[ build_qname("target_ns", "foo"), build_qname("sm_ns", "bar"), ], container="container", ) self.assertEqual(expected, result)
def test_end_Element(self): obj = Element() element = etree.Element("uno") self.parser.end_element(obj, element) self.assertIsNone(obj.form) self.parser.element_form = "qualified" self.parser.end_element(obj, element) self.assertEqual(FormType.QUALIFIED, obj.form)
def test_get_restrictions(self): obj = Element(min_occurs=1, max_occurs=1) expected = {"min_occurs": 1, "max_occurs": 1} self.assertEqual(expected, obj.get_restrictions()) obj.simple_type = SimpleType(restriction=Restriction(length=Length( value=9))) expected.update({"length": 9}) self.assertEqual(expected, obj.get_restrictions()) obj.nillable = False self.assertEqual(expected, obj.get_restrictions()) obj.nillable = True expected.update({"nillable": True}) self.assertEqual(expected, obj.get_restrictions())
def test_end_Element(self): obj = Element() self.parser.end_element(obj) self.assertIsNone(obj.form) self.parser.element_form = "qualified" self.parser.end_element(obj) self.assertEqual(FormType.QUALIFIED, obj.form) obj = Attribute() self.parser.end_element(obj) self.assertIsNone(obj.form)
def test_children(self): one = SimpleType(id="1") two = ComplexType(id="10") three = Alternative(id="11") four = Alternative(id="12") element = Element( name="super", complex_type=two, simple_type=one, alternatives=[three, four], ) children = element.children() self.assertIsInstance(children, Generator) self.assertEqual([one, two, three, four], list(children)) children = list(element.children(lambda x: x.id == "10")) self.assertEqual([two], children) children = list(element.children(lambda x: x.id == "12")) self.assertEqual([four], children) children = list(element.children(lambda x: int(x.id) % 2 == 0)) self.assertEqual([two, four], children)
def test_end_attribute(self): attribute = Attribute() self.parser.end_attribute(attribute) self.assertIsNone(attribute.form) self.parser.attribute_form = "qualified" self.parser.end_attribute(attribute) self.assertEqual(FormType.QUALIFIED, attribute.form) obj = Element() self.parser.end_attribute(obj) self.assertIsNone(obj.form)
def test_element_children_with_parents_restrictions(self): choice = Choice(elements=[Element(name="elem1")]) complex_type = ComplexType( sequence=Sequence(choices=[choice], min_occurs=0, max_occurs=3)) parent_restrictions = Restrictions.from_element(complex_type) children = SchemaMapper.element_children(complex_type, parent_restrictions) child, restrictions = next(children) expected = Restrictions(min_occurs=0, max_occurs=3, sequential=True, choice=str(id(choice))) self.assertEqual(expected, restrictions)
def test_end_schema( self, mock_set_schema_forms, mock_set_schema_namespaces, mock_add_default_imports, mock_resolve_schemas_locations, ): schema = Schema() element = Element("schema") self.parser.end_schema(schema, element) mock_set_schema_forms.assert_called_once_with(schema) mock_set_schema_namespaces.assert_called_once_with(schema, element) mock_add_default_imports.assert_called_once_with(schema) mock_resolve_schemas_locations.assert_called_once_with(schema)
def test_property_bases(self): obj = Element() obj.ns_map["xs"] = Namespace.XS.uri self.assertEqual(["xs:anyType"], list(obj.bases)) obj.type = "foo" self.assertEqual(["foo"], list(obj.bases)) obj.type = None obj.complex_type = ComplexType() self.assertEqual([], list(obj.bases))
def test_end_restriction(self): restriction = Restriction() not_restriction = Element() element = etree.Element("uno") self.parser.end_restriction(not_restriction, element) self.parser.end_restriction(restriction, element) default_open_content = DefaultOpenContent() self.parser.default_open_content = default_open_content self.parser.end_restriction(restriction, element) self.assertIs(default_open_content, restriction.open_content) open_content = OpenContent() restriction.open_content = open_content self.parser.end_restriction(restriction, element) self.assertIs(open_content, restriction.open_content)
def test_end_extension(self): extension = Extension() not_extension = Element() element = etree.Element("uno") self.parser.end_extension(not_extension, element) self.parser.end_extension(extension, element) default_open_content = DefaultOpenContent() self.parser.default_open_content = default_open_content self.parser.end_extension(extension, element) self.assertIs(default_open_content, extension.open_content) open_content = OpenContent() extension.open_content = open_content self.parser.end_extension(extension, element) self.assertIs(open_content, extension.open_content)
def test_build_class_extensions(self, mock_children_extensions): bar_type = AttrTypeFactory.create(qname="bar") foo_type = AttrTypeFactory.create(qname="foo") some_type = AttrTypeFactory.create(qname="{xsdata}something") bar = ExtensionFactory.create(type=bar_type) double = ExtensionFactory.create(type=bar_type) foo = ExtensionFactory.create(type=foo_type) mock_children_extensions.return_value = [bar, double, foo] self_ext = ExtensionFactory.create(type=some_type, restrictions=Restrictions( min_occurs=1, max_occurs=1)) item = ClassFactory.create() element = Element(type="something") SchemaMapper.build_class_extensions(element, item) self.assertEqual(3, len(item.extensions)) self.assertCountEqual([bar, self_ext, foo], item.extensions)
def test_end_extension(self): extension = Extension() not_extension = Element() self.parser.end_extension(not_extension) self.parser.end_extension(extension) default_open_content = DefaultOpenContent() self.parser.default_open_content = default_open_content self.parser.end_extension(extension) self.assertIs(default_open_content, extension.open_content) open_content = OpenContent() extension.open_content = open_content self.parser.end_extension(extension) self.assertIs(open_content, extension.open_content) obj = ComplexType() self.parser.end_extension(obj) self.assertIsNone(obj.open_content)
def test_end_restriction(self): restriction = Restriction() not_restriction = Element() self.parser.end_restriction(not_restriction) self.parser.end_restriction(restriction) default_open_content = DefaultOpenContent() self.parser.default_open_content = default_open_content self.parser.end_restriction(restriction) self.assertIs(default_open_content, restriction.open_content) open_content = OpenContent() restriction.open_content = open_content self.parser.end_restriction(restriction) self.assertIs(open_content, restriction.open_content) obj = ComplexType() self.parser.end_open_content(obj) self.assertIsNone(obj.open_content)
def test_build_class( self, mock_real_name, mock_display_help, mock_is_nillable, mock_is_abstract, mock_substitutions, mock_build_class_extensions, mock_build_class_attributes, mock_element_namespace, ): mock_real_name.return_value = "name" mock_display_help.return_value = "sos" mock_is_abstract.return_value = True mock_is_nillable.return_value = True mock_substitutions.return_value = ["foo", "bar"] mock_element_namespace.return_value = "foo:name" element = Element() result = self.builder.build_class(element, container="foo") mock_build_class_attributes.assert_called_once_with(element, result) mock_build_class_extensions.assert_called_once_with(element, result) mock_element_namespace.assert_called_once_with(element) expected = ClassFactory.create( name="name", type=Element, help="sos", abstract=True, nillable=True, namespace="foo:name", ns_map=element.ns_map, package=None, module=self.schema.module, source_namespace=self.schema.target_namespace, substitutions=["foo", "bar"], container="foo", ) self.assertEqual(expected, result)
def test_build_class_extensions(self, mock_children_extensions): bar_type = AttrTypeFactory.create(name="bar", index=3) foo_type = AttrTypeFactory.create(name="foo", index=1) some_type = AttrTypeFactory.create(name="something", index=0) bar = ExtensionFactory.create(type=bar_type) double = ExtensionFactory.create(type=bar_type) foo = ExtensionFactory.create(type=foo_type) mock_children_extensions.return_value = [bar, double, foo] self_ext = ExtensionFactory.create( type=some_type, restrictions=Restrictions(min_occurs=1, max_occurs=1) ) item = ClassFactory.create() element = Element(type="something") self.builder.build_class_extensions(element, item) self.assertEqual(3, len(item.extensions)) self.assertEqual(self_ext, item.extensions[0]) self.assertIs(foo, item.extensions[1]) self.assertIs(double, item.extensions[2])
def test_build(self, mock_build_class): schema = self.schema override = Override() redefine = Redefine() redefine.annotation = Annotation() redefine.complex_types.append(ComplexType()) override.annotation = Annotation() override.groups.append(Group()) override.simple_types.append(SimpleType()) schema.simple_types.append(SimpleType()) schema.attribute_groups.append(AttributeGroup()) schema.groups.append(Group()) schema.attributes.append(Attribute()) schema.complex_types.append(ComplexType()) schema.elements.append(Element()) schema.redefines.append(redefine) schema.overrides.append(override) self.builder.build() mock_build_class.assert_has_calls( [ mock.call(override.simple_types[0], container=override.class_name), mock.call(override.groups[0], container=override.class_name), mock.call(redefine.complex_types[0], container=redefine.class_name), mock.call(schema.simple_types[0], container=schema.class_name), mock.call(schema.complex_types[0], container=schema.class_name), mock.call(schema.groups[0], container=schema.class_name), mock.call(schema.attribute_groups[0], container=schema.class_name), mock.call(schema.elements[0], container=schema.class_name), mock.call(schema.attributes[0], container=schema.class_name), ] )
def test_property_real_type(self): obj = Element() self.assertEqual("", obj.real_type) # Inner classes depend on the this to be None obj.complex_type = ComplexType() self.assertEqual("", obj.real_type) restriction = Restriction(base="xs:int") obj.simple_type = SimpleType(restriction=restriction) self.assertEqual(restriction.base, obj.real_type) obj.ref = "foo" self.assertEqual(obj.ref, obj.real_type) obj.type = "bar" self.assertEqual(obj.type, obj.real_type) obj.alternatives.append(Alternative(type="foo")) obj.alternatives.append(Alternative(type="bar")) obj.alternatives.append(Alternative(type="thug")) self.assertEqual("bar foo thug", obj.real_type)