Esempio n. 1
0
    def test_find_enum(self):
        native_type = AttrTypeFactory.create()
        matching_external = AttrTypeFactory.create("foo")
        missing_external = AttrTypeFactory.create("bar")
        enumeration = ClassFactory.enumeration(1, qname="foo")
        inner = ClassFactory.enumeration(1, qname="foobar")

        target = ClassFactory.create(
            attrs=[
                AttrFactory.create(types=[
                    native_type,
                    matching_external,
                    missing_external,
                ])
            ],
            inner=[inner],
        )
        self.sanitizer.container.extend([target, enumeration])

        actual = self.sanitizer.find_enum(native_type)
        self.assertIsNone(actual)

        actual = self.sanitizer.find_enum(matching_external)
        self.assertEqual(enumeration, actual)

        actual = self.sanitizer.find_enum(missing_external)
        self.assertIsNone(actual)
Esempio n. 2
0
    def test_copy_attribute_properties(self, mock_copy_inner_class):
        source = ClassFactory.elements(1, qname="Foobar")
        source.attrs[0].restrictions.max_length = 100
        source.attrs[0].restrictions.min_length = 1
        source.attrs[0].help = "foo"
        source.attrs[0].types = [
            AttrTypeFactory.create(qname="first"),
            AttrTypeFactory.create(qname="second"),
        ]

        target = ClassFactory.elements(1)
        attr = target.attrs[0]
        attr.restrictions.min_length = 2
        attr.types.clear()
        attr.types.append(AttrTypeFactory.create(qname=source.name))

        self.assertEqual("Foobar", attr.types[0].name)
        self.processor.copy_attribute_properties(source, target, attr,
                                                 attr.types[0])

        self.assertEqual("first", attr.types[0].name)
        self.assertEqual("second", attr.types[1].name)
        self.assertEqual("foo", attr.help)
        self.assertEqual(Restrictions(min_length=2, max_length=100),
                         attr.restrictions)
        mock_copy_inner_class.assert_has_calls([
            mock.call(source, target, attr, source.attrs[0].types[0]),
            mock.call(source, target, attr, source.attrs[0].types[1]),
        ])
Esempio n. 3
0
    def test_process_attribute(self, mock_find):
        target = ClassFactory.create(
            attrs=[
                AttrFactory.create(types=[AttrTypeFactory.create("foo")]),
                AttrFactory.create(types=[AttrTypeFactory.create("bar")]),
            ]
        )
        mock_find.side_effect = [-1, 2]

        first_attr = target.attrs[0]
        second_attr = target.attrs[1]
        first_attr.restrictions.max_occurs = 2

        attr_qname = first_attr.types[0].qname
        reference_attrs = AttrFactory.list(2)

        self.processor.create_substitutions()
        self.processor.substitutions[attr_qname] = reference_attrs
        self.processor.process_attribute(target, first_attr)

        self.assertEqual(4, len(target.attrs))

        self.assertEqual(reference_attrs[0], target.attrs[0])
        self.assertIsNot(reference_attrs[0], target.attrs[0])
        self.assertEqual(reference_attrs[1], target.attrs[3])
        self.assertIsNot(reference_attrs[1], target.attrs[3])
        self.assertEqual(2, target.attrs[0].restrictions.max_occurs)
        self.assertEqual(2, target.attrs[3].restrictions.max_occurs)

        self.processor.process_attribute(target, second_attr)
        self.assertEqual(4, len(target.attrs))
Esempio n. 4
0
    def test_rename_class_dependencies(self):
        attr_type = AttrTypeFactory.create("{foo}bar")

        target = ClassFactory.create(
            extensions=[
                ExtensionFactory.create(),
                ExtensionFactory.create(attr_type.clone()),
            ],
            attrs=[
                AttrFactory.create(),
                AttrFactory.create(
                    types=[AttrTypeFactory.create(),
                           attr_type.clone()]),
            ],
            inner=[
                ClassFactory.create(
                    extensions=[ExtensionFactory.create(attr_type.clone())],
                    attrs=[
                        AttrFactory.create(),
                        AttrFactory.create(types=[
                            AttrTypeFactory.create(),
                            attr_type.clone()
                        ]),
                    ],
                )
            ],
        )

        self.sanitizer.rename_class_dependencies(target, "{foo}bar", "thug")
        dependencies = set(target.dependencies())
        self.assertNotIn("{foo}bar", dependencies)
        self.assertIn("thug", dependencies)
