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_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 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_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_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_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_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_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_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_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_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])
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_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_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_perform_create(self): serializer = Mock(name='serializer') self.view.perform_create(serializer) serializer.save.assert_called_with(user=self.view.request.user)
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_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_contains(self): self.assertTrue(Q(foo__contains='_')(Mock(foo='the_quick')), ) self.assertFalse(Q(foo__contains='_')(Mock(foo='the!quick')), )
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_gt(self): self.assertTrue(Q(foo__gt=30)(Mock(foo=31))) self.assertFalse(Q(foo__gt=30)(Mock(foo=30)))
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_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_missing_op__nested(self): with self.assertRaises(ValueError): Q(foo__bar__baz=30)(Mock())
def test_missing_op(self): with self.assertRaises(ValueError): Q(foo=30)(Mock())
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_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))