def test_element_children(self): sequence_one = Sequence.create(elements=[Element.create(), Element.create()]) sequence_two = Sequence.create( max_occurs=2, elements=[Element.create(), Element.create()] ) restriction = Restriction.create( enumerations=[Enumeration.create(value=x) for x in "abc"], sequence=sequence_two, ) complex_type = ComplexType.create( attributes=[Attribute.create(), Attribute.create()], sequence=sequence_one, simple_content=SimpleContent.create(restriction=Restriction.create()), complex_content=ComplexContent.create(restriction=restriction,), ) children = self.builder.element_children(complex_type) 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_real_name(self): obj = Attribute.create(ref="bar") self.assertEqual("bar", obj.real_name) obj.name = "foo" self.assertEqual("foo", obj.real_name) with self.assertRaises(SchemaValueError): Attribute.create().real_name
def test_build( self, mock_build_class, mock_override_children, mock_redefine_children ): for _ in range(2): self.schema.simple_types.append(SimpleType.create()) self.schema.attribute_groups.append(AttributeGroup.create()) self.schema.groups.append(Group.create()) self.schema.attributes.append(Attribute.create()) self.schema.complex_types.append(ComplexType.create()) self.schema.elements.append(Element.create()) self.schema.redefines.append(Redefine.create()) self.schema.overrides.append(Override.create()) override_element = Element.create() override_attribute = Attribute.create() override_complex_type = ComplexType.create() redefine_simple_type = SimpleType.create() redefine_group = Group.create() redefine_attribute_group = AttributeGroup.create() mock_redefine_children.side_effect = [ [redefine_simple_type, redefine_group], [redefine_attribute_group], ] mock_override_children.side_effect = [ [override_element, override_attribute], [override_complex_type], ] mock_build_class.side_effect = classes = ClassFactory.list(18) self.assertEqual(classes, self.builder.build()) mock_build_class.assert_has_calls( [ mock.call(override_element), mock.call(override_attribute), mock.call(override_complex_type), mock.call(redefine_simple_type), mock.call(redefine_group), mock.call(redefine_attribute_group), mock.call(self.schema.simple_types[0]), mock.call(self.schema.simple_types[1]), mock.call(self.schema.attribute_groups[0]), mock.call(self.schema.attribute_groups[1]), mock.call(self.schema.groups[0]), mock.call(self.schema.groups[1]), mock.call(self.schema.attributes[0]), mock.call(self.schema.attributes[1]), mock.call(self.schema.complex_types[0]), mock.call(self.schema.complex_types[1]), mock.call(self.schema.elements[0]), mock.call(self.schema.elements[1]), ] )
def test_children_extensions(self): complex_type = ComplexType.create( attributes=[Attribute.create(index=i) for i in range(2)], simple_content=SimpleContent.create( restriction=Restriction.create(base="bk:b", index=4) ), complex_content=ComplexContent.create( extension=Extension.create(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_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_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_has_anonymous_class(self): obj = Element.create() self.assertFalse(self.builder.has_anonymous_class(obj)) obj = Element.create(type="foo") self.assertFalse(self.builder.has_anonymous_class(obj)) obj = Element.create(complex_type=ComplexType.create()) self.assertTrue(self.builder.has_anonymous_class(obj)) obj = Attribute.create() self.assertFalse(self.builder.has_anonymous_class(obj))
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_get_restrictions(self): obj = Attribute.create() 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.create(restriction=Restriction.create( length=Length.create(value=1))) expected.update(dict(length=1)) self.assertEqual(expected, obj.get_restrictions())
def test_property_real_type(self): obj = Attribute.create() self.assertIsNone(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.create() self.assertIsNone(obj.real_type) obj.simple_type.restriction = Restriction.create(base="thug") self.assertEqual(obj.simple_type.restriction.base, obj.real_type)
def test_build_class_attribute( self, mock_real_name, mock_display_help, mock_prefix, mock_default_value, mock_is_fixed, mock_is_wildcard, 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, name="xs: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_is_wildcard.return_value = True mock_element_namespace.return_value = "http://something/common" mock_get_restrictions.return_value = {"required": True} attribute = Attribute.create(default="false", index=66, ns_map={"foo": "bar"}) self.builder.build_class_attribute(item, attribute, Restrictions()) expected = AttrFactory.create( name=mock_real_name.return_value, types=mock_build_class_attribute_types.return_value, local_type=Attribute.__name__, namespace=mock_element_namespace.return_value, help=mock_display_help.return_value, default=mock_default_value.return_value, fixed=mock_is_fixed.return_value, wildcard=mock_is_wildcard.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)
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_element_namespace(self): self.schema.target_namespace = "foobar" element = Element.create(ref="foo:something") element.ns_map["foo"] = "bar" self.assertEqual("bar", self.builder.element_namespace(element)) element = Element.create(form=FormType.QUALIFIED) self.assertEqual("foobar", self.builder.element_namespace(element)) element = Element.create() self.assertEqual("", self.builder.element_namespace(element)) element.target_namespace = "tns" self.assertEqual("tns", self.builder.element_namespace(element)) attribute = Attribute.create() self.assertIsNone(self.builder.element_namespace(attribute)) attribute.target_namespace = "tns" self.assertEqual("tns", self.builder.element_namespace(attribute))
def test_property_extends(self): obj = Attribute.create() self.assertIsNone(obj.extends)
def test_property_is_attribute(self): obj = Attribute.create() self.assertTrue(obj)