Permalink
Browse files

Allow exceptions as first argument and docstring updates

  • Loading branch information...
1 parent e711b0b commit 3dccd6fb61bf1a86334bfa3786195c3dbb1f8ac4 @dcramer committed Feb 7, 2012
Showing with 15 additions and 1 deletion.
  1. +10 −1 mock_django/managers.py
  2. +5 −0 tests/mock_django/managers/tests.py
View
@@ -58,8 +58,14 @@ def assert_chain_calls(self, *calls):
def ManagerMock(manager, *return_value):
"""
+ Set the results to two items:
+
>>> objects = ManagerMock(Post.objects, 'queryset', 'result')
>>> assert objects.filter() == objects.all()
+
+ Force an exception:
+
+ >>> objects = ManagerMock(Post.objects, Exception())
"""
def make_get(self):
@@ -82,6 +88,9 @@ def _get(*a, **k):
m = _ManagerMock()
m.model = model
m.get = make_get(m)
- m.__iter__.side_effect = lambda *a, **k: iter(return_value)
+ if len(return_value) == 1 and isinstance(return_value[0], Exception):
+ m.__iter__.side_effect = return_value[0]
+ else:
+ m.__iter__.side_effect = lambda *a, **k: iter(return_value)
m.__getitem__ = lambda s, n: list(s)[n]
return m
@@ -16,6 +16,11 @@ def test_iter(self):
inst = ManagerMock(manager, 'foo')
self.assertEquals(list(inst.all()), ['foo'])
+ def test_iter_exception(self):
+ manager = make_manager()
+ inst = ManagerMock(manager, Exception())
+ self.assertRaises(Exception, list, inst.all())
+
def test_getitem(self):
manager = make_manager()
inst = ManagerMock(manager, 'foo')

0 comments on commit 3dccd6f

Please sign in to comment.