From fc974bced0cd6afd78a38fcba2d0471cfd81cf1b Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 24 Feb 2024 12:44:29 +0100 Subject: [PATCH] Add test for passing None to add_formatter --- src/tablib/core.py | 2 +- tests/test_tablib.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/tablib/core.py b/src/tablib/core.py index 2679fd24..522a4920 100644 --- a/src/tablib/core.py +++ b/src/tablib/core.py @@ -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 diff --git a/tests/test_tablib.py b/tests/test_tablib.py index 63f6d659..0c0db38d 100755 --- a/tests/test_tablib.py +++ b/tests/test_tablib.py @@ -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