def setUp(self): if self.View is None: raise SkipTest('abstract test') self.view = self.View() self.view.request = Mock(name='request') self.view.model = Mock(name='model') self.view.kwargs = dict(self.kwargs)
def test_A_simplejson(self): with mock.reset_modules('thorn.utils.json'): from thorn.utils import json obj = Mock(name='obj') encode = Mock(name='encode') self.assertIs(json.dumps(obj, encode=encode), encode.return_value) encode.assert_called_with(obj, cls=json.JsonEncoder)
def test_compare_requests(self): a = Mock(name='r1') a.urlident = 'foo' b = Mock(name='r2') b.urlident = 'bar' self.assertTrue(self.dispatcher._compare_requests(a, a)) self.assertFalse(self.dispatcher._compare_requests(a, b))
def test_on_pre_save__disabled_on_object_creation(self): self.dispatch.use_transitions = True instance = Mock(Name='instance') instance.pk = None sender = Mock(name='sender') self.dispatch.on_pre_save(instance, sender, raw=False) sender.objects.get.assert_not_called()
def test_not_in(self): x1 = Mock(foo=Mock( bar='PUBLISHED', baz='PENDING', )) self.assertTrue(Q(foo__bar__not_in={'PENDING', 'X', 'Y', 'Z'})(x1)) self.assertFalse(Q(foo__baz__not_in={'PENDING', 'X', 'Y', 'Z'})(x1))
def test_on_pre_save__disabled_on_object_creation(self): self.dispatch.use_transitions = True instance = Mock(Name='instance') instance.pk = None sender = Mock(name='sender') self.dispatch.on_pre_save(instance, sender, raw=False) sender.objects.get.assert_not_called()
def test_on_pre_save(self): self.dispatch.use_transitions = True instance = Mock(Name='instance') sender = Mock(name='sender') self.dispatch.on_pre_save(instance, sender, raw=False) sender.objects.get.assert_called_once_with(pk=instance.pk) self.assertIs(instance._previous_version, sender.objects.get())
def test_nested_fields(self): obj = Mock( author=Mock( full_name='George Costanza', is_staff=True, rank=3.03, violations=None, ), tags=['abc', 'def', 'ghi'], ) self.assertTrue( Q(author__is_staff__eq=True, author__full_name__contains=' ', author__full_name__startswith='George', author__full_name__endswith='Costanza', author__rank__gt=2.3, author__violations__is=None, tags__contains='abc')(obj)) self.assertFalse( Q(author__is_staff__eq=True, author__full_name__contains=' ', author__full_name__startswith='George', author__full_name__endswith='Costanza', author__rank__gt=2.3, author__violations__is=None, tags__contains='zzz')(obj)) self.assertFalse( Q(author__is_staff__eq=False, author__full_name__contains=' ', author__full_name__startswith='George', author__full_name__endswith='Costanza', author__rank__gt=2.3, author__violations__is=None)(obj))
def test_group_requests(self, groupbymax): reqs = [Mock(name='r1'), Mock(name='r2'), Mock(name='r3')] ret = self.dispatcher.group_requests(reqs) groupbymax.assert_called_with( reqs, max=self.app.settings.THORN_CHUNKSIZE, key=self.dispatcher._compare_requests, ) self.assertIs(ret, groupbymax())
def test_on_pre_save__ObjectDoesNotExist(self): self.dispatch.use_transitions = True instance = Mock(Name='instance') instance._previous_version = None sender = Mock(name='sender') sender.objects.get.side_effect = ObjectDoesNotExist() self.dispatch.on_pre_save(instance, sender, raw=False) sender.objects.get.assert_called_once_with(pk=instance.pk) self.assertIsNone(instance._previous_version)
def test_subscribers_for_event(self): source1, source2 = Mock(name='source1'), Mock(name='source2') source1.return_value = [1, 2, 3] source2.return_value = [4, 5, 6] self.dispatcher.subscriber_sources = [source1, source2] self.assertListEqual( list(self.dispatcher.subscribers_for_event('foo.bar')), [1, 2, 3, 4, 5, 6], )
def test_now_in(self): x1 = Mock(foo=Mock( bar='PUBLISHED', baz='PUBLISHED', )) x1._previous_version = Mock(foo=Mock( bar='PENDING', baz='PUBLISHED', )) self.assertTrue(Q(foo__bar__now_in={'PUBLISHED', 'X', 'Y', 'Z'})(x1)) self.assertFalse(Q(foo__baz__now_in={'PUBLISHED', 'X', 'Y', 'Z'})(x1))
def test_now_contains(self): x1 = Mock(foo=Mock( bar='The quick brown fox', baz='The quick brown fox'), ) x1._previous_version = Mock(foo=Mock( bar='The quick red fox', baz='The quick brown fox', )) self.assertTrue(Q(foo__bar__now_contains='brown')(x1)) self.assertFalse(Q(foo__baz__now_contains='brown')(x1))
def test_now_endswith(self): x1 = Mock(foo=Mock( bar='The quick brown fox', baz='The quick brown fox', )) x1._previous_version = Mock(foo=Mock( bar='The lazy red fox', baz='The lazy brown fox', )) self.assertTrue(Q(foo__bar__now_endswith='brown fox')(x1)) self.assertFalse(Q(foo__baz__now_endswith='brown fox')(x1))
def test_nested_Q_objects(self): x = Mock(foo=Mock(bar=1, baz=2)) q1 = Q(Q(foo__x__eq=True), foo__bar__eq=1) & Q(foo__baz__eq=2) q2 = Q(foo__bar__eq=1) & Q(foo__baz__eq=3) q3 = Q(foo__baz__eq=3) | Q(foo__bar__eq=1) q4 = Q(foo__bar__eq=1) & Q(Q(foo__x__ne=True), foo__baz__eq=2) self.assertTrue(q1(x)) self.assertFalse(q2(x)) self.assertTrue((~q2)(x)) self.assertTrue(q3(x)) self.assertFalse(q4(x))
def test_send(self, send_event): event = Mock(name='event') payload = Mock(name='payload') user = Mock(name='user') res = Dispatcher().send(event, payload, user, timeout=3.03, kw=9) send_event.s.assert_called_once_with( event, payload, user.pk, 3.03, ) send_event.s().apply_async.assert_called_once_with() self.assertIs(res, send_event.s().apply_async())
def test_encode_payload(self): data = Mock(name='data') codec = Mock(name='codec') self._app.settings.THORN_CODECS = { 'application/foo': codec, } self.assertIs( self.dispatcher.encode_payload(data, 'application/x-mooOOO'), data, ) self.assertIs( self.dispatcher.encode_payload(data, 'application/foo'), codec.return_value, ) codec.assert_called_with(data)
def test_configured_subscribers__callback(self): callback1 = Mock(name='callback1') callback1.return_value = ['http://a.com/1', 'http://a.com/2'] callback2 = Mock(name='callback2') callback2.return_value = iter(['http://b.com/1', 'http://b.com/2']) self._app.settings.THORN_SUBSCRIBERS = { 'foo.bar': [callback1, callback2], } self._app.Subscriber.from_dict = subscriber_from_dict self.assertListEqual( [dict(event='foo.bar', url='http://a.com/1'), dict(event='foo.bar', url='http://a.com/2'), dict(event='foo.bar', url='http://b.com/1'), dict(event='foo.bar', url='http://b.com/2')], self.dispatcher._configured_subscribers('foo.bar') )
def test_send(self, group, dispatch_requests): def eval_genexp(x): list(x) return group.return_value group.side_effect = eval_genexp reqs = [Mock(name='r1'), Mock(name='r2'), Mock(name='r2')] self.dispatcher.prepare_requests = Mock(name='prepare_requests') self.dispatcher.prepare_requests.return_value = reqs self.dispatcher.group_requests = Mock(name='group_requests') self.dispatcher.group_requests.return_value = [[r] for r in reqs] self.dispatcher.send(Mock(), Mock(), Mock(), Mock()) dispatch_requests.s.assert_has_calls( [call([req.as_dict()]) for req in reqs])
class test_signal_handler(SignalCase): def setup(self): self.dispatcher = Mock(name='dispatcher') self.sender = Mock(name='sender') self.handler = signals.signal_handler( self.dispatcher, 1, 2, 3, sender=self.sender, foo=4, ) def test_init(self): self.assertIs(self.handler.dispatcher, self.dispatcher) self.assertIs(self.handler.sender, self.sender) self.assertTupleEqual(self.handler.args, (1, 2, 3)) self.assertDictEqual(self.handler.kwargs, {'foo': 4}) def test_call(self): fun = Mock(name='fun') ret = self.handler(fun) self.dispatcher.assert_called_with(fun, 1, 2, 3, foo=4) self.dispatcher().connect.assert_called_with(sender=self.sender) self.assertIs(ret, fun)
def test_compare_requests(self): a = Mock(name='r1') a.urlident = 'foo' b = Mock(name='r2') b.urlident = 'bar' self.assertTrue(self.dispatcher._compare_requests(a, a)) self.assertFalse(self.dispatcher._compare_requests(a, b))
def test_eq__False(self): self.assertFalse(Q(foo__eq=False)(Mock(foo=True))) self.assertFalse(Q(foo__eq=False)(Mock(foo=30))) self.assertFalse(Q(foo__eq=False)(Mock(foo='foo'))) self.assertTrue(Q(foo__eq=False)(Mock(foo=''))) self.assertTrue(Q(foo__eq=False)(Mock(foo=None))) self.assertTrue(Q(foo__eq=False)(Mock(foo=0)))
def test_now_contains(self): x1 = Mock(foo=Mock(bar='The quick brown fox', baz='The quick brown fox'), ) x1._previous_version = Mock(foo=Mock( bar='The quick red fox', baz='The quick brown fox', )) self.assertTrue(Q(foo__bar__now_contains='brown')(x1)) self.assertFalse(Q(foo__baz__now_contains='brown')(x1))
def test_prepare_requests(self): event = Mock(name='event') event.name = 'foo.bar' self.dispatcher.Request = Mock(name='Request') self.dispatcher.subscribers_for_event = Mock(name='subscribers') targets = self.dispatcher.subscribers_for_event.return_value = [ Mock(name='r1'), Mock(name='r2'), Mock(name='r3'), ] targets[0].content_type = 'A' targets[1].content_type = 'A' targets[2].content_type = 'B' codecB = Mock(name='codecB') self._app.settings.THORN_CODECS = {'B': codecB} list(self.dispatcher.prepare_requests( event.name, {'foo': 'bar'}, 501, 30.3, kw=1, )) self._app.Request.assert_has_calls([ call(event.name, {'foo': 'bar'}, 501, targets[0], timeout=30.3, kw=1), call(event.name, {'foo': 'bar'}, 501, targets[1], timeout=30.3, kw=1), call(event.name, codecB(), 501, targets[2], timeout=30.3, kw=1), ])
def test_on_m2m_change__post_add(self): handler = self.dispatch.actions['post_add'] = Mock(name='post_add') self.dispatch.on_m2m_change( sender=self.Model, action='post_add', instance=self.instance, model=self.Model, foo=1, ) handler.assert_called_with( self.instance, sender=self.Model, model=self.Model, foo=1, )
def test_now_endswith(self): x1 = Mock(foo=Mock( bar='The quick brown fox', baz='The quick brown fox', )) x1._previous_version = Mock(foo=Mock( bar='The lazy red fox', baz='The lazy brown fox', )) self.assertTrue(Q(foo__bar__now_endswith='brown fox')(x1)) self.assertFalse(Q(foo__baz__now_endswith='brown fox')(x1))
def test_now_not_in(self): x1 = Mock(foo=Mock( bar='PUBLISHED', baz='PUBLISHED', )) x1._previous_version = Mock(foo=Mock( bar='PENDING', baz='PUBLISHED', )) self.assertTrue( Q(foo__bar__now_not_in={'PENDING', 'X', 'Y', 'Z'})(x1), ) self.assertFalse( Q(foo__baz__now_not_in={'PENDING', 'X', 'Y', 'Z'})(x1), )
def test_all_must_match(self): obj = Mock( full_name='George Costanza', is_staff=True, rank=3.03, violations=None, ) self.assertTrue( Q(is_staff__eq=True, full_name__contains=' ', full_name__startswith='George', full_name__endswith='Costanza', rank__gt=2.3, violations__is=None)(obj)) self.assertFalse( Q(is_staff__eq=False, full_name__contains=' ', full_name__startswith='George', full_name__endswith='Costanza', rank__gt=2.3, violations__is=None)(obj))
def test_now_eq(self): x1 = Mock(foo=Mock(bar=1, baz=2)) x1._previous_version = Mock(foo=Mock(bar=0, baz=2)) q = Q(foo__bar__now_eq=1) self.assertTrue(q(x1)) x2 = Mock(foo=Mock(bar=1, baz=2)) x2._previous_version = Mock(foo=Mock(bar=1, baz=2)) self.assertFalse(q(x2)) x3 = Mock(state1='PUBLISHED', state2='PUBLISHED') x3._previous_version = Mock( state1='PENDING', state2='PUBLISHED', ) self.assertTrue(Q(state1__now_eq='PUBLISHED')(x3)) self.assertFalse(Q(state2__now_eq='PUBLISHED')(x3))
def test_lte(self): self.assertTrue(Q(foo__lte=30)(Mock(foo=29))) self.assertTrue(Q(foo__lte=30)(Mock(foo=30))) self.assertFalse(Q(foo__lte=30)(Mock(foo=31)))
def test_gt(self): self.assertTrue(Q(foo__gt=30)(Mock(foo=31))) self.assertFalse(Q(foo__gt=30)(Mock(foo=30)))
def test_ne(self): self.assertTrue(Q(foo__ne=30)(Mock(foo=20))) self.assertTrue(Q(foo__ne=30)(Mock(foo=None))) self.assertFalse(Q(foo__ne=30)(Mock(foo=30)))
def test_now_ne(self): x1 = Mock(foo=Mock(bar=1, baz=1)) x1._previous_version = Mock(foo=Mock(bar=0, baz=2)) self.assertTrue(Q(foo__bar__now_ne=2)(x1)) self.assertFalse(Q(foo__baz__now_ne=2)(x1))
def test_eq(self): self.assertTrue(Q(foo__eq=30)(Mock(foo=30))) self.assertFalse(Q(foo__eq=30)(Mock(foo=10))) self.assertFalse(Q(foo__eq=30)(Mock(foo=None)))
def test_now_is_not(self): x1 = Mock(foo=Mock(bar=30, baz=30)) x1._previous_version = Mock(foo=Mock(bar=None, baz=10)) self.assertTrue(Q(foo__bar__now_is_not=None)(x1)) self.assertFalse(Q(foo__baz__now_is_not=None)(x1))
def test_now_gt(self): x1 = Mock(foo=Mock(bar=22, baz=22)) x1._previous_version = Mock(foo=Mock(bar=10, baz=42)) self.assertTrue(Q(foo__bar__now_gt=20)(x1)) self.assertFalse(Q(foo__baz__now_gt=20)(x1))
def test_dumps(self): obj = Mock(name='obj') encode = Mock(name='encode') self.assertIs(dumps(obj, encode=encode), encode.return_value) encode.assert_called_with(obj, cls=JsonEncoder)
def test_now_gt(self): x1 = Mock(foo=Mock(bar=22, baz=22)) x1._previous_version = Mock(foo=Mock(bar=10, baz=42)) self.assertTrue(Q(foo__bar__now_gt=20)(x1)) self.assertFalse(Q(foo__baz__now_gt=20)(x1))
def test_now_eq(self): x1 = Mock(foo=Mock(bar=1, baz=2)) x1._previous_version = Mock(foo=Mock(bar=0, baz=2)) q = Q(foo__bar__now_eq=1) self.assertTrue(q(x1)) x2 = Mock(foo=Mock(bar=1, baz=2)) x2._previous_version = Mock(foo=Mock(bar=1, baz=2)) self.assertFalse(q(x2)) x3 = Mock(state1='PUBLISHED', state2='PUBLISHED') x3._previous_version = Mock( state1='PENDING', state2='PUBLISHED', ) self.assertTrue(Q(state1__now_eq='PUBLISHED')(x3)) self.assertFalse(Q(state2__now_eq='PUBLISHED')(x3))
def test_now_lte(self): x1 = Mock(foo=Mock(bar=22, baz=22)) x1._previous_version = Mock(foo=Mock(bar=42, baz=12)) self.assertTrue(Q(foo__bar__now_lte=22)(x1)) self.assertFalse(Q(foo__baz__now_lte=22)(x1))
def test_now_is_not(self): x1 = Mock(foo=Mock(bar=30, baz=30)) x1._previous_version = Mock(foo=Mock(bar=None, baz=10)) self.assertTrue(Q(foo__bar__now_is_not=None)(x1)) self.assertFalse(Q(foo__baz__now_is_not=None)(x1))
def test_now_lte(self): x1 = Mock(foo=Mock(bar=22, baz=22)) x1._previous_version = Mock(foo=Mock(bar=42, baz=12)) self.assertTrue(Q(foo__bar__now_lte=22)(x1)) self.assertFalse(Q(foo__baz__now_lte=22)(x1))
def test_contains(self): self.assertTrue(Q(foo__contains='_')(Mock(foo='the_quick')), ) self.assertFalse(Q(foo__contains='_')(Mock(foo='the!quick')), )
def test_dumps(self): obj = Mock(name='obj') encode = Mock(name='encode') self.assertIs(dumps(obj, encode=encode), encode.return_value) encode.assert_called_with(obj, cls=JsonEncoder)
def test_now_ne(self): x1 = Mock(foo=Mock(bar=1, baz=1)) x1._previous_version = Mock(foo=Mock(bar=0, baz=2)) self.assertTrue(Q(foo__bar__now_ne=2)(x1)) self.assertFalse(Q(foo__baz__now_ne=2)(x1))
def setup(self): self.dispatcher = Mock(name='dispatcher') self.sender = Mock(name='sender') self.handler = signals.signal_handler( self.dispatcher, 1, 2, 3, sender=self.sender, foo=4, )
def test_missing_op(self): with self.assertRaises(ValueError): Q(foo=30)(Mock())
def test_perform_create(self): serializer = Mock(name='serializer') self.view.perform_create(serializer) serializer.save.assert_called_with(user=self.view.request.user)
def test_missing_op__nested(self): with self.assertRaises(ValueError): Q(foo__bar__baz=30)(Mock())