def test_table_append_column_should_raise_ValueError_when_wrong_size(self): table = Table(headers=["python", "rules"]) table.extend([[1, 2], [3, 4], [5, 6]]) with self.assertRaises(ValueError): table.append_column("new column", [3, 5, 7, 8]) self.assertEquals(table.headers, ["python", "rules"]) self.assertEquals(table[:], [[1, 2], [3, 4], [5, 6]])
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_should_save_data_into_text_file(self): temp_fp = tempfile.NamedTemporaryFile(delete=False) temp_fp.close() my_table = Table(headers=['ham', 'spam', 'eggs']) my_table.append({'ham': '', 'spam': '', 'eggs': ''}) my_table.append({'ham': 1, 'spam': 2, 'eggs': 3}) my_table.append({'ham': 11, 'spam': 22, 'eggs': 33}) my_table.write('text', temp_fp.name) output = my_table.write('text') fp = open(temp_fp.name, 'r') contents = fp.read() fp.close() os.remove(temp_fp.name) self.assertEqual(contents, dedent(''' +-----+------+------+ | ham | spam | eggs | +-----+------+------+ | | | | | 1 | 2 | 3 | | 11 | 22 | 33 | +-----+------+------+ ''').strip()) self.assertEquals(contents, output)
def test_append_column_should_raise_ValueError_when_column_already_exists(self): table = Table(headers=["python", "rules"]) table.extend([[1, 2], [3, 4], [5, 6]]) with self.assertRaises(ValueError): table.append_column("python", [3, 5, 7]) self.assertEquals(table.headers, ["python", "rules"]) self.assertEquals(table[:], [[1, 2], [3, 4], [5, 6]])
def test_read_csv_should_automatically_convert_data_types(self): data = dedent(''' "spam","eggs","ham" "42","3","2011-01-02" "","3.14","2012-01-11" "21","","2010-01-03" "2","2.71","" ''') temp_fp = tempfile.NamedTemporaryFile(delete=False) temp_fp.write(data) temp_fp.close() my_table = Table() my_table.read('csv', temp_fp.name) os.remove(temp_fp.name) self.assertEquals(type(my_table[0][0]), int) self.assertEquals(type(my_table[1][0]), type(None)) self.assertEquals(type(my_table[2][0]), int) self.assertEquals(type(my_table[3][0]), int) self.assertEquals(type(my_table[0][1]), float) self.assertEquals(type(my_table[1][1]), float) self.assertEquals(type(my_table[2][1]), type(None)) self.assertEquals(type(my_table[3][1]), float) self.assertEquals(type(my_table[0][2]), datetime.date) self.assertEquals(type(my_table[1][2]), datetime.date) self.assertEquals(type(my_table[2][2]), datetime.date) self.assertEquals(type(my_table[3][2]), type(None))
def test_to_dict_should_handle_encodings_correctly(self): table = Table(headers=['spam', 'eggs', 'ham'], input_encoding='iso-8859-1', output_encoding='utf16') table.append([42, 3.14, 2.71]) table.append(['python', 'rules', 'yeh']) table.append([u'Álvaro'.encode('iso-8859-1'), '...', 'Justen']) table_dict = table.to_dict(key='spam', value='ham') expected = { 42: 2.71, u'python'.encode('utf16'): u'yeh'.encode('utf16'), u'Álvaro'.encode('utf16'): u'Justen'.encode('utf16') } self.assertEqual(table_dict, expected) table_dict_2 = table.to_dict() expected_2 = { u'spam'.encode('utf16'): [42, u'python'.encode('utf16'), u'Álvaro'.encode('utf16')], u'eggs'.encode('utf16'): [3.14, u'rules'.encode('utf16'), u'...'.encode('utf16')], u'ham'.encode('utf16'): [2.71, u'yeh'.encode('utf16'), u'Justen'.encode('utf16')] } self.assertEqual(table_dict_2, expected_2)
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_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_table_should_be_a_sequence(self): table = Table(headers=["spam", "eggs"]) table.extend([["python", "rules"], ["answer", 42]]) items = [] for item in table: items.append(item) self.assertEquals(items[0], ["python", "rules"]) self.assertEquals(items[1], ["answer", 42])
def test_should_import_nothing_from_empty_csv_without_exceptions(self): temp_fp = tempfile.NamedTemporaryFile(delete=False) temp_fp.close() my_table = Table() my_table.read('csv', temp_fp.name) os.remove(temp_fp.name) self.assertEquals(str(my_table), '')
def test_headers_of_one_table_should_not_affect_other(self): table_1 = Table() table_1.headers.append('spam') table_1.headers.append('eggs') table_2 = Table() self.assertEquals(len(table_1.headers), 2) self.assertEquals(len(table_2.headers), 0)
def test_table_should_be_a_sequence(self): table = Table(headers=['spam', 'eggs']) table.extend([['python', 'rules'], ['answer', 42]]) items = [] for item in table: items.append(item) self.assertEquals(items[0], ['python', 'rules']) self.assertEquals(items[1], ['answer', 42])
def test_write_should_create_table_even_if_only_headers_present(self): self.connection.query('DROP TABLE ' + self.table) self.connection.commit() table = Table(headers=['spam', 'eggs']) table.write('mysql', self.connection_string) self.cursor.execute('SELECT * FROM ' + self.table) cols = [x[0] for x in self.cursor.description] self.assertEquals(set(cols), set(['spam', 'eggs']))
def test_table_set_item_should_work_for_column(self): table = Table(headers=['python', 'rules']) table.extend([[1, 2], [3, 4], [5, 6]]) table['python'] = [2, 4, 6] self.assertEquals(table[:], [[2, 2], [4, 4], [6, 6]]) with self.assertRaises(KeyError): should_raise_exception = table['not-found'] with self.assertRaises(ValueError): table['rules'] = [1, 2, 3, 4]
def test_table_set_item_should_work_for_column(self): table = Table(headers=["python", "rules"]) table.extend([[1, 2], [3, 4], [5, 6]]) table["python"] = [2, 4, 6] self.assertEquals(table[:], [[2, 2], [4, 4], [6, 6]]) with self.assertRaises(KeyError): should_raise_exception = table["not-found"] with self.assertRaises(ValueError): table["rules"] = [1, 2, 3, 4]
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_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_identify_connection_parameters_with_custom_port(self): table = Table() plugin_mysql = table._load_plugin('mysql') config, table_name = plugin_mysql._get_mysql_config('u:p@h:0/d/t') self.assertEquals(config['user'], 'u') self.assertEquals(config['passwd'], 'p') self.assertEquals(config['host'], 'h') self.assertEquals(config['port'], 0) self.assertEquals(config['db'], 'd') self.assertEquals(table_name, 't')
def test_ordering_table_without_data(self): my_table = Table(headers=['ham', 'spam', 'eggs']) my_table.order_by('spam') self.assertEqual( str(my_table), dedent(''' +-----+------+------+ | ham | spam | eggs | +-----+------+------+ ''').strip())
def test_to_html_with_headers_and_rows_with_some_columns_empty(self): my_table = Table(headers=['ham', 'spam', 'eggs']) my_table.append({'ham': 'spam'}) my_table.append({'spam': 'eggs'}) my_table.append({'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>spam</td> <td></td> <td></td> </tr> <tr> <td></td> <td>eggs</td> <td></td> </tr> <tr> <td></td> <td></td> <td>ham</td> </tr> </tbody> </table> ''').strip() self.assertEquals(output, expected)
def test_table_index(self): table = Table(headers=['python', 'rules']) table.extend([[1, 2], [3, 4], [5, 6], [7, 8], [1, 2], [9, 0]]) self.assertEquals(table.index([1, 2]), 0) self.assertEquals(table.index({'python': 1, 'rules': 2}), 0) self.assertEquals(table.index([5, 6]), 2) self.assertEquals(table.index([1, 2], 1), 4) with self.assertRaises(ValueError): non_ecxiste = table.index([1, 9]) with self.assertRaises(ValueError): not_found = table.index([1, 2], 1, 3)
def test_normalize_types_should_convert_types_correctly(self): table = Table(headers=['spam', 'eggs', 'ham', 'Monty', 'Python']) table.append(['1', '2.71', '2011-01-01', '2011-01-01 02:03:04', 'asd']) table.append([None, None, None, None, None]) table.append([None, None, None, None, 42]) table.normalize_types() self.assertEquals(table[0][0], 1) self.assertEquals(table[0][1], 2.71) self.assertEquals(table[0][2], datetime.date(2011, 1, 1)) self.assertEquals(table[0][3], datetime.datetime(2011, 1, 1, 2, 3, 4)) self.assertEquals(table[0][4], 'asd')
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_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_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_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_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_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_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_ordering_table_without_data(self): my_table = Table(headers=["ham", "spam", "eggs"]) my_table.order_by("spam") self.assertEqual( str(my_table), dedent( """ +-----+------+------+ | ham | spam | eggs | +-----+------+------+ """ ).strip(), )
def test_table_remove(self): table = Table(headers=['python', 'rules']) table.extend([[1, 2], [3, 4], [5, 6], [1, 2]]) table.remove({'python': 3, 'rules': 4}) self.assertEquals(table[:], [[1, 2], [5, 6], [1, 2]]) table.remove([1, 2]) self.assertEquals(table[:], [[5, 6], [1, 2]])
def test_input_and_output_character_encoding_in_method_to_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.rows.append(['Píton'.decode('utf8').encode('utf16')]) my_table.to_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_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_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_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_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_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_normalize_types_should_convert_types_correctly(self): table = Table(headers=["spam", "eggs", "ham", "Monty", "Python"]) table.append(["1", "2.71", "2011-01-01", "2011-01-01 02:03:04", "asd"]) table.append([None, None, None, None, None]) table.append([None, None, None, None, 42]) table.normalize_types() self.assertEquals(table[0][0], 1) self.assertEquals(table[0][1], 2.71) self.assertEquals(table[0][2], datetime.date(2011, 1, 1)) self.assertEquals(table[0][3], datetime.datetime(2011, 1, 1, 2, 3, 4)) self.assertEquals(table[0][4], "asd")
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_table_index(self): table = Table(headers=["python", "rules"]) table.extend([[1, 2], [3, 4], [5, 6], [7, 8], [1, 2], [9, 0]]) self.assertEquals(table.index([1, 2]), 0) self.assertEquals(table.index({"python": 1, "rules": 2}), 0) self.assertEquals(table.index([5, 6]), 2) self.assertEquals(table.index([1, 2], 1), 4) with self.assertRaises(ValueError): non_ecxiste = table.index([1, 9]) with self.assertRaises(ValueError): not_found = table.index([1, 2], 1, 3)
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___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_should_import_data_from_csv_with_only_one_line(self): temp_fp = tempfile.NamedTemporaryFile(delete=False) temp_fp.write(dedent('''\ "ham","spam","eggs" ''')) temp_fp.close() my_table = Table() my_table.read('csv', temp_fp.name) os.remove(temp_fp.name) self.assertEquals(str(my_table), dedent(''' +-----+------+------+ | ham | spam | eggs | +-----+------+------+ ''').strip())
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_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_table_insert(self): table = Table(headers=['python', 'rules']) table.extend([[1, 2], [3, 4], [5, 6], [7, 8], [1, 2]]) table.insert(0, [4, 2]) table.insert(1, {'python': 9, 'rules': 9}) self.assertEquals(table[0], [4, 2]) self.assertEquals(table[1], [9, 9]) self.assertEquals(len(table), 7)
def test_normalize_method_should_transform_all_rows_to_lists(self): table = Table(headers=['spam', 'eggs', 'ham']) table.append(['ham', 'eggs', 'spam']) table.append({'ham': 42}) table.append({'eggs': 3.14, 'spam': 2.71}) expected = [['ham', 'eggs', 'spam'], [None, None, 42], [2.71, 3.14, None]] self.assertEqual(table[:], expected)
def test_vertical_histogram(self): seed(1234) # Setting the seed to get repeatable results numbers = normal(size=1000) my_table = Table(headers=['values']) my_table.extend([[value] for value in numbers]) output = my_table.write('histogram', column='values', height=5, orientation='vertical', bins=10) expected = dedent(''' 265 | || ||| |||| |||||| -3.56 2.76 ''').strip() self.assertEquals(output, expected)
def test_input_and_output_encoding_should_affect_read_csv(self): data = '"Álvaro"\n"Píton"' temp_fp = tempfile.NamedTemporaryFile(delete=False) temp_fp.write(data.decode('utf8').encode('iso-8859-1')) temp_fp.close() my_table = Table(input_encoding='iso-8859-1', output_encoding='utf16') my_table.read('csv', temp_fp.name) os.remove(temp_fp.name) output = dedent(''' +--------+ | Álvaro | +--------+ | Píton | +--------+ ''').strip().decode('utf8').encode('utf16') self.assertEqual(str(my_table), output)
def test_write_csv_without_filename_should_return_csv_data(self): data = dedent(''' "spam","eggs","ham" "42","3.0","2011-01-02" "1","3.14","2012-01-11" "21","6.28","2010-01-03" "2","2.71","2" ''').strip() + '\n' temp_fp = tempfile.NamedTemporaryFile(delete=False) temp_fp.write(data) temp_fp.close() my_table = Table() my_table.read('csv', temp_fp.name) contents = my_table.write('csv') os.remove(temp_fp.name) self.assertEquals(contents, data)
def test_read_should_accept_a_SQL_instead_of_table_name(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) connection_string = '/'.join(self.connection_string.split('/')[:-1]) new_table = Table() sql = ('SELECT * FROM {} WHERE number > 500 AND number < 510 ORDER BY ' 'number DESC LIMIT 2, 10') new_table.read('mysql', connection_string, query=sql.format(self.table)) self.assertEquals(new_table[:], [[x] for x in range(507, 500, -1)])
def test_to_html_with_only_headers(self): my_table = Table(headers=['ham', 'spam', 'eggs', 'blah']) output = my_table.write('html', css_classes=False) expected = dedent(''' <table> <thead> <tr> <th>ham</th> <th>spam</th> <th>eggs</th> <th>blah</th> </tr> </thead> </table> ''').strip() self.assertEquals(output, expected)
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_should_be_able_to_change_delimiters_on_read(self): data = dedent(''' 'spam';'eggs';'ham'\r '42';'3.0';'2011-01-02'\r '1';'3.14';'2012-01-11'\r '21';'6.28';'2010-01-03'\r ''').strip() + '\r\n' temp_fp = tempfile.NamedTemporaryFile(delete=False) temp_fp.write(data) temp_fp.close() my_table = Table() my_table.read('csv', temp_fp.name, delimiter=';', quote_char="'", line_terminator='\r\n') os.remove(temp_fp.name) self.assertEquals(my_table[0], [42, 3.0, datetime.date(2011, 1, 2)]) self.assertEquals(my_table[1], [1, 3.14, datetime.date(2012, 1, 11)]) self.assertEquals(my_table[2], [21, 6.28, datetime.date(2010, 1, 3)])
def test_ordering_numbers_as_strings(self): my_table = Table(headers=['spam']) my_table.append(['5']) my_table.append([31]) my_table.append(['42']) my_table.order_by('spam') self.assertEqual( str(my_table), dedent(''' +------+ | spam | +------+ | 31 | | 42 | | 5 | +------+ ''').strip())
def test_ordering_numbers(self): my_table = Table(headers=['spam']) my_table.append([5]) my_table.append([42]) my_table.append([3.14]) my_table.order_by('spam') self.assertEqual( str(my_table), dedent(''' +------+ | spam | +------+ | 3.14 | | 5 | | 42 | +------+ ''').strip())