Пример #1
0
    def test_get_restrictions(self):
        obj = SimpleType()
        self.assertEqual({}, obj.get_restrictions())

        expected = dict(length=2)
        obj.restriction = Restriction(length=Length(value=2))
        self.assertEqual(expected, obj.get_restrictions())
Пример #2
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"),
        ])
Пример #3
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))
Пример #4
0
    def test_property_is_enumeration(self):
        obj = SimpleType()
        self.assertFalse(obj.is_enumeration)

        obj.restriction = Restriction()
        self.assertFalse(obj.is_enumeration)

        obj.restriction.enumerations.append(Enumeration())
        self.assertTrue(obj.is_enumeration)
Пример #5
0
    def test_property_real_type(self):
        obj = Union()
        obj.member_types = "thug life"
        self.assertEqual(obj.member_types, obj.real_type)

        obj = Union(simple_types=[
            SimpleType(restriction=Restriction(base="foo")),
            SimpleType(restriction=Restriction(base="bar")),
        ])

        self.assertEqual("foo bar", obj.real_type)
Пример #6
0
    def test_property_attr_types(self):
        obj = Union()
        obj.member_types = "thug life"
        self.assertEqual(["thug", "life"], list(obj.attr_types))

        obj = Union(simple_types=[
            SimpleType(restriction=Restriction(base="foo")),
            SimpleType(restriction=Restriction(base="bar")),
        ])

        self.assertEqual(["foo", "bar"], list(obj.attr_types))
Пример #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_get_restrictions(self):
        first = Restriction(min_exclusive=MinExclusive(value="1"),
                            min_inclusive=MinInclusive(value="2"))
        second = Restriction(min_length=MinLength(value="3"),
                             max_exclusive=MaxExclusive(value="4"))
        obj = Union(simple_types=[
            SimpleType(restriction=first),
            SimpleType(restriction=second),
        ])

        expected = {
            "max_exclusive": "4",
            "min_exclusive": "1",
            "min_inclusive": "2",
            "min_length": "3",
        }
        self.assertEqual(expected, obj.get_restrictions())
Пример #9
0
    def test_get_restrictions(self):
        first = Restriction(min_exclusive=MinExclusive(value=1),
                            min_inclusive=MinInclusive(value=2))
        second = Restriction(min_length=MinLength(value=3),
                             max_exclusive=MaxExclusive(value=4))
        obj = Union(simple_types=[
            SimpleType(restriction=first),
            SimpleType(restriction=second),
        ])

        expected = {
            "max_exclusive": 4,
            "min_exclusive": 1,
            "min_inclusive": 2,
            "min_length": 3,
        }
        self.assertEqual(expected, obj.get_restrictions())
Пример #10
0
    def test_get_restrictions(self):
        obj = Attribute()
        self.assertEqual({}, obj.get_restrictions())

        obj.use = UseType.REQUIRED
        expected = {"max_occurs": 1, "min_occurs": 1, "required": True}
        self.assertEqual(expected, obj.get_restrictions())

        obj.simple_type = SimpleType(restriction=Restriction(length=Length(value=1)))
        expected.update(dict(length=1))
        self.assertEqual(expected, obj.get_restrictions())
Пример #11
0
    def test_property_real_type(self):
        obj = Restriction(base="foo")
        self.assertEqual(obj.base, obj.real_type)

        obj.enumerations.append(Enumeration())
        self.assertIsNone(obj.real_type)

        obj = Restriction(simple_type=SimpleType(restriction=Restriction(
            base="bar")))

        self.assertEqual("bar", obj.real_type)
Пример #12
0
    def test_get_restrictions_with_nested_simple_type(self):
        obj = Restriction(
            min_length=MinLength(value=2),
            simple_type=SimpleType(restriction=Restriction(
                max_length=MaxLength(value=10),
                min_length=MinLength(value=5),
            )),
        )

        expected = {"max_length": 10, "min_length": 2}
        self.assertEqual(expected, obj.get_restrictions())
Пример #13
0
    def test_build_inner_classes_with_enumeration(self, mock_build_class):
        inner = ClassFactory.enumeration(2)
        mock_build_class.return_value = inner

        enumeration = SimpleType(
            restriction=Restriction(enumerations=[Enumeration(value="a")])
        )

        result = self.builder.build_inner_classes(enumeration)
        self.assertIsInstance(result, Iterator)
        self.assertEqual([inner], list(result))
        self.assertIsNone(enumeration.name)
Пример #14
0
    def test_property_attr_types(self):
        obj = Restriction()
        self.assertEqual([], list(obj.attr_types))

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

        obj.enumerations.append(Enumeration())
        self.assertEqual([], list(obj.attr_types))

        obj = Restriction(simple_type=SimpleType(restriction=Restriction(base="bar")))

        self.assertEqual(["bar"], list(obj.attr_types))
