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_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_property_real_name(self): obj = Attribute(ref="bar") self.assertEqual("bar", obj.real_name) obj.name = "foo" self.assertEqual("foo", obj.real_name) with self.assertRaises(SchemaValueError): Attribute().real_name
def test_property_real_type(self): obj = Attribute() self.assertEqual("", obj.real_type) obj.ref = "foo" self.assertEqual(obj.ref, obj.real_type) obj.type = "bar" self.assertEqual(obj.type, obj.real_type) obj.simple_type = SimpleType() self.assertEqual("", obj.real_type) obj.simple_type.restriction = Restriction(base="thug") self.assertEqual(obj.simple_type.restriction.base, obj.real_type)
def test_property_attr_types(self): obj = Attribute() self.assertEqual([], list(obj.attr_types)) obj.ref = "foo" self.assertEqual([obj.ref], list(obj.attr_types)) obj.type = "bar" self.assertEqual([obj.type], list(obj.attr_types)) obj.simple_type = SimpleType() self.assertEqual([], list(obj.attr_types)) obj.simple_type.restriction = Restriction(base="thug") self.assertEqual([obj.simple_type.restriction.base], list(obj.attr_types))
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_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_end_attribute(self): attribute = Attribute() element = etree.Element("uno") self.parser.end_attribute(attribute, element) self.assertIsNone(attribute.form) self.parser.attribute_form = "qualified" self.parser.end_attribute(attribute, element) self.assertEqual(FormType.QUALIFIED, attribute.form)
def test_get_restrictions(self): obj = Attribute() self.assertEqual({}, obj.get_restrictions()) obj.use = UseType.REQUIRED expected = {"required": True} self.assertEqual(expected, obj.get_restrictions()) obj.use = UseType.PROHIBITED expected = {"prohibited": True} self.assertEqual(expected, obj.get_restrictions()) obj.simple_type = SimpleType(restriction=Restriction(length=Length( value=1))) expected["length"] = 1 self.assertEqual(expected, obj.get_restrictions())
def test_build_class_attribute_types(self, mock_build_inner_classes, mock_real_type): mock_real_type.return_value = " xs:integer xs:string " mock_build_inner_classes.return_value = [] item = ClassFactory.create() attribute = Attribute(default="false") actual = SchemaMapper.build_class_attribute_types(item, attribute) expected = [AttrTypeFactory.xs_int(), AttrTypeFactory.xs_string()] self.assertEqual(expected, actual)
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_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_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_get_restrictions(self): obj = Attribute() self.assertEqual({}, obj.get_restrictions()) obj.use = UseType.REQUIRED expected = {"max_occurs": 1, "min_occurs": 1, "required": True} self.assertEqual(expected, obj.get_restrictions()) obj.simple_type = SimpleType(restriction=Restriction(length=Length(value=1))) expected.update(dict(length=1)) self.assertEqual(expected, obj.get_restrictions())
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 = None mock_build_inner_classes.return_value = [] mock_default_type.return_value = DataType.STRING item = ClassFactory.create() attribute = Attribute(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(self, mock_build_inner_classes, mock_attr_types): mock_attr_types.return_value = ["xs:integer", "xs:string"] mock_build_inner_classes.return_value = [] item = ClassFactory.create() attribute = Attribute(default="false") actual = SchemaMapper.build_class_attribute_types(item, attribute) expected = [ AttrTypeFactory.native(DataType.INTEGER), AttrTypeFactory.native(DataType.STRING), ] self.assertEqual(expected, actual)
def test_build_class_attribute_types_when_obj_has_inner_class( self, mock_build_inner_classes, mock_attr_types): inner_class = ClassFactory.create(qname="foo") mock_attr_types.return_value = ["xs:integer", "xs:string"] mock_build_inner_classes.return_value = [inner_class] item = ClassFactory.create() attribute = Attribute(default="false") actual = SchemaMapper.build_class_attribute_types(item, attribute) expected = [ AttrTypeFactory.native(DataType.INTEGER), AttrTypeFactory.native(DataType.STRING), AttrTypeFactory.create(qname=inner_class.qname, forward=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_children_extensions(self): complex_type = ComplexType( attributes=[Attribute(index=i) for i in range(2)], simple_content=SimpleContent(restriction=Restriction(base="bk:b", index=4)), complex_content=ComplexContent(extension=Extension(base="bk:ext", index=7)), ) item = ClassFactory.create() children = self.builder.children_extensions(complex_type, item) expected = list( map( ExtensionFactory.create, [ AttrTypeFactory.create(name="bk:b", index=4), AttrTypeFactory.create(name="bk:ext", index=7), ], ) ) self.assertIsInstance(children, GeneratorType) self.assertEqual(expected, list(children))
def test_children_extensions(self): complex_type = ComplexType( attributes=[Attribute() for _ in range(2)], simple_content=SimpleContent(restriction=Restriction(base="bk:b")), complex_content=ComplexContent(extension=Extension(base="bk:c")), ) complex_type.simple_content.restriction.index = 4 complex_type.complex_content.extension.index = 7 item = ClassFactory.create(ns_map={"bk": "book"}) children = SchemaMapper.children_extensions(complex_type, item) expected = list( map( ExtensionFactory.create, [ AttrTypeFactory.create(qname=build_qname("book", "b")), AttrTypeFactory.create(qname=build_qname("book", "c")), ], )) self.assertIsInstance(children, GeneratorType) self.assertEqual(expected, list(children))
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_bases(self): obj = Attribute() self.assertEqual([], list(obj.bases)) obj.type = "foo" self.assertEqual(["foo"], list(obj.bases))
def test_property_extensions(self): obj = Attribute() self.assertEqual([], list(obj.extensions))
def test_property_is_attribute(self): obj = Attribute() self.assertTrue(obj)