Exemplo n.º 1
0
    def test_group_fields(self):
        target = ClassFactory.create(attrs=AttrFactory.list(2))
        target.attrs[0].restrictions.min_occurs = 10
        target.attrs[0].restrictions.max_occurs = 15
        target.attrs[1].restrictions.min_occurs = 5
        target.attrs[1].restrictions.max_occurs = 20

        expected = AttrFactory.create(
            name="attr_B_Or_attr_C",
            tag="Choice",
            index=0,
            types=[AttrTypeFactory.xs_any()],
            choices=[
                AttrFactory.create(
                    tag=target.attrs[0].tag,
                    name="attr_B",
                    types=target.attrs[0].types,
                ),
                AttrFactory.create(
                    tag=target.attrs[1].tag,
                    name="attr_C",
                    types=target.attrs[1].types,
                ),
            ],
        )
        expected_res = Restrictions(min_occurs=5, max_occurs=20)

        self.sanitizer.group_fields(target, list(target.attrs))
        self.assertEqual(1, len(target.attrs))
        self.assertEqual(expected, target.attrs[0])
        self.assertEqual(expected_res, target.attrs[0].restrictions)
Exemplo n.º 2
0
    def test_flatten_attribute_types_when_source_has_only_one_attribute(
        self, mock_find_class, mock_copy_inner_classes
    ):
        type_a = AttrTypeFactory.create(name="a")
        type_b = AttrTypeFactory.create(name="b")
        common = ClassFactory.create(
            name="bar",
            attrs=AttrFactory.list(
                1,
                name="b",
                types=[type_b],
                restrictions=RestrictionsFactory.create(required=True, min_occurs=2),
            ),
        )

        mock_find_class.return_value = common

        parent = ClassFactory.create()
        attr = AttrFactory.create(
            name="a",
            types=[type_a],
            restrictions=RestrictionsFactory.create(min_occurs=1),
        )

        self.analyzer.flatten_attribute_types(parent, attr)

        self.assertEqual([type_b], attr.types)
        self.assertEqual(
            {"required": True, "min_occurs": 2}, attr.restrictions.asdict()
        )
        mock_find_class.assert_called_once_with(parent.source_qname(type_a.name))
        mock_copy_inner_classes.assert_called_once_with(common, parent)
Exemplo n.º 3
0
    def test_dependencies(self):
        obj = ClassFactory.create(
            attrs=[
                AttrFactory.create(types=[AttrTypeFactory.xs_decimal()]),
                AttrFactory.create(
                    types=[
                        AttrTypeFactory.create(name="xs:annotated", forward_ref=True)
                    ]
                ),
                AttrFactory.create(
                    types=[
                        AttrTypeFactory.create(name="xs:openAttrs"),
                        AttrTypeFactory.create(name="xs:localAttribute"),
                    ]
                ),
            ],
            extensions=ExtensionFactory.list(
                1, type=AttrTypeFactory.create(name="xs:localElement")
            ),
            inner=[
                ClassFactory.create(
                    attrs=AttrFactory.list(2, types=AttrTypeFactory.list(1, name="foo"))
                )
            ],
        )

        expected = {
            QName("{http://www.w3.org/2001/XMLSchema}localAttribute"),
            QName("{http://www.w3.org/2001/XMLSchema}localElement"),
            QName("{http://www.w3.org/2001/XMLSchema}openAttrs"),
            QName("{xsdata}foo"),
        }
        self.assertEqual(expected, obj.dependencies())
Exemplo n.º 4
0
    def test_add_substitution_attrs(self, mock_find_attribute):
        target = ClassFactory.elements(2)
        mock_find_attribute.side_effect = [-1, 2]

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

        attr_name = first_attr.name
        attr_qname = target.source_qname(attr_name)
        reference_attrs = AttrFactory.list(2)

        self.analyzer.substitutions_index[attr_qname] = reference_attrs
        self.analyzer.add_substitution_attrs(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)

        second_attr.wildcard = True
        self.analyzer.add_substitution_attrs(target, second_attr)
        self.assertEqual(4, len(target.attrs))

        self.analyzer.add_substitution_attrs(target, AttrFactory.enumeration())
        self.assertEqual(4, len(target.attrs))
