From 04c507e37be80b9ba5b88fdc6140acdad43f3ed3 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Sun, 22 Jun 2014 18:25:42 +0100 Subject: [PATCH] Added tests for indexed class --- wagtail/tests/models.py | 28 +++++++++++ .../wagtailsearch/tests/test_indexed_class.py | 49 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 wagtail/wagtailsearch/tests/test_indexed_class.py diff --git a/wagtail/tests/models.py b/wagtail/tests/models.py index 86c85e7f9..504648933 100644 --- a/wagtail/tests/models.py +++ b/wagtail/tests/models.py @@ -346,3 +346,31 @@ class SearchTestChild(SearchTest): indexed.SearchField('subtitle', partial_match=True), indexed.SearchField('extra_content'), ] + + +class SearchTestOldConfig(models.Model, indexed.Indexed): + """ + This tests that the Indexed class can correctly handle models that + use the old "indexed_fields" configuration format. + """ + indexed_fields = { + # A search field with predictive search and boosting + 'title': { + 'type': 'string', + 'analyzer': 'edgengram_analyzer', + 'boost': 100, + }, + + # A filter field + 'live': { + 'type': 'boolean', + 'index': 'not_analyzed', + }, + } + +class SearchTestOldConfigList(models.Model, indexed.Indexed): + """ + This tests that the Indexed class can correctly handle models that + use the old "indexed_fields" configuration format using a list. + """ + indexed_fields = ['title', 'content'] diff --git a/wagtail/wagtailsearch/tests/test_indexed_class.py b/wagtail/wagtailsearch/tests/test_indexed_class.py new file mode 100644 index 000000000..a54f135c3 --- /dev/null +++ b/wagtail/wagtailsearch/tests/test_indexed_class.py @@ -0,0 +1,49 @@ +from django.test import TestCase +from wagtail.tests import models +import json + +from wagtail.wagtailsearch import indexed + + +class TestContentTypeNames(TestCase): + def test_base_content_type_name(self): + name = models.SearchTestChild.indexed_get_toplevel_content_type() + self.assertEqual(name, 'tests_searchtest') + + def test_qualified_content_type_name(self): + name = models.SearchTestChild.indexed_get_content_type() + self.assertEqual(name, 'tests_searchtest_tests_searchtestchild') + + +class TestIndexedFieldsBackwardsCompatibility(TestCase): + def test_indexed_fields_backwards_compatibility(self): + # Get search fields + search_fields = models.SearchTestOldConfig.get_search_fields() + + search_fields_dict = dict( + ((field.field_name, type(field)), field) + for field in search_fields + ) + + # Check that the fields were found + self.assertEqual(len(search_fields_dict), 2) + self.assertIn(('title', indexed.SearchField), search_fields_dict.keys()) + self.assertIn(('live', indexed.FilterField), search_fields_dict.keys()) + + # Check that the title field has the correct settings + self.assertTrue(search_fields_dict[('title', indexed.SearchField)].partial_match) + self.assertEqual(search_fields_dict[('title', indexed.SearchField)].boost, 100) + + def test_indexed_fields_backwards_compatibility_list(self): + # Get search fields + search_fields = models.SearchTestOldConfigList.get_search_fields() + + search_fields_dict = dict( + ((field.field_name, type(field)), field) + for field in search_fields + ) + + # Check that the fields were found + self.assertEqual(len(search_fields_dict), 2) + self.assertIn(('title', indexed.SearchField), search_fields_dict.keys()) + self.assertIn(('content', indexed.SearchField), search_fields_dict.keys())