示例#1
0
    def test_set_namespace_map(self):
        schema = Schema()
        ns_map = {}

        self.parser.set_namespace_map(schema, ns_map)

        expected = {
            "xlink": "http://www.w3.org/1999/xlink",
            "xml": "http://www.w3.org/XML/1998/namespace",
            "xs": "http://www.w3.org/2001/XMLSchema",
            "xsi": "http://www.w3.org/2001/XMLSchema-instance",
        }
        self.assertEqual(expected, schema.ns_map)

        self.parser.set_namespace_map(schema, None)
        self.assertEqual(expected, schema.ns_map)

        ns_map = {"foo": "bar", "not": "http://www.w3.org/2001/XMLSchema"}
        schema = Schema()
        expected = {
            "foo": "bar",
            "xlink": "http://www.w3.org/1999/xlink",
            "xml": "http://www.w3.org/XML/1998/namespace",
            "not": "http://www.w3.org/2001/XMLSchema",
            "xsi": "http://www.w3.org/2001/XMLSchema-instance",
        }

        self.parser.set_namespace_map(schema, ns_map)
        self.assertEqual(expected, schema.ns_map)

        foo = []
        self.parser.set_namespace_map(foo, ns_map)
        self.assertFalse(hasattr(foo, "ns_map"))
示例#2
0
    def test_set_namespace_map(self):
        schema = Schema()
        element = etree.Element("schema")

        self.parser.set_namespace_map(element, schema)

        expected = {
            "xlink": "http://www.w3.org/1999/xlink",
            "xml": "http://www.w3.org/XML/1998/namespace",
            "xs": "http://www.w3.org/2001/XMLSchema",
            "xsi": "http://www.w3.org/2001/XMLSchema-instance",
        }
        self.assertEqual(expected, schema.ns_map)

        element = etree.Element("schema",
                                nsmap={
                                    "foo": "bar",
                                    "not": "http://www.w3.org/2001/XMLSchema"
                                })
        schema = Schema()
        expected = {
            "foo": "bar",
            "xlink": "http://www.w3.org/1999/xlink",
            "xml": "http://www.w3.org/XML/1998/namespace",
            "not": "http://www.w3.org/2001/XMLSchema",
            "xsi": "http://www.w3.org/2001/XMLSchema-instance",
        }

        self.parser.set_namespace_map(element, schema)
        self.assertEqual(expected, schema.ns_map)
示例#3
0
    def test_add_default_imports(self):
        schema = Schema()
        schema.imports.append(Import(namespace="foo"))

        self.parser.add_default_imports(schema)
        self.assertEqual(1, len(schema.imports))

        xsi = Namespace.XSI.uri
        schema.ns_map["foo"] = xsi
        self.parser.add_default_imports(schema)
        self.assertEqual(2, len(schema.imports))
        self.assertEqual(Import(namespace=xsi), schema.imports[0])
示例#4
0
    def test_set_schema_namespaces(self):
        schema = Schema()

        self.parser.set_schema_namespaces(schema)
        self.assertIsNone(schema.target_namespace)

        self.parser.target_namespace = "bar"
        self.parser.set_schema_namespaces(schema)
        self.assertEqual("bar", schema.target_namespace)

        schema.target_namespace = "foo"
        self.parser.set_schema_namespaces(schema)
        self.assertEqual("foo", schema.target_namespace)
示例#5
0
    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)
示例#6
0
    def test_merge(self):
        target = Definitions()
        source = Definitions()
        source.types = Types()
        source.messages.append(Message())
        source.port_types.append(PortType())
        source.bindings.append(Binding())
        source.services.append(Service())
        source.extended.append(AnyElement())

        source_two = copy.deepcopy(source)
        source_two.types.schemas.append(Schema())

        target.merge(source)
        self.assertEqual(source.types, target.types)
        self.assertEqual(0, len(target.types.schemas))
        self.assertEqual(1, len(target.messages))
        self.assertEqual(1, len(target.port_types))
        self.assertEqual(1, len(target.bindings))
        self.assertEqual(1, len(target.services))
        self.assertEqual(1, len(target.extended))

        target.merge(source_two)
        target.merge(Definitions())
        self.assertEqual(1, len(target.types.schemas))
        self.assertEqual(2, len(target.messages))
        self.assertEqual(2, len(target.port_types))
        self.assertEqual(2, len(target.bindings))
        self.assertEqual(2, len(target.services))
        self.assertEqual(2, len(target.extended))
