Exemplo n.º 1
0
    def test_end_complex_type(self):
        complex_type = ComplexType()
        not_complex_type = Element()
        element = etree.Element("uno")

        self.parser.end_complex_type(not_complex_type, element)
        self.parser.end_complex_type(complex_type, element)

        self.assertEqual(0, len(complex_type.attribute_groups))
        self.assertIsNone(complex_type.open_content)

        self.parser.default_attributes = "tns:attrs"
        self.parser.end_complex_type(complex_type, element)

        expected = AttributeGroup.create(ref="tns:attrs")
        self.assertEqual([expected], complex_type.attribute_groups)
        self.assertIsNone(complex_type.open_content)

        default_open_content = DefaultOpenContent()
        self.parser.default_attributes = None
        self.parser.default_open_content = default_open_content
        self.parser.end_complex_type(complex_type, element)
        self.assertIs(default_open_content, complex_type.open_content)

        open_content = OpenContent()
        complex_type.open_content = open_content
        self.parser.end_complex_type(complex_type, element)
        self.assertIs(open_content, complex_type.open_content)
Exemplo n.º 2
0
    def test_build(
        self, mock_build_class, mock_override_children, mock_redefine_children
    ):

        for _ in range(2):
            self.schema.simple_types.append(SimpleType.create())
            self.schema.attribute_groups.append(AttributeGroup.create())
            self.schema.groups.append(Group.create())
            self.schema.attributes.append(Attribute.create())
            self.schema.complex_types.append(ComplexType.create())
            self.schema.elements.append(Element.create())
            self.schema.redefines.append(Redefine.create())
            self.schema.overrides.append(Override.create())

        override_element = Element.create()
        override_attribute = Attribute.create()
        override_complex_type = ComplexType.create()
        redefine_simple_type = SimpleType.create()
        redefine_group = Group.create()
        redefine_attribute_group = AttributeGroup.create()
        mock_redefine_children.side_effect = [
            [redefine_simple_type, redefine_group],
            [redefine_attribute_group],
        ]

        mock_override_children.side_effect = [
            [override_element, override_attribute],
            [override_complex_type],
        ]
        mock_build_class.side_effect = classes = ClassFactory.list(18)

        self.assertEqual(classes, self.builder.build())
        mock_build_class.assert_has_calls(
            [
                mock.call(override_element),
                mock.call(override_attribute),
                mock.call(override_complex_type),
                mock.call(redefine_simple_type),
                mock.call(redefine_group),
                mock.call(redefine_attribute_group),
                mock.call(self.schema.simple_types[0]),
                mock.call(self.schema.simple_types[1]),
                mock.call(self.schema.attribute_groups[0]),
                mock.call(self.schema.attribute_groups[1]),
                mock.call(self.schema.groups[0]),
                mock.call(self.schema.groups[1]),
                mock.call(self.schema.attributes[0]),
                mock.call(self.schema.attributes[1]),
                mock.call(self.schema.complex_types[0]),
                mock.call(self.schema.complex_types[1]),
                mock.call(self.schema.elements[0]),
                mock.call(self.schema.elements[1]),
            ]
        )
Exemplo n.º 3
0
    def test_build_inner_classes(self, mock_build_class):
        inner_classes = ClassFactory.list(2)
        mock_build_class.side_effect = inner_classes

        simple_type = SimpleType.create()
        complex_type = ComplexType.create()
        enumeration = SimpleType.create(
            restriction=Restriction.create(enumerations=[Enumeration.create(value="a")])
        )

        element = Element.create(
            alternatives=[
                Alternative.create(complex_type=complex_type, id="a"),
                Alternative.create(simple_type=simple_type, id="b"),
                Alternative.create(simple_type=enumeration, id="c"),
            ]
        )
        result = self.builder.build_inner_classes(element)
        self.assertIsInstance(result, Iterator)
        self.assertEqual(inner_classes, list(result))
        self.assertEqual("a", complex_type.name)
        self.assertEqual("c", enumeration.name)

        mock_build_class.assert_has_calls(
            [mock.call(complex_type), mock.call(enumeration)]
        )
Exemplo n.º 4
0
    def test_children_extensions(self):
        complex_type = ComplexType.create(
            attributes=[Attribute.create(index=i) for i in range(2)],
            simple_content=SimpleContent.create(
                restriction=Restriction.create(base="bk:b", index=4)
            ),
            complex_content=ComplexContent.create(
                extension=Extension.create(base="bk:ext", index=7)
            ),
        )

        item = ClassFactory.create()
        children = self.builder.children_extensions(complex_type, item)
        expected = list(
            map(
                ExtensionFactory.create,
                [
                    AttrTypeFactory.create(name="bk:b", index=4),
                    AttrTypeFactory.create(name="bk:ext", index=7),
                ],
            )
        )

        self.assertIsInstance(children, GeneratorType)
        self.assertEqual(expected, list(children))