Esempio n. 5
0
    def test_constant_value(self):
        attr = AttrFactory.create(
            types=[AttrTypeFactory.native(DataType.STRING)], default="foo")
        self.assertEqual('"foo"', self.filters.constant_value(attr))

        attr = AttrFactory.create(types=[AttrTypeFactory.create(qname="foo")])
        self.assertEqual("Foo", self.filters.constant_value(attr))

        attr = AttrFactory.create(
            types=[AttrTypeFactory.create(alias="alias")])
        self.assertEqual("Alias", self.filters.constant_value(attr))
Esempio n. 6
0
    def test_process_attribute_default_enum(self, mock_find_enum,
                                            mock_logger_warning):
        enum_one = ClassFactory.enumeration(1, qname="{a}root")
        enum_one.attrs[0].default = "1"
        enum_one.attrs[0].name = "one"
        enum_two = ClassFactory.enumeration(1, qname="inner")
        enum_two.attrs[0].default = "2"
        enum_two.attrs[0].name = "two"
        enum_three = ClassFactory.enumeration(1, qname="missing_member")

        mock_find_enum.side_effect = [
            None,
            enum_one,
            None,
            enum_two,
            enum_three,
        ]

        target = ClassFactory.create(
            qname="target",
            attrs=[
                AttrFactory.create(
                    types=[
                        AttrTypeFactory.create(),
                        AttrTypeFactory.create(qname="foo"),
                    ],
                    default="1",
                ),
                AttrFactory.create(
                    types=[
                        AttrTypeFactory.create(),
                        AttrTypeFactory.create(qname="bar", forward=True),
                    ],
                    default="2",
                ),
                AttrFactory.create(default="3"),
            ],
        )

        actual = []
        for attr in target.attrs:
            self.sanitizer.process_attribute_default(target, attr)
            actual.append(attr.default)

        self.assertEqual(["@enum@{a}root::one", "@enum@inner::two", None],
                         actual)
        mock_logger_warning.assert_called_once_with(
            "No enumeration member matched %s.%s default value `%s`",
            target.name,
            target.attrs[2].local_name,
            "3",
        )
Esempio n. 7
0
    def test_clone_attribute(self):
        attr = AttrFactory.create(
            restrictions=Restrictions(length=1),
            types=[
                AttrTypeFactory.create(qname="x"),
                AttrTypeFactory.create(qname="y"),
                AttrTypeFactory.native(DataType.INT),
            ],
        )
        restrictions = Restrictions(length=2)

        clone = ClassUtils.clone_attribute(attr, restrictions)

        self.assertEqual(2, clone.restrictions.length)
        self.assertIsNot(attr, clone)
Esempio n. 8
0
    def test_promote(self):
        target = ClassFactory.elements(2)
        inner = ClassFactory.enumeration(3)

        target.inner.append(inner)
        target.inner.append(ClassFactory.simple_type())  # Irrelevant
        attr_type = AttrTypeFactory.create(qname=inner.qname, forward=True)

        target.attrs[0].types.append(attr_type.clone())
        target.attrs[1].types.append(attr_type.clone())

        self.container.add(target)
        self.assertEqual(3, len(self.container.data))

        self.processor.process(target)

        new_qname = build_qname(inner.target_namespace,
                                f"{target.name}_{inner.name}")

        self.assertEqual(4, len(self.container.data))
        new_inner = self.container.find(new_qname)

        self.assertEqual(1, len(target.inner))
        self.assertNotEqual(new_inner.qname, inner.qname)
        self.assertEqual(new_inner.attrs, inner.attrs)
        self.assertEqual(new_inner.qname, target.attrs[0].types[1].qname)
        self.assertEqual(new_inner.qname, target.attrs[1].types[1].qname)
        self.assertFalse(target.attrs[0].types[1].forward)
        self.assertFalse(target.attrs[1].types[1].forward)
