From 51b4f0f53d93a69f3beaba67e3211cd001ff4087 Mon Sep 17 00:00:00 2001 From: Morgan Aubert Date: Tue, 15 Nov 2016 13:15:51 -0500 Subject: [PATCH] Fix #3134 Changes include: * fixing the FieldError exception that can occur as described in #3134 * add tests for the "search_garbage_collect" command --- CHANGELOG.txt | 1 + CONTRIBUTORS.rst | 1 + docs/releases/1.8.rst | 1 + wagtail/wagtailsearch/models.py | 4 +- wagtail/wagtailsearch/tests/test_queries.py | 44 ++++++++++++++++++++- 5 files changed, 49 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 5fef53f387..7da7827d66 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -26,6 +26,7 @@ Changelog * Fix: Form builder email notifications now output multiple values correctly (Sævar Öfjörð Magnússon) * Fix: Closing 'more' dropdown on explorer no longer jumps to the top of the page (Ducky) * Fix: Users with only publish permission are no longer given implicit permission to delete pages (Matt Westcott) + * Fix: `search_garbage_collect` management command now works when wagtailsearchpromotions is not installed (Morgan Aubert) 1.7 (20.10.2016) diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index 2b50635e7a..be43b6bfbd 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -185,6 +185,7 @@ Contributors * Benoît Vogel * Manuel E. Gutierrez * Albert O'Connor +* Morgan Aubert Translators =========== diff --git a/docs/releases/1.8.rst b/docs/releases/1.8.rst index 5ef19bd250..28a6034a1e 100644 --- a/docs/releases/1.8.rst +++ b/docs/releases/1.8.rst @@ -57,6 +57,7 @@ Bug fixes * Form builder email notifications now output multiple values correctly (Sævar Öfjörð Magnússon) * Closing 'more' dropdown on explorer no longer jumps to the top of the page (Ducky) * Users with only publish permission are no longer given implicit permission to delete pages (Matt Westcott) + * ``search_garbage_collect`` management command now works when wagtailsearchpromotions is not installed (Morgan Aubert) Upgrade considerations ====================== diff --git a/wagtail/wagtailsearch/models.py b/wagtail/wagtailsearch/models.py index a31faf4837..b87f27ab6c 100644 --- a/wagtail/wagtailsearch/models.py +++ b/wagtail/wagtailsearch/models.py @@ -41,7 +41,9 @@ class Query(models.Model): """ Deletes all Query records that have no daily hits or editors picks """ - cls.objects.filter(daily_hits__isnull=True, editors_picks__isnull=True).delete() + extra_filter_kwargs = {'editors_picks__isnull': True, } if hasattr(cls, 'editors_picks') \ + else {} + cls.objects.filter(daily_hits__isnull=True, **extra_filter_kwargs).delete() @classmethod def get(cls, query_string): diff --git a/wagtail/wagtailsearch/tests/test_queries.py b/wagtail/wagtailsearch/tests/test_queries.py index eb5db981c3..b8d7510d01 100644 --- a/wagtail/wagtailsearch/tests/test_queries.py +++ b/wagtail/wagtailsearch/tests/test_queries.py @@ -1,10 +1,15 @@ from __future__ import absolute_import, unicode_literals +import datetime + +from django.conf import settings from django.core import management from django.test import TestCase +from django.test.utils import override_settings from django.utils.six import StringIO +from wagtail.contrib.wagtailsearchpromotions.models import SearchPromotion from wagtail.tests.utils import WagtailTestUtils from wagtail.wagtailsearch import models from wagtail.wagtailsearch.utils import normalise_query_string @@ -110,9 +115,46 @@ class TestQueryPopularity(TestCase): class TestGarbageCollectCommand(TestCase): def test_garbage_collect_command(self): + nowdt = datetime.datetime.now() + old_hit_date = (nowdt - datetime.timedelta(days=14)).date() + recent_hit_date = (nowdt - datetime.timedelta(days=1)).date() + + # Add 10 hits that are more than one week old ; the related queries and the daily hits + # should be deleted bu the search_garbage_collect command. + querie_ids_to_be_deleted = [] + for i in range(10): + q = models.Query.get("Hello {}".format(i)) + q.add_hit(date=old_hit_date) + querie_ids_to_be_deleted.append(q.id) + + # Add 10 hits that are less than one week old ; these ones should not be deleted. + recent_querie_ids = [] + for i in range(10): + q = models.Query.get("World {}".format(i)) + q.add_hit(date=recent_hit_date) + recent_querie_ids.append(q.id) + + # Add 10 queries that are promoted. These ones should not be deleted. + promoted_querie_ids = [] + for i in range(10): + q = models.Query.get("Foo bar {}".format(i)) + q.add_hit(date=old_hit_date) + SearchPromotion.objects.create(query=q, page_id=1, sort_order=0, description='Test') + promoted_querie_ids.append(q.id) + management.call_command('search_garbage_collect', interactive=False, stdout=StringIO()) - # TODO: Test that this command is acctually doing its job + self.assertFalse(models.Query.objects.filter(id__in=querie_ids_to_be_deleted).exists()) + self.assertFalse(models.QueryDailyHits.objects.filter( + date=old_hit_date, query_id__in=querie_ids_to_be_deleted).exists()) + + self.assertEqual(models.Query.objects.filter(id__in=recent_querie_ids).count(), 10) + self.assertEqual(models.QueryDailyHits.objects.filter( + date=recent_hit_date, query_id__in=recent_querie_ids).count(), 10) + + self.assertEqual(models.Query.objects.filter(id__in=promoted_querie_ids).count(), 10) + self.assertEqual(models.QueryDailyHits.objects.filter( + date=recent_hit_date, query_id__in=promoted_querie_ids).count(), 0) class TestQueryChooserView(TestCase, WagtailTestUtils):