Пример #15
0
    def test_property_real_type(self):
        obj = Attribute()
        self.assertEqual("", obj.real_type)

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

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

        obj.simple_type = SimpleType()
        self.assertEqual("", obj.real_type)

        obj.simple_type.restriction = Restriction(base="thug")
        self.assertEqual(obj.simple_type.restriction.base, obj.real_type)
Пример #16
0
    def test_build_inner_classes_with_enumeration(self, mock_build_class):
        inner = ClassFactory.enumeration(2)
        mock_build_class.return_value = inner

        enumeration = SimpleType(restriction=Restriction(
            enumerations=[Enumeration(value="a")]))

        result = SchemaMapper.build_inner_classes(enumeration, "module",
                                                  "target_ns")
        self.assertIsInstance(result, Iterator)
        self.assertEqual([inner], list(result))
        self.assertIsNone(enumeration.name)

        mock_build_class.assert_called_once_with(enumeration, Tag.SIMPLE_TYPE,
                                                 "module", "target_ns")
Пример #17
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),
            ]
        )
Пример #18
0
    def test_get_restrictions(self):
        obj = Element(min_occurs=1, max_occurs=1)
        expected = {"min_occurs": 1, "max_occurs": 1}
        self.assertEqual(expected, obj.get_restrictions())

        obj.simple_type = SimpleType(restriction=Restriction(length=Length(
            value=9)))
        expected.update({"length": 9})
        self.assertEqual(expected, obj.get_restrictions())

        obj.nillable = False
        self.assertEqual(expected, obj.get_restrictions())

        obj.nillable = True
        expected.update({"nillable": True})
        self.assertEqual(expected, obj.get_restrictions())
Пример #19
0
    def test_get_restrictions(self):
        obj = Attribute()
        self.assertEqual({}, obj.get_restrictions())

        obj.use = UseType.REQUIRED
        expected = {"required": True}
        self.assertEqual(expected, obj.get_restrictions())

        obj.use = UseType.PROHIBITED
        expected = {"prohibited": True}
        self.assertEqual(expected, obj.get_restrictions())

        obj.simple_type = SimpleType(restriction=Restriction(length=Length(
            value=1)))
        expected["length"] = 1
        self.assertEqual(expected, obj.get_restrictions())
Пример #20
0
    def test_property_attr_types(self):
        obj = Attribute()
        self.assertEqual([], 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.simple_type = SimpleType()
        self.assertEqual([], list(obj.attr_types))

        obj.simple_type.restriction = Restriction(base="thug")
        self.assertEqual([obj.simple_type.restriction.base],
                         list(obj.attr_types))
Пример #21
0
    def test_property_real_type(self):
        obj = SimpleType()
        self.assertIsNone(obj.real_type)

        obj.union = Union(member_types="thug")
        self.assertEqual("thug", obj.real_type)

        obj.list = List(item_type="foo")
        self.assertEqual("foo", obj.real_type)

        obj.restriction = Restriction(base="bar")
        self.assertEqual("bar", obj.real_type)

        obj = SimpleType(restriction=Restriction())
        obj.restriction.enumerations.append(Enumeration())
        self.assertIsNone(obj.real_type)
Пример #22
0
    def test_property_attr_types(self):
        obj = SimpleType()
        self.assertEqual([], list(obj.attr_types))

        obj.union = Union(member_types="thug")
        self.assertEqual(["thug"], list(obj.attr_types))

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

        obj.restriction = Restriction(base="bar")
        self.assertEqual(["bar"], list(obj.attr_types))

        obj = SimpleType(restriction=Restriction())
        obj.restriction.enumerations.append(Enumeration())
        self.assertEqual([], list(obj.attr_types))
Пример #23
0
    def test_get_restrictions(self):
        obj = Element(min_occurs=1, max_occurs=1)
        expected = {"min_occurs": 1, "max_occurs": 1}
        self.assertEqual(expected, obj.get_restrictions())

        obj.simple_type = SimpleType(restriction=Restriction(length=Length(value=9)))
        expected["length"] = 9
        self.assertEqual(expected, obj.get_restrictions())

        obj.nillable = False
        self.assertEqual(expected, obj.get_restrictions())

        obj.nillable = True
        expected["nillable"] = True
        self.assertEqual(expected, obj.get_restrictions())

        obj.type = "NMTOKENS"
        expected["tokens"] = True
        self.assertEqual(expected, obj.get_restrictions())
Пример #24
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)
Пример #25
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))
Пример #26
0
    def test_property_real_name(self):
        obj = SimpleType()
        self.assertEqual("value", obj.real_name)

        obj.name = "foo"
        self.assertEqual("foo", obj.real_name)
Пример #27
0
 def test_property_extensions(self):
     obj = SimpleType()
     self.assertEqual([], list(obj.extensions))
Пример #28
0
 def test_property_is_attribute(self):
     obj = SimpleType()
     self.assertTrue(obj.is_attribute)