Esempio n. 9
0
    def test_copy_attributes(self, mock_clone_attribute, mock_copy_inner_classes):
        mock_clone_attribute.side_effect = lambda x, y: x.clone()
        target = ClassFactory.create(
            attrs=[AttrFactory.create(name="a"), AttrFactory.create(name="b")]
        )
        source = ClassFactory.create(
            attrs=[
                AttrFactory.create(name="c", index=sys.maxsize),
                AttrFactory.create(name="a"),
                AttrFactory.create(name="b"),
                AttrFactory.create(name="d"),
            ]
        )
        extension = ExtensionFactory.create(AttrTypeFactory.create(qname="foo"))
        target.extensions.append(extension)

        ClassUtils.copy_attributes(source, target, extension)

        self.assertEqual(["a", "b", "d", "c"], [attr.name for attr in target.attrs])

        mock_copy_inner_classes.assert_has_calls(
            [
                mock.call(source, target, source.attrs[0]),
                mock.call(source, target, source.attrs[3]),
            ]
        )
        mock_clone_attribute.assert_has_calls(
            [
                mock.call(source.attrs[0], extension.restrictions),
                mock.call(source.attrs[3], extension.restrictions),
            ]
        )
Esempio n. 10
0
    def test_process_type_with_forward_reference(self, mock_process_inner_type):
        attr = AttrFactory.create()
        target = ClassFactory.create()
        attr_type = AttrTypeFactory.create(forward=True)

        self.processor.process_type(target, attr, attr_type)
        mock_process_inner_type.assert_called_once_with(target, attr, attr_type)
Esempio n. 11
0
    def test_build_class_attribute_from_dict(self):
        target = ClassFactory.create()
        data = {"sub1": 1, "sub2": "value"}
        DictMapper.build_class_attribute(target, "a", data)

        expected = AttrFactory.create(
            name="a",
            tag=Tag.ELEMENT,
            types=[AttrTypeFactory.create(qname="a", forward=True)],
        )

        expected_inner = ClassFactory.create(
            qname="a",
            tag=Tag.ELEMENT,
            module="",
            ns_map={},
            attrs=[
                AttrFactory.native(DataType.SHORT, name="sub1"),
                AttrFactory.native(DataType.STRING, name="sub2"),
            ],
        )

        self.assertEqual(expected, target.attrs[0])
        self.assertEqual(expected_inner, target.inner[0])
        self.assertEqual(1, len(target.inner))
Esempio n. 12
0
    def test_copy_inner_class_with_missing_inner(self):
        source = ClassFactory.create()
        target = ClassFactory.create()
        attr = AttrFactory.create()
        attr_type = AttrTypeFactory.create(forward=True, qname=target.qname)

        with self.assertRaises(CodeGenerationError):
            ClassUtils.copy_inner_class(source, target, attr, attr_type)
Esempio n. 13
0
    def test_reset_unsupported_types_ignore_user_types(self):
        attr_type = AttrTypeFactory.create(qname="foo")
        attr = AttrFactory.create(types=[attr_type], fixed=True, default="123")
        target = ClassFactory.create()
        target.attrs.append(attr)

        self.processor.process(target)
        self.assertEqual(attr_type, attr.types[0])
Esempio n. 14
0
    def test_process_extension_with_dependency_type(
            self, mock_process_dependency_extension):
        extension = ExtensionFactory.create(AttrTypeFactory.create("foo"))
        target = ClassFactory.elements(1, extensions=[extension])

        self.processor.process_extension(target, extension)
        mock_process_dependency_extension.assert_called_once_with(
            target, extension)
Esempio n. 15
0
    def test_copy_inner_class_skip_non_forward_reference(self):
        source = ClassFactory.create()
        target = ClassFactory.create()
        attr = AttrFactory.create()
        attr_type = AttrTypeFactory.create()
        ClassUtils.copy_inner_class(source, target, attr, attr_type)

        self.assertFalse(attr_type.circular)
        self.assertEqual(0, len(target.inner))
Esempio n. 16
0
    def test_process_inner_type_with_circular_reference(
            self, mock_copy_attribute_properties, mock_update_restrictions):
        target = ClassFactory.create()
        attr = AttrFactory.create()
        attr_type = AttrTypeFactory.create(circular=True)

        self.processor.process_inner_type(target, attr, attr_type)
        self.assertEqual(0, mock_copy_attribute_properties.call_count)
        self.assertEqual(0, mock_update_restrictions.call_count)
