summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMads Jensen <mje@inducks.org>2017-07-01 15:30:34 +0200
committerTim Graham <timograham@gmail.com>2017-09-04 21:08:02 -0400
commitf4135783add90e8e392db81b3592f4e3b9f01754 (patch)
tree840bc1d36cd716dffda286610cf0227c16747a74 /tests
parent66657eb01f36081f33d847390e4f7034ff3e9f52 (diff)
Fixed #28126 -- Added GistIndex to contrib.postgres.
Thanks to Marc Tamlyn for the initial patch.
Diffstat (limited to 'tests')
-rw-r--r--tests/postgres_tests/migrations/0001_setup_extensions.py7
-rw-r--r--tests/postgres_tests/test_indexes.py70
2 files changed, 74 insertions, 3 deletions
diff --git a/tests/postgres_tests/migrations/0001_setup_extensions.py b/tests/postgres_tests/migrations/0001_setup_extensions.py
index b00c6c0838..067c21849e 100644
--- a/tests/postgres_tests/migrations/0001_setup_extensions.py
+++ b/tests/postgres_tests/migrations/0001_setup_extensions.py
@@ -4,11 +4,13 @@ from django.db import migrations
try:
from django.contrib.postgres.operations import (
- BtreeGinExtension, CITextExtension, CreateExtension, CryptoExtension,
- HStoreExtension, TrigramExtension, UnaccentExtension,
+ BtreeGinExtension, BtreeGistExtension, CITextExtension,
+ CreateExtension, CryptoExtension, HStoreExtension, TrigramExtension,
+ UnaccentExtension,
)
except ImportError:
BtreeGinExtension = mock.Mock()
+ BtreeGistExtension = mock.Mock()
CITextExtension = mock.Mock()
CreateExtension = mock.Mock()
CryptoExtension = mock.Mock()
@@ -21,6 +23,7 @@ class Migration(migrations.Migration):
operations = [
BtreeGinExtension(),
+ BtreeGistExtension(),
CITextExtension(),
# Ensure CreateExtension quotes extension names by creating one with a
# dash in its name.
diff --git a/tests/postgres_tests/test_indexes.py b/tests/postgres_tests/test_indexes.py
index ac19f53ab8..1e5dc7c380 100644
--- a/tests/postgres_tests/test_indexes.py
+++ b/tests/postgres_tests/test_indexes.py
@@ -1,4 +1,4 @@
-from django.contrib.postgres.indexes import BrinIndex, GinIndex
+from django.contrib.postgres.indexes import BrinIndex, GinIndex, GistIndex
from django.db import connection
from django.test import skipUnlessDBFeature
@@ -92,6 +92,46 @@ class GinIndexTests(PostgreSQLTestCase):
self.assertEqual(kwargs, {'fields': ['title'], 'name': 'test_title_gin'})
+class GistIndexTests(PostgreSQLTestCase):
+
+ def test_suffix(self):
+ self.assertEqual(GistIndex.suffix, 'gist')
+
+ def test_eq(self):
+ index = GistIndex(fields=['title'], fillfactor=64)
+ same_index = GistIndex(fields=['title'], fillfactor=64)
+ another_index = GistIndex(fields=['author'], buffering=True)
+ self.assertEqual(index, same_index)
+ self.assertNotEqual(index, another_index)
+
+ def test_name_auto_generation(self):
+ index = GistIndex(fields=['field'])
+ index.set_name_with_model(CharFieldModel)
+ self.assertEqual(index.name, 'postgres_te_field_1e0206_gist')
+
+ def test_deconstruction(self):
+ index = GistIndex(fields=['title'], name='test_title_gist', buffering=False, fillfactor=80)
+ path, args, kwargs = index.deconstruct()
+ self.assertEqual(path, 'django.contrib.postgres.indexes.GistIndex')
+ self.assertEqual(args, ())
+ self.assertEqual(
+ kwargs,
+ {
+ 'fields': ['title'],
+ 'name': 'test_title_gist',
+ 'buffering': False,
+ 'fillfactor': 80,
+ }
+ )
+
+ def test_deconstruction_no_customization(self):
+ index = GistIndex(fields=['title'], name='test_title_gist')
+ path, args, kwargs = index.deconstruct()
+ self.assertEqual(path, 'django.contrib.postgres.indexes.GistIndex')
+ self.assertEqual(args, ())
+ self.assertEqual(kwargs, {'fields': ['title'], 'name': 'test_title_gist'})
+
+
class SchemaTests(PostgreSQLTestCase):
def get_constraints(self, table):
@@ -154,3 +194,31 @@ class SchemaTests(PostgreSQLTestCase):
with connection.schema_editor() as editor:
editor.remove_index(CharFieldModel, index)
self.assertNotIn(index_name, self.get_constraints(CharFieldModel._meta.db_table))
+
+ def test_gist_index(self):
+ # Ensure the table is there and doesn't have an index.
+ self.assertNotIn('field', self.get_constraints(CharFieldModel._meta.db_table))
+ # Add the index.
+ index_name = 'char_field_model_field_gist'
+ index = GistIndex(fields=['field'], name=index_name)
+ with connection.schema_editor() as editor:
+ editor.add_index(CharFieldModel, index)
+ constraints = self.get_constraints(CharFieldModel._meta.db_table)
+ # The index was added.
+ self.assertEqual(constraints[index_name]['type'], GistIndex.suffix)
+ # Drop the index.
+ with connection.schema_editor() as editor:
+ editor.remove_index(CharFieldModel, index)
+ self.assertNotIn(index_name, self.get_constraints(CharFieldModel._meta.db_table))
+
+ def test_gist_parameters(self):
+ index_name = 'integer_array_gist_buffering'
+ index = GistIndex(fields=['field'], name=index_name, buffering=True, fillfactor=80)
+ with connection.schema_editor() as editor:
+ editor.add_index(CharFieldModel, index)
+ constraints = self.get_constraints(CharFieldModel._meta.db_table)
+ self.assertEqual(constraints[index_name]['type'], GistIndex.suffix)
+ self.assertEqual(constraints[index_name]['options'], ['buffering=on', 'fillfactor=80'])
+ with connection.schema_editor() as editor:
+ editor.remove_index(CharFieldModel, index)
+ self.assertNotIn(index_name, self.get_constraints(CharFieldModel._meta.db_table))