예제 #1
0
    def test_merge_attributes(self):
        target = ClassFactory.create(
            attrs=[
                AttrFactory.element(name="a", index=10),
                AttrFactory.element(name="b", index=1),
                AttrFactory.element(name="c", index=2),
                AttrFactory.attribute(name="id", index=0),
            ]
        )

        source = target.clone()

        target.attrs[0].restrictions.min_occurs = 2
        target.attrs[0].restrictions.max_occurs = 3

        source.attrs[1].restrictions.min_occurs = 3
        source.attrs[1].restrictions.max_occurs = 4
        source.attrs[3].restrictions.min_occurs = 3
        source.attrs[3].restrictions.max_occurs = 4
        source.attrs.append(AttrFactory.enumeration(name="d", index=4))

        ClassUtils.merge_attributes(target, source)

        names = ["id", "b", "c", "d", "a"]
        min_occurs = [0, 0, 0, None, 0]
        max_occurs = [4, 4, 1, None, 3]

        self.assertEqual(names, [x.name for x in target.attrs])
        self.assertEqual(min_occurs, [x.restrictions.min_occurs for x in target.attrs])
        self.assertEqual(max_occurs, [x.restrictions.max_occurs for x in target.attrs])
예제 #2
0
    def test_field_choices(self):
        attr = AttrFactory.create(choices=[
            AttrFactory.element(
                namespace="foo",
                types=[type_float],
                restrictions=Restrictions(max_exclusive="10"),
            ),
            AttrFactory.element(namespace="bar"),
            AttrFactory.any(namespace="##other"),
            AttrFactory.element(name="bar", default="aa"),
            AttrFactory.element(name="tok",
                                restrictions=Restrictions(tokens=True)),
        ])

        actual = self.filters.field_choices(attr, "foo", ["a", "b"])
        expected = (
            {
                "name": "attr_B",
                "type": "Type[float]",
                "max_exclusive": 10.0
            },
            {
                "name": "attr_C",
                "namespace": "bar",
                "type": "Type[str]"
            },
            {
                "namespace": "##other",
                "wildcard": True,
                "type": "Type[object]",
            },
            {
                "default": '"aa"',
                "name": "bar",
                "type": "Type[str]"
            },
            {
                "default_factory": "list",
                "name": "tok",
                "tokens": True,
                "type": "Type[List[str]]",
            },
        )

        self.assertEqual(expected, actual)

        self.filters.docstring_style = DocstringStyle.ACCESSIBLE
        attr.choices[0].help = "help"
        actual = self.filters.field_choices(attr, None, [])
        self.assertEqual(attr.choices[0].help, actual[0]["doc"])
        self.assertNotIn("doc", actual[1])
예제 #3
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)
예제 #4
0
    def test_field_metadata_name(self):
        attr = AttrFactory.element(name="bar")
        attr.local_name = "foo"

        actual = self.filters.field_metadata(attr, None, ["cls"])
        self.assertEqual("foo", actual["name"])

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

        attr = AttrFactory.create(tag=Tag.ANY, name="bar")
        attr.local_name = "foo"
        actual = self.filters.field_metadata(attr, None, ["cls"])
        self.assertNotIn("name", actual)
예제 #5
0
 def test_field_metadata(self):
     attr = AttrFactory.element()
     expected = {"name": "attr_B", "type": "Element"}
     self.assertEqual(expected,
                      self.filters.field_metadata(attr, None, ["cls"]))
     self.assertEqual(expected,
                      self.filters.field_metadata(attr, "foo", ["cls"]))
예제 #6
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)
예제 #7
0
    def test_property_is_enumeration(self):
        obj = ClassFactory.enumeration(2)
        self.assertTrue(obj.is_enumeration)

        obj.attrs.append(AttrFactory.element())
        self.assertFalse(obj.is_enumeration)

        obj.attrs.clear()
        self.assertFalse(obj.is_enumeration)
예제 #8
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()
예제 #9
0
    def test_field_metadata_namespace(self):
        attr = AttrFactory.element(namespace="foo")
        expected = {"name": "attr_B", "namespace": "foo", "type": "Element"}

        actual = self.filters.field_metadata(attr, None, ["cls"])
        self.assertEqual(expected, actual)

        actual = self.filters.field_metadata(attr, "foo", ["cls"])
        self.assertNotIn("namespace", actual)

        attr = AttrFactory.attribute(namespace="foo")
        expected = {"name": "attr_C", "namespace": "foo", "type": "Attribute"}
        actual = self.filters.field_metadata(attr, None, ["cls"])
        self.assertEqual(expected, actual)

        actual = self.filters.field_metadata(attr, "foo", ["cls"])
        self.assertIn("namespace", actual)
예제 #10
0
    def test__eq__(self):
        attr = AttrFactory.element()
        clone = attr.clone()

        self.assertIsNot(attr, clone)
        self.assertEqual(attr, clone)

        attr.default = "foo"
        self.assertEqual(attr, clone)

        attr.restrictions.length = 10
        self.assertEqual(attr, clone)

        attr.index = -1
        self.assertEqual(attr, clone)

        attr.namespace = __file__
        self.assertNotEqual(attr, clone)
