summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Roztocil <jakub@roztocil.name>2014-04-22 22:01:16 +0200
committerAnssi Kääriäinen <akaariai@gmail.com>2014-05-05 14:40:06 +0300
commit4b6ba2c1d1dcf6d7d587ee19b3565879ab8ee219 (patch)
treedab41bf2baf29c9d907244aa55ff3b717dc88c96
parent6a6f294f518a96334c73b2e5f934cdd2f0feda7e (diff)
[1.7.x] Fixed #22489 -- missing implemenation for search lookup
When custom lookups were added, converting the search lookup to use the new Lookup infrastructure wasn't done. Some changes were needed to the added test, main change done by committer was ensuring the test works on MySQL versions prior to 5.6. Backport of 7131e14d00 from master
-rw-r--r--django/db/models/lookups.py7
-rw-r--r--tests/lookup/models.py12
-rw-r--r--tests/lookup/tests.py30
3 files changed, 47 insertions, 2 deletions
diff --git a/django/db/models/lookups.py b/django/db/models/lookups.py
index 7714668b9b..86ec9c2222 100644
--- a/django/db/models/lookups.py
+++ b/django/db/models/lookups.py
@@ -358,6 +358,13 @@ default_lookups['isnull'] = IsNull
class Search(BuiltinLookup):
lookup_name = 'search'
+
+ def as_sql(self, qn, connection):
+ lhs, lhs_params = self.process_lhs(qn, connection)
+ rhs, rhs_params = self.process_rhs(qn, connection)
+ sql_template = connection.ops.fulltext_search_sql(field_name=lhs)
+ return sql_template, lhs_params + rhs_params
+
default_lookups['search'] = Search
diff --git a/tests/lookup/models.py b/tests/lookup/models.py
index df8b26c4e9..0b4c88b8ed 100644
--- a/tests/lookup/models.py
+++ b/tests/lookup/models.py
@@ -65,3 +65,15 @@ class Player(models.Model):
def __str__(self):
return self.name
+
+
+# To test __search lookup a fulltext index is needed. This
+# is only available when using MySQL 5.6, or when using MyISAM
+# tables. As 5.6 isn't common yet, lets use MyISAM table for
+# testing. The table is manually created by the test method.
+class MyISAMArticle(models.Model):
+ headline = models.CharField(max_length=100)
+
+ class Meta:
+ db_table = 'myisam_article'
+ managed = False
diff --git a/tests/lookup/tests.py b/tests/lookup/tests.py
index 04d9f0fc9e..6744314625 100644
--- a/tests/lookup/tests.py
+++ b/tests/lookup/tests.py
@@ -2,11 +2,13 @@ from __future__ import unicode_literals
from datetime import datetime
from operator import attrgetter
+from unittest import skipUnless
from django.core.exceptions import FieldError
-from django.test import TestCase, skipUnlessDBFeature
+from django.db import connection
+from django.test import TestCase, TransactionTestCase, skipUnlessDBFeature
-from .models import Author, Article, Tag, Game, Season, Player
+from .models import Author, Article, Tag, Game, Season, Player, MyISAMArticle
class LookupTests(TestCase):
@@ -710,3 +712,27 @@ class LookupTests(TestCase):
self.assertEqual(Player.objects.filter(games__season__gt=333).distinct().count(), 2)
self.assertEqual(Player.objects.filter(games__season__year__gt=2010).distinct().count(), 2)
self.assertEqual(Player.objects.filter(games__season__gt__gt=222).distinct().count(), 2)
+
+
+class LookupTransactionTests(TransactionTestCase):
+ available_apps = ['lookup']
+
+ @skipUnless(connection.vendor == 'mysql', 'requires MySQL')
+ def test_mysql_lookup_search(self):
+ # To use fulltext indexes on MySQL either version 5.6 is needed, or one must use
+ # MyISAM tables. Neither of these combinations is currently available on CI, so
+ # lets manually create a MyISAM table for Article model.
+ with connection.cursor() as cursor:
+ cursor.execute(
+ "CREATE TEMPORARY TABLE myisam_article ("
+ " id INTEGER PRIMARY KEY AUTO_INCREMENT, "
+ " headline VARCHAR(100) NOT NULL "
+ ") ENGINE MYISAM")
+ dr = MyISAMArticle.objects.create(headline='Django Reinhardt')
+ MyISAMArticle.objects.create(headline='Ringo Star')
+ # NOTE: Needs to be created after the article has been saved.
+ cursor.execute(
+ 'CREATE FULLTEXT INDEX myisam_article_ft ON myisam_article (headline)')
+ self.assertQuerysetEqual(
+ MyISAMArticle.objects.filter(headline__search='Reinhardt'),
+ [dr], lambda x: x)