Exemplo n.º 5
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))
Exemplo n.º 6
0
    def test_sanitize_attribute_sequence(self):
        def len_sequential(target: Class):
            return len([
                attr for attr in target.attrs if attr.restrictions.sequential
            ])

        restrictions = Restrictions(max_occurs=2, sequential=True)
        target = ClassFactory.create(attrs=[
            AttrFactory.create(restrictions=restrictions.clone()),
            AttrFactory.create(restrictions=restrictions.clone()),
        ])

        attrs_clone = [attr.clone() for attr in target.attrs]

        self.sanitizer.process_attribute_sequence(target, target.attrs[0])
        self.assertEqual(2, len_sequential(target))

        target.attrs[0].restrictions.sequential = False
        self.sanitizer.process_attribute_sequence(target, target.attrs[0])
        self.assertEqual(1, len_sequential(target))

        self.sanitizer.process_attribute_sequence(target, target.attrs[1])
        self.assertEqual(0, len_sequential(target))

        target.attrs = attrs_clone
        target.attrs[1].restrictions.sequential = False
        self.sanitizer.process_attribute_sequence(target, target.attrs[0])
        self.assertEqual(0, len_sequential(target))

        target.attrs[0].restrictions.sequential = True
        target.attrs[0].restrictions.max_occurs = 0
        target.attrs[1].restrictions.sequential = True
        self.sanitizer.process_attribute_sequence(target, target.attrs[0])
        self.assertEqual(1, len_sequential(target))
Exemplo n.º 7
0
    def test_sanitize_attribute_sequence(self):
        def len_sequential(target):
            return len(
                [attr for attr in attrs if attr.restrictions.sequential])

        restrictions = Restrictions(max_occurs=2, sequential=True)
        attrs = [
            AttrFactory.create(restrictions=restrictions.clone()),
            AttrFactory.create(restrictions=restrictions.clone()),
        ]
        attrs_clone = [attr.clone() for attr in attrs]

        ClassUtils.sanitize_attribute_sequence(attrs, 0)
        self.assertEqual(2, len_sequential(attrs))

        attrs[0].restrictions.sequential = False
        ClassUtils.sanitize_attribute_sequence(attrs, 0)
        self.assertEqual(1, len_sequential(attrs))

        ClassUtils.sanitize_attribute_sequence(attrs, 1)
        self.assertEqual(0, len_sequential(attrs))

        attrs_clone[1].restrictions.sequential = False
        ClassUtils.sanitize_attribute_sequence(attrs_clone, 0)
        self.assertEqual(0, len_sequential(attrs_clone))
Exemplo n.º 8
0
    def test_dependencies(self):
        obj = ClassFactory.create(
            attrs=[
                AttrFactory.create(types=[AttrTypeFactory.xs_decimal()]),
                AttrFactory.create(types=[
                    AttrTypeFactory.create(qname=QName(Namespace.XS.uri,
                                                       "annotated"),
                                           forward=True)
                ]),
                AttrFactory.create(types=[
                    AttrTypeFactory.create(
                        qname=QName(Namespace.XS.uri, "openAttrs")),
                    AttrTypeFactory.create(
                        qname=QName(Namespace.XS.uri, "localAttribute")),
                ]),
            ],
            extensions=[
                ExtensionFactory.create(type=AttrTypeFactory.create(
                    qname=QName(Namespace.XS.uri, "foobar"))),
                ExtensionFactory.create(type=AttrTypeFactory.create(
                    qname=QName(Namespace.XS.uri, "foobar"))),
            ],
            inner=[
                ClassFactory.create(attrs=AttrFactory.list(
                    2, types=AttrTypeFactory.list(1, qname="foo")))
            ],
        )

        expected = [
            QName("{http://www.w3.org/2001/XMLSchema}openAttrs"),
            QName("{http://www.w3.org/2001/XMLSchema}localAttribute"),
            QName("{http://www.w3.org/2001/XMLSchema}foobar"),
            QName("{xsdata}foo"),
        ]
        self.assertEqual(expected, list(obj.dependencies()))
