def test_dictionary_methods(self): namespaces = dict(xs=XSD_NAMESPACE) mapper = NamespaceMapper(namespaces) mapper['xsi'] = XSI_NAMESPACE self.assertEqual(mapper, dict(xs=XSD_NAMESPACE, xsi=XSI_NAMESPACE)) del mapper['xs'] self.assertEqual(len(mapper), 1) self.assertEqual(mapper, dict(xsi=XSI_NAMESPACE)) mapper.clear() self.assertEqual(mapper, {})
def test_default_namespace(self): namespaces = dict(xs=XSD_NAMESPACE, xsi=XSI_NAMESPACE) mapper = NamespaceMapper(namespaces) self.assertIsNone(mapper.default_namespace) mapper[''] = 'tns0' self.assertEqual(mapper.default_namespace, 'tns0')
def test_strip_namespaces(self): namespaces = dict(xs=XSD_NAMESPACE, xsi=XSI_NAMESPACE) mapper = NamespaceMapper(namespaces, strip_namespaces=True) self.assertEqual(mapper.map_qname('{%s}name' % XSD_NAMESPACE), 'name') self.assertEqual(mapper.map_qname('{unknown}name'), 'name') mapper.strip_namespaces = False self.assertEqual(mapper.map_qname('{%s}name' % XSD_NAMESPACE), 'xs:name') self.assertEqual(mapper.map_qname('{unknown}name'), '{unknown}name')
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)
def test_init(self): namespaces = dict(xs=XSD_NAMESPACE, xsi=XSI_NAMESPACE) mapper = NamespaceMapper(namespaces) self.assertEqual(mapper, namespaces)
def test_transfer(self): mapper = NamespaceMapper(namespaces={ 'xs': XSD_NAMESPACE, 'xsi': XSI_NAMESPACE }) namespaces = {'xs': 'foo'} mapper.transfer(namespaces) self.assertEqual(len(mapper), 2) self.assertEqual(len(namespaces), 1) namespaces = {'xs': XSD_NAMESPACE} mapper.transfer(namespaces) self.assertEqual(len(mapper), 2) self.assertEqual(len(namespaces), 0) namespaces = {'xs': XSI_NAMESPACE, 'tns0': 'http://xmlschema.test/ns'} mapper.transfer(namespaces) self.assertEqual(len(mapper), 3) self.assertIn('tns0', mapper) self.assertEqual(len(namespaces), 1) self.assertIn('xs', namespaces) mapper = NamespaceMapper() namespaces = {'xs': XSD_NAMESPACE} mapper.transfer(namespaces) self.assertEqual(mapper, {'xs': XSD_NAMESPACE}) self.assertEqual(namespaces, {})
def test_unmap_qname(self): namespaces = dict(xs=XSD_NAMESPACE, xsi=XSI_NAMESPACE) mapper = NamespaceMapper(namespaces) self.assertEqual(mapper.unmap_qname(''), '') self.assertEqual(mapper.unmap_qname('xs:element'), '{%s}element' % XSD_NAMESPACE) self.assertEqual(mapper.unmap_qname('{foo}bar'), '{foo}bar') self.assertEqual(mapper.unmap_qname('xsd:element'), 'xsd:element') with self.assertRaises(ValueError) as ctx: mapper.unmap_qname('xs::element') self.assertIn("wrong format", str(ctx.exception)) with self.assertRaises(TypeError) as ctx: mapper.unmap_qname(None) self.assertIn("must be a string-like object", str(ctx.exception)) with self.assertRaises(TypeError) as ctx: mapper.unmap_qname(99) self.assertIn("must be a string-like object", str(ctx.exception)) self.assertEqual(mapper.unmap_qname('element'), 'element') mapper[''] = 'foo' self.assertEqual(mapper.unmap_qname('element'), '{foo}element') self.assertEqual(mapper.unmap_qname('element', name_table=['element']), 'element')
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))