Merge lp:~thisfred/desktopcouch/eq_ne into lp:desktopcouch

Proposed by Eric Casteleijn
Status: Merged
Approved by: Eric Casteleijn
Approved revision: 243
Merged at revision: 252
Proposed branch: lp:~thisfred/desktopcouch/eq_ne
Merge into: lp:desktopcouch
Diff against target: 56 lines (+35/-0)
2 files modified
desktopcouch/records/__init__.py (+13/-0)
desktopcouch/records/tests/test_record.py (+22/-0)
To merge this branch: bzr merge lp:~thisfred/desktopcouch/eq_ne
Reviewer Review Type Date Requested Status
James Tait (community) Approve
Chad Miller (community) Approve
Nicola Larosa Pending
Review via email: mp+45163@code.launchpad.net

Commit message

Added __eq__ and __ne__ to MergeableList class so they can be compared to regular lists.

Description of the change

By popular demand: it's now possible to compare mergeable lists to regular list objects (or anything that's a MutableSequence)

To post a comment you must log in.
Revision history for this message
Chad Miller (cmiller) wrote :

Nice! I'd like to see some deeper structure in the tests.

Revision history for this message
Chad Miller (cmiller) :
review: Approve
Revision history for this message
James Tait (jamestait) wrote :

Like Chad, I'd like to see a bit more depth in the tests, but it looks reasonable.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'desktopcouch/records/__init__.py'
2--- desktopcouch/records/__init__.py 2010-11-23 19:46:06 +0000
3+++ desktopcouch/records/__init__.py 2011-01-04 19:37:17 +0000
4@@ -276,6 +276,19 @@
5 return len(self._data) - 1
6 return len(self._data)
7
8+ def __eq__(self, other):
9+ if not isinstance(other, MutableSequence):
10+ return False
11+ if len(self) != len(other):
12+ return False
13+ for i in range(len(self)):
14+ if self[i] != other[i]:
15+ return False
16+ return True
17+
18+ def __ne__(self, other):
19+ return not self.__eq__(other)
20+
21 def insert(self, index, value):
22 """Insert value at index."""
23 new_uuid = str(uuid.uuid4())
24
25=== modified file 'desktopcouch/records/tests/test_record.py'
26--- desktopcouch/records/tests/test_record.py 2010-11-23 18:47:46 +0000
27+++ desktopcouch/records/tests/test_record.py 2011-01-04 19:37:17 +0000
28@@ -87,6 +87,28 @@
29 self.assertEqual("bar", mergeable_list[1])
30 self.assertEqual("bar", mergeable_list[-1])
31
32+ def test_eq(self):
33+ """Test comparison of regular lists to mergeable lists."""
34+ the_list = ['foo', 'bar', 'baz']
35+ mergeable_list = MergeableList(the_list)
36+ self.assertEqual(mergeable_list, the_list)
37+ self.assertEqual(mergeable_list, mergeable_list)
38+
39+ def test_eq2(self):
40+ """Test comparison of regular lists to mergeable lists."""
41+ the_list = [{'foo': 'bar'}, {'baz': 'qux'}, {'fnord': 'fierljeppen'}]
42+ mergeable_list = MergeableList(the_list)
43+ self.assertEqual(mergeable_list, the_list)
44+ self.assertEqual(mergeable_list, mergeable_list)
45+
46+ def test_ne(self):
47+ """Test comparison of regular lists to mergeable lists."""
48+ the_list = ['foo', 'bar', 'baz']
49+ mergeable_list = MergeableList(the_list)
50+ self.assertNotEqual([], mergeable_list)
51+ self.assertNotEqual([1, 2, 3], mergeable_list)
52+ self.assertNotEqual('peanut butter & chocolate', mergeable_list)
53+
54 def test_del(self):
55 """Test deletion of uuid keys."""
56 mergeable_list = MergeableList(['foo', 'bar', 'baz'])

Subscribers

People subscribed via source and target branches