Exemplo n.º 9
0
    def test_rename_dependency(self):
        attr_type = AttrTypeFactory.create("{foo}bar")

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

        self.sanitizer.rename_dependency(target, "{foo}bar", "thug")
        dependencies = set(target.dependencies())
        self.assertNotIn("{foo}bar", dependencies)
        self.assertIn("thug", dependencies)
Exemplo n.º 10
0
    def test_process_attributes_prevent_duplicates(self):
        a = AttrFactory.create(name="a")
        a_a = AttrFactory.create(name="a")
        b = AttrFactory.create(name="b")
        obj = ClassFactory.create(attrs=[a, a_a, b])

        generator.process_attributes(obj, [])
        self.assertEqual([a, b], obj.attrs)
Exemplo n.º 11
0
    def test_has_duplicate_attribute_names(self):
        obj = ClassFactory.create()
        obj.attrs.append(AttrFactory.create(name="a"))

        self.assertFalse(generator.has_duplicate_attrs(obj))

        obj.attrs.append(AttrFactory.create(name="a"))
        self.assertTrue(generator.has_duplicate_attrs(obj))
Exemplo n.º 12
0
    def test_property_is_factory(self):
        self.assertTrue(AttrFactory.any_attribute().is_factory)

        element = AttrFactory.element()
        self.assertFalse(element.is_factory)

        element.restrictions.max_occurs = 2
        self.assertTrue(element.is_factory)
Exemplo n.º 13
0
 def test_hash_attributes_names(self):
     obj = ClassFactory.create(attrs=[
         AttrFactory.create(name="a", local_name="-"),
         AttrFactory.create(name="a", local_name="_"),
     ])
     generator.hash_attributes_names(obj)
     self.assertEqual("lq", obj.attrs[0].name)
     self.assertEqual("xw", obj.attrs[1].name)
Exemplo n.º 14
0
    def test_constant_value(self):
        attr = AttrFactory.create(types=[AttrTypeFactory.xs_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))
Exemplo n.º 15
0
    def test_field_metadata_choices(self):
        attr = AttrFactory.create(choices=AttrFactory.list(2, tag=Tag.ELEMENT))
        actual = self.filters.field_metadata(attr, "foo", [])
        expected = [
            {"name": "attr_B", "type": "Type[str]"},
            {"name": "attr_C", "type": "Type[str]"},
        ]

        self.assertEqual(expected, actual["choices"])
Exemplo n.º 16
0
    def test_group_fields_limit_name(self):
        target = ClassFactory.create(attrs=AttrFactory.list(3))
        self.sanitizer.group_fields(target, list(target.attrs))

        self.assertEqual(1, len(target.attrs))
        self.assertEqual("attr_B_Or_attr_C_Or_attr_D", target.attrs[0].name)

        target = ClassFactory.create(attrs=AttrFactory.list(4))
        self.sanitizer.group_fields(target, list(target.attrs))
        self.assertEqual("choice", target.attrs[0].name)
Exemplo n.º 17
0
    def test_attribute_metadata_name(self):
        attr = AttrFactory.element(local_name="foo", name="bar")
        actual = attribute_metadata(attr, None)
        self.assertEqual("foo", actual["name"])

        attr = AttrFactory.element(local_name="foo", name="Foo")
        self.assertNotIn("name", attribute_metadata(attr, None))

        attr = AttrFactory.create(tag=Tag.ANY, local_name="foo", name="bar")
        self.assertNotIn("name", attribute_metadata(attr, None))
Exemplo n.º 18
0
    def test_property_has_wild_attr(self):
        obj = ClassFactory.create()
        self.assertFalse(obj.has_wild_attr)

        obj.attrs.append(AttrFactory.create())
        obj.attrs.append(AttrFactory.create())
        self.assertFalse(obj.has_wild_attr)

        obj.attrs.append(AttrFactory.any())
        self.assertTrue(obj.has_wild_attr)
