示例#1
0
    def test_map_qname(self):
        namespaces = dict(xs=XSD_NAMESPACE, xsi=XSI_NAMESPACE)
        mapper = NamespaceMapper(namespaces)

        mapper.insert_item('', XSD_NAMESPACE)
        self.assertEqual(mapper.map_qname(''), '')
        self.assertEqual(mapper.map_qname('{%s}element' % XSD_NAMESPACE),
                         'xs:element')
        mapper.pop('xs')
        self.assertEqual(mapper.map_qname('{%s}element' % XSD_NAMESPACE),
                         'element')

        with self.assertRaises(ValueError) as ctx:
            mapper.map_qname('{%selement' % XSD_NAMESPACE)
        self.assertIn("wrong format", str(ctx.exception))

        with self.assertRaises(ValueError) as ctx:
            mapper.map_qname('{%s}element}' % XSD_NAMESPACE)
        self.assertIn("wrong format", str(ctx.exception))

        with self.assertRaises(TypeError) as ctx:
            mapper.map_qname(None)
        self.assertIn("must be a string-like object", str(ctx.exception))

        with self.assertRaises(TypeError) as ctx:
            mapper.map_qname(99)
        self.assertIn("must be a string-like object", str(ctx.exception))
示例#2
0
    def test_insert_item(self):
        namespaces = dict(xs=XSD_NAMESPACE, xsi=XSI_NAMESPACE)
        mapper = NamespaceMapper(namespaces)

        mapper.insert_item('xs', XSD_NAMESPACE)
        self.assertEqual(len(mapper), 2)

        mapper.insert_item('', XSD_NAMESPACE)
        self.assertEqual(len(mapper), 3)
        mapper.insert_item('', XSD_NAMESPACE)
        self.assertEqual(len(mapper), 3)
        mapper.insert_item('', 'tns0')
        self.assertEqual(len(mapper), 4)

        mapper.insert_item('xs', XSD_NAMESPACE)
        self.assertEqual(len(mapper), 4)
        mapper.insert_item('xs', 'tns1')
        self.assertEqual(len(mapper), 5)
        mapper.insert_item('xs', 'tns2')
        self.assertEqual(len(mapper), 6)