Permalink
Browse files

Simple: Fix bug in score field collision

Previous commit 0a9c919 broke the simple backend for models that
didn't have an indexed score field.  Added a test to cover regression.
  • Loading branch information...
1 parent 1274a8d commit e6fa476c894dde58cb707bee88c8520115cab9e0 bigjust committed Mar 10, 2013
@@ -71,7 +71,7 @@ def search(self, query_string, **kwargs):
hits += len(qs)
for match in qs:
- del(match.__dict__['score'])
+ match.__dict__.pop('score', None)
result = result_class(match._meta.app_label, match._meta.module_name, match.pk, 0, **match.__dict__)
# For efficiency.
result._model = match.__class__
@@ -251,5 +251,12 @@
"author": "daniel3",
"pub_date": "2009-07-17 22:30:00"
}
+ },
+ {
+ "pk": 1,
+ "model": "core.ScoreMockModel",
+ "fields": {
+ "score": "42"
+ }
}
]
View
@@ -12,19 +12,18 @@ class MockModel(models.Model):
foo = models.CharField(max_length=255, blank=True)
pub_date = models.DateTimeField(default=datetime.datetime.now)
tag = models.ForeignKey(MockTag)
- score = models.CharField(max_length=10, blank=True)
def __unicode__(self):
return self.author
-
+
def hello(self):
return 'World!'
class AnotherMockModel(models.Model):
author = models.CharField(max_length=255)
pub_date = models.DateTimeField(default=datetime.datetime.now)
-
+
def __unicode__(self):
return self.author
@@ -42,7 +41,7 @@ class AFourthMockModel(models.Model):
author = models.CharField(max_length=255)
editor = models.CharField(max_length=255)
pub_date = models.DateTimeField(default=datetime.datetime.now)
-
+
def __unicode__(self):
return self.author
@@ -69,3 +68,9 @@ class ASixthMockModel(models.Model):
def __unicode__(self):
return self.name
+
+class ScoreMockModel(models.Model):
+ score = models.CharField(max_length=10)
+
+ def __unicode__(self):
+ return self.score
@@ -1,12 +1,18 @@
from haystack import indexes
-from core.models import MockModel
+from core.models import MockModel, ScoreMockModel
class SimpleMockSearchIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
name = indexes.CharField(model_attr='author')
pub_date = indexes.DateField(model_attr='pub_date')
- score = indexes.CharField(model_attr='score')
def get_model(self):
return MockModel
+
+class SimpleMockScoreIndex(indexes.SearchIndex, indexes.Indexable):
+ text = indexes.CharField(document=True, use_template=True)
+ score = indexes.CharField(model_attr='score')
+
+ def get_model(self):
+ return ScoreMockModel
@@ -5,7 +5,7 @@
from haystack import indexes
from haystack.query import SearchQuerySet
from haystack.utils.loading import UnifiedIndex
-from core.models import MockModel
+from core.models import MockModel, ScoreMockModel
from core.tests.mocks import MockSearchResult
from simple_tests.search_indexes import SimpleMockSearchIndex
@@ -33,8 +33,8 @@ def test_search(self):
# No query string should always yield zero results.
self.assertEqual(self.backend.search(u''), {'hits': 0, 'results': []})
- self.assertEqual(self.backend.search(u'*')['hits'], 23)
- self.assertEqual([result.pk for result in self.backend.search(u'*')['results']], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])
+ self.assertEqual(self.backend.search(u'*')['hits'], 24)
+ self.assertEqual([result.pk for result in self.backend.search(u'*')['results']], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 1])
self.assertEqual(self.backend.search(u'daniel')['hits'], 23)
self.assertEqual([result.pk for result in self.backend.search(u'daniel')['results']], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23])
@@ -73,11 +73,21 @@ def test_search(self):
def test_more_like_this(self):
self.backend.update(self.index, self.sample_objs)
- self.assertEqual(self.backend.search(u'*')['hits'], 23)
+ self.assertEqual(self.backend.search(u'*')['hits'], 24)
# Unsupported by 'simple'. Should see empty results.
self.assertEqual(self.backend.more_like_this(self.sample_objs[0])['hits'], 0)
+ def test_score_field_collision(self):
+
+ index = connections['default'].get_unified_index().get_index(ScoreMockModel)
+ sample_objs = ScoreMockModel.objects.all()
+
+ self.backend.update(index, self.sample_objs)
+
+ # 42 is the in the match, which will be removed from the result
+ self.assertEqual(self.backend.search(u'42')['results'][0].score, 0)
+
class LiveSimpleSearchQuerySetTestCase(TestCase):
fixtures = ['bulk_data.json']

0 comments on commit e6fa476

Please sign in to comment.