def test_increase_height_DataTree(self): """ Test increase_height function of DataTree. """ t1 = DataTree() self.assertEqual(t1.height(), 1) d1 = DataNode(1) d2 = DataNode(2) t1.root.add(d1) d1.add(d2) self.assertEqual(t1.height(), 3) t1.increase_height(1) self.assertEqual(t1.height(), 4) t1.increase_height(4) self.assertEqual(t1.height(), 8) self.assertRaises(TypeError, lambda: t1.increase_height('3')) self.assertRaises(ValueError, lambda: t1.increase_height(-4))
def txt_toTree(file, delimiter=None, options=None): """ Function transforms a txt file into a DataTree. Parameters ---------- file: file open input file in at least read mode delimiter: TODO options: list, optional Returns ------- tree: DataTree """ tree = DataTree(tree_type='txt') # TODO add more options # TODO add column or row storage col0 = DataNode('') for row in file.readlines(): col0.add(DataNode(row)) tree.root.add(col0) return tree
def csv_toTree(file, delimiter, ignore_index=True, options=None): """ Function transforms a csv file into a DataTree. Parameters ---------- file: file open input file in at least read mode delimiter: str ignore_index: bool, optional options: list, optional Returns ------- tree: DataTree """ # TODO add option column or row store csv_reader = csv.reader(file, delimiter=delimiter) tree = DataTree(tree_type='csv') i = 0 for row in csv_reader: row_node = DataNode('Row%s' % i) for col in row: col_node = DataNode(col) row_node.add(col_node) tree.root.add(row_node) i += 1 return tree
def test_add_DataNode2(self): """ Test add function of DataNode. Test fail of add function """ t1 = DataNode('1') self.assertRaises(TypeError, lambda: t1.add("DataNode"))
def test_merge_DataNode2(self): """ Test merge function. With wrong delimiter. """ t1 = DataNode('1') t2 = DataNode('2') self.assertRaises(TypeError, lambda: t1.merge(t2, 3))
def test_complex_DataNode3(self): """ Test a complex DataNode with all possible attributes """ data = ('1', None) result = DataNode(*data) self.assertEqual(result.data, data[0]) self.assertEqual(result.children, None) self.assertEqual(result.height(), 1)
def test_complex_DataNode2(self): """ Test a complex DataNode with all possible attributes List of childs """ data = ('1', [DataNode('child')]) result = DataNode(*data) self.assertEqual(result.data, data[0]) self.assertEqual(result.children, data[1]) self.assertEqual(result.height(), 2)
def test_complex_DataNode1(self): """ Test a complex DataNode with all possible attributes Single child """ data = ('1', DataNode('child')) result = DataNode(*data) self.assertEqual(result.data, data[0]) self.assertEqual(result.children, [data[1]]) self.assertEqual(result.height(), 2)
def test_is_leaf_DataNode(self): """ Test is_leaf function of DataNode. """ t1 = DataNode(1) self.assertEqual(t1.is_leaf(), True) t2 = DataNode(2) t1.add(t2) self.assertEqual(t1.is_leaf(), False) self.assertEqual(t2.is_leaf(), True)
def test_valid_other_functions_AttributeNode(self): """ Test if AttributeNode has same functions as DataNode. Functionallity is tested in TestDataNode """ t1 = AttributeNode("1", "2") # add() t1.add(DataNode('2')) self.assertIsNotNone(t1.children) # height() self.assertIsNotNone(t1.height()) # is_leaf() self.assertIsNotNone(t1.is_leaf()) # remove_children() self.assertIsNotNone(t1.remove_children()) # minimize_height() t1.minimize_height() # generate_string_representation() self.assertIsNotNone(t1.generate_string_representation()) # __str__() self.assertIsNotNone(str(t1))
def test___str___DataNode(self): """ Test __str__ function of DataNode. """ t1 = DataNode("1") self.assertEqual(str(t1), "1")
def help_xml_toTree(xml_node): """ Helps xml_ToTree function, walks through xml recursive Parameters ---------- xml_node: ElementType1 Returns ------- node: DataNode """ if xml_node.hasChildNodes(): tree_node = DataNode(xml_node.localName) for node in xml_node.childNodes: tree_node.add(help_xml_toTree(node)) return tree_node # TODO Add Attributes node = None if xml_node.nodeType == xml_node.TEXT_NODE: # TODO: guess xml_node.nodeValue == xml_node.data node = DataNode(xml_node.nodeValue.replace('\n ', '')) elif xml_node.nodeType == xml_node.ELEMENT_NODE: # TODO: guess xml_node.tagName == xml_node.localName node = DataNode(xml_node.localName) else: # TODO: Implement the other nodeTypes print('Warning: NodeType not supported yet') node = DataNode(xml_node.localName) return node
def test_simple_DataNode(self): """ Test a simple DataNode with only a value attribute """ data = "1" result = DataNode(data) self.assertEqual(result.data, data) self.assertEqual(result.children, None)
def test_height_DataNode(self): """ Test height function of DataNode. """ t1 = DataNode(1) self.assertEqual(t1.height(), 1) t1.add(DataNode(11)) self.assertEqual(t1.height(), 2)
def test_height_DataTree(self): """ Test height function of DataTree. """ t1 = DataTree() self.assertEqual(t1.height(), 1) d1 = DataNode(1) d2 = DataNode(2) d3 = DataNode(3) d4 = DataNode(4) t1.root.add(d1) self.assertEqual(t1.height(), 2) d1.add(d2) self.assertEqual(t1.height(), 3) d2.add(d3) self.assertEqual(t1.height(), 4) t1.root.add(d4) self.assertEqual(t1.height(), 4)
def test_simpleDataTree(self): """ Test Creation of simple DataTree. """ t1 = DataTree('type') self.assertEqual(t1.tree_type, 'type') d1 = DataNode(1) d2 = DataNode(2) d3 = DataNode(3) t1.root.add(d1) d1.add(d2) d2.add(d3) self.assertEqual(t1.height(), 4) self.assertIsNotNone(t1.root.children)
def test_complex_AttributeNode2(self): """ Test a complex AttributeNode with all possible attributes No String key and value """ data = (1, 2, DataNode('child')) result = AttributeNode(*data) self.assertEqual(result.key, str(data[0])) self.assertEqual(result.data, str(data[1])) self.assertEqual(result.children, [data[2]]) self.assertEqual(result.height(), 2)
def test_remove_children_DataNode(self): """ Test remove_children function of DataNode. """ # Test it with t1 having children t1 = DataNode('t1') t2 = DataNode('t2') t1.add(t2) self.assertEqual(t1.is_leaf(), False) self.assertEqual(t2.is_leaf(), True) self.assertIsNotNone(t1.children) result = t1.remove_children() self.assertIsNotNone(result) self.assertEqual(type(result), type([])) self.assertEqual(len(result), 1) self.assertEqual(t1.children, None) # Testing it with t1 having no children self.assertEqual(None, t1.remove_children())
def test_complex_AttributeNode1(self): """ Test a complex AttributeNode with all possible attributes Single child """ data = ("1", "2", DataNode('child')) result = AttributeNode(*data) self.assertEqual(result.key, data[0]) self.assertEqual(result.data, data[1]) self.assertEqual(result.children, [data[2]]) self.assertEqual(result.height(), 2)
def xlsx_toTree(file_name, options=None): """ Function transforms a xlsx file into a DataTree. Therefore it needs the path of the original file. Parameters ---------- file_name: str Returns ------- tree: DataTree """ # TODO: add options guess_type, data_only, keep_vba xlsx_wb = openpyxl.load_workbook(filename=file_name) tree = DataTree(tree_type='xlsx') # For each sheet create a node for sheet_name in xlsx_wb.sheetnames: sheet_node = DataNode('Sheet:' + sheet_name) sheet = xlsx_wb[sheet_name] # go through each cell in matrix max_rows x max_cols # and create tree like csv file i = 0 for row in sheet.rows: row_node = DataNode('Row%s' % i) i += 1 for cell in row: col_node = DataNode(str(cell.value)) row_node.add(col_node) sheet_node.add(row_node) tree.root.add(sheet_node) return tree
def test_merge_DataNode(self): """ Test merge function. With correct delimiter. """ t1 = DataNode('1') t2 = DataNode('2') self.assertEqual(t1.data, "1") t1.merge(t2) self.assertEqual(t1.data, "1 2") t1.merge(t2, ";") self.assertEqual(t1.data, "1 2;2")
def test_is_element_DataNode(self): """ Test is_element function of DataNode. """ t1 = DataNode(1) self.assertEqual(t1.is_element(), True)
def test_add_DataNode(self): """ Test add function of DataNode. """ t1 = DataNode('1') t2 = DataNode('2', children=[DataNode(21)]) t1.add(DataNode(11)) t1.add(DataNode(12)) t2.add(DataNode(22)) t2.add(DataNode(23)) self.assertEqual(len(t1.children), 2) self.assertEqual(len(t2.children), 3) self.assertEqual(t1.height(), 2) self.assertEqual(t2.height(), 2)
def test_xlsx_fromTree(self): """ Test the DataTree to xlsx function. """ # TODO: Test for right output # Prepare DataTree row0 = DataNode( 'Row0', children=[DataNode('Col1'), DataNode('Col2'), DataNode('Col3')]) row1 = DataNode( 'Row1', children=[DataNode('Row11'), DataNode('Row12'), DataNode('Row13')]) row2 = DataNode( 'Row2', children=[DataNode('Row21'), DataNode('Row22'), DataNode('Row23')]) row3 = DataNode( 'Row3', children=[DataNode('Row31'), DataNode('Row32'), DataNode('Row33')]) row4 = DataNode( 'Row4', children=[DataNode('Row41'), DataNode('Row42'), DataNode('Row43')]) tree = DataTree(tree_type='xlsx') tree.root.add(row0) tree.root.add(row1) tree.root.add(row2) tree.root.add(row3) tree.root.add(row4) f = open(PATH_INPUT_FILES + 'output_test.xlsx', "w+") f.close() xlsx_fromTree(tree, PATH_INPUT_FILES + 'output_test.xlsx') os.remove(PATH_INPUT_FILES + 'output_test.xlsx')
def test_txt_fromTree(self): """ Test the DataTree to txt function. """ # Prepare DataTree col0 = DataNode('') row0 = DataNode('Row0') row1 = DataNode('Row1') row2 = DataNode('Row2') row3 = DataNode('Row3') row4 = DataNode('Row4') tree = DataTree(tree_type='txt') col0.add(row0) col0.add(row1) col0.add(row2) col0.add(row3) col0.add(row4) tree.root.add(col0) # Run function outfile = StringIO() txt_fromTree(tree, outfile) outfile.seek(0) content = outfile.read() self.assertEqual(content, tree.generate_string_representation())
def test_is_tag_DataNode(self): """ Test is_tag function of DataNode. """ t1 = DataNode(1) self.assertEqual(t1.is_tag(), False)
def test_csv_fromTree(self): """ Test the DataTree to csv function. """ # Prepare DataTree row0 = DataNode( 'Row0', children=[DataNode('Col1'), DataNode('Col2'), DataNode('Col3')]) row1 = DataNode( 'Row1', children=[DataNode('Row11'), DataNode('Row12'), DataNode('Row13')]) row2 = DataNode( 'Row2', children=[DataNode('Row21'), DataNode('Row22'), DataNode('Row23')]) row3 = DataNode( 'Row3', children=[DataNode('Row31'), DataNode('Row32'), DataNode('Row33')]) row4 = DataNode( 'Row4', children=[DataNode('Row41'), DataNode('Row42'), DataNode('Row43')]) tree = DataTree(tree_type='csv') tree.root.add(row0) tree.root.add(row1) tree.root.add(row2) tree.root.add(row3) tree.root.add(row4) # Run function outfile = StringIO() csv_fromTree(tree, outfile) outfile.seek(0) content = outfile.read() with open(PATH_INPUT_FILES + 'input_test.csv', 'r') as inputfile: real_content = inputfile.readlines() inputfile.close() self.assertEqual(content, ''.join(real_content).replace('\n', '\r\n'))
def test_minimize_height_DataTree(self): """ Test minimize_height function of DataTree. """ t1 = DataTree() self.assertEqual(t1.height(), 1) d1 = DataNode(1) d2 = DataNode(2) d3 = DataNode(3) d4 = DataNode(4) t1.root.add(d1) d1.add(d2) d2.add(d3) d3.add(d4) self.assertEqual(t1.height(), 5) t1.minimize_height(1) self.assertEqual(t1.height(), 4) t1.minimize_height(3) self.assertEqual(t1.height(), 1) self.assertRaises(ValueError, lambda: t1.minimize_height(7)) self.assertRaises(TypeError, lambda: t1.minimize_height('3'))
def test_is_attribute_DataNode(self): """ Test is_attribute function of DataNode. """ t1 = DataNode(1) self.assertEqual(t1.is_attribute(), False)
def test_minimize_height_DataNode(self): """ Test minimize_height function of DataNode. """ t1, t2, t3 = DataNode('1'), DataNode('2'), DataNode('3') t1.add(t2) t2.add(t3) self.assertEqual(t1.height(), 3) t1.minimize_height() self.assertEqual(t1.height(), 2) t1.minimize_height() self.assertEqual(t1.height(), 1)