Exemple #1
0
    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))
Exemple #2
0
    def test_property_real_name(self):
        obj = Element.create(ref="bar")
        self.assertEqual("bar", obj.real_name)

        obj.name = "foo"
        self.assertEqual("foo", obj.real_name)

        with self.assertRaises(SchemaValueError):
            obj = Element.create()
            obj.real_name
Exemple #3
0
    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]),
            ]
        )
Exemple #4
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))
Exemple #5
0
    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.create(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)
Exemple #6
0
    def test_build_inner_classes(self, mock_build_class):
        inner_classes = ClassFactory.list(2)
        mock_build_class.side_effect = inner_classes

        simple_type = SimpleType.create()
        complex_type = ComplexType.create()
        enumeration = SimpleType.create(
            restriction=Restriction.create(enumerations=[Enumeration.create(value="a")])
        )

        element = Element.create(
            alternatives=[
                Alternative.create(complex_type=complex_type, id="a"),
                Alternative.create(simple_type=simple_type, id="b"),
                Alternative.create(simple_type=enumeration, id="c"),
            ]
        )
        result = self.builder.build_inner_classes(element)
        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), mock.call(enumeration)]
        )
Exemple #7
0
    def test_set_schema_forms_default(self):
        schema = Schema()
        schema.elements.append(Element.create())
        schema.elements.append(Element.create())
        schema.attributes.append(Element.create())
        schema.attributes.append(Element.create())

        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)
Exemple #8
0
    def test_property_has_children(self):
        element = ElementBase()
        self.assertFalse(element.has_children)

        element = Element.create()
        self.assertFalse(element.has_children)

        element.complex_type = ComplexType.create()
        self.assertTrue(element.has_children)
Exemple #9
0
    def test_property_is_mixed(self):
        obj = Element.create()
        self.assertFalse(obj.is_mixed)

        obj.complex_type = ComplexType.create()
        self.assertFalse(obj.is_mixed)

        obj.complex_type.mixed = True
        self.assertTrue(obj.is_mixed)
Exemple #10
0
    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)
Exemple #11
0
    def test_property_raw_type(self):
        obj = Element.create()
        self.assertEqual("xs:anyType", obj.raw_type)

        obj.type = "foo"
        self.assertEqual("foo", obj.raw_type)

        obj.type = None
        obj.complex_type = ComplexType.create()
        self.assertIsNone(obj.raw_type)
Exemple #12
0
    def test_build_inner_class_when_has_anonymous_class(
            self, mock_has_anonymous_class, mock_build_class):
        inner_class = ClassFactory.create()
        mock_build_class.return_value = inner_class
        mock_has_anonymous_class.return_value = True

        complex_type = ComplexType.create()
        element = Element.create(name="foo", complex_type=complex_type)

        self.assertEqual(inner_class, self.builder.build_inner_class(element))
        self.assertIsNone(element.complex_type)
        self.assertEqual("foo", complex_type.name)
Exemple #13
0
    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))
Exemple #14
0
    def test_end_schema(
        self,
        mock_set_schema_forms,
        mock_set_schema_namespaces,
        mock_add_default_imports,
        mock_resolve_schemas_locations,
    ):
        schema = Schema.create()
        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)
Exemple #15
0
    def test_build_inner_class_when_has_anonymous_enumeration(
            self, mock_has_anonymous_class, mock_has_anonymous_enumeration,
            mock_build_class):
        inner_class = ClassFactory.create()
        mock_build_class.return_value = inner_class
        mock_has_anonymous_class.return_value = False
        mock_has_anonymous_enumeration.return_value = True

        simple_type = SimpleType.create()
        element = Element.create(name="foo",
                                 simple_type=simple_type,
                                 type="xs:int")

        self.assertEqual(inner_class, self.builder.build_inner_class(element))
        self.assertIsNone(element.simple_type)
        self.assertIsNone(element.type)
        self.assertEqual("foo", simple_type.name)
Exemple #16
0
    def test_get_restrictions(self):
        obj = Element.create(min_occurs=1, max_occurs=1)
        expected = {"min_occurs": 1, "max_occurs": 1}
        self.assertEqual(expected, obj.get_restrictions())

        obj.simple_type = SimpleType.create(
            restriction=Restriction.create(length=Length.create(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())
Exemple #17
0
    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)
Exemple #18
0
    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)
Exemple #19
0
    def test_property_real_type(self):
        obj = Element.create()
        self.assertIsNone(obj.real_type)

        # Inner classes depend on the this to be None
        obj.complex_type = ComplexType.create()
        self.assertIsNone(obj.real_type)

        restriction = Restriction.create(base="xs:int")
        obj.simple_type = SimpleType.create(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.create(type="foo"))
        obj.alternatives.append(Alternative.create(type="bar"))
        obj.alternatives.append(Alternative.create(type="thug"))
        self.assertEqual("bar foo thug", obj.real_type)
Exemple #20
0
    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.create(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])
Exemple #21
0
    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.create()
        result = self.builder.build_class(element)

        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=self.builder.package,
            module=self.schema.module,
            source_namespace=self.schema.target_namespace,
            substitutions=["foo", "bar"],
        )
        self.assertEqual(expected, result)
Exemple #22
0
    def test_property_substitutions(self):
        obj = Element.create()
        self.assertEqual([], obj.substitutions)

        obj.substitution_group = "foo   bar xs:any"
        self.assertEqual(["foo", "bar", "xs:any"], obj.substitutions)
Exemple #23
0
 def test_property_is_attribute(self):
     obj = Element.create()
     self.assertTrue(obj)
Exemple #24
0
 def test_property_default_type(self):
     obj = Element.create()
     self.assertEqual(DataType.ANY_TYPE, obj.default_type)
Exemple #25
0
 def test_property_extends(self):
     obj = Element.create()
     self.assertIsNone(obj.extends)