예제 #1
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))
예제 #2
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(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)
예제 #3
0
    def test_property_is_mixed(self):
        obj = ComplexType()

        self.assertFalse(obj.is_mixed)

        obj.complex_content = ComplexContent()
        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)
예제 #4
0
 def test_element_children(self):
     sequence_one = Sequence(elements=[Element(), Element()])
     sequence_two = Sequence(max_occurs=2, elements=[Element(), Element()])
     restriction = Restriction(
         enumerations=[Enumeration(value=x) for x in "abc"], sequence=sequence_two,
     )
     complex_type = ComplexType(
         attributes=[Attribute(), Attribute()],
         sequence=sequence_one,
         simple_content=SimpleContent(restriction=Restriction()),
         complex_content=ComplexContent(restriction=restriction,),
     )
     restrictions = Restrictions.from_element(complex_type)
     children = self.builder.element_children(complex_type, restrictions)
     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))
예제 #5
0
    def test_end_schema(
        self,
        mock_set_schema_forms,
        mock_set_schema_namespaces,
        mock_add_default_imports,
        mock_resolve_schemas_locations,
    ):
        schema = Schema()
        schema.elements.append(Element())
        schema.elements.append(Element())
        schema.elements.append(Element())

        for el in schema.elements:
            self.assertEqual(1, el.min_occurs)
            self.assertEqual(1, el.max_occurs)

        self.parser.end_schema(schema)

        for el in schema.elements:
            self.assertIsNone(el.min_occurs)
            self.assertIsNone(el.max_occurs)

        self.parser.end_schema(ComplexType())

        mock_set_schema_forms.assert_called_once_with(schema)
        mock_set_schema_namespaces.assert_called_once_with(schema)
        mock_add_default_imports.assert_called_once_with(schema)
        mock_resolve_schemas_locations.assert_called_once_with(schema)
예제 #6
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()
        complex_type = ComplexType()
        enumeration = SimpleType(restriction=Restriction(
            enumerations=[Enumeration(value="a")]))

        element = Element(alternatives=[
            Alternative(complex_type=complex_type, id="a"),
            Alternative(simple_type=simple_type, id="b"),
            Alternative(simple_type=enumeration, id="c"),
        ])
        result = SchemaMapper.build_inner_classes(element, "module",
                                                  "target_ns")
        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, Tag.ALTERNATIVE, "module", "target_ns"),
            mock.call(enumeration, Tag.ALTERNATIVE, "module", "target_ns"),
        ])
예제 #7
0
    def test_children(self):
        one = SimpleType(id="1")
        two = ComplexType(id="10")
        three = Alternative(id="11")
        four = Alternative(id="12")

        element = Element(
            name="super",
            complex_type=two,
            simple_type=one,
            alternatives=[three, four],
        )

        children = element.children()

        self.assertIsInstance(children, Generator)
        self.assertEqual([one, two, three, four], list(children))

        children = list(element.children(lambda x: x.id == "10"))
        self.assertEqual([two], children)

        children = list(element.children(lambda x: x.id == "12"))
        self.assertEqual([four], children)

        children = list(element.children(lambda x: int(x.id) % 2 == 0))
        self.assertEqual([two, four], children)
예제 #8
0
    def test_map(self, mock_root_elements, mock_build_class):
        simple_type = ComplexType()
        complex_type = ComplexType()
        schema = Schema(target_namespace="fooNS", location="foo.xsd")

        mock_build_class.side_effect = ClassFactory.list(3)
        mock_root_elements.return_value = [
            (Tag.SCHEMA, Group),
            (Tag.OVERRIDE, simple_type),
            (Tag.REDEFINE, complex_type),
        ]

        actual = SchemaMapper.map(schema)
        self.assertEqual(3, len(actual))
        self.assertIsInstance(actual[0], Class)

        mock_root_elements.assert_called_once_with(schema)
예제 #9
0
    def test_property_has_children(self):
        element = ElementBase()
        self.assertFalse(element.has_children)

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

        element.complex_type = ComplexType()
        self.assertTrue(element.has_children)
예제 #10
0
    def test_property_is_mixed(self):
        obj = Element()
        self.assertFalse(obj.is_mixed)

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

        obj.complex_type.mixed = True
        self.assertTrue(obj.is_mixed)
예제 #11
0
    def test_property_raw_type(self):
        obj = Element(ns_map={"xs": Namespace.XS.uri})
        self.assertEqual("xs:anyType", obj.raw_type)

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

        obj.type = None
        obj.complex_type = ComplexType()
        self.assertIsNone(obj.raw_type)
