Beispiel #1
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)
Beispiel #2
0
    def test_sub_schemas(self):
        imports = [
            Import.create(schema_location="../foo.xsd"),
            Import.create(schema_location="../bar.xsd"),
        ]
        includes = [
            Include.create(schema_location="common.xsd"),
            Include.create(schema_location="uncommon.xsd"),
        ]
        redefines = [
            Redefine.create(schema_location="a.xsd"),
            Redefine.create(schema_location="b.xsd"),
        ]
        overrides = [
            Override.create(schema_location="a.xsd"),
            Override.create(schema_location="b.xsd"),
        ]
        schema = Schema.create(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.create()
        self.assertEqual([], list(schema.included()))
Beispiel #3
0
    def test_process_schema(
        self,
        mock_parse_schema,
        mock_process_included,
        mock_generate_classes,
        mock_logger_info,
    ):
        include = Include.create()
        override = Override.create()
        schema = Schema.create(target_namespace="thug")
        schema.includes.append(include)
        schema.overrides.append(override)
        mock_process_included.side_effect = [
            ClassFactory.list(2), ClassFactory.list(3)
        ]
        mock_generate_classes.return_value = ClassFactory.list(4)

        mock_parse_schema.return_value = schema

        path = Path(__file__)
        result = self.transformer.process_schema(path,
                                                 package="foo.bar",
                                                 target_namespace="foo-bar")

        self.assertEqual(9, len(result))
        self.assertTrue(path in self.transformer.processed)

        mock_parse_schema.assert_called_once_with(path, "foo-bar")
        mock_process_included.assert_has_calls([
            mock.call(include, "foo.bar", schema.target_namespace),
            mock.call(override, "foo.bar", schema.target_namespace),
        ])

        self.transformer.process_schema(path, None, None)
        mock_logger_info.assert_called_once_with("Parsing schema...")
Beispiel #4
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
Beispiel #5
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),
        ])
Beispiel #6
0
    def test_add_default_imports(self):
        schema = Schema.create()
        schema.imports.append(Import.create(namespace="foo"))

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

        xsi = Namespace.XSI.value
        schema.ns_map["foo"] = xsi
        self.parser.add_default_imports(schema)
        self.assertEqual(2, len(schema.imports))
        self.assertEqual(Import.create(namespace=xsi), schema.imports[0])
Beispiel #7
0
    def test_module(self):
        schema = Schema.create(location=Path(__file__),
                               target_namespace="http://xsdata/foo")

        self.assertEqual("test_schema.py", 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))
Beispiel #8
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)
Beispiel #9
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)
Beispiel #10
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.schema_location:
            return

        obj.location = self.schema_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)
Beispiel #11
0
    def test_generate_classes(
        self,
        mock_builder_init,
        mock_builder_build,
        mock_count_classes,
        mock_logger_info,
    ):
        schema = Schema.create()
        classes = ClassFactory.list(2)

        mock_builder_build.return_value = classes
        mock_count_classes.return_value = 2, 4
        self.transformer.generate_classes(schema, "foo.bar")

        mock_builder_init.assert_called_once_with(schema=schema,
                                                  package="foo.bar")
        mock_builder_build.assert_called_once_with()
        mock_logger_info.assert_has_calls([
            mock.call("Compiling schema..."),
            mock.call("Builder: %d main and %d inner classes", 2, 4),
        ])
Beispiel #12
0
    def test_resolve_schemas_locations(self, mock_resolve_path,
                                       mock_resolve_local_path):
        schema = Schema.create()
        self.parser.resolve_schemas_locations(schema)

        self.parser.schema_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.create(schema_location="o1"))
        schema.overrides.append(Override.create(schema_location="o2"))
        schema.redefines.append(Redefine.create(schema_location="r1"))
        schema.redefines.append(Redefine.create(schema_location="r2"))
        schema.includes.append(Include.create(schema_location="i1"))
        schema.includes.append(Include.create(schema_location="i2"))
        schema.imports.append(
            Import.create(schema_location="i3", namespace="ns_i3"))
        schema.imports.append(
            Import.create(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.schema_location),
                             sub.location)
Beispiel #13
0
 def setUp(self):
     super(ClassBuilderTests, self).setUp()
     self.schema = Schema.create(location=Path(__file__),
                                 target_namespace="builder")
     self.builder = ClassBuilder(schema=self.schema, package="tests")
Beispiel #14
0
    def set_schema_namespaces(self, obj: xsd.Schema, element: Element):
        """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

        self.set_namespace_map(element, obj)