Esempio n. 17
0
    def test_copy_inner_class_check_circular_reference(self):
        source = ClassFactory.create()
        target = ClassFactory.create()
        attr = AttrFactory.create()
        attr_type = AttrTypeFactory.create(forward=True, qname=target.qname)
        source.inner.append(target)

        ClassUtils.copy_inner_class(source, target, attr, attr_type)
        self.assertTrue(attr_type.circular)
        self.assertEqual(0, len(target.inner))
Esempio n. 18
0
    def test_property_native_types(self):
        attr = AttrFactory.create(types=[
            AttrTypeFactory.create(qname="foo"),
            AttrTypeFactory.native(DataType.INT),
            AttrTypeFactory.native(DataType.SHORT),
            AttrTypeFactory.native(DataType.INTEGER),
            AttrTypeFactory.native(DataType.FLOAT),
        ])

        self.assertCountEqual([float, int], attr.native_types)
Esempio n. 19
0
    def test_process_type_with_dependency_type(self, mock_process_native_type,
                                               mock_process_dependency_type):
        attr = AttrFactory.create()
        target = ClassFactory.create()
        attr_type = AttrTypeFactory.create()

        self.processor.process_type(target, attr, attr_type)
        self.assertEqual(0, mock_process_native_type.call_count)
        mock_process_dependency_type.assert_called_once_with(
            target, attr, attr_type)
Esempio n. 20
0
    def test_process_inner_type_with_complex_type(
            self, mock_copy_attribute_properties, mock_update_restrictions):
        target = ClassFactory.create()
        inner = ClassFactory.elements(2, qname="a", status=Status.PROCESSED)
        attr = AttrFactory.create(types=[AttrTypeFactory.create(qname="a")])

        target.inner.append(inner)
        self.processor.process_inner_type(target, attr, attr.types[0])
        self.assertIn(inner, target.inner)
        self.assertEqual(0, mock_copy_attribute_properties.call_count)
        self.assertEqual(0, mock_update_restrictions.call_count)
Esempio n. 21
0
    def test_build_class_extensions(self, mock_children_extensions):
        bar_type = AttrTypeFactory.create(qname="bar")
        foo_type = AttrTypeFactory.create(qname="foo")

        bar = ExtensionFactory.create(bar_type)
        double = ExtensionFactory.create(bar_type)
        foo = ExtensionFactory.create(foo_type)

        mock_children_extensions.return_value = [bar, double, foo]
        self_ext = ExtensionFactory.reference(
            qname="{xsdata}something",
            restrictions=Restrictions(min_occurs=1, max_occurs=1),
        )

        item = ClassFactory.create()
        element = Element(type="something")
        SchemaMapper.build_class_extensions(element, item)

        self.assertEqual(3, len(item.extensions))
        self.assertCountEqual([bar, self_ext, foo], item.extensions)
Esempio n. 22
0
    def test_process_dependency_type_with_enumeration_type(self, mock_find_dependency):
        enumeration = ClassFactory.enumeration(2)
        enumeration.attrs[1].restrictions.format = "base16"
        mock_find_dependency.return_value = enumeration

        target = ClassFactory.simple_type()
        attr = target.attrs[0]
        attr.types[0] = AttrTypeFactory.create(qname=enumeration.qname)

        self.processor.process_dependency_type(target, attr, attr.types[0])
        self.assertEqual("base16", attr.restrictions.format)
Esempio n. 23
0
    def test_rename_attr_dependencies_with_choices(self):
        attr_type = AttrTypeFactory.create(qname="foo", reference=1)
        target = ClassFactory.create(attrs=[
            AttrFactory.create(choices=[
                AttrFactory.create(types=[attr_type.clone()]),
            ])
        ])

        self.sanitizer.rename_class_dependencies(target, 1, "bar")
        dependencies = set(target.dependencies())
        self.assertNotIn("foo", dependencies)
        self.assertIn("bar", dependencies)
