def get_lossy_tree(self): tree = Tree() with tree.node('root', 'tale') as root: root.node('level', 'absurd') root.node('level', 'stupid') root.node('handle', 'lame') return tree
def test_render_child_node_as_root(self): tree = Tree() with tree.node('root') as root: child = root.node('name') child.node('first', 'Bob') child.node('last', 'Wiley') self.assertIsInstance(child.render(), basestring)
def test_data_node_with_children_and_text(self): tree = Tree() with tree.node('a', 'A', href="http://bigjason.com") as a: a.node('b', "Link") self.assertEqual( tree(), '<a href="http://bigjason.com">A<b>Link</b></a>' )
def test_run_as_callable(self): tree = Tree() with tree.node('root') as root: root.node('item', 1) actual = tree('dict') expected = {'root': {'item': 1}} self.assertDictEqual(actual, expected)
def get_dirty_tree(self): tree = Tree() with tree.node("author") as author: author.node('name', 'Terry Pratchett') with author.node('genre') as genre: genre.node('fantasy', 'true') genre.node('comedy', 'true') genre.node('horror', 'false') return tree
def get_unified_tree(self): tree = Tree() with tree.node("author") as author: author.node('name', 'Terry Pratchett') author.node('genre', 'Fantasy/Comedy') with author.node('novels', count=2) as novels: novels.node('novel', 'Small Gods', year=1992) novels.node('novel', 'The Fifth Elephant', year=1999) novels.node('novel', 'Feet of Clay', year=1996) return tree
def test_children_distinct_names_are_different(self): tree = Tree() with tree.node('root') as root: root.node('person', 'One') root.node('different', 'Two') root.node('strokes', 'Three') render = DictRenderer() self.assertSetEqual( render._children_distinct_names(root.__children__), set(["person", "different", "strokes"]) )
def test_render_child_node_as_root(self): tree = Tree() with tree.node('root') as root: child = root.node('name') child.node('first', 'Bob') child.node('last', 'Wiley') self.assertIsInstance( child.render(), basestring )
def test_children_distinct_names(self): tree = Tree() with tree.node('tree') as root: root.node('person', 'One') root.node('person', 'Two') root.node('person', 'Three') render = DictRenderer() self.assertSetEqual( render._children_distinct_names(root.__children__), set(["person"]) )
def get_author(self): tree = Tree() with tree.node('author', rating="<b>5/6 Stars</b>") as author: author.node('name', 'Terry Pratchett') author.node('genere', 'Fantasy/Comedy') author.node('country', abbreviation="UK") author.node('living') with author.node('novels', count=2) as novels: novels.node('novel', 'Small Gods', year=1992) novels.node('novel', 'The Fifth Elephant', year=1999) with novels.node('shorts', count=2) as shorts: shorts.node('short', "Short Story 1") shorts.node('short', "Short Story 2") return tree
def translate_story(entry, project_id, requestor_id): tree = Tree() # tree.instruct("xml", version="1.0", encoding="UTF-8") # Not supported by datatree 0.2.0 :( with tree.story() as story: story.project_id(project_id, type="integer") story.story_type(entry["Story Type"]) story.current_state(entry["Current State"]) story.description(entry["Description"]) story.name(entry["Story"]) if requestor_id: story.requested_by(requestor_id) # not using entry["Requested By"] to prevent invalid membership error at PT end story.created_at(entry["Created at"], type="datetime") # story.updated_at(entry["Updated at"], type="datetime") story.labels(entry["Labels"]) with story.notes(type="array") as notes: for n in entry["Notes Full"]: with notes.note() as note: note.text(n[0]) note.noted_at(translate_time(n[1], quotes=False), type="datetime") # note.author(n[2]) # prevent valid membership check at PT end return tree(pretty=True)
def _get_complex_structure(self): tree = Tree() tree.instruct('xml') #tree.cdata(r"<b>I am some text.</b>") tree.declare('DOCTYPE', __.author, __.SYSTEM, 'SomeDTD.dtd') with tree.node('author') as author: author.node('name', 'Terry Pratchett') author.node('genre', 'Fantasy/Comedy') author.comment("Only 2 books listed") with author.node('novels', count=2) as novels: novels.node('novel', 'Small Gods', year=1992) novels.node('novel', 'The Fifth Elephant', year=1999) return tree
def test_render_instruction(self): tree = Tree() tree.instruct('process', do="Good") self.assertIn(tree(), '<?process do="Good"?>')
def test_render_instruction_xml(self): tree = Tree() tree.instruct('xml') self.assertIn(tree(), '<?xml version="1.0" encoding="UTF-8"?>')
def test_render_declaration_no_values(self): tree = Tree() tree.declare('ELEMENT') self.assertIn(tree(), r'<!ELEMENT>')
from datatree import Tree if __name__ == '__main__': tree = Tree() #tree.instruct('xml', version='1.0') with tree.node("author") as author: author.node('name', 'Terry Pratchett') author.node('genre', 'Fantasy/Comedy') author.comment("Only 2 books listed") with author.node('novels', count=2) as novels: novels.node('novel', 'Small Gods', year=1992) novels.node('novel', 'The Fifth Elephant', year=1999) novels.node("novel", "Guards! Guards!", year=1989) print 'XML:' print author(pretty=True) print print print 'JSON:' print author('json', pretty=True) print print print 'YAML:' print author('yaml') print print print 'Dict:' print author('dict', pretty_string=True)
def test_add_child_node(self): tree = Tree() node = tree.node('A Value') self.assertEqual(tree.__children__[0], node)
def test_render_comment(self): tree = Tree() tree.node('root').comment("Something Here") self.assertIn('<!-- Something Here -->', tree('xml'))
def test_render_cdata_not_string(self): int_val = 1234567891011121314151617181920 tree = Tree() tree.node('root').cdata(int_val) self.assertIn('<![cdata[{0}]]>'.format(str(int_val)), tree('xml'))
def get_flat_tree(self): tree = Tree() with tree.node("author") as author: author.node('name', 'Terry Pratchett') author.node('genre', 'Fantasy/Comedy') return tree
def test_add_node_return(self): tree = Tree() root = tree.node('root') self.assertEqual(root, tree.__children__[0])
def test_render_cdata_string(self): tree = Tree() tree.node('root').cdata("Some Value") self.assertIn('<![cdata[Some Value]]>', tree('xml'))
def test_render_declaration(self): tree = Tree() tree.declare('ELEMENT', __.Value, 'A value here.') self.assertIn(tree(), r'<!ELEMENT Value "A value here.">')
def _get_test_tree(self): tree = Tree() tree.instruct() tree.node('name', 'Bob') tree.node('age', 12) return tree
def test_data_node_with_children_and_text(self): tree = Tree() with tree.node('a', 'A', href="http://bigjason.com") as a: a.node('b', "Link") self.assertEqual(tree(), '<a href="http://bigjason.com">A<b>Link</b></a>')