def test_sort(self): "Sort a table in place" t = TableFu(self.csv_file) self.table.pop(0) self.table.sort(key=lambda row: row[0]) t.sort('Author') self.assertEqual(t[0].cells, self.table[0])
def get_tablefu(self): """ Trick the data out with TableFu. """ path = os.path.join(settings.CSV_DIR, self.csv_name) data = open(path, 'r') return TableFu(data, **self.get_tablefu_opts())
def test_datum_values(self): "Ensure every cell has the right value" t = TableFu(self.csv_file) columns = self.table.pop(0) for i, row in enumerate(t.rows): for index, column in enumerate(columns): self.assertEqual(self.table[i][index], str(row[column]))
def test_transpose(self): t = TableFu(self.table) result = [[ 'Author', 'Samuel Beckett', 'James Joyce', 'Nicholson Baker', 'Vladimir Sorokin', 'Ayn Rand' ], [ 'Best Book', 'Malone Muert', 'Ulysses', 'Mezannine', 'The Queue', 'Atlas Shrugged' ], ['Number of Pages', '120', '644', '150', '263', '1088'], [ 'Style', 'Modernism', 'Modernism', 'Minimalism', 'Satire', 'Science fiction' ]] transposed = t.transpose() self.assertEqual(transposed.table, result[1:]) self.assertEqual(transposed.columns, [ 'Author', 'Samuel Beckett', 'James Joyce', 'Nicholson Baker', 'Vladimir Sorokin', 'Ayn Rand', ])
def test_header_th_style(self): t = TableFu(self.csv_file, style={'Author': 'text-align:left;'}) hed = t.headers[0] self.assertEqual( hed.as_th(), '<th style="text-align:left;" class="header">Author</th>' )
def test_sort_option_int(self): "Sorting the table by an int field, Number of Pages" t = TableFu(self.csv_file) pages = t.values('Number of Pages') pages = sorted(pages, reverse=True) t.sort('Number of Pages', reverse=True) self.assertEqual(t.values('Number of Pages'), pages)
def test_datum_td_style(self): t = TableFu(self.csv_file, style={'Author': 'text-align:left;'}) beckett = t[0]['Author'] self.assertEqual( beckett.as_td(), '<td style="text-align:left;" class="datum">Samuel Beckett</td>' )
def test_map_values(self): """ Test mapping a function to specific column values """ t = TableFu(self.table) result = [s.lower() for s in t.values('Style')] self.assertEqual(result, t.map(str.lower, 'Style'))
def test_row_map(self): """ Test map a function to rows, or a subset of fields """ t = TableFu(self.table) result = [s.lower() for s in t.values('Style')] self.assertEqual(result, t.map(lambda row: row['Style'].value.lower()))
def test_limit_columns(self): "Column definitions are passed to rows" t = TableFu(self.csv_file) t.columns = ['Author', 'Style'] self.assertEqual( str(t[0]), 'Samuel Beckett, Modernism' )
def test_row_tr(self): "Output a row as a <tr> element" t = TableFu(self.csv_file) row = t[0] self.assertEqual( row.as_tr(), '<tr id="row0" class="row even"><td style="" class="datum">Samuel Beckett</td><td style="" class="datum">Malone Muert</td><td style="" class="datum">120</td><td style="" class="datum">Modernism</td></tr>' )
def test_datum_td(self): "Output a cell as a <td> element" t = TableFu(self.csv_file) beckett = t[0]['Author'] self.assertEqual( beckett.as_td(), '<td style="" class="datum">Samuel Beckett</td>' )
def test_list_row(self): "Convert a row back to a list" t = TableFu(self.csv_file) modernism = t[0] self.assertEqual( list(modernism), modernism.values() )
def test_items(self): "Get key-value pairs for a row" t = TableFu(self.csv_file) modernism = t[0] self.assertEqual( modernism.items(), zip(modernism.keys(), modernism.values()) )
def test_map_many_values(self): """ Test mapping a function to multiple columns """ t = TableFu(self.table) result = [[s.lower() for s in t.values(value)] for value in ['Best Book', 'Style']] self.assertEqual(result, t.map(str.lower, 'Best Book', 'Style'))
def test_facet(self): "Facet tables based on shared column values" t = TableFu(self.csv_file) tables = t.facet_by('Style') style_row = self.table[4] self.assertEqual( style_row, tables[2][0].cells )
def test_bad_key(self): "Non-existent columns raise a KeyError" t = TableFu(self.csv_file) for row in t.rows: self.assertRaises( KeyError, row.__getitem__, 'not-a-key' )
def test_cell_format(self): "Format a cell" t = TableFu(self.csv_file) t.formatting = {'Name': {'filter': 'link', 'args': ['URL']}} self.assertEqual( str(t[0]['Name']), '<a href="http://www.chrisamico.com" title="ChrisAmico.com">ChrisAmico.com</a>' )
def test_check_row(self): "Check that row numbers are assigned correctly" t = TableFu(self.csv_file) self.table.pop(0) for i, row in enumerate(self.table): self.assertEqual( t[i].cells, self.table[i] )
def test_transform_to_int(self): """ Convert the Number of Pages field to integers """ t = TableFu(self.csv_file) pages = t.values('Number of Pages') t.transform('Number of Pages', int) for s, i in zip(pages, t.values('Number of Pages')): self.assertEqual(int(s), i)
def test_use_url(self): "Use a response from urllib2.urlopen as our base file" url = "http://spreadsheets.google.com/pub?key=thJa_BvqQuNdaFfFJMMII0Q&output=csv" response1 = urllib2.urlopen(url) response2 = urllib2.urlopen(url) reader = csv.reader(response1) columns = reader.next() t = TableFu(response2) self.assertEqual(columns, t.columns)
def test_update_values(self): "Update multiple cell values for a given row" t = TableFu(self.csv_file) modernism = t[0] kerouac = { 'Author': 'Jack Kerouac', 'Best Book': 'On the Road', 'Number of Pages': '320', 'Style': 'Beat' } modernism.update(kerouac) self.assertEqual(set(kerouac.values()), set(modernism.cells))
def test_json(self): try: import json except ImportError: try: import simplejson as json except ImportError: return t = TableFu(self.csv_file) self.csv_file.seek(0) reader = csv.DictReader(self.csv_file) jsoned = json.dumps([row for row in reader]) self.assertEqual(t.json(), jsoned)
def test_get_row(self): "Get one row by slicing the table" t = TableFu(self.csv_file) self.assertEqual(t[1], list(t.rows)[1])
def test_from_url(self): url = "http://spreadsheets.google.com/pub?key=thJa_BvqQuNdaFfFJMMII0Q&output=csv" t1 = TableFu.from_url(url) t2 = TableFu(urllib2.urlopen(url)) self.assertEqual(t1.table, t2.table)
def test_from_file(self): t1 = TableFu.from_file('tests/arra.csv') t2 = TableFu(open('tests/arra.csv')) self.assertEqual(t1.table, t2.table)
def test_count_rows(self): "Count rows, not including headings" t = TableFu(self.csv_file) self.table.pop(0) self.assertEqual(len(list(t.rows)), len(self.table)) self.assertEqual(len(t), len(self.table))
def test_get_headers(self): "Get the table's headers" t = TableFu(self.csv_file) self.assertEqual(t.headers, self.table[0])
def test_set_columns(self): "Set new columns for a table" t = TableFu(self.csv_file) columns = ['Style', 'Author'] t.columns = columns self.assertEqual(t.columns, columns)
def test_get_columns(self): "Get a table's (default) columns" t = TableFu(self.csv_file) self.assertEqual(t.columns, self.table[0])