Skip to content

Commit

Permalink
Throwing InvalidDimensions when append_col with header is called but …
Browse files Browse the repository at this point in the history
…only headers exists

Related jazzband#33
  • Loading branch information
Mike Waldner committed Aug 10, 2011
1 parent 3d02b86 commit d611233
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
6 changes: 6 additions & 0 deletions tablib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,14 @@ def insert_col(self, index, col=None, header=None):
# 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):
Expand Down
66 changes: 63 additions & 3 deletions test_tablib.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ def test_empty_append_with_headers(self):

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."""
Expand Down Expand Up @@ -92,6 +104,53 @@ def test_add_column_no_data_no_headers(self):
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."""

Expand Down Expand Up @@ -128,7 +187,7 @@ def test_get_col(self):
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."""

Expand Down Expand Up @@ -224,6 +283,7 @@ def test_html_export(self):

self.assertEqual(html, self.founders.html)


def test_html_export_none_value(self):
"""HTML export"""

Expand Down Expand Up @@ -547,7 +607,7 @@ def test_unicode_csv(self):


data.csv

def test_csv_column_select(self):
"""Build up a CSV and test selecting a column"""

Expand Down Expand Up @@ -588,7 +648,7 @@ def test_csv_column_sort(self):
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)
Expand Down

0 comments on commit d611233

Please sign in to comment.