Exemplo n.º 1
0
    def test_add_substitution_attrs(self, mock_find_attribute):
        target = ClassFactory.elements(2)
        mock_find_attribute.side_effect = [-1, 2]

        first_attr = target.attrs[0]
        second_attr = target.attrs[1]
        first_attr.restrictions.max_occurs = 2

        attr_name = first_attr.name
        attr_qname = target.source_qname(attr_name)
        reference_attrs = AttrFactory.list(2)

        self.analyzer.substitutions_index[attr_qname] = reference_attrs
        self.analyzer.add_substitution_attrs(target, first_attr)

        self.assertEqual(4, len(target.attrs))

        self.assertEqual(reference_attrs[0], target.attrs[0])
        self.assertIsNot(reference_attrs[0], target.attrs[0])
        self.assertEqual(reference_attrs[1], target.attrs[3])
        self.assertIsNot(reference_attrs[1], target.attrs[3])
        self.assertEqual(2, target.attrs[0].restrictions.max_occurs)
        self.assertEqual(2, target.attrs[3].restrictions.max_occurs)

        second_attr.wildcard = True
        self.analyzer.add_substitution_attrs(target, second_attr)
        self.assertEqual(4, len(target.attrs))

        self.analyzer.add_substitution_attrs(target, AttrFactory.enumeration())
        self.assertEqual(4, len(target.attrs))
Exemplo n.º 2
0
    def test_process_attribute_default_with_enumeration(self):
        target = ClassFactory.create()
        attr = AttrFactory.enumeration()
        attr.restrictions.max_occurs = 2
        attr.fixed = True

        self.sanitizer.process_attribute_default(target, attr)
        self.assertTrue(attr.fixed)
Exemplo n.º 3
0
    def test_process(self):
        target = ClassFactory.elements(2)
        self.processor.process(target)

        self.assertEqual(2, len(target.attrs))

        target.attrs.append(AttrFactory.enumeration())
        self.processor.process(target)
        self.assertEqual(1, len(target.attrs))
        self.assertTrue(target.attrs[0].is_enumeration)
Exemplo n.º 4
0
    def test_process(self):
        target = ClassFactory.elements(2)
        self.processor.process(target)

        self.assertEqual(2, len(target.attrs))
        self.assertEqual(0, len(target.inner))

        target = ClassFactory.enumeration(2)
        self.processor.process(target)

        self.assertEqual(2, len(target.attrs))
        self.assertEqual(0, len(target.inner))

        enumerations = list(target.attrs)
        target.attrs.append(AttrFactory.element())
        self.processor.process(target)

        self.assertEqual(1, len(target.attrs))
        self.assertEqual(1, len(target.inner))
        self.assertEqual(enumerations, target.inner[0].attrs)

        target.attrs.append(AttrFactory.enumeration())
        self.processor.process(target)
        self.assertEqual(1, len(target.attrs))
        self.assertEqual(1, len(target.inner))
        self.assertEqual(3, len(target.inner[0].attrs))

        target.attrs.append(AttrFactory.element())
        self.processor.process(target)
        self.assertEqual(2, len(target.attrs))
        self.assertEqual(1, len(target.inner))
        self.assertEqual(3, len(target.inner[0].attrs))

        target.attrs.append(AttrFactory.enumeration())

        with self.assertRaises(AnalyzerValueError) as cm:
            self.processor.process(target)

        self.assertEqual("Mixed enumeration with more than one normal field.",
                         str(cm.exception))
Exemplo n.º 5
0
    def test_process(self, mock_create_substitutions, mock_process_attribute):
        def init_substitutions():
            self.processor.substitutions = {}

        mock_create_substitutions.side_effect = init_substitutions

        target = ClassFactory.create(
            attrs=[AttrFactory.enumeration(), AttrFactory.any(), AttrFactory.element(),]
        )

        self.processor.process(target)
        self.processor.process(ClassFactory.create())
        mock_process_attribute.assert_called_once_with(target, target.attrs[2])
        mock_create_substitutions.assert_called_once()
Exemplo n.º 6
0
    def test_process(self):
        one = AttrFactory.attribute(fixed=True)
        one_clone = one.clone()
        restrictions = Restrictions(min_occurs=10, max_occurs=15)
        two = AttrFactory.element(restrictions=restrictions, fixed=True)
        two_clone = two.clone()
        two_clone.restrictions.min_occurs = 5
        two_clone.restrictions.max_occurs = 5
        two_clone_two = two.clone()
        two_clone_two.restrictions.min_occurs = 4
        two_clone_two.restrictions.max_occurs = 4
        three = AttrFactory.element()
        four = AttrFactory.enumeration()
        four_clone = four.clone()
        five = AttrFactory.element()
        five_clone = five.clone()
        five_clone_two = five.clone()

        target = ClassFactory.create(attrs=[
            one,
            one_clone,
            two,
            two_clone,
            two_clone_two,
            three,
            four,
            four_clone,
            five,
            five_clone,
            five_clone_two,
        ])

        winners = [one, two, three, four, five]

        self.processor.process(target)
        self.assertEqual(winners, target.attrs)

        self.assertTrue(one.fixed)
        self.assertIsNone(one.restrictions.min_occurs)
        self.assertIsNone(one.restrictions.max_occurs)
        self.assertFalse(two.fixed)
        self.assertEqual(4, two.restrictions.min_occurs)
        self.assertEqual(24, two.restrictions.max_occurs)
        self.assertIsNone(three.restrictions.min_occurs)
        self.assertIsNone(three.restrictions.max_occurs)
        self.assertIsNone(four.restrictions.min_occurs)
        self.assertIsNone(four.restrictions.max_occurs)
        self.assertEqual(0, five.restrictions.min_occurs)
        self.assertEqual(3, five.restrictions.max_occurs)
Exemplo n.º 7
0
    def test_process_dependency_extension_with_enum_type(
        self,
        mock_find_dependency,
        mock_process_simple_extension,
        mock_process_complex_extension,
    ):
        extension = ExtensionFactory.create()
        target = ClassFactory.create(extensions=[extension])
        source = ClassFactory.create(type=ComplexType)
        source.attrs.append(AttrFactory.enumeration())
        mock_find_dependency.return_value = source

        self.processor.process_extension(target, extension)
        self.assertEqual(0, mock_process_complex_extension.call_count)

        mock_process_simple_extension.assert_called_once_with(
            source, target, extension)
Exemplo n.º 8
0
    def test_process_attribute_name(self):
        attr = AttrFactory.create(name="a")

        self.sanitizer.process_attribute_name(attr)
        self.assertEqual("a", attr.name)

        attr.name = "1"
        self.sanitizer.process_attribute_name(attr)
        self.assertEqual("value_1", attr.name)

        attr.name = "foo_+-bar"
        self.sanitizer.process_attribute_name(attr)
        self.assertEqual("foo   bar", attr.name)

        attr.name = "+ -  *"
        self.sanitizer.process_attribute_name(attr)
        self.assertEqual("value", attr.name)

        attr = AttrFactory.enumeration(default="-20.55")
        self.sanitizer.process_attribute_name(attr)
        self.assertEqual("value_minus_-20.55", attr.name)
Exemplo n.º 9
0
 def test_property_is_enumeration(self):
     self.assertTrue(AttrFactory.enumeration().is_enumeration)
     self.assertFalse(AttrFactory.element().is_enumeration)