Exemplo n.º 5
0
    def test_element_children(self):
        sequence_one = Sequence.create(elements=[Element.create(), Element.create()])
        sequence_two = Sequence.create(
            max_occurs=2, elements=[Element.create(), Element.create()]
        )
        restriction = Restriction.create(
            enumerations=[Enumeration.create(value=x) for x in "abc"],
            sequence=sequence_two,
        )
        complex_type = ComplexType.create(
            attributes=[Attribute.create(), Attribute.create()],
            sequence=sequence_one,
            simple_content=SimpleContent.create(restriction=Restriction.create()),
            complex_content=ComplexContent.create(restriction=restriction,),
        )

        children = self.builder.element_children(complex_type)
        expected = [
            (sequence_two.elements[0], Restrictions.from_element(sequence_two)),
            (sequence_two.elements[1], Restrictions.from_element(sequence_two)),
            (restriction.enumerations[0], Restrictions.from_element(restriction)),
            (restriction.enumerations[1], Restrictions.from_element(restriction)),
            (restriction.enumerations[2], Restrictions.from_element(restriction)),
            (sequence_one.elements[0], Restrictions.from_element(sequence_one)),
            (sequence_one.elements[1], Restrictions.from_element(sequence_one)),
            (complex_type.attributes[0], Restrictions.from_element(complex_type)),
            (complex_type.attributes[1], Restrictions.from_element(complex_type)),
        ]
        self.assertIsInstance(children, GeneratorType)
        self.assertEqual(expected, list(children))
Exemplo n.º 6
0
    def test_property_has_children(self):
        element = ElementBase()
        self.assertFalse(element.has_children)

        element = Element.create()
        self.assertFalse(element.has_children)

        element.complex_type = ComplexType.create()
        self.assertTrue(element.has_children)
Exemplo n.º 7
0
    def test_property_is_mixed(self):
        obj = Element.create()
        self.assertFalse(obj.is_mixed)

        obj.complex_type = ComplexType.create()
        self.assertFalse(obj.is_mixed)

        obj.complex_type.mixed = True
        self.assertTrue(obj.is_mixed)
Exemplo n.º 8
0
    def test_property_raw_type(self):
        obj = Element.create()
        self.assertEqual("xs:anyType", obj.raw_type)

        obj.type = "foo"
        self.assertEqual("foo", obj.raw_type)

        obj.type = None
        obj.complex_type = ComplexType.create()
        self.assertIsNone(obj.raw_type)
Exemplo n.º 9
0
    def test_has_anonymous_class(self):
        obj = Element.create()
        self.assertFalse(self.builder.has_anonymous_class(obj))

        obj = Element.create(type="foo")
        self.assertFalse(self.builder.has_anonymous_class(obj))

        obj = Element.create(complex_type=ComplexType.create())
        self.assertTrue(self.builder.has_anonymous_class(obj))

        obj = Attribute.create()
        self.assertFalse(self.builder.has_anonymous_class(obj))
Exemplo n.º 10
0
    def test_build_inner_class_when_has_anonymous_class(
            self, mock_has_anonymous_class, mock_build_class):
        inner_class = ClassFactory.create()
        mock_build_class.return_value = inner_class
        mock_has_anonymous_class.return_value = True

        complex_type = ComplexType.create()
        element = Element.create(name="foo", complex_type=complex_type)

        self.assertEqual(inner_class, self.builder.build_inner_class(element))
        self.assertIsNone(element.complex_type)
        self.assertEqual("foo", complex_type.name)
Exemplo n.º 11
0
    def test_property_is_mixed(self):
        obj = ComplexType.create()

        self.assertFalse(obj.is_mixed)

        obj.complex_content = ComplexContent.create()
        self.assertFalse(obj.is_mixed)

        obj.complex_content.mixed = True
        self.assertTrue(obj.is_mixed)

        obj.complex_content.mixed = False
        obj.mixed = True
        self.assertTrue(obj.is_mixed)
Exemplo n.º 12
0
    def test_property_real_type(self):
        obj = Element.create()
        self.assertIsNone(obj.real_type)

        # Inner classes depend on the this to be None
        obj.complex_type = ComplexType.create()
        self.assertIsNone(obj.real_type)

        restriction = Restriction.create(base="xs:int")
        obj.simple_type = SimpleType.create(restriction=restriction)
        self.assertEqual(restriction.base, obj.real_type)

        obj.ref = "foo"
        self.assertEqual(obj.ref, obj.real_type)

        obj.type = "bar"
        self.assertEqual(obj.type, obj.real_type)

        obj.alternatives.append(Alternative.create(type="foo"))
        obj.alternatives.append(Alternative.create(type="bar"))
        obj.alternatives.append(Alternative.create(type="thug"))
        self.assertEqual("bar foo thug", obj.real_type)
Exemplo n.º 13
0
 def test_property_extends(self):
     obj = ComplexType.create()
     self.assertIsNone(obj.extends)