def test_column_key(self): file = os.getenv('SEABORN_TABLE_LARGE_FILE') if file: start = time.time() table = SeabornTable.file_to_obj(file) end = time.time() log.debug("Loading %s took %.2f seconds", file, end - start) self.assertLess(end - start, 20) table.column_key = table.columns[0] else: table = SeabornTable.str_to_obj(text=self.answer, deliminator=' | ', tab='|', column_key='#') for i in range(0, len(table), int(len(table) / 20) or 1): expected_row = table.table[i] start = time.time() key = expected_row[0] result_row = table.get(key) end = time.time() log.debug("test column key lookup took: %.2f seconds", end - start) self.assertEqual(expected_row, result_row) for key, row in table.items(): self.assertEqual(table.get(key), row) self.assertEqual(list(table.keys()), table.get_column(table.columns[0]))
def test_quote_empty_str(self): table = SeabornTable([['aaa', 'a_b_c', 'c'], [1, None, ''], ['', None, 'Aida']]) answer = ''' aaa a_b_c c 1 "" "" "" "" Aida '''.strip().replace('\n ', '\n') results = table.obj_to_str(quote_empty_str=True, deliminator=' ') log.debug(results) self.assertEqual(results, answer)
def test_excel_csv(self): table = SeabornTable([['aaa', 'a_b_c', 'c'], [1, '2\n2', '3'], ['4', '5', '"Verdi: "Aida""']]) result_file = self.get_data_path('_result', 'test_excel_csv.csv') table.obj_to_csv(file_path=result_file) table2 = SeabornTable.csv_to_obj(file_path=result_file) table2.naming_convention_columns("underscore") log.debug('\nAnswer:\n%s\n\nResult:\n%s\n\n' % (str(table), str(table2))) self.assertEqual(str(table), str(table2), 'Write then Read changed the data') self.remove_file(result_file)
def test_dict_of_list(self): columns = self.list_of_list[0] dict_of_list = {} for i, k in enumerate(columns): dict_of_list[k] = [row[i] for row in self.list_of_list[1:]] table = SeabornTable(dict_of_list, columns, deliminator=' | ', tab='| ') log.debug('\nAnswer:\n%s\n\nResult:\n%s\n\n' % (self.answer, str(table))) self.assertEqual(self.answer, str(table)) table.reverse()
def validate_test_condition(self, source): data = [[1, '', 3, 'hidden'], [None, '2.1', 3.1, 'h'], [1.12, 'very long cell', 3.12, 'h2'], [1, 'very long cell to be clipped', 3, '']] result_file = self.get_data_path('_live_table', self.BASENAME + '.' + source) expected_file = self.get_data_path('expected', self.BASENAME + '_%s.' + source) table = SeabornTable(data[:1], row_columns=['A', 'B', 'C', 'Hidden'], live_tables=[ dict(file_path=result_file, min_widths=2, max_widths={ 'A': 10, 'B': 10, 'C': 10 }, clip_widths=14, recreate=True), ]) self.assert_result_file(expected_file % 0, result_file) table.append(data[1]) self.assert_result_file(expected_file % 1, result_file) for index, row in enumerate(data[2:], start=2): table.append( {col: row[i] for i, col in enumerate(table.row_columns)}) self.assert_result_file(expected_file % index, result_file) table.close_live_table() self.assert_result_file(expected_file % (index + 1), result_file) self.remove_file(result_file)
def test_key_error(self): table = SeabornTable(self.list_of_list, deliminator=' | ') try: missing = table[0]['Missing'] raise AssertionError("Failed to throw KeyError") except KeyError as ex: return
def test_quote_texts(self): table = SeabornTable.md_to_obj(self.get_data_path('test_file.md')) expected_file = self.get_data_path('expected', 'test_quote_texts.txt') result_file = self.get_data_path('_result', 'test_quote_texts.txt') table.obj_to_txt(file_path=result_file, quote_texts=['None']) self.assert_result_file(expected_file, result_file) self.remove_file(result_file)
def test_pertibate(self): def row_filter(**kwargs): if (kwargs['column 1'] == 1 and kwargs['column 3'] == 'b' and kwargs['col2'] == 'Hello'): return False return True table = SeabornTable.pertibate_to_obj( columns=[ '#', 'column 1', 'col2', 'column 3', 'output column ', 'output col2' ], pertibate_values={ 'column 1': [1, 2], 'col2': ['Hello', 'World'], 'column 3': ['a', 'b', 'c'] }, generated_columns={ 'output col2': lambda **kwargs: kwargs['column 1'], '#': lambda _row_index, **kwargs: _row_index }, filter_func=row_filter, deliminator=' | ', tab='| ', max_size=100) self.assertEqual(self.answer, str(table)) return table
def test_list_of_list(self): table = SeabornTable( self.list_of_list, deliminator=' | ', tab='| ', ) log.debug('\nAnswer:\n%s\n\nResult:\n%s\n\n' % (self.answer, str(table))) self.assertEqual(self.answer, str(table))
def report_table(cls, thread_id=None, columns=None): report = cls.time_report(thread_id) if not report: return '' columns = columns or [ 'Function', 'Line Number', 'Message', 'Start', 'End', 'Count', 'Min', 'Max', 'Avg', 'Sum' ] table = SeabornTable(report['Messages'], columns=columns) return table
def test_list_of_dict(self): columns = self.list_of_list[0] list_of_dict = [{k: row[i] for i, k in enumerate(columns)} for row in self.list_of_list[1:]] table = SeabornTable(list_of_dict, columns, deliminator=' | ', tab='| ') self.assertEqual(self.answer, str(table))
def test_dict_of_dict(self): columns = self.list_of_list[0] dict_of_dict = {} for i, row in enumerate(self.list_of_list[1:]): dict_of_dict[i] = {k: row[i] for i, k in enumerate(columns)} table = SeabornTable(dict_of_dict, columns, deliminator=' | ', tab='| ') log.debug('\nAnswer:\n%s\n\nResult:\n%s\n\n' % (self.answer, str(table))) self.assertEqual(self.answer, str(table))
def validate_test_condition(self, source): data = [['cell 1, 1.0\ncell 1, 1.1', 'cell 1, 2', 'cell 1, 3'], [ 'cell 2, 1', 'cell 2, 2.0\ncell 2, 2.1\ncell 2, 2.2', 'cell 2, 3' ], [ 'cell 3, 1', '\ncell 3, 2', 'cell 3, 3.0\ncell 3, 3.1\ncell 3, 3.2\ncell 3, 3.3' ], [ 'cell 4, 1.0\n cell 4, 1.1', 'cell 4, 2.0\ncell 4, 2.1', 'cell 4, 3.0\ncell 4, 3.1\ncell 4, 3.2' ], ['\n\ncell 5, 1', 'cell 5, 2', 'cell 5, 3']] table = SeabornTable( data, row_columns=['Header 1', 'Header 2.0\nHeader 2.1', 'Header 3']) basename = self.BASENAME % source expected_file = self.get_data_path('expected', basename) result_file = self.get_data_path('_shared', basename) table.obj_to_file(result_file, break_line=True) self.assert_result_file(expected_file, result_file) self.remove_file(result_file) table.close_live_table()
def validate_test_condition(self, source): tables = [ SeabornTable.file_to_obj( file_path=self.get_data_path('test_share_columns_%s.rst' % i)) for i in range(4) ] for table in tables: table.share_column_widths(tables, self.SHARED_LIMIT) basename = self.BASENAME % source expected_file = self.get_data_path('expected', basename) result_file = self.get_data_path('_shared', basename) with open(result_file, 'w') as fn: for table in tables: text = table.obj_to_type(source) + u'\n\n' if sys.version_info[0] == 2: text = text.encode('utf-8') fn.write(text) self.assert_result_file(expected_file, result_file) self.remove_file(result_file)
def validate_test_condition(self, source): expected = SeabornTable( columns=['TEST COL 1', 'TEST COL TWO', 'LAST COL']) text = expected.obj_to_type(source) result = SeabornTable.type_to_obj(source, text=text) expected_file = self.get_data_path('expected', 'header_only.%s' % source) result_file = self.get_data_path('_header_only', 'header_only.%s' % source) expected.obj_to_file(expected_file) result.obj_to_file(result_file) self.assert_result_file(expected_file, result_file) self.remove_file(result_file) self.assertEqual(expected, result)
def test_mark_down(self): with open(self.get_data_path('test.md')) as f: prev = f.read() test = SeabornTable.mark_down_to_dict_of_obj( self.get_data_path('test.md')) paragraphs = prev.split("####")[1:] header = word = text = '' for paragraph in paragraphs: header, text = paragraph.split('\n', 1) testing = str( test[header.strip()].obj_to_mark_down(title_columns=False)) text = text.replace("```\n# comment\n```", "").strip() for word in ':- ': text = text.replace(word, '') testing = text.replace(word, '') testing = testing.replace(word, '') log.debug('\nAnswer:\n%s\n\nResult:\n%s\n\n' % (text, testing)) self.assertEqual( text, testing, "Values don't match:\n%s\n%s" % (repr(testing), repr(text)))
def get_base_table(self): return SeabornTable.file_to_obj( file_path=self.get_data_path('test_file.rst'))
def validate_test_condition(self, source): file_path = self.get_data_path('test_file.%s' % source) table = SeabornTable.file_to_obj(eval_cells=False, file_path=file_path) assert isinstance(table[0]['TU'], BASESTRING)