Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 85 lines (67 sloc) 2.269 kb
2fe2d6f1 »
2012-02-20 QuerySets are now mocked correctly when doing things like manager.all…
1 """
2 mock_django.query
3 ~~~~~~~~~~~~~~~~~
4
5 :copyright: (c) 2012 DISQUS.
6 :license: Apache License 2.0, see LICENSE for more details.
7 """
8
9 import mock
74c474a2 »
2012-02-20 Improve handling of DoesNotExist and MultipleObjectsReturned cases. M…
10 from .shared import SharedMock
2fe2d6f1 »
2012-02-20 QuerySets are now mocked correctly when doing things like manager.all…
11
12 __all__ = ('QuerySetMock',)
13
14
15 def QuerySetMock(model, *return_value):
16 """
17 Set the results to two items:
18
19 >>> objects = QuerySetMock(Post, 'return', 'values')
20 >>> assert objects.filter() == objects.all()
21
22 Force an exception:
23
24 >>> objects = QuerySetMock(Post, Exception())
3edb99af »
2012-08-16 Added some explanatory docstrings and comments.
25
26 Note that only methods returning querysets are currently
27 explicitly supported; since we use SharedMock, others all behave
28 as if they did, so use with caution:
29
30 >>> objects.count() == objects.all()
31 True
2fe2d6f1 »
2012-02-20 QuerySets are now mocked correctly when doing things like manager.all…
32 """
33
74c474a2 »
2012-02-20 Improve handling of DoesNotExist and MultipleObjectsReturned cases. M…
34 def make_get(self, model):
2fe2d6f1 »
2012-02-20 QuerySets are now mocked correctly when doing things like manager.all…
35 def _get(*a, **k):
36 results = list(self)
37 if len(results) > 1:
74c474a2 »
2012-02-20 Improve handling of DoesNotExist and MultipleObjectsReturned cases. M…
38 raise model.MultipleObjectsReturned
2fe2d6f1 »
2012-02-20 QuerySets are now mocked correctly when doing things like manager.all…
39 try:
40 return results[0]
41 except IndexError:
74c474a2 »
2012-02-20 Improve handling of DoesNotExist and MultipleObjectsReturned cases. M…
42 raise model.DoesNotExist
2fe2d6f1 »
2012-02-20 QuerySets are now mocked correctly when doing things like manager.all…
43 return _get
44
45 def make_getitem(self):
46 def _getitem(k):
47 if isinstance(k, slice):
48 self.__start = k.start
49 self.__stop = k.stop
50 else:
51 return list(self)[k]
52 return self
53 return _getitem
54
55 def make_iterator(self):
56 def _iterator(*a, **k):
57 if len(return_value) == 1 and isinstance(return_value[0], Exception):
58 raise return_value[0]
59
60 start = getattr(self, '__start', None)
61 stop = getattr(self, '__stop', None)
62 for x in return_value[start:stop]:
63 yield x
64 return _iterator
65
66 actual_model = model
67 if actual_model:
68 model = mock.MagicMock(spec=actual_model())
69 else:
70 model = mock.MagicMock()
71
74c474a2 »
2012-02-20 Improve handling of DoesNotExist and MultipleObjectsReturned cases. M…
72 m = SharedMock()
2fe2d6f1 »
2012-02-20 QuerySets are now mocked correctly when doing things like manager.all…
73 m.__start = None
74 m.__stop = None
75 m.__iter__.side_effect = lambda: iter(m.iterator())
76 m.__getitem__.side_effect = make_getitem(m)
77 m.model = model
74c474a2 »
2012-02-20 Improve handling of DoesNotExist and MultipleObjectsReturned cases. M…
78 m.get = make_get(m, actual_model)
3edb99af »
2012-08-16 Added some explanatory docstrings and comments.
79
80 # Note since this is a SharedMock, *all* auto-generated child
81 # attributes will have the same side_effect ... might not make
82 # sense for some like count().
2fe2d6f1 »
2012-02-20 QuerySets are now mocked correctly when doing things like manager.all…
83 m.iterator.side_effect = make_iterator(m)
84 return m
Something went wrong with that request. Please try again.