示例#7
0
    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))
示例#8
0
    def test_module(self):
        schema = Schema(location="foo/bar.xsd",
                        target_namespace="http://xsdata/foo")

        self.assertEqual("bar", schema.module)
        schema.location = "foo/bar.noext"
        self.assertEqual("bar.noext", schema.module)

        schema.location = None
        self.assertEqual("foo", schema.module)

        schema.target_namespace = None
        with self.assertRaises(SchemaValueError) as cm:
            schema.module

        self.assertEqual("Unknown schema module.", str(cm.exception))
示例#9
0
    def set_schema_forms(self, obj: xsd.Schema):
        """
        Set the default form type for elements and attributes.

        Global elements and attributes are by default qualified.
        """
        if self.element_form:
            obj.element_form_default = FormType(self.element_form)
        if self.attribute_form:
            obj.attribute_form_default = FormType(self.attribute_form)

        for child_element in obj.elements:
            child_element.form = FormType.QUALIFIED

        for child_attribute in obj.attributes:
            child_attribute.form = FormType.QUALIFIED
示例#10
0
    def convert_schema(self, schema: Schema):
        """Convert a schema instance to codegen classes and process imports to
        other schemas."""
        for sub in schema.included():
            if sub.location:
                self.process_schema(sub.location, schema.target_namespace)

        assert schema.location is not None

        self.class_map[schema.location] = self.generate_classes(schema)
示例#11
0
    def test_set_schema_namespaces(self, mock_set_namespace_map):
        schema = Schema()
        element = etree.Element("schema")

        self.parser.set_schema_namespaces(schema, element)
        self.assertIsNone(schema.target_namespace)

        self.parser.target_namespace = "bar"
        self.parser.set_schema_namespaces(schema, element)
        self.assertEqual("bar", schema.target_namespace)

        schema.target_namespace = "foo"
        self.parser.set_schema_namespaces(schema, element)
        self.assertEqual("foo", schema.target_namespace)

        mock_set_namespace_map.assert_has_calls([
            mock.call(element, schema),
            mock.call(element, schema),
            mock.call(element, schema),
        ])
示例#12
0
    def test_process_definitions(
        self,
        mock_parse_definitions,
        mock_convert_definitions,
        mock_convert_schema,
        mock_process_classes,
    ):

        uri = "http://xsdata/services.xsd"
        definitions = Definitions(types=Types(schemas=[Schema(), Schema()]))

        mock_parse_definitions.return_value = definitions

        self.transformer.process_definitions(uri)

        mock_convert_schema.assert_has_calls(
            [mock.call(x) for x in definitions.schemas]
        )
        mock_parse_definitions.assert_called_once_with(uri, namespace=None)
        mock_convert_definitions.assert_called_once_with(definitions)
        mock_process_classes.assert_called_once_with()
示例#13
0
    def test_sub_schemas(self):
        imports = [
            Import(schema_location="../foo.xsd"),
            Import(schema_location="../bar.xsd"),
        ]
        includes = [
            Include(schema_location="common.xsd"),
            Include(schema_location="uncommon.xsd"),
        ]
        redefines = [
            Redefine(schema_location="a.xsd"),
            Redefine(schema_location="b.xsd"),
        ]
        overrides = [
            Override(schema_location="a.xsd"),
            Override(schema_location="b.xsd"),
        ]
        schema = Schema(imports=imports,
                        includes=includes,
                        redefines=redefines,
                        overrides=overrides)

        actual = schema.included()
        expected = imports + includes + redefines + overrides
        self.assertIsInstance(actual, Iterator)
        self.assertEqual(expected, list(actual))

        schema = Schema()
        self.assertEqual([], list(schema.included()))
