diff --git a/tablib/core.py b/tablib/core.py index c5faad3a..3019f92d 100644 --- a/tablib/core.py +++ b/tablib/core.py @@ -1032,6 +1032,25 @@ def subset(self, rows=None, cols=None): return _dset + def compare(self, other): + if not isinstance(other, Dataset): + return + + if not self.headers or not other.headers: + raise HeadersNeeded + + if self.height != other.height: + return False + + if sorted(self.headers) != sorted(other.headers): + return False + + for header in self.headers: + for a, b in zip(self[header], other[header]): + if a != b: + return False + + return True class Databook(object): diff --git a/test_tablib.py b/test_tablib.py index be41ee71..5a43beb1 100755 --- a/test_tablib.py +++ b/test_tablib.py @@ -8,7 +8,7 @@ import os import tablib from tablib.compat import markup, unicode, is_py3 -from tablib.core import Row +from tablib.core import Row, HeadersNeeded class TablibTestCase(unittest.TestCase): @@ -933,6 +933,28 @@ def test_databook_formatter_support_kwargs(self): """Test XLSX export with formatter configuration.""" self.founders.export('xlsx', freeze_panes=False) + def test_compare(self): + empty_data = tablib.Dataset() + + original_data = tablib.Dataset() + original_data.headers = ['i', 'b', 's'] + original_data.append([1, True, "aaa"]) + + reordered_data = tablib.Dataset() + reordered_data.headers = ['s', 'i', 'b'] + reordered_data.append(["aaa", 1, True]) + + different_data = tablib.Dataset() + different_data.headers = ['i', 'b', 's'] + different_data.append([0, False, "bbb"]) + + self.assertTrue(original_data.compare(original_data)) + self.assertTrue(original_data.compare(reordered_data)) + self.assertFalse(original_data.compare(different_data)) + self.assertFalse(reordered_data.compare(different_data)) + self.assertTrue(original_data.compare(None) is None) + self.assertRaises(HeadersNeeded, original_data.compare, empty_data) + if __name__ == '__main__': unittest.main()