예제 #11
0
    def test_build_class_complex_type(self):
        element = AnyElement(
            qname="{xsdata}root",
            children=[
                AnyElement(qname="{xsdata}child", text="primitive"),
                AnyElement(qname="{inner}child",
                           attributes={
                               "{foo}bar": "1",
                               "{bar}foo": "2"
                           }),
            ],
        )

        actual = ElementMapper.build_class(element, "target")
        expected = ClassFactory.create(
            tag=Tag.ELEMENT,
            qname="{target}root",
            namespace="xsdata",
            module="",
            ns_map={},
            attrs=[
                AttrFactory.native(
                    DataType.STRING,
                    tag=Tag.ELEMENT,
                    name="child",
                    namespace="xsdata",
                    index=0,
                ),
                AttrFactory.element(
                    name="child",
                    namespace="inner",
                    types=[
                        AttrTypeFactory.create(qname="{target}child",
                                               forward=True)
                    ],
                    index=1,
                ),
            ],
        )
        self.assertEqual(1, len(actual.inner))

        actual.inner.clear()
        self.assertEqual(expected, actual)
예제 #12
0
    def test_group_fields_limit_name(self):
        target = ClassFactory.create(attrs=AttrFactory.list(3))
        for attr in target.attrs:
            attr.restrictions.choice = "1"

        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))
        for attr in target.attrs:
            attr.restrictions.choice = "1"

        self.sanitizer.group_fields(target, list(target.attrs))
        self.assertEqual("choice", target.attrs[0].name)

        target = ClassFactory.create()
        attr = AttrFactory.element(restrictions=Restrictions(choice="1"))
        target.attrs.append(attr)
        target.attrs.append(attr.clone())
        self.sanitizer.group_fields(target, list(target.attrs))
        self.assertEqual("choice", target.attrs[0].name)
예제 #13
0
 def test_property_is_group(self):
     self.assertTrue(AttrFactory.group().is_group)
     self.assertTrue(AttrFactory.attribute_group().is_group)
     self.assertFalse(AttrFactory.element().is_group)
예제 #14
0
 def test_property_is_enumeration(self):
     self.assertTrue(AttrFactory.enumeration().is_enumeration)
     self.assertFalse(AttrFactory.element().is_enumeration)
예제 #15
0
 def test_property_is_attribute(self):
     self.assertTrue(AttrFactory.attribute().is_attribute)
     self.assertTrue(AttrFactory.any_attribute().is_attribute)
     self.assertFalse(AttrFactory.element().is_attribute)
예제 #16
0
    def test_process_attribute_restrictions(self):
        required = Restrictions(min_occurs=1, max_occurs=1)
        attr = AttrFactory.attribute(restrictions=required.clone())
        self.sanitizer.process_attribute_restrictions(attr)
        self.assertIsNone(attr.restrictions.min_occurs)
        self.assertIsNone(attr.restrictions.max_occurs)

        tokens = Restrictions(required=True,
                              tokens=True,
                              min_occurs=1,
                              max_occurs=1)
        attr = AttrFactory.element(restrictions=tokens.clone())
        self.sanitizer.process_attribute_restrictions(attr)
        self.assertFalse(attr.restrictions.required)
        self.assertIsNone(attr.restrictions.min_occurs)
        self.assertIsNone(attr.restrictions.max_occurs)

        attr = AttrFactory.element(restrictions=tokens.clone())
        attr.restrictions.max_occurs = 2
        self.sanitizer.process_attribute_restrictions(attr)
        self.assertFalse(attr.restrictions.required)
        self.assertIsNotNone(attr.restrictions.min_occurs)
        self.assertIsNotNone(attr.restrictions.max_occurs)

        multiple = Restrictions(min_occurs=0, max_occurs=2)
        attr = AttrFactory.create(tag=Tag.EXTENSION, restrictions=multiple)
        self.sanitizer.process_attribute_restrictions(attr)
        self.assertTrue(attr.restrictions.required)
        self.assertIsNone(attr.restrictions.min_occurs)
        self.assertIsNone(attr.restrictions.max_occurs)

        multiple = Restrictions(max_occurs=2, required=True)
        attr = AttrFactory.element(restrictions=multiple, fixed=True)
        self.sanitizer.process_attribute_restrictions(attr)
        self.assertIsNone(attr.restrictions.required)
        self.assertEqual(0, attr.restrictions.min_occurs)
        self.assertFalse(attr.fixed)

        attr = AttrFactory.element(restrictions=required.clone())
        self.sanitizer.process_attribute_restrictions(attr)
        self.assertTrue(attr.restrictions.required)
        self.assertIsNone(attr.restrictions.min_occurs)
        self.assertIsNone(attr.restrictions.max_occurs)

        restrictions = Restrictions(required=True, min_occurs=0, max_occurs=1)
        attr = AttrFactory.element(restrictions=restrictions,
                                   default="A",
                                   fixed=True)
        self.sanitizer.process_attribute_restrictions(attr)
        self.assertIsNone(attr.restrictions.required)
        self.assertIsNone(attr.restrictions.min_occurs)
        self.assertIsNone(attr.restrictions.max_occurs)
        self.assertIsNone(attr.default)
        self.assertFalse(attr.fixed)

        attr = AttrFactory.element(restrictions=required.clone(), default="A")
        self.sanitizer.process_attribute_restrictions(attr)
        self.assertIsNone(attr.restrictions.required)

        attr = AttrFactory.element(restrictions=required.clone(), fixed=True)
        self.sanitizer.process_attribute_restrictions(attr)
        self.assertIsNone(attr.restrictions.required)

        attr = AttrFactory.element(restrictions=required.clone())
        attr.restrictions.nillable = True
        self.sanitizer.process_attribute_restrictions(attr)
        self.assertIsNone(attr.restrictions.required)
예제 #17
0
 def test_field_metadata_mixed(self):
     attr = AttrFactory.element(mixed=True)
     expected = {"mixed": True, "name": "attr_B", "type": "Element"}
     self.assertEqual(expected,
                      self.filters.field_metadata(attr, "foo", ["cls"]))