def test_remove_methods(self): table = OutputTable(headers=TEST_HEADERS) for row in TEST_ROWS: table.add_row(row) # Pick a row row_idx = 4 self.assertEqual(table.get_row(index=row_idx), [f"value 4{j}" for j in range(10)]) # remove row and check that the next one is in its place table.remove_row_by_index(index=row_idx) self.assertEqual(table.get_row(index=row_idx), [f"value 5{j}" for j in range(10)]) table = OutputTable(headers=TEST_HEADERS) for row in TEST_ROWS: table.add_row(row) # Pick column, remove and check that is replaced col_idx = 7 self.assertEqual(table.get_column(index=col_idx), ["Column 7"] + [f"value {i}7" for i in range(10)]) table.remove_column_by_index(index=col_idx) self.assertEqual(table.get_column(index=col_idx), ["Column 8"] + [f"value {i}8" for i in range(10)]) table.remove_column_by_header(header="Column 8") self.assertEqual(table.get_column(index=col_idx), ["Column 9"] + [f"value {i}9" for i in range(10)])
def test_to_formatted_string(self): table = OutputTable(headers=TEST_HEADERS) for row in TEST_ROWS: table.add_row(row) self.assertEqual(table.to_formatted_string(), TEST_TABLE_STR) table = OutputTable(headers=TEST_HEADERS) for row in TEST_ROWS: table.add_row(row) self.assertEqual( table.to_formatted_string(separator=',', ignore_widths=True), TEST_CSV_STR)
def test_add_get_methods(self): table = OutputTable(headers=["Column 0"]) # add/get single row/col table.add_row(["value 0,0"]) self.assertEqual(table.get_row(index=0), ["value 0,0"]) table.add_column(["Column 1", "value 0,1"]) self.assertEqual(table.headers(), ["Column 0", "Column 1"]) self.assertEqual(table.get_row(index=0), ["value 0,0", "value 0,1"]) self.assertEqual(table.get_column(index=1), ["Column 1", "value 0,1"]) # add/get row and column by index table.add_row(["value -1,0", "value -1,1"], index=0) self.assertEqual(table.get_row(index=0), ["value -1,0", "value -1,1"]) self.assertEqual(table.get_column(index=0), ["Column 0", "value -1,0", "value 0,0"]) self.assertEqual(table.get_column(index=1), ["Column 1", "value -1,1", "value 0,1"]) table.add_column(["Column 0.5", "value -1,0.5", "value 0,0.5"], index=1) self.assertEqual(table.get_column(index=1), ["Column 0.5", "value -1,0.5", "value 0,0.5"]) self.assertEqual(table.get_row(index=0), ["value -1,0", "value -1,0.5", "value -1,1"]) self.assertEqual(table.get_row(index=1), ["value 0,0", "value 0,0.5", "value 0,1"])
def test_set_width_methods(self): table = OutputTable(headers=TEST_HEADERS) for row in TEST_ROWS: table.add_row(row[:]) self.assertEqual(table._column_widths[0], 10) # Now set the width by header table.set_column_width_by_header(header='Column 0', width=10) self.assertEqual(table.column_widths()[0], 10) table.set_column_width_by_header(header='Column 4', width=2) self.assertEqual(table.column_widths()[4], 2) with self.assertRaises( TritonModelAnalyzerException, msg="Expected invalid header to raise Exception"): table.set_column_width_by_header(header='Column NOT PRESENT', width=2) # Now set the width by index table.set_column_width_by_index(index=3, width=10) self.assertEqual(table.column_widths()[3], 10) table.set_column_width_by_index(index=7, width=2) self.assertEqual(table.column_widths()[7], 2) with self.assertRaises( TritonModelAnalyzerException, msg="Expected invalid index to raise Exception"): table.set_column_width_by_index(index=12, width=2)
def test_to_formatted_string(self): table = OutputTable(headers=TEST_HEADERS) for row in TEST_ROWS: table.add_row(row) self.assertEqual( table.to_formatted_string(column_width=TEST_COLUMN_WIDTH), TEST_TABLE_STR) self.assertEqual(table.to_formatted_string(separator=','), TEST_CSV_STR)
def test_create_headers(self): table = OutputTable(headers=["Column 0"]) self.assertEqual(table.headers(), ["Column 0"])
def test_create_headers(self): table = OutputTable(headers=["Column 0"]) self.assertEqual(table.headers(), ["Column 0"]) self.assertEqual(table.column_widths(), [len("Column 0") + OutputTable.column_padding])