Esempio n. 24
0
    def test_rename_attr_dependencies_with_choices(self):
        attr_type = AttrTypeFactory.create("{foo}bar")
        target = ClassFactory.create(attrs=[
            AttrFactory.create(choices=[
                AttrFactory.create(types=[attr_type.clone()]),
            ])
        ])

        self.sanitizer.rename_class_dependencies(target, "{foo}bar", "thug")
        dependencies = set(target.dependencies())
        self.assertNotIn("{foo}bar", dependencies)
        self.assertIn("thug", dependencies)
Esempio n. 25
0
    def test_rename_attr_dependencies_with_default_enum(self):
        attr_type = AttrTypeFactory.create("{foo}bar")
        target = ClassFactory.create(attrs=[
            AttrFactory.create(types=[attr_type],
                               default=f"@enum@{attr_type.qname}::member"),
        ])

        self.sanitizer.rename_class_dependencies(target, "{foo}bar", "thug")
        dependencies = set(target.dependencies())
        self.assertEqual("@enum@thug::member", target.attrs[0].default)
        self.assertNotIn("{foo}bar", dependencies)
        self.assertIn("thug", dependencies)
Esempio n. 26
0
    def test_find_dependency(self):
        attr_type = AttrTypeFactory.create(qname="a")

        self.assertIsNone(self.processor.find_dependency(attr_type))

        complex = ClassFactory.create(qname="a", tag=Tag.COMPLEX_TYPE)
        self.processor.container.add(complex)
        self.assertEqual(complex, self.processor.find_dependency(attr_type))

        simple = ClassFactory.create(qname="a", tag=Tag.SIMPLE_TYPE)
        self.processor.container.add(simple)
        self.assertEqual(simple, self.processor.find_dependency(attr_type))
Esempio n. 27
0
    def test_field_type_with_multiple_types(self):
        attr = AttrFactory.create(types=[
            AttrTypeFactory.create(
                qname="life", alias="Boss:Life", forward=True),
            AttrTypeFactory.native(DataType.INT),
        ])
        attr.restrictions.max_occurs = 2

        self.assertEqual(
            'List[Union["A.Parent.BossLife", int]]',
            self.filters.field_type(attr, ["A", "Parent"]),
        )
Esempio n. 28
0
    def setUp(self):
        super().setUp()

        self.root_enum = ClassFactory.enumeration(2)
        self.inner_enum = ClassFactory.enumeration(2)
        self.target = ClassFactory.create(attrs=[
            AttrFactory.create(
                name="value",
                tag=Tag.UNION,
                types=[
                    AttrTypeFactory.create(qname=self.root_enum.qname),
                    AttrTypeFactory.create(qname=self.inner_enum.qname,
                                           forward=True),
                ],
            ),
        ])
        self.target.inner.append(self.inner_enum)

        self.container = ClassContainer()
        self.container.add(self.target)
        self.container.add(self.root_enum)
        self.processor = ClassEnumerationHandler(container=self.container)
Esempio n. 29
0
    def test_children_extensions(self):
        complex_type = ComplexType(
            attributes=[Attribute() for _ in range(2)],
            simple_content=SimpleContent(restriction=Restriction(base="bk:b")),
            complex_content=ComplexContent(extension=Extension(base="bk:c")),
        )
        complex_type.simple_content.restriction.index = 4
        complex_type.complex_content.extension.index = 7

        item = ClassFactory.create(ns_map={"bk": "book"})
        children = SchemaMapper.children_extensions(complex_type, item)
        expected = list(
            map(
                ExtensionFactory.create,
                [
                    AttrTypeFactory.create(qname=build_qname("book", "b")),
                    AttrTypeFactory.create(qname=build_qname("book", "c")),
                ],
            ))

        self.assertIsInstance(children, GeneratorType)
        self.assertEqual(expected, list(children))
Esempio n. 30
0
    def test_property_is_dependency(self):

        attr_type = AttrTypeFactory.create(forward=True, native=True, circular=True)
        self.assertFalse(attr_type.is_dependency)

        attr_type.forward = False
        self.assertFalse(attr_type.is_dependency)

        attr_type.native = False
        self.assertFalse(attr_type.is_dependency)

        attr_type.circular = False
        self.assertTrue(attr_type.is_dependency)