Permalink
Browse files

Restructure classes and add failing test for expected behavior of cha…

…ining call tracking
  • Loading branch information...
1 parent 3057b07 commit 3492ee24ecb5c9c465b4ae1079a8177498095099 @dcramer committed Feb 6, 2012
Showing with 51 additions and 18 deletions.
  1. +18 −9 mock_django/managers.py
  2. +10 −7 mock_django/models.py
  3. +3 −0 setup.py
  4. +20 −2 tests/mock_django/managers/tests.py
View
@@ -11,18 +11,27 @@
__all__ = ('ManagerMock',)
-def ManagerMock(manager, return_value=[]):
+class _ManagerMock(mock.MagicMock):
+ def _get_child_mock(self, **kwargs):
+ name = kwargs.get('name', '')
+ if name[:2] == name[-2:] == '__':
+ return super(_ManagerMock, self)._get_child_mock(**kwargs)
+ return self
+
+ def __getattr__(self, name):
+ result = super(_ManagerMock, self).__getattr__(name)
+ if result is self:
+ result._mock_name = result._mock_new_name = name
+ return result
+
+
+def ManagerMock(manager, *return_value):
"""
>>> objects = ManagerMock(Post.objects, ['queryset', 'result'])
>>> assert objects.filter() == objects.all()
"""
- class ManagerMock(mock.MagicMock):
- def _get_child_mock(self, **kwargs):
- name = kwargs.get('name', '')
- if name[:2] == name[-2:] == '__':
- return super(ManagerMock, self)._get_child_mock(**kwargs)
- return self
- m = ManagerMock()
- m.__iter__.return_value = iter(return_value)
+
+ m = _ManagerMock()
+ m.__iter__.side_effect = lambda *a, **k: iter(return_value)
m.__getitem__ = lambda s, n: list(s)[n]
return m
View
@@ -11,15 +11,18 @@
__all__ = ('ModelMock',)
+# TODO: make foreignkey_id == foreignkey.id
+class _ModelMock(mock.MagicMock):
+ def _get_child_mock(self, **kwargs):
+ name = kwargs.get('name', '')
+ if name == 'pk':
+ return self.id
+ return super(_ModelMock, self)._get_child_mock(**kwargs)
+
+
def ModelMock(model):
"""
>>> Post = ModelMock(Post)
>>> assert post.pk == post.id
"""
- class ModelMock(mock.MagicMock):
- def _get_child_mock(self, **kwargs):
- name = kwargs.get('name', '')
- if name == 'pk':
- return self.id
- return super(ModelMock, self)._get_child_mock(**kwargs)
- return ModelMock(spec=model())
+ return _ModelMock(spec=model())
View
@@ -14,6 +14,9 @@
'unittest2',
'mock',
],
+ tests_require=[
+ 'mock==dev',
+ ],
test_suite='nose.collector',
zip_safe=False,
include_package_data=True,
@@ -13,10 +13,28 @@ def make_manager():
class ManagerMockTestCase(TestCase):
def test_iter(self):
manager = make_manager()
- mock = ManagerMock(manager, ['foo'])
+ mock = ManagerMock(manager, 'foo')
self.assertEquals(list(mock.all()), ['foo'])
def test_getitem(self):
manager = make_manager()
- mock = ManagerMock(manager, ['foo'])
+ mock = ManagerMock(manager, 'foo')
self.assertEquals(mock.all()[0], 'foo')
+
+ def test_returns_self(self):
+ manager = make_manager()
+ mock = ManagerMock(manager, 'foo')
+
+ self.assertEquals(mock.all(), mock)
+
+ def test_call_tracking(self):
+ # only works in >= mock 0.8
+ manager = make_manager()
+ mock = ManagerMock(manager, 'foo')
+
+ mock = mock.filter(foo='bar').select_related('baz')
+ calls = mock.mock_calls
+
+ self.assertEquals(len(calls), 2)
+ self.assertEquals(calls[0], mock.call.filter(foo='bar'))
+ self.assertEquals(calls[1], mock.call.select_related('baz'))

0 comments on commit 3492ee2

Please sign in to comment.