def test_log_query(self): backends.reset_search_queries() self.assertEqual(len(backends.queries), 0) # Stow. old_site = haystack.site old_debug = settings.DEBUG test_site = SearchSite() test_site.register(MockModel) haystack.site = test_site settings.DEBUG = False msq = MockSearchQuery(backend=MockSearchBackend()) self.assertEqual(len(msq.get_results()), 100) self.assertEqual(len(backends.queries), 0) settings.DEBUG = True # Redefine it to clear out the cached results. msq2 = MockSearchQuery(backend=MockSearchBackend()) self.assertEqual(len(msq2.get_results()), 100) self.assertEqual(len(backends.queries), 1) self.assertEqual(backends.queries[0]['query_string'], '') msq3 = MockSearchQuery(backend=MockSearchBackend()) msq3.add_filter(SQ(foo='bar')) len(msq3.get_results()) self.assertEqual(len(backends.queries), 2) self.assertEqual(backends.queries[0]['query_string'], '') self.assertEqual(backends.queries[1]['query_string'], '') # Restore. haystack.site = old_site settings.DEBUG = old_debug
def setUp(self): super(SearchIndexTestCase, self).setUp() self.msb = MockSearchBackend() self.mi = GoodMockSearchIndex(MockModel, backend=self.msb) self.cmi = GoodCustomMockSearchIndex(MockModel, backend=self.msb) self.cnmi = GoodNullableMockSearchIndex(MockModel, backend=self.msb) self.sample_docs = { u'core.mockmodel.1': { 'content': u'Indexed!\n1', 'django_id': u'1', 'django_ct': u'core.mockmodel', 'extra': u'Stored!\n1', 'author': u'daniel1', 'pub_date': datetime.datetime(2009, 3, 17, 6, 0), 'id': u'core.mockmodel.1' }, u'core.mockmodel.2': { 'content': u'Indexed!\n2', 'django_id': u'2', 'django_ct': u'core.mockmodel', 'extra': u'Stored!\n2', 'author': u'daniel2', 'pub_date': datetime.datetime(2009, 3, 17, 7, 0), 'id': u'core.mockmodel.2' }, u'core.mockmodel.3': { 'content': u'Indexed!\n3', 'django_id': u'3', 'django_ct': u'core.mockmodel', 'extra': u'Stored!\n3', 'author': u'daniel3', 'pub_date': datetime.datetime(2009, 3, 17, 8, 0), 'id': u'core.mockmodel.3' } }
def setUp(self): super(SearchIndexTestCase, self).setUp() self.msb = MockSearchBackend() self.mi = GoodMockSearchIndex(MockModel, backend=self.msb) self.cmi = GoodCustomMockSearchIndex(MockModel, backend=self.msb) self.cnmi = GoodNullableMockSearchIndex(MockModel, backend=self.msb) self.sample_docs = { u"core.mockmodel.1": { "content": u"Indexed!\n1", "django_id": u"1", "django_ct": u"core.mockmodel", "extra": u"Stored!\n1", "author": u"daniel1", "pub_date": datetime.datetime(2009, 3, 17, 6, 0), "id": u"core.mockmodel.1", }, u"core.mockmodel.2": { "content": u"Indexed!\n2", "django_id": u"2", "django_ct": u"core.mockmodel", "extra": u"Stored!\n2", "author": u"daniel2", "pub_date": datetime.datetime(2009, 3, 17, 7, 0), "id": u"core.mockmodel.2", }, u"core.mockmodel.3": { "content": u"Indexed!\n3", "django_id": u"3", "django_ct": u"core.mockmodel", "extra": u"Stored!\n3", "author": u"daniel3", "pub_date": datetime.datetime(2009, 3, 17, 8, 0), "id": u"core.mockmodel.3", }, }
def test_more_like_this(self): mock = MockModel() mock.id = 1 msq = MockSearchQuery(backend=MockSearchBackend()) msq.more_like_this(mock) self.assertEqual(msq.get_count(), 100) self.assertEqual(msq.get_results()[0], MOCK_SEARCH_RESULTS[0])
def setUp(self): super(ModelSearchIndexTestCase, self).setUp() self.msb = MockSearchBackend() self.bmsi = BasicModelSearchIndex(MockModel, backend=self.msb) self.fmsi = FieldsModelSearchIndex(MockModel, backend=self.msb) self.emsi = ExcludesModelSearchIndex(MockModel, backend=self.msb) self.fwomsi = FieldsWithOverrideModelSearchIndex(MockModel, backend=self.msb) self.yabmsi = YetAnotherBasicModelSearchIndex(AThirdMockModel, backend=self.msb)
def test_more_like_this(self): mock = MockModel() mock.id = 1 msq = MockSearchQuery() msq.backend = MockSearchBackend('mlt') ui = connections['default'].get_unified_index() bmmsi = BasicMockModelSearchIndex() ui.build(indexes=[bmmsi]) bmmsi.update() msq.more_like_this(mock) self.assertEqual(msq.get_count(), 23) self.assertEqual(int(msq.get_results()[0].pk), MOCK_SEARCH_RESULTS[0].pk)
def test_run(self): # Stow. old_site = haystack.site test_site = SearchSite() test_site.register(MockModel) haystack.site = test_site msq = MockSearchQuery(backend=MockSearchBackend()) self.assertEqual(len(msq.get_results()), 100) self.assertEqual(msq.get_results()[0], MOCK_SEARCH_RESULTS[0]) # Restore. haystack.site = old_site
def setUp(self): super(SearchIndexTestCase, self).setUp() self.msb = MockSearchBackend() self.mi = GoodMockSearchIndex(MockModel, backend=self.msb) self.cmi = GoodCustomMockSearchIndex(MockModel, backend=self.msb) self.cnmi = GoodNullableMockSearchIndex(MockModel, backend=self.msb) self.gfmsi = GoodFacetedMockSearchIndex(MockModel, backend=self.msb) self.sample_docs = { u'core.mockmodel.1': { 'content': u'Indexed!\n1', 'django_id': u'1', 'django_ct': u'core.mockmodel', 'extra': u'Stored!\n1', 'author': u'daniel1', 'pub_date': datetime.datetime(2009, 3, 17, 6, 0), 'id': u'core.mockmodel.1' }, u'core.mockmodel.2': { 'content': u'Indexed!\n2', 'django_id': u'2', 'django_ct': u'core.mockmodel', 'extra': u'Stored!\n2', 'author': u'daniel2', 'pub_date': datetime.datetime(2009, 3, 17, 7, 0), 'id': u'core.mockmodel.2' }, u'core.mockmodel.3': { 'content': u'Indexed!\n3', 'django_id': u'3', 'django_ct': u'core.mockmodel', 'extra': u'Stored!\n3', 'author': u'daniel3', 'pub_date': datetime.datetime(2009, 3, 17, 8, 0), 'id': u'core.mockmodel.3' } }
def setUp(self): super(PickleSearchQuerySetTestCase, self).setUp() self.bsqs = SearchQuerySet(query=DummySearchQuery(backend=DummySearchBackend())) self.msqs = SearchQuerySet(query=MockSearchQuery(backend=MockSearchBackend())) self.mmsqs = SearchQuerySet(query=MockSearchQuery(backend=MixedMockSearchBackend())) # Stow. self.old_debug = settings.DEBUG settings.DEBUG = True self.old_site = haystack.site test_site = SearchSite() test_site.register(MockModel) test_site.register(CharPKMockModel) haystack.site = test_site backends.reset_search_queries()
class SearchIndexTestCase(TestCase): def setUp(self): super(SearchIndexTestCase, self).setUp() self.msb = MockSearchBackend() self.mi = GoodMockSearchIndex(MockModel, backend=self.msb) self.cmi = GoodCustomMockSearchIndex(MockModel, backend=self.msb) self.cnmi = GoodNullableMockSearchIndex(MockModel, backend=self.msb) self.sample_docs = { u'core.mockmodel.1': { 'content': u'Indexed!\n1', 'django_id': u'1', 'django_ct': u'core.mockmodel', 'extra': u'Stored!\n1', 'author': u'daniel1', 'pub_date': datetime.datetime(2009, 3, 17, 6, 0), 'id': u'core.mockmodel.1' }, u'core.mockmodel.2': { 'content': u'Indexed!\n2', 'django_id': u'2', 'django_ct': u'core.mockmodel', 'extra': u'Stored!\n2', 'author': u'daniel2', 'pub_date': datetime.datetime(2009, 3, 17, 7, 0), 'id': u'core.mockmodel.2' }, u'core.mockmodel.3': { 'content': u'Indexed!\n3', 'django_id': u'3', 'django_ct': u'core.mockmodel', 'extra': u'Stored!\n3', 'author': u'daniel3', 'pub_date': datetime.datetime(2009, 3, 17, 8, 0), 'id': u'core.mockmodel.3' } } def test_no_contentfield_present(self): self.assertRaises(indexes.SearchFieldError, BadSearchIndex1, MockModel, MockSearchBackend()) def test_too_many_contentfields_present(self): self.assertRaises(indexes.SearchFieldError, BadSearchIndex2, MockModel, MockSearchBackend()) def test_contentfield_present(self): try: mi = GoodMockSearchIndex(MockModel, backend=MockSearchBackend()) except: self.fail() def test_proper_fields(self): self.assertEqual(len(self.mi.fields), 4) self.assert_('content' in self.mi.fields) self.assert_(isinstance(self.mi.fields['content'], indexes.CharField)) self.assert_('author' in self.mi.fields) self.assert_(isinstance(self.mi.fields['author'], indexes.CharField)) self.assert_('pub_date' in self.mi.fields) self.assert_(isinstance(self.mi.fields['pub_date'], indexes.DateTimeField)) self.assert_('extra' in self.mi.fields) self.assert_(isinstance(self.mi.fields['extra'], indexes.CharField)) def test_get_query_set(self): self.assertEqual(len(self.mi.get_query_set()), 3) def test_prepare(self): mock = MockModel() mock.pk = 20 mock.user = '******' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.assertEqual(len(self.mi.prepare(mock)), 4) self.assertEqual(sorted(self.mi.prepare(mock).keys()), ['author', 'content', 'extra', 'pub_date']) def test_custom_prepare(self): mock = MockModel() mock.pk = 20 mock.user = '******' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.assertEqual(len(self.cmi.prepare(mock)), 5) self.assertEqual(sorted(self.cmi.prepare(mock).keys()), ['author', 'content', 'extra', 'pub_date', 'whee']) def test_custom_prepare_author(self): mock = MockModel() mock.pk = 20 mock.user = '******' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.assertEqual(len(self.cmi.prepare(mock)), 5) self.assertEqual(sorted(self.cmi.prepare(mock).keys()), ['author', 'content', 'extra', 'pub_date', 'whee']) self.assertEqual(self.cmi.prepared_data['author'], "Hi, I'm daniel20") def test_get_content_field(self): self.assertEqual(self.mi.get_content_field(), 'content') def test_update(self): self.mi.update() self.assertEqual(self.msb.docs, self.sample_docs) self.msb.clear() def test_update_object(self): self.assertEqual(self.msb.docs, {}) mock = MockModel() mock.pk = 20 mock.user = '******' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.mi.update_object(mock) self.assertEqual(self.msb.docs, {'core.mockmodel.20': {'django_id': u'20', 'django_ct': u'core.mockmodel', 'author': u'daniel20', 'extra': u'Stored!\n20', 'content': u'Indexed!\n20', 'pub_date': datetime.datetime(2009, 1, 31, 4, 19), 'id': 'core.mockmodel.20'}}) self.msb.clear() def test_remove_object(self): self.msb.docs = {'core.mockmodel.20': 'Indexed!\n20'} mock = MockModel() mock.pk = 20 self.mi.remove_object(mock) self.assertEqual(self.msb.docs, {}) self.msb.clear() def test_clear(self): self.msb.docs = { 'core.mockmodel.1': 'Indexed!\n1', 'core.mockmodel.2': 'Indexed!\n2', 'core.mockmodel.20': 'Indexed!\n20', } self.mi.clear() self.assertEqual(self.msb.docs, {}) self.msb.clear() def test_reindex(self): self.msb.docs = { 'core.mockmodel.1': 'Indexed!\n1', 'core.mockmodel.2': 'Indexed!\n2', 'core.mockmodel.20': 'Indexed!\n20', } self.mi.reindex() self.assertEqual(self.msb.docs, self.sample_docs) self.msb.clear() def test_inheritance(self): try: agmi = AltGoodMockSearchIndex(MockModel, backend=self.msb) except: self.fail() self.assertEqual(len(agmi.fields), 5) self.assert_('content' in agmi.fields) self.assert_(isinstance(agmi.fields['content'], indexes.CharField)) self.assert_('author' in agmi.fields) self.assert_(isinstance(agmi.fields['author'], indexes.CharField)) self.assert_('pub_date' in agmi.fields) self.assert_(isinstance(agmi.fields['pub_date'], indexes.DateTimeField)) self.assert_('extra' in agmi.fields) self.assert_(isinstance(agmi.fields['extra'], indexes.CharField)) self.assert_('additional' in agmi.fields) self.assert_(isinstance(agmi.fields['additional'], indexes.CharField)) def test_load_all_queryset(self): self.assertEqual([obj.id for obj in self.cmi.load_all_queryset()], [2, 3]) def test_nullable(self): mock = MockModel() mock.pk = 20 mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) prepared_data = self.cnmi.prepare(mock) self.assertEqual(len(prepared_data), 1) self.assertEqual(sorted(prepared_data.keys()), ['content'])
class SearchIndexTestCase(TestCase): def setUp(self): super(SearchIndexTestCase, self).setUp() self.msb = MockSearchBackend() self.mi = GoodMockSearchIndex(MockModel, backend=self.msb) self.cmi = GoodCustomMockSearchIndex(MockModel, backend=self.msb) self.cnmi = GoodNullableMockSearchIndex(MockModel, backend=self.msb) self.gfmsi = GoodFacetedMockSearchIndex(MockModel, backend=self.msb) self.sample_docs = { u'core.mockmodel.1': { 'content': u'Indexed!\n1', 'django_id': u'1', 'django_ct': u'core.mockmodel', 'extra': u'Stored!\n1', 'author': u'daniel1', 'pub_date': datetime.datetime(2009, 3, 17, 6, 0), 'id': u'core.mockmodel.1' }, u'core.mockmodel.2': { 'content': u'Indexed!\n2', 'django_id': u'2', 'django_ct': u'core.mockmodel', 'extra': u'Stored!\n2', 'author': u'daniel2', 'pub_date': datetime.datetime(2009, 3, 17, 7, 0), 'id': u'core.mockmodel.2' }, u'core.mockmodel.3': { 'content': u'Indexed!\n3', 'django_id': u'3', 'django_ct': u'core.mockmodel', 'extra': u'Stored!\n3', 'author': u'daniel3', 'pub_date': datetime.datetime(2009, 3, 17, 8, 0), 'id': u'core.mockmodel.3' } } def test_no_contentfield_present(self): self.assertRaises(SearchFieldError, BadSearchIndex1, MockModel, MockSearchBackend()) def test_too_many_contentfields_present(self): self.assertRaises(SearchFieldError, BadSearchIndex2, MockModel, MockSearchBackend()) def test_contentfield_present(self): try: mi = GoodMockSearchIndex(MockModel, backend=MockSearchBackend()) except: self.fail() def test_proper_fields(self): self.assertEqual(len(self.mi.fields), 4) self.assert_('content' in self.mi.fields) self.assert_(isinstance(self.mi.fields['content'], CharField)) self.assert_('author' in self.mi.fields) self.assert_(isinstance(self.mi.fields['author'], CharField)) self.assert_('pub_date' in self.mi.fields) self.assert_(isinstance(self.mi.fields['pub_date'], DateTimeField)) self.assert_('extra' in self.mi.fields) self.assert_(isinstance(self.mi.fields['extra'], CharField)) self.assertEqual(len(self.cmi.fields), 7) self.assert_('content' in self.cmi.fields) self.assert_(isinstance(self.cmi.fields['content'], CharField)) self.assert_('author' in self.cmi.fields) self.assert_(isinstance(self.cmi.fields['author'], CharField)) self.assert_('author_exact' in self.cmi.fields) self.assert_( isinstance(self.cmi.fields['author_exact'], FacetCharField)) self.assert_('pub_date' in self.cmi.fields) self.assert_(isinstance(self.cmi.fields['pub_date'], DateTimeField)) self.assert_('pub_date_exact' in self.cmi.fields) self.assert_( isinstance(self.cmi.fields['pub_date_exact'], FacetDateTimeField)) self.assert_('extra' in self.cmi.fields) self.assert_(isinstance(self.cmi.fields['extra'], CharField)) self.assert_('hello' in self.cmi.fields) self.assert_(isinstance(self.cmi.fields['extra'], CharField)) def test_get_queryset(self): self.assertEqual(len(self.mi.get_queryset()), 3) def test_prepare(self): mock = MockModel() mock.pk = 20 mock.author = 'daniel%s' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.assertEqual(len(self.mi.prepare(mock)), 7) self.assertEqual(sorted(self.mi.prepare(mock).keys()), [ 'author', 'content', 'django_ct', 'django_id', 'extra', 'id', 'pub_date' ]) def test_custom_prepare(self): mock = MockModel() mock.pk = 20 mock.author = 'daniel%s' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.assertEqual(len(self.cmi.prepare(mock)), 11) self.assertEqual(sorted(self.cmi.prepare(mock).keys()), [ 'author', 'author_exact', 'content', 'django_ct', 'django_id', 'extra', 'hello', 'id', 'pub_date', 'pub_date_exact', 'whee' ]) self.assertEqual(len(self.cmi.full_prepare(mock)), 11) self.assertEqual(sorted(self.cmi.full_prepare(mock).keys()), [ 'author', 'author_exact', 'content', 'django_ct', 'django_id', 'extra', 'hello', 'id', 'pub_date', 'pub_date_exact', 'whee' ]) def test_custom_prepare_author(self): mock = MockModel() mock.pk = 20 mock.author = 'daniel%s' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.assertEqual(len(self.cmi.prepare(mock)), 11) self.assertEqual(sorted(self.cmi.prepare(mock).keys()), [ 'author', 'author_exact', 'content', 'django_ct', 'django_id', 'extra', 'hello', 'id', 'pub_date', 'pub_date_exact', 'whee' ]) self.assertEqual(len(self.cmi.full_prepare(mock)), 11) self.assertEqual(sorted(self.cmi.full_prepare(mock).keys()), [ 'author', 'author_exact', 'content', 'django_ct', 'django_id', 'extra', 'hello', 'id', 'pub_date', 'pub_date_exact', 'whee' ]) self.assertEqual(self.cmi.prepared_data['author'], "Hi, I'm daniel20") self.assertEqual(self.cmi.prepared_data['author_exact'], "Hi, I'm daniel20") def test_custom_model_attr(self): mock = MockModel() mock.pk = 20 mock.author = 'daniel%s' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.assertEqual(len(self.cmi.prepare(mock)), 11) self.assertEqual(sorted(self.cmi.prepare(mock).keys()), [ 'author', 'author_exact', 'content', 'django_ct', 'django_id', 'extra', 'hello', 'id', 'pub_date', 'pub_date_exact', 'whee' ]) self.assertEqual(len(self.cmi.full_prepare(mock)), 11) self.assertEqual(sorted(self.cmi.full_prepare(mock).keys()), [ 'author', 'author_exact', 'content', 'django_ct', 'django_id', 'extra', 'hello', 'id', 'pub_date', 'pub_date_exact', 'whee' ]) self.assertEqual(self.cmi.prepared_data['hello'], u'World!') def test_custom_index_fieldname(self): mock = MockModel() mock.pk = 20 mock.author = 'daniel%s' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) cofnmi = GoodOverriddenFieldNameMockSearchIndex(MockModel, backend=self.msb) self.assertEqual(len(cofnmi.prepare(mock)), 6) self.assertEqual(sorted(cofnmi.prepare(mock).keys()), [ 'django_ct', 'django_id', 'hello', 'id', 'more_content', 'name_s' ]) self.assertEqual(cofnmi.prepared_data['name_s'], u'daniel20') self.assertEqual(cofnmi.get_content_field(), 'more_content') def test_get_content_field(self): self.assertEqual(self.mi.get_content_field(), 'content') def test_update(self): self.mi.update() self.assertEqual(self.msb.docs, self.sample_docs) self.msb.clear() def test_update_object(self): self.assertEqual(self.msb.docs, {}) mock = MockModel() mock.pk = 20 mock.author = 'daniel%s' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.mi.update_object(mock) self.assertEqual( self.msb.docs, { 'core.mockmodel.20': { 'django_id': u'20', 'django_ct': u'core.mockmodel', 'author': u'daniel20', 'extra': u'Stored!\n20', 'content': u'Indexed!\n20', 'pub_date': datetime.datetime(2009, 1, 31, 4, 19), 'id': 'core.mockmodel.20' } }) self.msb.clear() def test_remove_object(self): self.msb.docs = {'core.mockmodel.20': 'Indexed!\n20'} mock = MockModel() mock.pk = 20 self.mi.remove_object(mock) self.assertEqual(self.msb.docs, {}) self.msb.clear() def test_clear(self): self.msb.docs = { 'core.mockmodel.1': 'Indexed!\n1', 'core.mockmodel.2': 'Indexed!\n2', 'core.mockmodel.20': 'Indexed!\n20', } self.mi.clear() self.assertEqual(self.msb.docs, {}) self.msb.clear() def test_reindex(self): self.msb.docs = { 'core.mockmodel.1': 'Indexed!\n1', 'core.mockmodel.2': 'Indexed!\n2', 'core.mockmodel.20': 'Indexed!\n20', } self.mi.reindex() self.assertEqual(self.msb.docs, self.sample_docs) self.msb.clear() def test_inheritance(self): try: agmi = AltGoodMockSearchIndex(MockModel, backend=self.msb) except: self.fail() self.assertEqual(len(agmi.fields), 5) self.assert_('content' in agmi.fields) self.assert_(isinstance(agmi.fields['content'], CharField)) self.assert_('author' in agmi.fields) self.assert_(isinstance(agmi.fields['author'], CharField)) self.assert_('pub_date' in agmi.fields) self.assert_(isinstance(agmi.fields['pub_date'], DateTimeField)) self.assert_('extra' in agmi.fields) self.assert_(isinstance(agmi.fields['extra'], CharField)) self.assert_('additional' in agmi.fields) self.assert_(isinstance(agmi.fields['additional'], CharField)) def test_load_all_queryset(self): self.assertEqual([obj.id for obj in self.cmi.load_all_queryset()], [2, 3]) def test_nullable(self): mock = MockModel() mock.pk = 20 mock.author = None mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) prepared_data = self.cnmi.prepare(mock) self.assertEqual(len(prepared_data), 6) self.assertEqual(sorted(prepared_data.keys()), [ 'author', 'author_exact', 'content', 'django_ct', 'django_id', 'id' ]) prepared_data = self.cnmi.full_prepare(mock) self.assertEqual(len(prepared_data), 4) self.assertEqual(sorted(prepared_data.keys()), ['content', 'django_ct', 'django_id', 'id']) def test_custom_facet_fields(self): mock = MockModel() mock.pk = 20 mock.author = 'daniel' mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) prepared_data = self.gfmsi.prepare(mock) self.assertEqual(len(prepared_data), 8) self.assertEqual(sorted(prepared_data.keys()), [ 'author', 'author_foo', 'content', 'django_ct', 'django_id', 'id', 'pub_date', 'pub_date_exact' ]) prepared_data = self.gfmsi.full_prepare(mock) self.assertEqual(len(prepared_data), 8) self.assertEqual(sorted(prepared_data.keys()), [ 'author', 'author_foo', 'content', 'django_ct', 'django_id', 'id', 'pub_date', 'pub_date_exact' ]) self.assertEqual(prepared_data['author_foo'], u"Hi, I'm daniel") self.assertEqual(prepared_data['pub_date_exact'], '2010-10-26T01:54:32')
def test_contentfield_present(self): try: mi = GoodMockSearchIndex(MockModel, backend=MockSearchBackend()) except: self.fail()
def test_too_many_contentfields_present(self): self.assertRaises(SearchFieldError, BadSearchIndex2, MockModel, MockSearchBackend())
def test_no_contentfield_present(self): self.assertRaises(SearchFieldError, BadSearchIndex1, MockModel, MockSearchBackend())
class SearchIndexTestCase(TestCase): def setUp(self): super(SearchIndexTestCase, self).setUp() self.msb = MockSearchBackend() self.mi = GoodMockSearchIndex(MockModel, backend=self.msb) self.cmi = GoodCustomMockSearchIndex(MockModel, backend=self.msb) self.cnmi = GoodNullableMockSearchIndex(MockModel, backend=self.msb) self.gfmsi = GoodFacetedMockSearchIndex(MockModel, backend=self.msb) self.sample_docs = { u'core.mockmodel.1': { 'content': u'Indexed!\n1', 'django_id': u'1', 'django_ct': u'core.mockmodel', 'extra': u'Stored!\n1', 'author': u'daniel1', 'pub_date': datetime.datetime(2009, 3, 17, 6, 0), 'id': u'core.mockmodel.1' }, u'core.mockmodel.2': { 'content': u'Indexed!\n2', 'django_id': u'2', 'django_ct': u'core.mockmodel', 'extra': u'Stored!\n2', 'author': u'daniel2', 'pub_date': datetime.datetime(2009, 3, 17, 7, 0), 'id': u'core.mockmodel.2' }, u'core.mockmodel.3': { 'content': u'Indexed!\n3', 'django_id': u'3', 'django_ct': u'core.mockmodel', 'extra': u'Stored!\n3', 'author': u'daniel3', 'pub_date': datetime.datetime(2009, 3, 17, 8, 0), 'id': u'core.mockmodel.3' } } def test_no_contentfield_present(self): self.assertRaises(SearchFieldError, BadSearchIndex1, MockModel, MockSearchBackend()) def test_too_many_contentfields_present(self): self.assertRaises(SearchFieldError, BadSearchIndex2, MockModel, MockSearchBackend()) def test_contentfield_present(self): try: mi = GoodMockSearchIndex(MockModel, backend=MockSearchBackend()) except: self.fail() def test_proper_fields(self): self.assertEqual(len(self.mi.fields), 4) self.assert_('content' in self.mi.fields) self.assert_(isinstance(self.mi.fields['content'], CharField)) self.assert_('author' in self.mi.fields) self.assert_(isinstance(self.mi.fields['author'], CharField)) self.assert_('pub_date' in self.mi.fields) self.assert_(isinstance(self.mi.fields['pub_date'], DateTimeField)) self.assert_('extra' in self.mi.fields) self.assert_(isinstance(self.mi.fields['extra'], CharField)) self.assertEqual(len(self.cmi.fields), 7) self.assert_('content' in self.cmi.fields) self.assert_(isinstance(self.cmi.fields['content'], CharField)) self.assert_('author' in self.cmi.fields) self.assert_(isinstance(self.cmi.fields['author'], CharField)) self.assert_('author_exact' in self.cmi.fields) self.assert_(isinstance(self.cmi.fields['author_exact'], FacetCharField)) self.assert_('pub_date' in self.cmi.fields) self.assert_(isinstance(self.cmi.fields['pub_date'], DateTimeField)) self.assert_('pub_date_exact' in self.cmi.fields) self.assert_(isinstance(self.cmi.fields['pub_date_exact'], FacetDateTimeField)) self.assert_('extra' in self.cmi.fields) self.assert_(isinstance(self.cmi.fields['extra'], CharField)) self.assert_('hello' in self.cmi.fields) self.assert_(isinstance(self.cmi.fields['extra'], CharField)) def test_index_queryset(self): self.assertEqual(len(self.cmi.index_queryset()), 3) def test_get_queryset(self): self.assertEqual(len(self.cmi.get_queryset()), 3) def test_read_queryset(self): self.assertEqual(len(self.cmi.read_queryset()), 2) def test_prepare(self): mock = MockModel() mock.pk = 20 mock.author = 'daniel%s' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.assertEqual(len(self.mi.prepare(mock)), 7) self.assertEqual(sorted(self.mi.prepare(mock).keys()), ['author', 'content', 'django_ct', 'django_id', 'extra', 'id', 'pub_date']) def test_custom_prepare(self): mock = MockModel() mock.pk = 20 mock.author = 'daniel%s' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.assertEqual(len(self.cmi.prepare(mock)), 11) self.assertEqual(sorted(self.cmi.prepare(mock).keys()), ['author', 'author_exact', 'content', 'django_ct', 'django_id', 'extra', 'hello', 'id', 'pub_date', 'pub_date_exact', 'whee']) self.assertEqual(len(self.cmi.full_prepare(mock)), 11) self.assertEqual(sorted(self.cmi.full_prepare(mock).keys()), ['author', 'author_exact', 'content', 'django_ct', 'django_id', 'extra', 'hello', 'id', 'pub_date', 'pub_date_exact', 'whee']) def test_custom_prepare_author(self): mock = MockModel() mock.pk = 20 mock.author = 'daniel%s' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.assertEqual(len(self.cmi.prepare(mock)), 11) self.assertEqual(sorted(self.cmi.prepare(mock).keys()), ['author', 'author_exact', 'content', 'django_ct', 'django_id', 'extra', 'hello', 'id', 'pub_date', 'pub_date_exact', 'whee']) self.assertEqual(len(self.cmi.full_prepare(mock)), 11) self.assertEqual(sorted(self.cmi.full_prepare(mock).keys()), ['author', 'author_exact', 'content', 'django_ct', 'django_id', 'extra', 'hello', 'id', 'pub_date', 'pub_date_exact', 'whee']) self.assertEqual(self.cmi.prepared_data['author'], "Hi, I'm daniel20") self.assertEqual(self.cmi.prepared_data['author_exact'], "Hi, I'm daniel20") def test_custom_model_attr(self): mock = MockModel() mock.pk = 20 mock.author = 'daniel%s' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.assertEqual(len(self.cmi.prepare(mock)), 11) self.assertEqual(sorted(self.cmi.prepare(mock).keys()), ['author', 'author_exact', 'content', 'django_ct', 'django_id', 'extra', 'hello', 'id', 'pub_date', 'pub_date_exact', 'whee']) self.assertEqual(len(self.cmi.full_prepare(mock)), 11) self.assertEqual(sorted(self.cmi.full_prepare(mock).keys()), ['author', 'author_exact', 'content', 'django_ct', 'django_id', 'extra', 'hello', 'id', 'pub_date', 'pub_date_exact', 'whee']) self.assertEqual(self.cmi.prepared_data['hello'], u'World!') def test_custom_index_fieldname(self): mock = MockModel() mock.pk = 20 mock.author = 'daniel%s' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) cofnmi = GoodOverriddenFieldNameMockSearchIndex(MockModel, backend=self.msb) self.assertEqual(len(cofnmi.prepare(mock)), 6) self.assertEqual(sorted(cofnmi.prepare(mock).keys()), ['django_ct', 'django_id', 'hello', 'id', 'more_content', 'name_s']) self.assertEqual(cofnmi.prepared_data['name_s'], u'daniel20') self.assertEqual(cofnmi.get_content_field(), 'more_content') def test_get_content_field(self): self.assertEqual(self.mi.get_content_field(), 'content') def test_update(self): self.mi.update() self.assertEqual(self.msb.docs, self.sample_docs) self.msb.clear() def test_update_object(self): self.assertEqual(self.msb.docs, {}) mock = MockModel() mock.pk = 20 mock.author = 'daniel%s' % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.mi.update_object(mock) self.assertEqual(self.msb.docs, {'core.mockmodel.20': {'django_id': u'20', 'django_ct': u'core.mockmodel', 'author': u'daniel20', 'extra': u'Stored!\n20', 'content': u'Indexed!\n20', 'pub_date': datetime.datetime(2009, 1, 31, 4, 19), 'id': 'core.mockmodel.20'}}) self.msb.clear() def test_remove_object(self): self.msb.docs = {'core.mockmodel.20': 'Indexed!\n20'} mock = MockModel() mock.pk = 20 self.mi.remove_object(mock) self.assertEqual(self.msb.docs, {}) self.msb.clear() def test_clear(self): self.msb.docs = { 'core.mockmodel.1': 'Indexed!\n1', 'core.mockmodel.2': 'Indexed!\n2', 'core.mockmodel.20': 'Indexed!\n20', } self.mi.clear() self.assertEqual(self.msb.docs, {}) self.msb.clear() def test_reindex(self): self.msb.docs = { 'core.mockmodel.1': 'Indexed!\n1', 'core.mockmodel.2': 'Indexed!\n2', 'core.mockmodel.20': 'Indexed!\n20', } self.mi.reindex() self.assertEqual(self.msb.docs, self.sample_docs) self.msb.clear() def test_inheritance(self): try: agmi = AltGoodMockSearchIndex(MockModel, backend=self.msb) except: self.fail() self.assertEqual(len(agmi.fields), 5) self.assert_('content' in agmi.fields) self.assert_(isinstance(agmi.fields['content'], CharField)) self.assert_('author' in agmi.fields) self.assert_(isinstance(agmi.fields['author'], CharField)) self.assert_('pub_date' in agmi.fields) self.assert_(isinstance(agmi.fields['pub_date'], DateTimeField)) self.assert_('extra' in agmi.fields) self.assert_(isinstance(agmi.fields['extra'], CharField)) self.assert_('additional' in agmi.fields) self.assert_(isinstance(agmi.fields['additional'], CharField)) def test_load_all_queryset(self): self.assertEqual([obj.id for obj in self.cmi.load_all_queryset()], [2, 3]) def test_nullable(self): mock = MockModel() mock.pk = 20 mock.author = None mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) prepared_data = self.cnmi.prepare(mock) self.assertEqual(len(prepared_data), 6) self.assertEqual(sorted(prepared_data.keys()), ['author', 'author_exact', 'content', 'django_ct', 'django_id', 'id']) prepared_data = self.cnmi.full_prepare(mock) self.assertEqual(len(prepared_data), 4) self.assertEqual(sorted(prepared_data.keys()), ['content', 'django_ct', 'django_id', 'id']) def test_custom_facet_fields(self): mock = MockModel() mock.pk = 20 mock.author = 'daniel' mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) prepared_data = self.gfmsi.prepare(mock) self.assertEqual(len(prepared_data), 8) self.assertEqual(sorted(prepared_data.keys()), ['author', 'author_foo', 'content', 'django_ct', 'django_id', 'id', 'pub_date', 'pub_date_exact']) prepared_data = self.gfmsi.full_prepare(mock) self.assertEqual(len(prepared_data), 8) self.assertEqual(sorted(prepared_data.keys()), ['author', 'author_foo', 'content', 'django_ct', 'django_id', 'id', 'pub_date', 'pub_date_exact']) self.assertEqual(prepared_data['author_foo'], u"Hi, I'm daniel") self.assertEqual(prepared_data['pub_date_exact'], '2010-10-26T01:54:32')
class SearchIndexTestCase(TestCase): def setUp(self): super(SearchIndexTestCase, self).setUp() self.msb = MockSearchBackend() self.mi = GoodMockSearchIndex(MockModel, backend=self.msb) self.cmi = GoodCustomMockSearchIndex(MockModel, backend=self.msb) self.cnmi = GoodNullableMockSearchIndex(MockModel, backend=self.msb) self.sample_docs = { u"core.mockmodel.1": { "content": u"Indexed!\n1", "django_id": u"1", "django_ct": u"core.mockmodel", "extra": u"Stored!\n1", "author": u"daniel1", "pub_date": datetime.datetime(2009, 3, 17, 6, 0), "id": u"core.mockmodel.1", }, u"core.mockmodel.2": { "content": u"Indexed!\n2", "django_id": u"2", "django_ct": u"core.mockmodel", "extra": u"Stored!\n2", "author": u"daniel2", "pub_date": datetime.datetime(2009, 3, 17, 7, 0), "id": u"core.mockmodel.2", }, u"core.mockmodel.3": { "content": u"Indexed!\n3", "django_id": u"3", "django_ct": u"core.mockmodel", "extra": u"Stored!\n3", "author": u"daniel3", "pub_date": datetime.datetime(2009, 3, 17, 8, 0), "id": u"core.mockmodel.3", }, } def test_no_contentfield_present(self): self.assertRaises(SearchFieldError, BadSearchIndex1, MockModel, MockSearchBackend()) def test_too_many_contentfields_present(self): self.assertRaises(SearchFieldError, BadSearchIndex2, MockModel, MockSearchBackend()) def test_contentfield_present(self): try: mi = GoodMockSearchIndex(MockModel, backend=MockSearchBackend()) except: self.fail() def test_proper_fields(self): self.assertEqual(len(self.mi.fields), 4) self.assert_("content" in self.mi.fields) self.assert_(isinstance(self.mi.fields["content"], CharField)) self.assert_("author" in self.mi.fields) self.assert_(isinstance(self.mi.fields["author"], CharField)) self.assert_("pub_date" in self.mi.fields) self.assert_(isinstance(self.mi.fields["pub_date"], DateTimeField)) self.assert_("extra" in self.mi.fields) self.assert_(isinstance(self.mi.fields["extra"], CharField)) def test_get_queryset(self): self.assertEqual(len(self.mi.get_queryset()), 3) def test_prepare(self): mock = MockModel() mock.pk = 20 mock.author = "daniel%s" % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.assertEqual(len(self.mi.prepare(mock)), 7) self.assertEqual( sorted(self.mi.prepare(mock).keys()), ["author", "content", "django_ct", "django_id", "extra", "id", "pub_date"], ) def test_custom_prepare(self): mock = MockModel() mock.pk = 20 mock.author = "daniel%s" % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.assertEqual(len(self.cmi.prepare(mock)), 9) self.assertEqual( sorted(self.cmi.prepare(mock).keys()), ["author", "content", "django_ct", "django_id", "extra", "hello", "id", "pub_date", "whee"], ) self.assertEqual(len(self.cmi.full_prepare(mock)), 10) self.assertEqual( sorted(self.cmi.full_prepare(mock).keys()), ["author", "author_exact", "content", "django_ct", "django_id", "extra", "hello", "id", "pub_date", "whee"], ) def test_custom_prepare_author(self): mock = MockModel() mock.pk = 20 mock.author = "daniel%s" % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.assertEqual(len(self.cmi.prepare(mock)), 9) self.assertEqual( sorted(self.cmi.prepare(mock).keys()), ["author", "content", "django_ct", "django_id", "extra", "hello", "id", "pub_date", "whee"], ) self.assertEqual(len(self.cmi.full_prepare(mock)), 10) self.assertEqual( sorted(self.cmi.full_prepare(mock).keys()), ["author", "author_exact", "content", "django_ct", "django_id", "extra", "hello", "id", "pub_date", "whee"], ) self.assertEqual(self.cmi.prepared_data["author"], "Hi, I'm daniel20") self.assertEqual(self.cmi.prepared_data["author_exact"], "Hi, I'm daniel20") def test_custom_model_attr(self): mock = MockModel() mock.pk = 20 mock.author = "daniel%s" % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.assertEqual(len(self.cmi.prepare(mock)), 9) self.assertEqual( sorted(self.cmi.prepare(mock).keys()), ["author", "content", "django_ct", "django_id", "extra", "hello", "id", "pub_date", "whee"], ) self.assertEqual(len(self.cmi.full_prepare(mock)), 10) self.assertEqual( sorted(self.cmi.full_prepare(mock).keys()), ["author", "author_exact", "content", "django_ct", "django_id", "extra", "hello", "id", "pub_date", "whee"], ) self.assertEqual(self.cmi.prepared_data["hello"], u"World!") def test_custom_index_fieldname(self): mock = MockModel() mock.pk = 20 mock.author = "daniel%s" % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) cofnmi = GoodOverriddenFieldNameMockSearchIndex(MockModel, backend=self.msb) self.assertEqual(len(cofnmi.prepare(mock)), 6) self.assertEqual( sorted(cofnmi.prepare(mock).keys()), ["django_ct", "django_id", "hello", "id", "more_content", "name_s"] ) self.assertEqual(cofnmi.prepared_data["name_s"], u"daniel20") self.assertEqual(cofnmi.get_content_field(), "more_content") def test_get_content_field(self): self.assertEqual(self.mi.get_content_field(), "content") def test_update(self): self.mi.update() self.assertEqual(self.msb.docs, self.sample_docs) self.msb.clear() def test_update_object(self): self.assertEqual(self.msb.docs, {}) mock = MockModel() mock.pk = 20 mock.author = "daniel%s" % mock.id mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) self.mi.update_object(mock) self.assertEqual( self.msb.docs, { "core.mockmodel.20": { "django_id": u"20", "django_ct": u"core.mockmodel", "author": u"daniel20", "extra": u"Stored!\n20", "content": u"Indexed!\n20", "pub_date": datetime.datetime(2009, 1, 31, 4, 19), "id": "core.mockmodel.20", } }, ) self.msb.clear() def test_remove_object(self): self.msb.docs = {"core.mockmodel.20": "Indexed!\n20"} mock = MockModel() mock.pk = 20 self.mi.remove_object(mock) self.assertEqual(self.msb.docs, {}) self.msb.clear() def test_clear(self): self.msb.docs = { "core.mockmodel.1": "Indexed!\n1", "core.mockmodel.2": "Indexed!\n2", "core.mockmodel.20": "Indexed!\n20", } self.mi.clear() self.assertEqual(self.msb.docs, {}) self.msb.clear() def test_reindex(self): self.msb.docs = { "core.mockmodel.1": "Indexed!\n1", "core.mockmodel.2": "Indexed!\n2", "core.mockmodel.20": "Indexed!\n20", } self.mi.reindex() self.assertEqual(self.msb.docs, self.sample_docs) self.msb.clear() def test_inheritance(self): try: agmi = AltGoodMockSearchIndex(MockModel, backend=self.msb) except: self.fail() self.assertEqual(len(agmi.fields), 5) self.assert_("content" in agmi.fields) self.assert_(isinstance(agmi.fields["content"], CharField)) self.assert_("author" in agmi.fields) self.assert_(isinstance(agmi.fields["author"], CharField)) self.assert_("pub_date" in agmi.fields) self.assert_(isinstance(agmi.fields["pub_date"], DateTimeField)) self.assert_("extra" in agmi.fields) self.assert_(isinstance(agmi.fields["extra"], CharField)) self.assert_("additional" in agmi.fields) self.assert_(isinstance(agmi.fields["additional"], CharField)) def test_load_all_queryset(self): self.assertEqual([obj.id for obj in self.cmi.load_all_queryset()], [2, 3]) def test_nullable(self): mock = MockModel() mock.pk = 20 mock.author = None mock.pub_date = datetime.datetime(2009, 1, 31, 4, 19, 0) prepared_data = self.cnmi.prepare(mock) self.assertEqual(len(prepared_data), 4) self.assertEqual(sorted(prepared_data.keys()), ["content", "django_ct", "django_id", "id"])