示例#14
0
    def test_resolve_schemas_locations(
        self, mock_resolve_path, mock_resolve_local_path
    ):
        schema = Schema()
        self.parser.resolve_schemas_locations(schema)

        self.parser.location = Path.cwd()

        mock_resolve_path.side_effect = lambda x: Path.cwd().joinpath(x)
        mock_resolve_local_path.side_effect = lambda x, y: Path.cwd().joinpath(x)

        schema.overrides.append(Override(schema_location="o1"))
        schema.overrides.append(Override(schema_location="o2"))
        schema.redefines.append(Redefine(schema_location="r1"))
        schema.redefines.append(Redefine(schema_location="r2"))
        schema.includes.append(Include(schema_location="i1"))
        schema.includes.append(Include(schema_location="i2"))
        schema.imports.append(Import(schema_location="i3", namespace="ns_i3"))
        schema.imports.append(Import(schema_location="i4", namespace="ns_i4"))

        self.parser.resolve_schemas_locations(schema)

        mock_resolve_path.assert_has_calls(
            [
                mock.call("o1"),
                mock.call("o2"),
                mock.call("r1"),
                mock.call("r2"),
                mock.call("i1"),
                mock.call("i2"),
            ]
        )

        mock_resolve_local_path.assert_has_calls(
            [mock.call("i3", "ns_i3"), mock.call("i4", "ns_i4")]
        )

        for sub in schema.included():
            self.assertEqual(Path.cwd().joinpath(sub.location), sub.location)
示例#15
0
    def test_property_schemas(self):
        obj = Definitions()

        self.assertIsInstance(obj.schemas, Generator)
        self.assertEqual([], list(obj.schemas))

        obj.types = Types()
        self.assertEqual([], list(obj.schemas))

        schemas = [Schema(), Schema]
        obj.types.schemas.extend(schemas)

        self.assertEqual(schemas, list(obj.schemas))
示例#16
0
    def test_process_schema(
        self,
        mock_parse_schema,
        mock_convert_schema,
    ):
        schema = Schema()
        mock_parse_schema.return_value = schema
        uri = "http://xsdata/services.xsd"
        namespace = "fooNS"

        self.transformer.process_schema(uri, namespace)

        mock_convert_schema.assert_called_once_with(schema)
示例#17
0
    def test_process_schema(
        self,
        mock_parse_schema,
        mock_generate_classes,
        mock_logger_info,
    ):
        schema = Schema(target_namespace="thug")
        schema.includes.append(Include(location="foo"))
        schema.overrides.append(Override())
        schema.imports.append(Import(location="bar"))
        schema.imports.append(Import(location="fails"))
        schema_foo = Schema()
        schema_bar = Schema()

        mock_generate_classes.side_effect = [
            ClassFactory.list(1),
            ClassFactory.list(2),
            ClassFactory.list(3),
        ]

        mock_parse_schema.side_effect = [schema, schema_bar, None, schema_foo]

        self.transformer.process_schema("main", "foo-bar")

        self.assertEqual(["main", "bar", "fails", "foo"],
                         self.transformer.processed)

        self.assertEqual(3, len(self.transformer.class_map))
        self.assertEqual(3, len(self.transformer.class_map["main"]))
        self.assertEqual(2, len(self.transformer.class_map["foo"]))
        self.assertEqual(1, len(self.transformer.class_map["bar"]))

        mock_logger_info.assert_has_calls([
            mock.call("Parsing schema %s", "main"),
            mock.call("Parsing schema %s", "bar"),
            mock.call("Parsing schema %s", "fails"),
            mock.call("Parsing schema %s", "foo"),
        ])
示例#18
0
    def test_generate_classes(self, mock_mapper_map, mock_count_classes,
                              mock_logger_info):
        schema = Schema(location="edo.xsd")
        classes = ClassFactory.list(2)

        mock_mapper_map.return_value = classes
        mock_count_classes.return_value = 2, 4
        self.transformer.generate_classes(schema)

        mock_mapper_map.assert_called_once_with(schema)
        mock_logger_info.assert_has_calls([
            mock.call("Compiling schema %s", schema.location),
            mock.call("Builder: %d main and %d inner classes", 2, 4),
        ])
示例#19
0
    def test_process_definitions(
        self,
        mock_parse_definitions,
        mock_convert_definitions,
        mock_convert_schema,
    ):
        uris = [
            "http://xsdata/services.wsdl",
            "http://xsdata/abstractServices.wsdl",
            "http://xsdata/notfound.wsdl",
        ]
        fist_def = Definitions(types=Types(schemas=[Schema(), Schema()]))
        second_def = Definitions(bindings=[Binding()])
        mock_parse_definitions.side_effect = [fist_def, second_def, None]
        self.transformer.process_definitions(uris)

        mock_convert_schema.assert_has_calls(
            [mock.call(x) for x in fist_def.schemas])
        mock_parse_definitions.assert_has_calls([
            mock.call(uris[0], namespace=None),
            mock.call(uris[1], namespace=None)
        ])
        mock_convert_definitions.assert_called_once_with(fist_def)
