def test_add_elem(self): # tag name parent = new_elem('foo') add_elem(parent, 'bar', a='100', b='200', attrib=dict(c="300")) self.assertEqual(to_xml(parent), '<foo><bar a="100" b="200" c="300" /></foo>') # element parent = new_elem('foo') add_elem(parent, new_elem('bar', a='100', b='200'), attrib=dict(c="300"), other='value') self.assertEqual( to_xml(parent), '<foo><bar a="100" b="200" c="300" other="value" /></foo>') # XML parent = new_elem('foo') add_elem(parent, '<bar b="200" a="100" />', attrib=dict(c="300"), other='value') self.assertEqual( to_xml(parent), '<foo><bar a="100" b="200" c="300" other="value" /></foo>')
def test_xml_indent(self): a = new_elem('a', x='100', y='200') b = new_elem('b', m='1000', n='2000') c = new_elem('c', text_content='Hi') add_elem(a, b) add_elem(b, c) xml_indent(a) self.assertEqual(to_xml(a), '<a x="100" y="200">\n <b m="1000" n="2000">\n <c>Hi</c>\n </b>\n</a>\n')
def test_xml_indent(self): a = new_elem('a', x='100', y='200') b = new_elem('b', m='1000', n='2000') c = new_elem('c', text_content='Hi') add_elem(a, b) add_elem(b, c) xml_indent(a) self.assertEqual( to_xml(a), '<a x="100" y="200">\n <b m="1000" n="2000">\n <c>Hi</c>\n </b>\n</a>\n' )
def test_add_elem(self): # tag name parent = new_elem('foo') add_elem(parent, 'bar', a='100', b='200', attrib=dict(c="300")) self.assertEqual(to_xml(parent), '<foo><bar a="100" b="200" c="300" /></foo>') # element parent = new_elem('foo') add_elem(parent, new_elem('bar', a='100', b='200'), attrib=dict(c="300"), other='value') self.assertEqual(to_xml(parent), '<foo><bar a="100" b="200" c="300" other="value" /></foo>') # XML parent = new_elem('foo') add_elem(parent, '<bar b="200" a="100" />', attrib=dict(c="300"), other='value') self.assertEqual(to_xml(parent), '<foo><bar a="100" b="200" c="300" other="value" /></foo>')
def test_new_elem(self): # empty elem = new_elem('foo') self.assertEqual(to_xml(elem), '<foo />') # With attributes elem = new_elem('foo', attrib=dict(a='100', b='200')) self.assertEqual(to_xml(elem), '<foo a="100" b="200" />') # With text content elem = new_elem('foo', attrib=dict(a='100', b='200'), text_content='Hi') self.assertEqual(to_xml(elem), '<foo a="100" b="200">Hi</foo>') # With keyword parameters elem = new_elem('foo', attrib=dict(a='100', b='200'), other='value') self.assertEqual(to_xml(elem), '<foo a="100" b="200" other="value" />')
def test_from_xml(self): field = SchemaField.from_xml('<field name="foo" type="int64" />') self.assertEqual(field.name, 'foo') self.assertEqual(field.type, 'int64') self.assertEqual(field.key, False) field = SchemaField.from_xml('<field name="foo" type="int64" key="true" />') self.assertEqual(field.name, 'foo') self.assertEqual(field.type, 'int64') self.assertEqual(field.key, True) field = SchemaField.from_xml(xml.new_elem('field', attrib=dict(name='foo', type='int32', key=True))) self.assertEqual(field.name, 'foo') self.assertEqual(field.type, 'int32') self.assertEqual(field.key, True)
def test_from_xml(self): field = SchemaField.from_xml('<field name="foo" type="int64" />') self.assertEqual(field.name, 'foo') self.assertEqual(field.type, 'int64') self.assertEqual(field.key, False) field = SchemaField.from_xml( '<field name="foo" type="int64" key="true" />') self.assertEqual(field.name, 'foo') self.assertEqual(field.type, 'int64') self.assertEqual(field.key, True) field = SchemaField.from_xml( xml.new_elem('field', attrib=dict(name='foo', type='int32', key=True))) self.assertEqual(field.name, 'foo') self.assertEqual(field.type, 'int32') self.assertEqual(field.key, True)
def test_to_xml(self): a = new_elem('a', x='100', y='200') b = new_elem('b', m='1000', n='2000') c = new_elem('c', text_content='Hi') add_elem(a, b) add_elem(b, c) self.assertEqual(to_xml(a), '<a x="100" y="200"><b m="1000" n="2000"><c>Hi</c></b></a>') a = new_elem('a', x='100', y='200') b = new_elem('b', m='1000', n='2000') c = new_elem('c', text_content='Hi') add_elem(a, b) add_elem(b, c) self.assertEqual(to_xml(a, pretty=True), '<a x="100" y="200">\n <b m="1000" n="2000">\n <c>Hi</c>\n </b>\n</a>\n') a = new_elem('a', x='100', y='200') b = new_elem('b', m='1000', n='2000') c = new_elem('c', text_content='Hi') add_elem(a, b) add_elem(b, c) self.assertEqual(to_xml(to_xml(a), pretty=True), '<a x="100" y="200">\n <b m="1000" n="2000">\n <c>Hi</c>\n </b>\n</a>\n') a = new_elem('a', x='100', y='200') b = new_elem('b', m='1000', n='2000') c = new_elem('c', text_content='Hi') add_elem(a, b) add_elem(b, c) print(to_xml(a, encoding='ascii')) self.assertEqual(to_xml(a, encoding='ascii'), b'<?xml version=\'1.0\' encoding=\'ascii\'?>\n<a x="100" y="200"><b m="1000" n="2000"><c>Hi</c></b></a>')
def test_to_xml(self): a = new_elem('a', x='100', y='200') b = new_elem('b', m='1000', n='2000') c = new_elem('c', text_content='Hi') add_elem(a, b) add_elem(b, c) self.assertEqual( to_xml(a), '<a x="100" y="200"><b m="1000" n="2000"><c>Hi</c></b></a>') a = new_elem('a', x='100', y='200') b = new_elem('b', m='1000', n='2000') c = new_elem('c', text_content='Hi') add_elem(a, b) add_elem(b, c) self.assertEqual( to_xml(a, pretty=True), '<a x="100" y="200">\n <b m="1000" n="2000">\n <c>Hi</c>\n </b>\n</a>\n' ) a = new_elem('a', x='100', y='200') b = new_elem('b', m='1000', n='2000') c = new_elem('c', text_content='Hi') add_elem(a, b) add_elem(b, c) self.assertEqual( to_xml(to_xml(a), pretty=True), '<a x="100" y="200">\n <b m="1000" n="2000">\n <c>Hi</c>\n </b>\n</a>\n' ) a = new_elem('a', x='100', y='200') b = new_elem('b', m='1000', n='2000') c = new_elem('c', text_content='Hi') add_elem(a, b) add_elem(b, c) print(to_xml(a, encoding='ascii')) self.assertEqual( to_xml(a, encoding='ascii'), b'<?xml version=\'1.0\' encoding=\'ascii\'?>\n<a x="100" y="200"><b m="1000" n="2000"><c>Hi</c></b></a>' )