Skip to content

Commit

Permalink
Add test for passing None to add_formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
claudep committed Feb 24, 2024
1 parent 4c53ba3 commit fc974bc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/tablib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ def add_formatter(self, col, handler):
else:
raise KeyError

if not col > self.width:
if col is None or col <= self.width:
self._formatters.append((col, handler))
else:
raise InvalidDatasetIndex
Expand Down
16 changes: 16 additions & 0 deletions tests/test_tablib.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,22 @@ def _formatter(cell_value):
# Test once more as the result should be the same
self.assertEqual(self.founders.dict, expected)

def test_formatters_all_cols(self):
"""
Passing None as first add_formatter param apply formatter to all columns.
"""

def _formatter(cell_value):
return str(cell_value).upper()

self.founders.add_formatter(None, _formatter)

self.assertEqual(self.founders.dict, [
{'first_name': 'JOHN', 'last_name': 'ADAMS', 'gpa': '90'},
{'first_name': 'GEORGE', 'last_name': 'WASHINGTON', 'gpa': '67'},
{'first_name': 'THOMAS', 'last_name': 'JEFFERSON', 'gpa': '50'},
])

def test_unicode_renders_markdown_table(self):
# add another entry to test right field width for
# integer
Expand Down

0 comments on commit fc974bc

Please sign in to comment.