From d611233c80b6cfbba8681211e02aab3e468ed5e9 Mon Sep 17 00:00:00 2001 From: Mike Waldner Date: Wed, 10 Aug 2011 19:50:31 -0400 Subject: [PATCH] Throwing InvalidDimensions when append_col with header is called but only headers exists Related #33 --- tablib/core.py | 6 +++++ test_tablib.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/tablib/core.py b/tablib/core.py index f52ca92..166d18e 100644 --- a/tablib/core.py +++ b/tablib/core.py @@ -649,8 +649,14 @@ class Dataset(object): # pop the first item off, add to headers if not header: raise HeadersNeeded() + + # corner case - if header is set without data + elif header and self.height == 0: + raise InvalidDimensions + self.headers.insert(index, header) + if self.height and self.width: for i, row in enumerate(self._data): diff --git a/test_tablib.py b/test_tablib.py index 998233b..0c07d3a 100755 --- a/test_tablib.py +++ b/test_tablib.py @@ -58,6 +58,18 @@ class TablibTestCase(unittest.TestCase): self.assertRaises(tablib.InvalidDimensions, data.append, new_row) + def test_set_headers_with_incorrect_dimension(self): + """Verify headers correctly detects mismatch of number of + headers and data. + """ + + data.append(self.john) + + def set_header_callable(): + data.headers = ['first_name'] + + self.assertRaises(tablib.InvalidDimensions, set_header_callable) + def test_add_column(self): """Verify adding column works with/without headers.""" @@ -92,6 +104,53 @@ class TablibTestCase(unittest.TestCase): self.assertEqual(data.height, len(new_col)) + def test_add_column_with_header_ignored(self): + """Verify append_col() ignores the header if data.headers has + not previously been set + """ + + new_col = ('reitz', 'monke') + + data.append_col(new_col, header='first_name') + + self.assertEqual(data[0], tuple([new_col[0]])) + self.assertEqual(data.width, 1) + self.assertEqual(data.height, len(new_col)) + self.assertEqual(data.headers, None) + + + def test_add_column_with_header_and_headers_only_exist(self): + """Verify append_col() with header correctly detects mismatch when + headers exist but there is no existing row data + """ + + data.headers = ['first_name'] + #no data + + new_col = ('allen') + + def append_col_callable(): + data.append_col(new_col, header='middle_name') + + self.assertRaises(tablib.InvalidDimensions, append_col_callable) + + + def test_add_column_with_header_and_data_exists(self): + """Verify append_col() works when headers and rows exists""" + + data.headers = self.headers + data.append(self.john) + + new_col = [10]; + + data.append_col(new_col, header='age') + + self.assertEqual(data.height, 1) + self.assertEqual(data.width, 4) + self.assertEqual(data['age'], new_col) + self.assertEqual(len(data.headers), len(self.headers) + 1) + + def test_add_callable_column(self): """Verify adding column with values specified as callable.""" @@ -128,7 +187,7 @@ class TablibTestCase(unittest.TestCase): self.founders.get_col(self.headers.index('gpa')), [self.john[2], self.george[2], self.tom[2]]) - + def test_data_slicing(self): """Verify slicing by data.""" @@ -224,6 +283,7 @@ class TablibTestCase(unittest.TestCase): self.assertEqual(html, self.founders.html) + def test_html_export_none_value(self): """HTML export""" @@ -547,7 +607,7 @@ class TablibTestCase(unittest.TestCase): data.csv - + def test_csv_column_select(self): """Build up a CSV and test selecting a column""" @@ -588,7 +648,7 @@ class TablibTestCase(unittest.TestCase): data.sort(target_header) self.assertEquals(self.founders[orig_target_header], data[target_header]) - + def test_xls_import_set(self): """Generate and import XLS set serialization.""" data.append(self.john)