Ejemplo n.º 1
0
 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])
Ejemplo n.º 2
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())
Ejemplo n.º 3
0
 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]))
Ejemplo n.º 4
0
    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',
        ])
Ejemplo n.º 5
0
 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>'
     )
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
 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>'
     )
Ejemplo n.º 8
0
 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'))
Ejemplo n.º 9
0
 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()))
Ejemplo n.º 10
0
 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'
         )
Ejemplo n.º 11
0
 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>'
     )
Ejemplo n.º 12
0
 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>'
     )
Ejemplo n.º 13
0
 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()
     )
Ejemplo n.º 14
0
 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())
     )
Ejemplo n.º 15
0
 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'))
Ejemplo n.º 16
0
 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
     )
Ejemplo n.º 17
0
 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'
         )
Ejemplo n.º 18
0
    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>'
        )
Ejemplo n.º 19
0
 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]
         )
Ejemplo n.º 20
0
 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)
Ejemplo n.º 21
0
 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)
Ejemplo n.º 22
0
 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))
Ejemplo n.º 23
0
 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)
Ejemplo n.º 24
0
 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])
Ejemplo n.º 25
0
 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)
Ejemplo n.º 26
0
 def test_from_file(self):
     t1 = TableFu.from_file('tests/arra.csv')
     t2 = TableFu(open('tests/arra.csv'))
     self.assertEqual(t1.table, t2.table)
Ejemplo n.º 27
0
 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))
Ejemplo n.º 28
0
 def test_get_headers(self):
     "Get the table's headers"
     t = TableFu(self.csv_file)
     self.assertEqual(t.headers, self.table[0])
Ejemplo n.º 29
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)
Ejemplo n.º 30
0
 def test_get_columns(self):
     "Get a table's (default) columns"
     t = TableFu(self.csv_file)
     self.assertEqual(t.columns, self.table[0])