def test_batch(self): """Tests whether batching works.""" self._clear_index() video1 = self.create_video(name='Video1', update_index=False) video2 = self.create_video(name='Video2', update_index=False) video3 = self.create_video(name='Video3', update_index=False) expected = set() results = set((r.pk for r in SearchQuerySet())) self.assertEqual(results, expected) self.batches = 0 def count_batch(sender, **kwargs): self.batches = self.batches + 1 task_postrun.connect(count_batch, sender=haystack_update) haystack_batch_update.apply(args=(Video._meta.app_label, Video._meta.module_name), kwargs={'batch_size': 1}) self.assertEqual(self.batches, 3) expected = set((video1.pk, video2.pk, video3.pk)) results = set((int(r.pk) for r in SearchQuerySet())) self.assertEqual(results, expected)
def test_remove(self): """ ``remove`` kwarg should be passed on to the batches. """ self._clear_index() video1 = self.create_video(name='Video1', update_index=False) def get_remove_passed(sender, **kwargs): self.assertTrue('remove' in kwargs['kwargs']) self.remove = kwargs['kwargs']['remove'] task_postrun.connect(get_remove_passed, sender=haystack_update) expected = True haystack_batch_update.apply(args=(Video._meta.app_label, Video._meta.module_name), kwargs={'remove': expected}) self.assertEqual(self.remove, expected) expected = False haystack_batch_update.apply(args=(Video._meta.app_label, Video._meta.module_name), kwargs={'remove': expected}) self.assertEqual(self.remove, expected)
def test_distinct_pks(self): self._clear_index() video1 = self.create_video(name='Video1', update_index=False) self.create_watch(video1, days=5) self.create_watch(video1, days=3) self.create_watch(video1, days=2) six_days_ago = datetime.now() - timedelta(6) with mock.patch.object(haystack_update, 'delay') as delay: haystack_batch_update.apply(args=(Video._meta.app_label, Video._meta.module_name), kwargs={'start': six_days_ago, 'date_lookup': 'watch__timestamp'}) delay.assert_called_once_with(Video._meta.app_label, Video._meta.module_name, [video1.pk], remove=True)
def test_distinct_pks(self): self._clear_index() video1 = self.create_video(name='Video1', update_index=False) self.create_watch(video1, days=5) self.create_watch(video1, days=3) self.create_watch(video1, days=2) six_days_ago = datetime.now() - timedelta(6) with mock.patch.object(haystack_update, 'delay') as delay: haystack_batch_update.apply(args=(Video._meta.app_label, Video._meta.module_name), kwargs={ 'start': six_days_ago, 'date_lookup': 'watch__timestamp' }) delay.assert_called_once_with(Video._meta.app_label, Video._meta.module_name, [video1.pk], remove=True)
def test_date_filtering(self): """ It should be possible to filter the batch update by a start and end date for a given lookup. """ self._clear_index() video1 = self.create_video(name='Video1', update_index=False) video2 = self.create_video(name='Video2', update_index=False) video3 = self.create_video(name='Video3', update_index=False) video4 = self.create_video(name='Video4', update_index=False) self.create_watch(video2, days=5) self.create_watch(video3, days=7) self.create_watch(video4, days=9) expected = set() results = set((r.pk for r in SearchQuerySet())) self.assertEqual(results, expected) now = datetime.now() six_days_ago = now - timedelta(6) eight_days_ago = now - timedelta(8) haystack_batch_update.apply(args=(Video._meta.app_label, Video._meta.module_name), kwargs={ 'start': six_days_ago, 'date_lookup': 'watch__timestamp' }) expected = set((video2.pk, )) results = set((int(r.pk) for r in SearchQuerySet())) self.assertEqual(results, expected) self._clear_index() haystack_batch_update.apply(args=(Video._meta.app_label, Video._meta.module_name), kwargs={ 'end': six_days_ago, 'date_lookup': 'watch__timestamp' }) expected = set((video3.pk, video4.pk)) results = set((int(r.pk) for r in SearchQuerySet())) self.assertEqual(results, expected) self._clear_index() haystack_batch_update.apply(args=(Video._meta.app_label, Video._meta.module_name), kwargs={ 'start': eight_days_ago, 'end': six_days_ago, 'date_lookup': 'watch__timestamp' }) expected = set((video3.pk, )) results = set((int(r.pk) for r in SearchQuerySet())) self.assertEqual(results, expected)
def test_date_filtering(self): """ It should be possible to filter the batch update by a start and end date for a given lookup. """ self._clear_index() video1 = self.create_video(name='Video1', update_index=False) video2 = self.create_video(name='Video2', update_index=False) video3 = self.create_video(name='Video3', update_index=False) video4 = self.create_video(name='Video4', update_index=False) self.create_watch(video2, days=5) self.create_watch(video3, days=7) self.create_watch(video4, days=9) expected = set() results = set((r.pk for r in SearchQuerySet())) self.assertEqual(results, expected) now = datetime.now() six_days_ago = now - timedelta(6) eight_days_ago = now - timedelta(8) haystack_batch_update.apply(args=(Video._meta.app_label, Video._meta.module_name), kwargs={'start': six_days_ago, 'date_lookup': 'watch__timestamp'}) expected = set((video2.pk,)) results = set((int(r.pk) for r in SearchQuerySet())) self.assertEqual(results, expected) self._clear_index() haystack_batch_update.apply(args=(Video._meta.app_label, Video._meta.module_name), kwargs={'end': six_days_ago, 'date_lookup': 'watch__timestamp'}) expected = set((video3.pk, video4.pk)) results = set((int(r.pk) for r in SearchQuerySet())) self.assertEqual(results, expected) self._clear_index() with override_settings(DEBUG=True): haystack_batch_update.apply(args=(Video._meta.app_label, Video._meta.module_name), kwargs={'start': eight_days_ago, 'end': six_days_ago, 'date_lookup': 'watch__timestamp'}) queries = connections['default'].queries # The query here shouldn't use the index queryset as its base. # If it did, it'll have an OUTER JOIN in it. self.assertFalse('OUTER JOIN' in queries[0]['sql']) expected = set((video3.pk,)) results = set((int(r.pk) for r in SearchQuerySet())) self.assertEqual(results, expected)
def test_date_filtering(self): """ It should be possible to filter the batch update by a start and end date for a given lookup. """ self._clear_index() video1 = self.create_video(name='Video1', update_index=False) video2 = self.create_video(name='Video2', update_index=False) video3 = self.create_video(name='Video3', update_index=False) video4 = self.create_video(name='Video4', update_index=False) self.create_watch(video2, days=5) self.create_watch(video3, days=7) self.create_watch(video4, days=9) expected = set() results = set((r.pk for r in SearchQuerySet())) self.assertEqual(results, expected) now = datetime.now() six_days_ago = now - timedelta(6) eight_days_ago = now - timedelta(8) haystack_batch_update.apply(args=(Video._meta.app_label, Video._meta.module_name), kwargs={'start': six_days_ago, 'date_lookup': 'watch__timestamp'}) expected = set((video2.pk,)) results = set((int(r.pk) for r in SearchQuerySet())) self.assertEqual(results, expected) self._clear_index() haystack_batch_update.apply(args=(Video._meta.app_label, Video._meta.module_name), kwargs={'end': six_days_ago, 'date_lookup': 'watch__timestamp'}) expected = set((video3.pk, video4.pk)) results = set((int(r.pk) for r in SearchQuerySet())) self.assertEqual(results, expected) self._clear_index() haystack_batch_update.apply(args=(Video._meta.app_label, Video._meta.module_name), kwargs={'start': eight_days_ago, 'end': six_days_ago, 'date_lookup': 'watch__timestamp'}) expected = set((video3.pk,)) results = set((int(r.pk) for r in SearchQuerySet())) self.assertEqual(results, expected)
def test_date_filtering(self): """ It should be possible to filter the batch update by a start and end date for a given lookup. """ self._clear_index() video1 = self.create_video(name='Video1', update_index=False) video2 = self.create_video(name='Video2', update_index=False) video3 = self.create_video(name='Video3', update_index=False) video4 = self.create_video(name='Video4', update_index=False) self.create_watch(video2, days=5) self.create_watch(video3, days=7) self.create_watch(video4, days=9) expected = set() results = set((r.pk for r in SearchQuerySet())) self.assertEqual(results, expected) now = datetime.now() six_days_ago = now - timedelta(6) eight_days_ago = now - timedelta(8) haystack_batch_update.apply(args=(Video._meta.app_label, Video._meta.module_name), kwargs={ 'start': six_days_ago, 'date_lookup': 'watch__timestamp' }) expected = set((video2.pk, )) results = set((int(r.pk) for r in SearchQuerySet())) self.assertEqual(results, expected) self._clear_index() haystack_batch_update.apply(args=(Video._meta.app_label, Video._meta.module_name), kwargs={ 'end': six_days_ago, 'date_lookup': 'watch__timestamp' }) expected = set((video3.pk, video4.pk)) results = set((int(r.pk) for r in SearchQuerySet())) self.assertEqual(results, expected) self._clear_index() with override_settings(DEBUG=True): haystack_batch_update.apply(args=(Video._meta.app_label, Video._meta.module_name), kwargs={ 'start': eight_days_ago, 'end': six_days_ago, 'date_lookup': 'watch__timestamp' }) queries = connections['default'].queries # The query here shouldn't use the index queryset as its base. # If it did, it'll have an OUTER JOIN in it. self.assertFalse('OUTER JOIN' in queries[0]['sql']) expected = set((video3.pk, )) results = set((int(r.pk) for r in SearchQuerySet())) self.assertEqual(results, expected)