예제 #12
0
    def test_property_bases(self):
        obj = Element()
        obj.ns_map["xs"] = Namespace.XS.uri
        self.assertEqual(["xs:anyType"], list(obj.bases))

        obj.type = "foo"
        self.assertEqual(["foo"], list(obj.bases))

        obj.type = None
        obj.complex_type = ComplexType()
        self.assertEqual([], list(obj.bases))
예제 #13
0
    def test_element_children_with_parents_restrictions(self):
        choice = Choice(elements=[Element(name="elem1")])
        complex_type = ComplexType(
            sequence=Sequence(choices=[choice], min_occurs=0, max_occurs=3))
        parent_restrictions = Restrictions.from_element(complex_type)
        children = SchemaMapper.element_children(complex_type,
                                                 parent_restrictions)

        child, restrictions = next(children)
        expected = Restrictions(min_occurs=0,
                                max_occurs=3,
                                sequential=True,
                                choice=str(id(choice)))
        self.assertEqual(expected, restrictions)
예제 #14
0
    def test_end_default_open_content(self):
        default_open_content = DefaultOpenContent()
        default_open_content.any = Any()

        self.parser.end_default_open_content(default_open_content)
        self.assertEqual(default_open_content, self.parser.default_open_content)
        self.assertEqual(0, default_open_content.any.index)

        default_open_content.mode = Mode.SUFFIX
        self.parser.end_default_open_content(default_open_content)
        self.assertEqual(sys.maxsize, default_open_content.any.index)

        obj = ComplexType()
        self.parser.end_default_open_content(obj)
        self.assertIsNone(obj.open_content)
예제 #15
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),
            ]
        )
예제 #16
0
    def test_end_extension(self):
        extension = Extension()
        not_extension = Element()

        self.parser.end_extension(not_extension)
        self.parser.end_extension(extension)

        default_open_content = DefaultOpenContent()
        self.parser.default_open_content = default_open_content
        self.parser.end_extension(extension)

        self.assertIs(default_open_content, extension.open_content)

        open_content = OpenContent()
        extension.open_content = open_content
        self.parser.end_extension(extension)
        self.assertIs(open_content, extension.open_content)

        obj = ComplexType()
        self.parser.end_extension(obj)
        self.assertIsNone(obj.open_content)
예제 #17
0
    def test_end_restriction(self):
        restriction = Restriction()
        not_restriction = Element()

        self.parser.end_restriction(not_restriction)
        self.parser.end_restriction(restriction)

        default_open_content = DefaultOpenContent()
        self.parser.default_open_content = default_open_content
        self.parser.end_restriction(restriction)

        self.assertIs(default_open_content, restriction.open_content)

        open_content = OpenContent()
        restriction.open_content = open_content
        self.parser.end_restriction(restriction)
        self.assertIs(open_content, restriction.open_content)

        obj = ComplexType()
        self.parser.end_open_content(obj)
        self.assertIsNone(obj.open_content)
예제 #18
0
    def test_children_extensions(self):
        complex_type = ComplexType(
            attributes=[Attribute(index=i) for i in range(2)],
            simple_content=SimpleContent(restriction=Restriction(base="bk:b", index=4)),
            complex_content=ComplexContent(extension=Extension(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))
예제 #19
0
    def test_property_attr_types(self):
        obj = Element()
        self.assertEqual([], list(obj.attr_types))

        # Inner classes depend on the this to be None
        obj.complex_type = ComplexType()
        self.assertEqual([], list(obj.attr_types))

        restriction = Restriction(base="xs:int")
        obj.simple_type = SimpleType(restriction=restriction)
        self.assertEqual([restriction.base], list(obj.attr_types))

        obj.ref = "foo"
        self.assertEqual([obj.ref], list(obj.attr_types))

        obj.type = "bar"
        self.assertEqual([obj.type], list(obj.attr_types))

        obj.alternatives.append(Alternative(type="foo"))
        obj.alternatives.append(Alternative(type="bar"))
        obj.alternatives.append(Alternative(type="thug"))
        self.assertEqual(["bar", "foo", "bar", "thug"], list(obj.attr_types))
예제 #20
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))
예제 #21
0
    def test_property_real_type(self):
        obj = Element()
        self.assertEqual("", obj.real_type)

        # Inner classes depend on the this to be None
        obj.complex_type = ComplexType()
        self.assertEqual("", obj.real_type)

        restriction = Restriction(base="xs:int")
        obj.simple_type = SimpleType(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(type="foo"))
        obj.alternatives.append(Alternative(type="bar"))
        obj.alternatives.append(Alternative(type="thug"))
        self.assertEqual("bar foo thug", obj.real_type)
예제 #22
0
 def test_property_extensions(self):
     obj = ComplexType()
     self.assertEqual([], list(obj.extensions))