Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

…ining call tracking
  • Loading branch information...
commit 3492ee24ecb5c9c465b4ae1079a8177498095099 1 parent 3057b07
@dcramer dcramer authored
View
27 mock_django/managers.py
@@ -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
17 mock_django/models.py
@@ -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
3  setup.py
@@ -14,6 +14,9 @@
'unittest2',
'mock',
],
+ tests_require=[
+ 'mock==dev',
+ ],
test_suite='nose.collector',
zip_safe=False,
include_package_data=True,
View
22 tests/mock_django/managers/tests.py
@@ -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'))
Please sign in to comment.
Something went wrong with that request. Please try again.