예제 #1
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()))
예제 #2
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))
예제 #3
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)
예제 #4
0
    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),
            ]
        )
예제 #5
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"),
        ])
예제 #6
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"),
        ])