def test_to_html_with_headers_and_some_rows(self): my_table = Table(headers=['ham', 'spam', 'eggs']) my_table.append(['python', 'rules', '!']) my_table.append({'ham': 'spam', 'spam': 'eggs', 'eggs': 'ham'}) output = my_table.write('html', css_classes=False) expected = dedent(''' <table> <thead> <tr> <th>ham</th> <th>spam</th> <th>eggs</th> </tr> </thead> <tbody> <tr> <td>python</td> <td>rules</td> <td>!</td> </tr> <tr> <td>spam</td> <td>eggs</td> <td>ham</td> </tr> </tbody> </table> ''').strip() self.assertEquals(output, expected)
def test_to_html_with_a_parameter_should_save_a_file(self): temp_fp = tempfile.NamedTemporaryFile(delete=False) temp_fp.close() my_table = Table(headers=['ham', 'spam', 'eggs']) my_table.append(['python', 'rules', '!']) my_table.append({'ham': 'spam', 'spam': 'eggs', 'eggs': 'ham'}) my_table.write('html', temp_fp.name, css_classes=False) temp_fp = open(temp_fp.name) output = temp_fp.read() temp_fp.close() os.remove(temp_fp.name) expected = dedent(''' <table> <thead> <tr> <th>ham</th> <th>spam</th> <th>eggs</th> </tr> </thead> <tbody> <tr> <td>python</td> <td>rules</td> <td>!</td> </tr> <tr> <td>spam</td> <td>eggs</td> <td>ham</td> </tr> </tbody> </table> ''').strip() self.assertEquals(output, expected)
def test_to_dict_should_filter_create_dict_from_values(self): table = Table(headers=["spam", "eggs", "ham"]) table.append([42, 3.14, 2.71]) table.append(["python", "rules", "yeh"]) table_dict = table.to_dict(key="spam", value="ham") expected = {42: 2.71, "python": "yeh"} self.assertEqual(table_dict, expected)
def test_to_dict_should_filter_some_columns(self): table = Table(headers=["spam", "eggs", "ham"]) table.append([42, 3.14, 2.71]) table.append(["python", "rules", "yeh"]) table_dict = table.to_dict(only=("eggs", "ham")) expected = {"eggs": [3.14, "rules"], "ham": [2.71, "yeh"]} self.assertEqual(table_dict, expected)
def test_table_extend_should_add_nothing_if_an_exception_is_raised(self): table = Table(headers=['spam', 'eggs']) table.append(['hello', 'world']) with self.assertRaises(ValueError): table.extend([['python', 'rules'], ['answer', 42], [1, 2, 3]]) self.assertEquals(table[0], ['hello', 'world']) self.assertEquals(len(table), 1)
def test_should_not_indentify_non_fractional_floats_as_int(self): table = Table(headers=['ham']) table.append([1.0]) table.append([2.0]) table.append([3.0]) table._identify_type_of_data() self.assertEqual(table.types['ham'], float)
def test_delete_item_passing_string_should_delete_a_entire_column(self): table = Table(headers=["spam", "eggs", "ham"]) table.append(["python", 3.14, 1 + 5j]) table.append(["rules", 42, 3 + 4j]) del table["eggs"] self.assertEquals(table.headers, ["spam", "ham"]) self.assertEquals(table[:], [["python", 1 + 5j], ["rules", 3 + 4j]])
def test_to_dict_should_filter_some_columns(self): table = Table(headers=['spam', 'eggs', 'ham']) table.append([42, 3.14, 2.71]) table.append(['python', 'rules', 'yeh']) table_dict = table.to_dict(only=('eggs', 'ham')) expected = {'eggs': [3.14, 'rules'], 'ham': [2.71, 'yeh']} self.assertEqual(table_dict, expected)
def test_to_dict_should_create_a_dict_with_column_names_and_values(self): table = Table(headers=['spam', 'eggs']) table.append([42, 3.14]) table.append(['python', 'rules']) table_dict = table.to_dict() expected = {'spam': [42, 'python'], 'eggs': [3.14, 'rules']} self.assertEqual(table_dict, expected)
def test_delete_item_passing_string_should_delete_a_entire_column(self): table = Table(headers=['spam', 'eggs', 'ham']) table.append(['python', 3.14, 1 + 5j]) table.append(['rules', 42, 3 + 4j]) del table['eggs'] self.assertEquals(table.headers, ['spam', 'ham']) self.assertEquals(table[:], [['python', 1 + 5j], ['rules', 3 + 4j]])
def test_to_dict_should_filter_create_dict_from_values(self): table = Table(headers=['spam', 'eggs', 'ham']) table.append([42, 3.14, 2.71]) table.append(['python', 'rules', 'yeh']) table_dict = table.to_dict(key='spam', value='ham') expected = {42: 2.71, 'python': 'yeh'} self.assertEqual(table_dict, expected)
def test_read_should_automatically_identify_data_types(self): self.connection.query('DROP TABLE ' + self.table) self.connection.commit() table = Table(headers=['spam', 'eggs', 'ham', 'monty', 'python']) numbers = range(1000) for i in numbers: table.append([ i, i + 0.1, 'some string', '2012-01-01', '2011-12-31 23:59:59' ]) table.write('mysql', self.connection_string) new_table = Table() new_table.read('mysql', self.connection_string) self.assertEquals(len(new_table), 1000) self.assertEquals(new_table.types['spam'], int) self.assertEquals(new_table.types['eggs'], float) self.assertEquals(new_table.types['ham'], str) self.assertEquals(new_table.types['monty'], datetime.date) self.assertEquals(new_table.types['python'], datetime.datetime) connection_string = '/'.join(self.connection_string.split('/')[:-1]) other_table = Table() other_table.read('mysql', connection_string, query='SELECT spam, ham, python FROM ' + self.table) self.assertEquals(len(other_table), 1000) self.assertEquals(len(other_table.types), 3) self.assertEquals(other_table.types['spam'], int) self.assertEquals(other_table.types['ham'], str) self.assertEquals(other_table.types['python'], datetime.datetime)
def test_table_extend_should_add_nothing_if_an_exception_is_raised(self): table = Table(headers=["spam", "eggs"]) table.append(["hello", "world"]) with self.assertRaises(ValueError): table.extend([["python", "rules"], ["answer", 42], [1, 2, 3]]) self.assertEquals(table[0], ["hello", "world"]) self.assertEquals(len(table), 1)
def test_to_dict_should_create_a_dict_with_column_names_and_values(self): table = Table(headers=["spam", "eggs"]) table.append([42, 3.14]) table.append(["python", "rules"]) table_dict = table.to_dict() expected = {"spam": [42, "python"], "eggs": [3.14, "rules"]} self.assertEqual(table_dict, expected)
def test_should_not_indentify_non_fractional_floats_as_int(self): table = Table(headers=["ham"]) table.append([1.0]) table.append([2.0]) table.append([3.0]) table._identify_type_of_data() self.assertEqual(table.types["ham"], float)
def test_get_item_should_return_column_values_when_passing_string(self): table = Table(headers=["spam", "eggs"]) table.append(["python", 3.14]) table.append(["rules", 42]) spam_column = ["python", "rules"] eggs_column = [3.14, 42] self.assertEqual(table["spam"], spam_column) self.assertEqual(table["eggs"], eggs_column)
def test_get_item_should_return_column_values_when_passing_string(self): table = Table(headers=['spam', 'eggs']) table.append(['python', 3.14]) table.append(['rules', 42]) spam_column = ['python', 'rules'] eggs_column = [3.14, 42] self.assertEqual(table['spam'], spam_column) self.assertEqual(table['eggs'], eggs_column)
def test_decode_method_should_normalize_and_use_input_encoding(self): my_table = Table( headers=[u"ham".encode("utf16"), u"spam".encode("utf16"), u"eggs".encode("utf16")], input_encoding="utf16" ) my_table.append((123, 456, 789)) my_table.append((987, 654, u"python".encode("utf16"))) my_table.decode() my_table.encode() self.assertEquals(my_table[1][2], u"python")
def test_should_deal_correctly_with_quotes(self): self.connection.query('DROP TABLE ' + self.table) self.connection.commit() table = Table(headers=['eggs']) table.append(['spam"ham']) table.write('mysql', self.connection_string) table_2 = Table() table_2.read('mysql', self.connection_string) self.assertEquals(table_2[0][0], 'spam"ham')
def test_empty_string_should_not_affect_data_type(self): table = Table(headers=["spam", "eggs", "ham", "Monty", "Python"]) table.append([1, 2.71, "2011-01-01", "2011-01-01 00:00:00", "asd"]) table.append(["", "", "", "", ""]) table._identify_type_of_data() self.assertEquals(table.types["spam"], int) self.assertEquals(table.types["eggs"], float) self.assertEquals(table.types["ham"], datetime.date) self.assertEquals(table.types["Monty"], datetime.datetime) self.assertEquals(table.types["Python"], str)
def test_write_csv_should_accept_filepointer(self): temp_fp = tempfile.NamedTemporaryFile() my_table = Table(headers=['Álvaro']) my_table.append(['Píton']) my_table.write('csv', temp_fp) expected = '"Álvaro"\n"Píton"\n' temp_fp.seek(0) output = temp_fp.read() temp_fp.close() self.assertEqual(output, expected)
def test_should_indentify_type_str_correctly(self): table = Table(headers=["eggs", "ham"]) table.append(["spam eggs", 1]) table.append(["spam spam", 3.14]) table.append(["eggs spam", "testing"]) table.append(["spam spam", "2011-11-23"]) table.append(["spam ham", "2011-11-23 02:00:17"]) table._identify_type_of_data() self.assertEqual(table.types["eggs"], str) self.assertEqual(table.types["ham"], str)
def test_empty_string_should_not_affect_data_type(self): table = Table(headers=['spam', 'eggs', 'ham', 'Monty', 'Python']) table.append([1, 2.71, '2011-01-01', '2011-01-01 00:00:00', 'asd']) table.append(['', '', '', '', '']) table._identify_type_of_data() self.assertEquals(table.types['spam'], int) self.assertEquals(table.types['eggs'], float) self.assertEquals(table.types['ham'], datetime.date) self.assertEquals(table.types['Monty'], datetime.datetime) self.assertEquals(table.types['Python'], str)
def test_empty_string_should_not_affect_data_type(self): table = Table(headers=['spam', 'eggs', 'ham', 'Monty', 'Python']) table.append([1, 2.71, '2011-01-01', '2011-01-01 00:00:00', 'asd']) table.append(['', '', '', '', '']) table._identify_type_of_data() self.assertEquals(table.types['spam'], int) self.assertEquals(table.types['eggs'], float) self.assertEquals(table.types['ham'], datetime.date) self.assertEquals(table.types['Monty'], datetime.datetime) self.assertEquals(table.types['Python'], str)
def test_read_should_accept_order_by(self): self.connection.query('DROP TABLE ' + self.table) self.connection.commit() table = Table(headers=['number']) numbers = range(1000) for i in numbers: table.append([i]) table.write('mysql', self.connection_string) new_table = Table() new_table.read('mysql', self.connection_string, order_by='number desc') self.assertEquals(new_table[:], [[x] for x in numbers[::-1]])
def test_write_should_add_rows_correctly(self): self.connection.query('DROP TABLE ' + self.table) self.connection.commit() table = Table(headers=['spam', 'eggs']) table.append(['python', 'rules']) table.append(['free software', 'ownz']) table.write('mysql', self.connection_string) self.cursor.execute('SELECT * FROM ' + self.table) rows = [row for row in self.cursor.fetchall()] self.assertEquals(rows, [('python', 'rules'), ('free software', 'ownz')])
def test_running_identify_data_type_with_normalized_types_should_return_correct_results(self): table = Table(headers=['spam', 'eggs', 'ham', 'Monty', 'Python']) table.append([1, 2.71, '2011-01-01', '2011-01-01 00:00:00', 'asd']) table.append(['', '', '', '', '']) table.normalize_types() table._identify_type_of_data() self.assertEquals(table.types['spam'], int) self.assertEquals(table.types['eggs'], float) self.assertEquals(table.types['ham'], datetime.date) self.assertEquals(table.types['Monty'], datetime.datetime) self.assertEquals(table.types['Python'], str)
def test_decode_method_should_normalize_and_use_input_encoding(self): my_table = Table(headers=[ u'ham'.encode('utf16'), u'spam'.encode('utf16'), u'eggs'.encode('utf16') ], input_encoding='utf16') my_table.append((123, 456, 789)) my_table.append((987, 654, u'python'.encode('utf16'))) my_table.decode() my_table.encode() self.assertEquals(my_table[1][2], u'python')
def test_None_should_be_saved_as_NULL_and_returned_as_None(self): self.connection.query('DROP TABLE ' + self.table) self.connection.commit() table = Table(headers=['spam', 'eggs', 'ham', 'Monty', 'Python']) table.append([1, 2.71, '2011-01-01', '2011-01-01 00:00:00', 'asd']) table.append([None, None, None, None, None]) table.write('mysql', self.connection_string) self.cursor.execute('SELECT * FROM ' + self.table) rows = self.cursor.fetchall() for i in range(5): self.assertIs(rows[-1][i], None)
def test_should_accept_None_in_rows(self): my_table = Table(headers=['spam']) my_table.append([None]) self.assertEqual( unicode(my_table), dedent(''' +------+ | spam | +------+ | None | +------+ ''').strip())
def test_table_with_many_headers_and_one_row_same_size(self): my_table = Table(headers=['ham', 'spam', 'eggs']) my_table.append({'ham': 123, 'spam': 4567, 'eggs': 8910}) self.assertEqual( str(my_table), dedent(''' +-----+------+------+ | ham | spam | eggs | +-----+------+------+ | 123 | 4567 | 8910 | +-----+------+------+ ''').strip())
def test_running_identify_data_type_with_normalized_types_should_return_correct_results( self): table = Table(headers=['spam', 'eggs', 'ham', 'Monty', 'Python']) table.append([1, 2.71, '2011-01-01', '2011-01-01 00:00:00', 'asd']) table.append(['', '', '', '', '']) table.normalize_types() table._identify_type_of_data() self.assertEquals(table.types['spam'], int) self.assertEquals(table.types['eggs'], float) self.assertEquals(table.types['ham'], datetime.date) self.assertEquals(table.types['Monty'], datetime.datetime) self.assertEquals(table.types['Python'], str)
def test_write_should_slugfy_column_names(self): self.connection.query('DROP TABLE ' + self.table) self.connection.commit() table = Table(headers=['col with spaces', 'col-with-dashes']) table.append(['testing', 123]) table.append(['testing again', 456]) table.write('mysql', self.connection_string) other_table = Table() other_table.read('mysql', self.connection_string) self.assertEquals(other_table.headers, ['col_with_spaces', 'col_with_dashes'])
def test_None_in_rows(self): my_table = Table(headers=['a', 'b', 'c']) my_table.append([None, None, None]) self.assertEqual( str(my_table), dedent(''' +------+------+------+ | a | b | c | +------+------+------+ | None | None | None | +------+------+------+ ''').strip())
def test_table_set_item_should_work_for_rows(self): table = Table(headers=["python", "rules"]) table.append([1, 2]) table[0] = [4, 2] self.assertEquals(table[:], [[4, 2]]) table.extend([[3, 4], [5, 6]]) table[1:] = [[7, 8], [9, 0]] self.assertEquals(table[:], [[4, 2], [7, 8], [9, 0]]) table[-3:] = [[5, 5], [7, 7], [9, 9]] self.assertEquals(table[:], [[5, 5], [7, 7], [9, 9]]) with self.assertRaises(ValueError): table[1] = [1, 2, 3] with self.assertRaises(ValueError): table[(1, 2)] = [1, 2]
def test_character_count_in_row_data_should_use_unicode(self): my_table = Table(headers=['First name', 'Last name']) my_table.append({'First name': 'Álvaro', 'Last name': 'Justen'}) my_table.append(('Flávio', 'Amieiro')) self.assertEqual( str(my_table), dedent(''' +------------+-----------+ | First name | Last name | +------------+-----------+ | Álvaro | Justen | | Flávio | Amieiro | +------------+-----------+ ''').strip())
def test_table_set_item_should_work_for_rows(self): table = Table(headers=['python', 'rules']) table.append([1, 2]) table[0] = [4, 2] self.assertEquals(table[:], [[4, 2]]) table.extend([[3, 4], [5, 6]]) table[1:] = [[7, 8], [9, 0]] self.assertEquals(table[:], [[4, 2], [7, 8], [9, 0]]) table[-3:] = [[5, 5], [7, 7], [9, 9]] self.assertEquals(table[:], [[5, 5], [7, 7], [9, 9]]) with self.assertRaises(ValueError): table[1] = [1, 2, 3] with self.assertRaises(ValueError): table[(1, 2)] = [1, 2]
def test_output_character_encoding_in_method___str__(self): my_table = Table(headers=['Álvaro'.decode('utf8').encode('utf16')], input_encoding='utf16', output_encoding='iso-8859-1') my_table.append(['Píton'.decode('utf8').encode('utf16')]) output = dedent(''' +--------+ | Álvaro | +--------+ | Píton | +--------+ ''').strip().decode('utf8').encode('iso-8859-1') self.assertEqual(str(my_table), output)
def test_should_insert_data_in_correct_order(self): self.connection.query('DROP TABLE ' + self.table) self.connection.commit() table = Table(headers=['name', 'age']) table.append(['Justen', 24]) table.append(['Someone', 99]) table.write('mysql', self.connection_string) self.cursor.execute('SELECT * FROM ' + self.table) rows = [x for x in self.cursor.fetchall()] self.assertEquals(len(rows), 2) self.assertEquals(rows[0][0], 'Justen') self.assertEquals(rows[0][1], 24) self.assertEquals(rows[1][0], 'Someone') self.assertEquals(rows[1][1], 99)
def test_character_count_in_headers_should_be_unicode(self): my_table = Table(headers=['ÁÀÃÂÇ', 'ÇÉÈẼÊ']) my_table.append(('spam', 'eggs')) my_table.append(('eggs', 'spam')) self.assertEqual( str(my_table), dedent(''' +-------+-------+ | ÁÀÃÂÇ | ÇÉÈẼÊ | +-------+-------+ | spam | eggs | | eggs | spam | +-------+-------+ ''').strip())
def test_input_and_output_encoding_should_affect_method_write_csv(self): temp_fp = tempfile.NamedTemporaryFile(delete=False) temp_fp.close() my_table = Table(headers=['Álvaro'.decode('utf8').encode('utf16')], input_encoding='utf16', output_encoding='iso-8859-1') my_table.append(['Píton'.decode('utf8').encode('utf16')]) my_table.write('csv', temp_fp.name) fp = open(temp_fp.name) file_contents = fp.read() fp.close() os.remove(temp_fp.name) output = '"Álvaro"\n"Píton"\n'.decode('utf8').encode('iso-8859-1') self.assertEqual(file_contents, output)
def test_should_indentify_type_float_correctly(self): table = Table(headers=['ham']) table.append(["3"]) table.append(["3.14"]) table.append([""]) table.append(["2.71"]) table._identify_type_of_data() self.assertEqual(table.types['ham'], float)
def test_should_accept_None_in_rows(self): my_table = Table(headers=["spam"]) my_table.append([None]) self.assertEqual( unicode(my_table), dedent( """ +------+ | spam | +------+ | None | +------+ """ ).strip(), )
def test_should_indentify_type_float_correctly(self): table = Table(headers=['ham']) table.append(["3"]) table.append(["3.14"]) table.append([""]) table.append(["2.71"]) table._identify_type_of_data() self.assertEqual(table.types['ham'], float)
def test_None_in_rows(self): my_table = Table(headers=["a", "b", "c"]) my_table.append([None, None, None]) self.assertEqual( str(my_table), dedent( """ +------+------+------+ | a | b | c | +------+------+------+ | None | None | None | +------+------+------+ """ ).strip(), )
def test_table_with_many_headers_and_one_row_same_size(self): my_table = Table(headers=["ham", "spam", "eggs"]) my_table.append({"ham": 123, "spam": 4567, "eggs": 8910}) self.assertEqual( str(my_table), dedent( """ +-----+------+------+ | ham | spam | eggs | +-----+------+------+ | 123 | 4567 | 8910 | +-----+------+------+ """ ).strip(), )
def test_table_with_headers_little_than_rows(self): my_table = Table(headers=["ham", "spam", "eggs"]) my_table.append({"ham": "ham spam ham", "spam": "spam eggs spam", "eggs": "eggs ham eggs"}) self.assertEqual( str(my_table), dedent( """ +--------------+----------------+---------------+ | ham | spam | eggs | +--------------+----------------+---------------+ | ham spam ham | spam eggs spam | eggs ham eggs | +--------------+----------------+---------------+ """ ).strip(), )
def test_ordering_unicode(self): my_table = Table(headers=['spam']) my_table.append(['á']) my_table.append(['Á']) my_table.order_by('spam') self.assertEqual( str(my_table), dedent(''' +------+ | spam | +------+ | Á | | á | +------+ ''').strip())
def test___unicode__should_return_unicode_no_matter_the_input_encoding( self): my_table = Table(headers=['ÁÀÃÂÇ', 'ÇÉÈẼÊ']) my_table.append(('spam', 'eggs')) my_table.append(('eggs', 'spam')) self.assertEqual( unicode(my_table), dedent(''' +-------+-------+ | ÁÀÃÂÇ | ÇÉÈẼÊ | +-------+-------+ | spam | eggs | | eggs | spam | +-------+-------+ ''').strip().decode('utf8'))
def test_table_with_many_headers_and_rows_same_size(self): my_table = Table(headers=['ham', 'spam', 'eggs']) my_table.append({'ham': 123, 'spam': 4567, 'eggs': 8910}) my_table.append({'ham': 321, 'spam': 7654, 'eggs': 1098}) my_table.append({'ham': 'abc', 'spam': 'defg', 'eggs': 'hijk'}) self.assertEqual( str(my_table), dedent(''' +-----+------+------+ | ham | spam | eggs | +-----+------+------+ | 123 | 4567 | 8910 | | 321 | 7654 | 1098 | | abc | defg | hijk | +-----+------+------+ ''').strip())
def test_character_count_in_row_data_should_use_unicode(self): my_table = Table(headers=["First name", "Last name"]) my_table.append({"First name": "Álvaro", "Last name": "Justen"}) my_table.append(("Flávio", "Amieiro")) self.assertEqual( str(my_table), dedent( """ +------------+-----------+ | First name | Last name | +------------+-----------+ | Álvaro | Justen | | Flávio | Amieiro | +------------+-----------+ """ ).strip(), )
def test_character_count_in_headers_should_be_unicode(self): my_table = Table(headers=["ÁÀÃÂÇ", "ÇÉÈẼÊ"]) my_table.append(("spam", "eggs")) my_table.append(("eggs", "spam")) self.assertEqual( str(my_table), dedent( """ +-------+-------+ | ÁÀÃÂÇ | ÇÉÈẼÊ | +-------+-------+ | spam | eggs | | eggs | spam | +-------+-------+ """ ).strip(), )
def test_should_be_able_to_change_delimiters_on_write(self): temp_fp = tempfile.NamedTemporaryFile() temp_fp.close() my_table = Table(headers=['ham', 'spam', 'eggs']) my_table.append({'ham': 'ham spam ham', 'spam': 'spam eggs spam', 'eggs': 'eggs ham eggs'}) my_table.write('csv', temp_fp.name, delimiter=';', quote_char="'", line_terminator='\r\n') fp = open(temp_fp.name) contents = fp.read() fp.close() os.remove(temp_fp.name) self.assertEquals(contents, dedent('''\ 'ham';'spam';'eggs'\r 'ham spam ham';'spam eggs spam';'eggs ham eggs'\r '''))
def test_ordering_unicode(self): my_table = Table(headers=["spam"]) my_table.append(["á"]) my_table.append(["Á"]) my_table.order_by("spam") self.assertEqual( str(my_table), dedent( """ +------+ | spam | +------+ | Á | | á | +------+ """ ).strip(), )
def test_ordering_table_with_one_column(self): my_table = Table(headers=["spam"]) my_table.append(["ham"]) my_table.append(["eggs"]) my_table.append(["idle"]) my_table.order_by("spam") output = dedent( """ +------+ | spam | +------+ | eggs | | ham | | idle | +------+ """ ).strip() self.assertEqual(str(my_table), output)
def test_write_csv_should_create_the_file_correctly_with_headers(self): temp_fp = tempfile.NamedTemporaryFile() temp_fp.close() my_table = Table(headers=['ham', 'spam', 'eggs']) my_table.append({'ham': 'ham spam ham', 'spam': 'spam eggs spam', 'eggs': 'eggs ham eggs'}) my_table.write('csv', temp_fp.name) fp = open(temp_fp.name) contents = fp.read() fp.close() os.remove(temp_fp.name) self.assertEquals(contents, dedent('''\ "ham","spam","eggs" "ham spam ham","spam eggs spam","eggs ham eggs" '''))
def test_ordering_two_columns_table_by_second_header(self): my_table = Table(headers=["spam", "ham"]) my_table.append(("eggs", "ham")) my_table.append(("ham", "eggs")) my_table.append(("ham", "123")) my_table.order_by("ham") output = dedent( """ +------+------+ | spam | ham | +------+------+ | ham | 123 | | ham | eggs | | eggs | ham | +------+------+ """ ).strip() self.assertEqual(str(my_table), output)
def test_order_by_method_should_order_data_internally(self): my_table = Table(headers=["spam", "ham", "eggs"]) my_table.append({"spam": "Eric", "eggs": "Idle"}) my_table.append({"ham": "John", "eggs": "Cleese"}) my_table.append({"ham": "Terry", "spam": "Jones"}) my_table.order_by("spam", "asc") expected_output = dedent( """ +-------+-------+--------+ | spam | ham | eggs | +-------+-------+--------+ | None | John | Cleese | | Eric | None | Idle | | Jones | Terry | None | +-------+-------+--------+ """ ).strip() self.assertEqual(str(my_table), expected_output)