Exemplo n.º 19
0
    def test_merge_redefined_type_with_circular_group(self, mock_copy_group_attributes):
        source = ClassFactory.create()
        target = source.clone()
        target.container = Tag.REDEFINE
        first_attr = AttrFactory.create()
        second_attr = AttrFactory.create(name=source.name)
        target.attrs.extend((first_attr, second_attr))

        self.validator.merge_redefined_type(source, target)

        mock_copy_group_attributes.assert_called_once_with(source, target, second_attr)
Exemplo n.º 20
0
    def test_process_enumerations_with_mixed_types(self):
        obj = ClassFactory.create(attrs=[
            AttrFactory.create(default="aaBB"),
            AttrFactory.create(default=1, types=[AttrTypeFactory.xs_int()]),
        ])

        generator.process_enumerations(obj)
        actual = [(attr.name, attr.default) for attr in obj.attrs]
        expected = [("VALUE_1", 1), ("AA_BB", '"aaBB"')]

        self.assertEqual(expected, actual)
Exemplo n.º 21
0
    def test_process_attributes_prevent_duplicates_after_process(self, *args):
        obj = ClassFactory.create(attrs=[
            AttrFactory.create(name="a."),
            AttrFactory.create(name="a-"),
            AttrFactory.create(name="a*"),
        ])

        generator.process_attributes(obj, [])
        actual = [(attr.name, attr.local_name) for attr in obj.attrs]
        expected = [("ys4", "a."), ("ys0", "a-"), ("yso", "a*")]
        self.assertEqual(expected, actual)
Exemplo n.º 22
0
    def test_property_has_suffix_attr(self):
        obj = ClassFactory.create()

        self.assertFalse(obj.has_suffix_attr)

        obj.attrs.append(AttrFactory.create())
        obj.attrs.append(AttrFactory.create())
        self.assertFalse(obj.has_suffix_attr)

        obj.attrs[1].index = sys.maxsize
        self.assertTrue(obj.has_suffix_attr)
Exemplo n.º 23
0
    def test_process_attribute_default_enum(self, mock_find_enum,
                                            mock_promote_inner_class,
                                            mock_logger_warning):
        enum_one = ClassFactory.enumeration(1, qname="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@root::one", "@enum@inner::two", None], actual)
        mock_promote_inner_class.assert_called_once_with(target, enum_two)
        mock_logger_warning.assert_called_once_with(
            "No enumeration member matched %s.%s default value `%s`",
            target.name,
            target.attrs[2].local_name,
            "3",
        )
Exemplo n.º 24
0
    def test_field_metadata_namespace(self):
        attr = AttrFactory.element(namespace="foo")
        expected = {"name": "attr_B", "namespace": "foo", "type": "Element"}

        self.assertEqual(expected, self.filters.field_metadata(attr, None, []))
        self.assertNotIn("namespace", self.filters.field_metadata(attr, "foo", []))

        attr = AttrFactory.attribute(namespace="foo")
        expected = {"name": "attr_C", "namespace": "foo", "type": "Attribute"}

        self.assertEqual(expected, self.filters.field_metadata(attr, None, []))
        self.assertIn("namespace", self.filters.field_metadata(attr, "foo", []))
Exemplo n.º 25
0
    def test_field_metadata_name(self):
        attr = AttrFactory.element(name="bar")
        attr.local_name = "foo"
        self.assertEqual("foo", self.filters.field_metadata(attr, None, [])["name"])

        attr = AttrFactory.element(name="Foo")
        attr.local_name = "foo"
        self.assertNotIn("name", self.filters.field_metadata(attr, None, []))

        attr = AttrFactory.create(tag=Tag.ANY, name="bar")
        attr.local_name = "foo"
        self.assertNotIn("name", self.filters.field_metadata(attr, None, []))
