def test_populate(self): table = TempyTable().populate(self.data) # Check table sizes self.assertEqual(len(table.body), 15) self.assertEqual(len(table.body[0]), 10) # test add row new_data = copy(self.data) new_data.append(list(range(1, 11))) table.populate(new_data) self.assertEqual(len(table.body), 16) # test resize new_data.append(list(range(1, 12))) table.populate(new_data) self.assertEqual(len(table.body), 17) self.assertEqual(len(table.body[0]), 11) self.assertEqual(len(table.body[1]), 11) self.assertEqual(len(table.body[-1]), 11) # test non normalize: new_data[3].append('test2') table.populate(new_data, normalize=False) self.assertTrue('test2' in table.body[3][10]) self.assertEqual(len(table.body[1]), 10) self.assertEqual(len(table.body[3]), 11) with self.assertRaises(IndexError): table.body[6][11] with self.assertRaises(WidgetDataError): table.populate(None)
def test_pop_row(self): table = TempyTable(data=self.data) # test pop last r = table.pop_row() self.assertEqual(r, self.data[-1]) # test pop get tags r = table.pop_row(tags=True) test_row = Tr()(Td()(c) for c in self.data[-2]) for cell, t_cell in zip(r, test_row): self.assertEqual(cell, t_cell) # test pop by index r = table.pop_row(0) self.assertEqual(r, self.data[0])
def test_map_row(self): table = TempyTable(data=self.data) table.map_row(lambda x: x - 1) self.assertEqual(-1, table.childs[0].childs[0].childs[0].childs[0]) # applies function x - 2 for first row table.map_row(lambda x: x - 2, 0) self.assertEqual(-3, table.childs[0].childs[0].childs[1].childs[0])
def test_map_col(self): table = TempyTable(data=self.data) table.map_col(lambda x: x - 1) self.assertEqual(-1, table.childs[0].childs[0].childs[0].childs[0]) # applies function x - 2 for second column table.map_col(lambda x: x - 2, 1) self.assertEqual(-3, table.childs[0].childs[0].childs[1].childs[0])
def test_make_scope(self): table = TempyTable(data=self.data) table.make_scope(col_scope_list=[(0, 0)]) self.assertEqual('col', table.childs[0].childs[0].childs[0].attrs['scope']) table.make_scope(row_scope_list=[(0, 0)]) self.assertEqual('row', table.childs[0].childs[0].childs[0].attrs['scope'])
def test_col_class(self): table = TempyTable(data=self.data) table.col_class('class_example') self.assertEqual({'class_example'}, table.childs[0].childs[0].childs[0].attrs['klass']) # first column of each row table.col_class('class_example_new', 0) self.assertEqual({'class_example_new', 'class_example'}, table.childs[0].childs[0].childs[0].attrs['klass'])
def test_row_class(self): table = TempyTable(data=self.data) table.row_class('class_example') self.assertEqual({'class_example'}, table.childs[0].childs[0].attrs['klass']) # first row for each column table.row_class('class_example_new', 0) self.assertEqual({'class_example_new', 'class_example'}, table.childs[0].childs[0].attrs['klass'])
def test_pop_cell(self): table = TempyTable(data=self.data) # test pop last r = table.pop_cell() self.assertEqual(r, self.data[-1][-1]) # test pop get tags r = table.pop_cell(tags=True) test_cell = Td()(self.data[-2][-1]) self.assertEqual(r, test_cell) # test pop by index row r = table.pop_cell(0) self.assertEqual(r, self.data[0][-1]) # test pop by index row andcol r = table.pop_cell(0, 0) self.assertEqual(r, self.data[0][0])
def test_clear(self): table = TempyTable(data=self.data) table.clear() self.assertTrue(table.body.is_empty)
def test_init_from_data_full(self): table = TempyTable(data=self.data, head=True, foot=True) self.assertEqual(len(table.body), 13) self.assertIsInstance(table.header, Thead) self.assertIsInstance(table.footer, Tfoot)
def test_init_from_data(self): table = TempyTable(data=self.data) self.assertEqual(len(table.body), 15) self.assertEqual(len(table.body[0]), 10) self.verify_content(table)
def test_caption(self): table = TempyTable(caption='Test Table') self.assertTrue('Test Table' in table.caption)
def test_skeleton_creation(self): table = TempyTable(rows=15, cols=10) self.assertTrue(table.body) # Check table sizes self.assertEqual(len(table.body), 15) self.assertEqual(len(table.body[0]), 10)
def test_is_col_within_bounds(self): table = TempyTable(data=self.data) self.assertTrue( table.is_col_within_bounds(0, table.childs[0].childs[0])) with self.assertRaises(WidgetDataError): table.is_col_within_bounds(20, table.childs[0].childs[0])
def test_is_row_within_bounds(self): table = TempyTable(data=self.data) self.assertTrue(table.is_row_within_bounds(0)) with self.assertRaises(WidgetDataError): table.is_row_within_bounds(20)
# -*- coding: utf-8 -*- from tempy.widgets import TempyTable from tempy.tags import * from tempy.elements import Css data = [['Name', 'Last Name', 'Age', 'Telephone'], ['John', 'Doe', '34', '555666777', '444555333'], ['Michael', 'Roberts', '22', '555766777', '244555333']] special_cell = Td()(A(href='python.org'), P()('first paragraph', P()('nested paragraph'))) table1 = TempyTable(rows=3, cols=4, data=data, head=True, caption='User information', width='100%') table2 = TempyTable(data=data, caption='User information2', width='50%', border='1px solid black') table3 = TempyTable(data=data, caption='User information3', width='50%') table4 = TempyTable(data=data, caption='User information4', width='50%') table2.pop_cell() special_cell.append_to(table2.childs[0].childs[2]) css_tag = Css({ '.class_example_1': {'color': 'blue'}, '.class_example_2': {'color': 'pink'}, '.class_example_3': {'background-color': 'grey'}, }) # set class for every cell table1.col_class('class_example_2') # applies function to upper string for every cell table1.map_col(lambda x: x.upper())
def test_empty_creation(self): table = TempyTable() self.assertFalse(table.body.childs) self.assertTrue(table.body) # Future non-regression, TempyTable should remain a Table Tag self.assertIsInstance(table, Table)