Skip to content

Commit

Permalink
fix up __eq__ for ListResult
Browse files Browse the repository at this point in the history
  • Loading branch information
djmitche committed May 28, 2013
1 parent c2cbf11 commit 1b35d30
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
9 changes: 7 additions & 2 deletions master/buildbot/data/base.py
Expand Up @@ -125,12 +125,17 @@ def __repr__(self):

def __eq__(self, other):
if isinstance(other, ListResult):
return list(self) == other \
return self.data == other.data \
and self.offset == other.offset \
and self.total == other.total \
and self.limit == other.limit
else:
return list(self) == other
return self.data == other \
and self.offset == self.limit == None \
and (self.total is None or self.total == len(other))

def __ne__(self, other):
return not (self == other)


class Link(object):
Expand Down
33 changes: 33 additions & 0 deletions master/buildbot/test/unit/test_data_base.py
Expand Up @@ -84,6 +84,39 @@ def test_sets_master(self):
self.assertIdentical(self.master, self.ep.master)


class ListResult(unittest.TestCase):

def test_constructor(self):
lr = base.ListResult([1,2,3], offset=10, total=20, limit=3)
self.assertEqual(lr.data, [1,2,3])
self.assertEqual(lr.offset, 10)
self.assertEqual(lr.total, 20)
self.assertEqual(lr.limit, 3)

def test_repr(self):
lr = base.ListResult([1,2,3], offset=10, total=20, limit=3)
self.assertTrue(`lr`.startswith('ListResult'))

def test_eq(self):
lr1 = base.ListResult([1,2,3], offset=10, total=20, limit=3)
lr2 = base.ListResult([1,2,3], offset=20, total=30, limit=3)
lr3 = base.ListResult([1,2,3], offset=20, total=30, limit=3)
self.assertEqual(lr2, lr3)
self.assertNotEqual(lr1, lr2)
self.assertNotEqual(lr1, lr3)

def test_eq_to_list(self):
list = [1,2,3]
lr1 = base.ListResult([1,2,3], offset=10, total=20, limit=3)
self.assertNotEqual(lr1, list)
lr2 = base.ListResult([1,2,3], offset=None, total=None, limit=None)
self.assertEqual(lr2, list)
lr3 = base.ListResult([1,2,3], total=3)
self.assertEqual(lr3, list)
lr4 = base.ListResult([1,2,3], total=4)
self.assertNotEqual(lr4, list)


class Link(unittest.TestCase):

def test_path(self):
Expand Down
6 changes: 4 additions & 2 deletions master/buildbot/test/unit/test_data_connector.py
Expand Up @@ -185,7 +185,8 @@ def test_get_filters(self):

@d.addCallback
def check(gotten):
self.assertEqual(gotten, [{'val': 900}, {'val': 901}])
self.assertEqual(gotten, base.ListResult(
[{'val': 900}, {'val': 901}], total=2))
ep.get.assert_called_once_with(mock.ANY, {})
return d

Expand All @@ -197,7 +198,8 @@ def test_get_resultSpec_args(self):

@d.addCallback
def check(gotten):
self.assertEqual(gotten, [{'val': 919}, {'val': 918}])
self.assertEqual(gotten, base.ListResult(
[{'val': 919}, {'val': 918}], total=10, limit=2))
ep.get.assert_called_once_with(mock.ANY, {})
return d

Expand Down

0 comments on commit 1b35d30

Please sign in to comment.