diff options
| author | Matthew Somerville <matthew-github@dracos.co.uk> | 2015-06-05 17:37:48 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-05-13 12:38:21 -0400 |
| commit | 1962a96a30e02de78a674a2e02979c00cc55655b (patch) | |
| tree | 2abaf90658eaf619563b96aa7c8c810e5b83f365 /tests/postgres_tests | |
| parent | d7334b405fb0e677e79cb064074df68188f0ddb5 (diff) | |
Fixed #24938 -- Added PostgreSQL trigram support.
Diffstat (limited to 'tests/postgres_tests')
| -rw-r--r-- | tests/postgres_tests/migrations/0001_setup_extensions.py | 4 | ||||
| -rw-r--r-- | tests/postgres_tests/test_trigram.py | 53 |
2 files changed, 56 insertions, 1 deletions
diff --git a/tests/postgres_tests/migrations/0001_setup_extensions.py b/tests/postgres_tests/migrations/0001_setup_extensions.py index 07d5bfc7e7..400dd091f4 100644 --- a/tests/postgres_tests/migrations/0001_setup_extensions.py +++ b/tests/postgres_tests/migrations/0001_setup_extensions.py @@ -5,12 +5,13 @@ from django.db import migrations try: from django.contrib.postgres.operations import ( - CreateExtension, HStoreExtension, UnaccentExtension, + CreateExtension, HStoreExtension, TrigramExtension, UnaccentExtension, ) except ImportError: from django.test import mock CreateExtension = mock.Mock() HStoreExtension = mock.Mock() + TrigramExtension = mock.Mock() UnaccentExtension = mock.Mock() @@ -21,5 +22,6 @@ class Migration(migrations.Migration): # dash in its name. CreateExtension('uuid-ossp'), HStoreExtension(), + TrigramExtension(), UnaccentExtension(), ] diff --git a/tests/postgres_tests/test_trigram.py b/tests/postgres_tests/test_trigram.py new file mode 100644 index 0000000000..b340b41869 --- /dev/null +++ b/tests/postgres_tests/test_trigram.py @@ -0,0 +1,53 @@ +from django.contrib.postgres.search import TrigramDistance, TrigramSimilarity +from django.test import modify_settings + +from . import PostgreSQLTestCase +from .models import CharFieldModel, TextFieldModel + + +@modify_settings(INSTALLED_APPS={'append': 'django.contrib.postgres'}) +class TrigramTest(PostgreSQLTestCase): + Model = CharFieldModel + + @classmethod + def setUpTestData(cls): + cls.Model.objects.bulk_create([ + cls.Model(field='Matthew'), + cls.Model(field='Cat sat on mat.'), + cls.Model(field='Dog sat on rug.'), + ]) + + def test_trigram_search(self): + self.assertQuerysetEqual( + self.Model.objects.filter(field__trigram_similar='Mathew'), + ['Matthew'], + transform=lambda instance: instance.field, + ) + + def test_trigram_similarity(self): + search = 'Bat sat on cat.' + self.assertQuerysetEqual( + self.Model.objects.filter( + field__trigram_similar=search, + ).annotate(similarity=TrigramSimilarity('field', search)).order_by('-similarity'), + [('Cat sat on mat.', 0.625), ('Dog sat on rug.', 0.333333)], + transform=lambda instance: (instance.field, instance.similarity), + ordered=True, + ) + + def test_trigram_similarity_alternate(self): + self.assertQuerysetEqual( + self.Model.objects.annotate( + distance=TrigramDistance('field', 'Bat sat on cat.'), + ).filter(distance__lte=0.7).order_by('distance'), + [('Cat sat on mat.', 0.375), ('Dog sat on rug.', 0.666667)], + transform=lambda instance: (instance.field, instance.distance), + ordered=True, + ) + + +class TrigramTextFieldTest(TrigramTest): + """ + TextField has the same behavior as CharField regarding trigram lookups. + """ + Model = TextFieldModel |