Exemplo n.º 26
0
    def test_process_enumerations(self):
        obj = ClassFactory.create(attrs=[
            AttrFactory.create(default="2020-12-13"),
            AttrFactory.create(default="2020-12-14"),
        ])

        generator.process_enumerations(obj)
        actual = [(attr.name, attr.default) for attr in obj.attrs]
        expected = [
            ("VALUE_2020_12_13", '"2020-12-13"'),
            ("VALUE_2020_12_14", '"2020-12-14"'),
        ]

        self.assertEqual(expected, actual)
Exemplo n.º 27
0
    def test_process(self, mock_create_substitutions, mock_process_attribute):
        def init_substitutions():
            self.processor.substitutions = {}

        mock_create_substitutions.side_effect = init_substitutions

        target = ClassFactory.create(
            attrs=[AttrFactory.enumeration(), AttrFactory.any(), AttrFactory.element(),]
        )

        self.processor.process(target)
        self.processor.process(ClassFactory.create())
        mock_process_attribute.assert_called_once_with(target, target.attrs[2])
        mock_create_substitutions.assert_called_once()
Exemplo n.º 28
0
    def test_class_docstring(self):
        target = ClassFactory.create(attrs=[
            AttrFactory.element(help="help"),
            AttrFactory.element(help="Foo\nBar"),
            AttrFactory.element(),
        ])

        expected = '''"""
:ivar attr_b: help
:ivar attr_c: Foo
Bar
:ivar attr_d:
"""'''
        self.assertEqual(expected, class_docstring(target))
Exemplo n.º 29
0
    def test_process(self):
        one = AttrFactory.attribute(fixed=True)
        one_clone = one.clone()
        restrictions = Restrictions(min_occurs=10, max_occurs=15)
        two = AttrFactory.element(restrictions=restrictions, fixed=True)
        two_clone = two.clone()
        two_clone.restrictions.min_occurs = 5
        two_clone.restrictions.max_occurs = 5
        two_clone_two = two.clone()
        two_clone_two.restrictions.min_occurs = 4
        two_clone_two.restrictions.max_occurs = 4
        three = AttrFactory.element()
        four = AttrFactory.enumeration()
        four_clone = four.clone()
        five = AttrFactory.element()
        five_clone = five.clone()
        five_clone_two = five.clone()

        target = ClassFactory.create(attrs=[
            one,
            one_clone,
            two,
            two_clone,
            two_clone_two,
            three,
            four,
            four_clone,
            five,
            five_clone,
            five_clone_two,
        ])

        winners = [one, two, three, four, five]

        self.processor.process(target)
        self.assertEqual(winners, target.attrs)

        self.assertTrue(one.fixed)
        self.assertIsNone(one.restrictions.min_occurs)
        self.assertIsNone(one.restrictions.max_occurs)
        self.assertFalse(two.fixed)
        self.assertEqual(4, two.restrictions.min_occurs)
        self.assertEqual(24, two.restrictions.max_occurs)
        self.assertIsNone(three.restrictions.min_occurs)
        self.assertIsNone(three.restrictions.max_occurs)
        self.assertIsNone(four.restrictions.min_occurs)
        self.assertIsNone(four.restrictions.max_occurs)
        self.assertEqual(0, five.restrictions.min_occurs)
        self.assertEqual(3, five.restrictions.max_occurs)
Exemplo n.º 30
0
    def test_property_xml_type(self):
        attr = AttrFactory.create(tag=Tag.ELEMENT)
        self.assertEqual("Element", attr.xml_type)

        attr = AttrFactory.create(tag=Tag.ATTRIBUTE)
        self.assertEqual("Attribute", attr.xml_type)

        attr = AttrFactory.create(tag=Tag.ANY_ATTRIBUTE)
        self.assertEqual("Attributes", attr.xml_type)

        attr = AttrFactory.create(tag=Tag.ANY)
        self.assertEqual("Wildcard", attr.xml_type)

        attr = AttrFactory.create(tag=Tag.RESTRICTION)
        self.assertIsNone(attr.xml_type)