Exemple #1
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)]
        )
Exemple #2
0
    def test_property_real_name(self):
        obj = SimpleType.create(name="foo")
        self.assertEqual("foo", obj.real_name)

        with self.assertRaises(SchemaValueError):
            obj = SimpleType.create()
            self.assertFalse(hasattr(obj, "ref"))
            obj.real_name
Exemple #3
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]),
            ]
        )
Exemple #4
0
    def test_property_real_type(self):
        obj = Union.create()
        obj.member_types = "thug life"
        self.assertEqual(obj.member_types, obj.real_type)

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

        self.assertEqual("foo bar", obj.real_type)
Exemple #5
0
    def test_get_restrictions(self):
        obj = SimpleType.create()
        self.assertEqual({}, obj.get_restrictions())

        expected = dict(length=2)
        obj.restriction = Restriction.create(length=Length.create(value=2))
        self.assertEqual(expected, obj.get_restrictions())
Exemple #6
0
    def test_property_real_type(self):
        obj = SimpleType.create()
        self.assertIsNone(obj.real_type)

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

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

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

        obj = SimpleType.create(restriction=Restriction.create())
        obj.restriction.enumerations.append(Enumeration.create())
        self.assertIsNone(obj.real_type)
Exemple #7
0
    def test_property_is_enumeration(self):
        obj = SimpleType.create()
        self.assertFalse(obj.is_enumeration)

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

        obj.restriction.enumerations.append(Enumeration.create())
        self.assertTrue(obj.is_enumeration)
Exemple #8
0
    def test_build_inner_class_when_union_has_anonymous_enumeration(
            self, mock_has_anonymous_class, mock_has_anonymous_enumeration,
            mock_build_class):
        inner_class = ClassFactory.create()
        mock_build_class.return_value = inner_class
        mock_has_anonymous_class.return_value = False
        mock_has_anonymous_enumeration.return_value = False

        union = Union.create(name="foo", type="xs:int")
        simple_type_a = SimpleType.create(restriction=Restriction.create(
            enumerations=[Enumeration.create(value="a")]))
        simple_type_b = SimpleType.create(restriction=Restriction.create(
            enumerations=[Enumeration.create(value="b")]))
        union.simple_types = [simple_type_a, simple_type_b]

        self.assertEqual(inner_class, self.builder.build_inner_class(union))
        self.assertEqual([simple_type_a], union.simple_types)
        mock_build_class.assert_called_once_with(simple_type_b)
Exemple #9
0
    def test_property_real_type(self):
        obj = Restriction.create(base="foo")
        self.assertEqual(obj.base, obj.real_type)

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

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

        self.assertEqual("bar", obj.real_type)
Exemple #10
0
    def test_get_restrictions_with_nested_simple_type(self):
        obj = Restriction.create(
            min_length=MinLength.create(value=2),
            simple_type=SimpleType.create(restriction=Restriction.create(
                max_length=MaxLength.create(value=10),
                min_length=MinLength.create(value=5),
            )),
        )

        expected = {"max_length": 10, "min_length": 2}
        self.assertEqual(expected, obj.get_restrictions())
Exemple #11
0
    def test_get_restrictions(self):
        obj = Attribute.create()
        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.create(restriction=Restriction.create(
            length=Length.create(value=1)))
        expected.update(dict(length=1))
        self.assertEqual(expected, obj.get_restrictions())
Exemple #12
0
    def test_build_inner_classes_with_enumeration(self, mock_build_class):
        inner = ClassFactory.enumeration(2)
        mock_build_class.return_value = inner

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

        result = self.builder.build_inner_classes(enumeration)
        self.assertIsInstance(result, Iterator)
        self.assertEqual([inner], list(result))
        self.assertIsNone(enumeration.name)
Exemple #13
0
    def test_get_restrictions(self):
        first = Restriction.create(
            min_exclusive=MinExclusive.create(value=1),
            min_inclusive=MinInclusive.create(value=2),
        )
        second = Restriction.create(
            min_length=MinLength.create(value=3),
            max_exclusive=MaxExclusive.create(value=4),
        )
        obj = Union.create(simple_types=[
            SimpleType.create(restriction=first),
            SimpleType.create(restriction=second),
        ])

        expected = {
            "max_exclusive": 4,
            "min_exclusive": 1,
            "min_inclusive": 2,
            "min_length": 3,
        }
        self.assertEqual(expected, obj.get_restrictions())
Exemple #14
0
    def test_build_inner_class_when_is_anonymous_enumeration(
            self, mock_has_anonymous_class, mock_has_anonymous_enumeration,
            mock_build_class):
        inner_class = ClassFactory.create()
        mock_build_class.return_value = inner_class
        mock_has_anonymous_class.return_value = False
        mock_has_anonymous_enumeration.return_value = False

        simple_type = SimpleType.create(restriction=Restriction.create())
        simple_type.restriction.enumerations.append(Enumeration.create())

        self.assertEqual(inner_class,
                         self.builder.build_inner_class(simple_type))
        self.assertEqual("value", simple_type.name)
        self.assertIsNone(simple_type.restriction)
Exemple #15
0
    def test_property_real_type(self):
        obj = Attribute.create()
        self.assertIsNone(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.create()
        self.assertIsNone(obj.real_type)

        obj.simple_type.restriction = Restriction.create(base="thug")
        self.assertEqual(obj.simple_type.restriction.base, obj.real_type)
Exemple #16
0
    def test_get_restrictions(self):
        obj = Element.create(min_occurs=1, max_occurs=1)
        expected = {"min_occurs": 1, "max_occurs": 1}
        self.assertEqual(expected, obj.get_restrictions())

        obj.simple_type = SimpleType.create(
            restriction=Restriction.create(length=Length.create(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())
Exemple #17
0
    def test_has_anonymous_enumeration(self):
        for clazz in [Element, Attribute]:
            obj = clazz.create()
            self.assertFalse(self.builder.has_anonymous_enumeration(obj))

            obj = clazz.create(simple_type=SimpleType.create())
            self.assertFalse(self.builder.has_anonymous_enumeration(obj))

            obj.simple_type.restriction = Restriction.create()
            self.assertFalse(self.builder.has_anonymous_enumeration(obj))

            obj.simple_type.restriction.enumerations.append(
                Enumeration.create())
            self.assertTrue(self.builder.has_anonymous_enumeration(obj))

            obj.type = "foo"
            self.assertFalse(self.builder.has_anonymous_enumeration(obj))
Exemple #18
0
    def test_build_inner_class_when_has_anonymous_enumeration(
            self, mock_has_anonymous_class, mock_has_anonymous_enumeration,
            mock_build_class):
        inner_class = ClassFactory.create()
        mock_build_class.return_value = inner_class
        mock_has_anonymous_class.return_value = False
        mock_has_anonymous_enumeration.return_value = True

        simple_type = SimpleType.create()
        element = Element.create(name="foo",
                                 simple_type=simple_type,
                                 type="xs:int")

        self.assertEqual(inner_class, self.builder.build_inner_class(element))
        self.assertIsNone(element.simple_type)
        self.assertIsNone(element.type)
        self.assertEqual("foo", simple_type.name)
Exemple #19
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)
Exemple #20
0
 def test_property_extends(self):
     obj = SimpleType.create()
     self.assertIsNone(obj.extends)
Exemple #21
0
    def test_property_real_name(self):
        obj = SimpleType.create()
        self.assertEqual("value", obj.real_name)

        obj.name = "foo"
        self.assertEqual("foo", obj.real_name)
Exemple #22
0
 def test_property_is_attribute(self):
     obj = SimpleType.create()
     self.assertTrue(obj.is_attribute)