Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added assert_chain_calls to the manager

  • Loading branch information...
commit 58d94d519ead9076180267abb82e5b3660cd43db 1 parent 3492ee2
David Cramer dcramer authored

Showing 2 changed files with 40 additions and 12 deletions. Show diff stats Hide diff stats

  1. +27 0 mock_django/managers.py
  2. +13 12 tests/mock_django/managers/tests.py
27 mock_django/managers.py
@@ -12,6 +12,12 @@
12 12
13 13
14 14 class _ManagerMock(mock.MagicMock):
  15 + def __init__(self, *args, **kwargs):
  16 + super(_ManagerMock, self).__init__(*args, **kwargs)
  17 + parent = mock.MagicMock()
  18 + parent.child = self
  19 + self.__parent = parent
  20 +
15 21 def _get_child_mock(self, **kwargs):
16 22 name = kwargs.get('name', '')
17 23 if name[:2] == name[-2:] == '__':
@@ -24,6 +30,27 @@ def __getattr__(self, name):
24 30 result._mock_name = result._mock_new_name = name
25 31 return result
26 32
  33 + def assert_chain_calls(self, *calls):
  34 + """
  35 + Asserts that a chained method was called (parents in the chain do not
  36 + matter, nor are they tracked).
  37 +
  38 + >>> obj.assert_chain_calls(call.filter(foo='bar'))
  39 + >>> obj.assert_chain_calls(call.select_related('baz'))
  40 + """
  41 + all_calls = list(self.__parent.mock_calls)
  42 +
  43 + not_found = []
  44 + for kall in calls:
  45 + try:
  46 + all_calls.remove(kall)
  47 + except ValueError:
  48 + not_found.append(kall)
  49 + if not_found:
  50 + raise AssertionError(
  51 + '%r not all found in call list' % (tuple(not_found),)
  52 + )
  53 +
27 54
28 55 def ManagerMock(manager, *return_value):
29 56 """
25 tests/mock_django/managers/tests.py
@@ -13,28 +13,29 @@ def make_manager():
13 13 class ManagerMockTestCase(TestCase):
14 14 def test_iter(self):
15 15 manager = make_manager()
16   - mock = ManagerMock(manager, 'foo')
17   - self.assertEquals(list(mock.all()), ['foo'])
  16 + inst = ManagerMock(manager, 'foo')
  17 + self.assertEquals(list(inst.all()), ['foo'])
18 18
19 19 def test_getitem(self):
20 20 manager = make_manager()
21   - mock = ManagerMock(manager, 'foo')
22   - self.assertEquals(mock.all()[0], 'foo')
  21 + inst = ManagerMock(manager, 'foo')
  22 + self.assertEquals(inst.all()[0], 'foo')
23 23
24 24 def test_returns_self(self):
25 25 manager = make_manager()
26   - mock = ManagerMock(manager, 'foo')
  26 + inst = ManagerMock(manager, 'foo')
27 27
28   - self.assertEquals(mock.all(), mock)
  28 + self.assertEquals(inst.all(), inst)
29 29
30 30 def test_call_tracking(self):
31 31 # only works in >= mock 0.8
32 32 manager = make_manager()
33   - mock = ManagerMock(manager, 'foo')
  33 + inst = ManagerMock(manager, 'foo')
34 34
35   - mock = mock.filter(foo='bar').select_related('baz')
36   - calls = mock.mock_calls
  35 + inst.filter(foo='bar').select_related('baz')
37 36
38   - self.assertEquals(len(calls), 2)
39   - self.assertEquals(calls[0], mock.call.filter(foo='bar'))
40   - self.assertEquals(calls[1], mock.call.select_related('baz'))
  37 + calls = inst.mock_calls
  38 +
  39 + self.assertGreater(len(calls), 1)
  40 + inst.assert_chain_calls(mock.call.filter(foo='bar'))
  41 + inst.assert_chain_calls(mock.call.select_related('baz'))

0 comments on commit 58d94d5

Please sign in to comment.
Something went wrong with that request. Please try again.