Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dataset.compare() method #241

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions tablib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
24 changes: 23 additions & 1 deletion test_tablib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()