Example #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()
        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"),
        ])
Example #2
0
    def test_get_restrictions(self):
        self.assertEqual({}, Restriction().get_restrictions())

        obj = Restriction(
            min_exclusive=MinExclusive(value=1),
            min_inclusive=MinInclusive(value=2),
            min_length=MinLength(value=3),
            max_exclusive=MaxExclusive(value=4),
            max_inclusive=MaxInclusive(value=5),
            max_length=MaxLength(value=6),
            total_digits=TotalDigits(value=7),
            fraction_digits=FractionDigits(value=8),
            length=Length(value=9),
            white_space=WhiteSpace(value="collapse"),
            patterns=[Pattern(value="[0-9]"),
                      Pattern(value="[A-Z]")],
            enumerations=[Enumeration(value="str")],
        )
        expected = {
            "fraction_digits": 8,
            "length": 9,
            "max_exclusive": 4,
            "max_inclusive": 5,
            "max_length": 6,
            "min_exclusive": 1,
            "min_inclusive": 2,
            "min_length": 3,
            "pattern": "[0-9]|[A-Z]",
            "total_digits": 7,
            "white_space": "collapse",
        }

        self.assertEqual(expected, obj.get_restrictions())
Example #3
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))
Example #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)
Example #5
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)
Example #6
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)
Example #7
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))
Example #8
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")
Example #9
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)
Example #10
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))
Example #11
0
 def test_property_is_fixed(self):
     obj = Enumeration()
     self.assertTrue(obj.is_fixed)
Example #12
0
 def test_property_is_attribute(self):
     obj = Enumeration()
     self.assertTrue(obj.is_attribute)
Example #13
0
 def test_get_restrictions(self):
     obj = Enumeration()
     self.assertEqual({}, obj.get_restrictions())
Example #14
0
 def test_property_default(self):
     obj = Enumeration(value="foo")
     self.assertEqual("foo", obj.default)
Example #15
0
 def test_property_real_type(self):
     obj = Enumeration()
     self.assertEqual("", obj.real_type)
Example #16
0
 def test_property_real_name(self):
     obj = Enumeration(value="foo")
     self.assertEqual("foo", obj.real_name)
Example #17
0
 def test_property_real_type(self):
     obj = Enumeration()
     self.assertIsNone(obj.real_type)