示例#20
0
    def root_elements(cls, schema: Schema):
        """Return all valid schema elements that can be converted to
        classes."""

        for override in schema.overrides:
            for child in override.children(condition=cls.is_class):
                yield Tag.OVERRIDE, child

        for redefine in schema.redefines:
            for child in redefine.children(condition=cls.is_class):
                yield Tag.REDEFINE, child

        for child in schema.children(condition=cls.is_class):
            yield Tag.SCHEMA, child
示例#21
0
    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)
示例#22
0
    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)
示例#23
0
    def test_map(self, mock_root_elements, mock_build_class):
        simple_type = ComplexType()
        complex_type = ComplexType()
        schema = Schema(target_namespace="fooNS", location="foo.xsd")

        mock_build_class.side_effect = ClassFactory.list(3)
        mock_root_elements.return_value = [
            (Tag.SCHEMA, Group),
            (Tag.OVERRIDE, simple_type),
            (Tag.REDEFINE, complex_type),
        ]

        actual = SchemaMapper.map(schema)
        self.assertEqual(3, len(actual))
        self.assertIsInstance(actual[0], Class)

        mock_root_elements.assert_called_once_with(schema)
示例#24
0
    def test_wget_included(self, mock_wget):
        schema = Schema(
            imports=[
                Import(location="foo.xsd"),
                Import(),
                Import(location="foo.xsd", schema_location="../foo.xsd"),
            ]
        )

        self.downloader.wget_included(schema)
        mock_wget.assert_has_calls(
            [
                mock.call(schema.imports[0].location, None),
                mock.call(
                    schema.imports[2].location, schema.imports[2].schema_location
                ),
            ]
        )
示例#25
0
    def test_convert_schema(self, mock_process_schema, mock_generate_classes):
        schema = Schema(target_namespace="thug", location="main")
        schema.includes.append(Include(location="foo"))
        schema.overrides.append(Override())
        schema.imports.append(Import(location="bar"))
        schema.imports.append(Import(location="fails"))

        mock_generate_classes.return_value = ClassFactory.list(2)

        self.transformer.convert_schema(schema)

        self.assertEqual(1, len(self.transformer.class_map))
        self.assertEqual(2, len(self.transformer.class_map["main"]))
        mock_process_schema.assert_has_calls([
            mock.call("bar", "thug"),
            mock.call("fails", "thug"),
            mock.call("foo", "thug"),
        ])
示例#26
0
    def resolve_schemas_locations(self, obj: xsd.Schema):
        """Resolve the locations of the schema overrides, redefines, includes
        and imports relatively to the schema location."""
        if not self.location:
            return

        obj.location = self.location
        for over in obj.overrides:
            over.location = self.resolve_path(over.schema_location)

        for red in obj.redefines:
            red.location = self.resolve_path(red.schema_location)

        for inc in obj.includes:
            inc.location = self.resolve_path(inc.schema_location)

        for imp in obj.imports:
            imp.location = self.resolve_local_path(imp.schema_location, imp.namespace)
示例#27
0
    def test_process_schema(
        self,
        mock_parse_schema,
        mock_convert_schema,
        mock_logger_info,
        mock_logger_debug,
    ):
        schema = Schema()

        mock_parse_schema.return_value = schema
        uri = "http://xsdata/services.xsd"
        namespace = "fooNS"

        self.transformer.process_schema(uri, namespace)
        self.transformer.process_schema(uri, namespace)

        self.assertEqual([uri], self.transformer.processed)

        mock_convert_schema.assert_called_once_with(schema)
        mock_logger_info.assert_called_once_with("Parsing schema %s", "services.xsd")
        mock_logger_debug.assert_called_once_with(
            "Skipping already processed: %s", "services.xsd"
        )
示例#28
0
 def set_schema_namespaces(self, obj: xsd.Schema):
     """Set the given schema's target namespace and add the default
     namespaces if the are missing xsi, xlink, xml, xs."""
     obj.target_namespace = obj.target_namespace or self.target_namespace
示例#29
0
 def test_meta(self):
     schema = Schema()
     self.assertEqual("schema", schema.Meta.name)
     self.assertEqual(Namespace.XS.uri, schema.Meta.namespace)
示例#30
0
 def setUp(self):
     super().setUp()
     self.schema = Schema(location="file://foo.xsd")
     self.builder = ClassBuilder(schema=self.schema)