2016-10-13 20:39:44 +08:00
|
|
|
from django.contrib.postgres.indexes import BrinIndex, GinIndex
|
2016-08-08 19:50:25 +08:00
|
|
|
from django.db import connection
|
2016-10-13 20:39:44 +08:00
|
|
|
from django.test import skipUnlessDBFeature
|
2016-08-08 19:50:25 +08:00
|
|
|
|
|
|
|
from . import PostgreSQLTestCase
|
2017-03-18 02:01:25 +08:00
|
|
|
from .models import CharFieldModel, DateTimeArrayModel, IntegerArrayModel
|
2016-10-13 20:39:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
@skipUnlessDBFeature('has_brin_index_support')
|
|
|
|
class BrinIndexTests(PostgreSQLTestCase):
|
|
|
|
|
2017-02-16 02:33:55 +08:00
|
|
|
def test_suffix(self):
|
|
|
|
self.assertEqual(BrinIndex.suffix, 'brin')
|
|
|
|
|
2016-10-13 20:39:44 +08:00
|
|
|
def test_not_eq(self):
|
|
|
|
index = BrinIndex(fields=['title'])
|
|
|
|
index_with_page_range = BrinIndex(fields=['title'], pages_per_range=16)
|
|
|
|
self.assertNotEqual(index, index_with_page_range)
|
|
|
|
|
2017-03-18 02:01:25 +08:00
|
|
|
def test_name_auto_generation(self):
|
|
|
|
"""
|
|
|
|
A name longer than 30 characters (since len(BrinIndex.suffix) is 4
|
|
|
|
rather than usual limit of 3) is okay for PostgreSQL. For this test,
|
|
|
|
the name of the field ('datetimes') must be at least 7 characters to
|
|
|
|
generate a name longer than 30 characters.
|
|
|
|
"""
|
|
|
|
index = BrinIndex(fields=['datetimes'])
|
|
|
|
index.set_name_with_model(DateTimeArrayModel)
|
|
|
|
self.assertEqual(index.name, 'postgres_te_datetim_abf104_brin')
|
|
|
|
|
2016-10-13 20:39:44 +08:00
|
|
|
def test_deconstruction(self):
|
|
|
|
index = BrinIndex(fields=['title'], name='test_title_brin')
|
|
|
|
path, args, kwargs = index.deconstruct()
|
|
|
|
self.assertEqual(path, 'django.contrib.postgres.indexes.BrinIndex')
|
|
|
|
self.assertEqual(args, ())
|
|
|
|
self.assertEqual(kwargs, {'fields': ['title'], 'name': 'test_title_brin', 'pages_per_range': None})
|
|
|
|
|
2017-01-17 00:28:30 +08:00
|
|
|
def test_deconstruction_with_pages_per_range(self):
|
2016-10-13 20:39:44 +08:00
|
|
|
index = BrinIndex(fields=['title'], name='test_title_brin', pages_per_range=16)
|
|
|
|
path, args, kwargs = index.deconstruct()
|
|
|
|
self.assertEqual(path, 'django.contrib.postgres.indexes.BrinIndex')
|
|
|
|
self.assertEqual(args, ())
|
|
|
|
self.assertEqual(kwargs, {'fields': ['title'], 'name': 'test_title_brin', 'pages_per_range': 16})
|
|
|
|
|
|
|
|
def test_invalid_pages_per_range(self):
|
2017-01-17 00:28:30 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, 'pages_per_range must be None or a positive integer'):
|
2016-10-13 20:39:44 +08:00
|
|
|
BrinIndex(fields=['title'], name='test_title_brin', pages_per_range=0)
|
2016-08-08 19:50:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
class GinIndexTests(PostgreSQLTestCase):
|
|
|
|
|
2017-02-16 02:33:55 +08:00
|
|
|
def test_suffix(self):
|
|
|
|
self.assertEqual(GinIndex.suffix, 'gin')
|
|
|
|
|
2016-08-08 19:50:25 +08:00
|
|
|
def test_eq(self):
|
|
|
|
index = GinIndex(fields=['title'])
|
|
|
|
same_index = GinIndex(fields=['title'])
|
|
|
|
another_index = GinIndex(fields=['author'])
|
|
|
|
self.assertEqual(index, same_index)
|
|
|
|
self.assertNotEqual(index, another_index)
|
|
|
|
|
|
|
|
def test_name_auto_generation(self):
|
|
|
|
index = GinIndex(fields=['field'])
|
|
|
|
index.set_name_with_model(IntegerArrayModel)
|
|
|
|
self.assertEqual(index.name, 'postgres_te_field_def2f8_gin')
|
|
|
|
|
|
|
|
def test_deconstruction(self):
|
2017-05-31 05:17:32 +08:00
|
|
|
index = GinIndex(
|
|
|
|
fields=['title'],
|
|
|
|
name='test_title_gin',
|
|
|
|
fastupdate=True,
|
|
|
|
gin_pending_list_limit=128,
|
|
|
|
)
|
2016-08-08 19:50:25 +08:00
|
|
|
path, args, kwargs = index.deconstruct()
|
|
|
|
self.assertEqual(path, 'django.contrib.postgres.indexes.GinIndex')
|
|
|
|
self.assertEqual(args, ())
|
2017-05-31 05:17:32 +08:00
|
|
|
self.assertEqual(
|
|
|
|
kwargs,
|
|
|
|
{
|
|
|
|
'fields': ['title'],
|
|
|
|
'name': 'test_title_gin',
|
|
|
|
'fastupdate': True,
|
|
|
|
'gin_pending_list_limit': 128,
|
|
|
|
}
|
|
|
|
)
|
2016-08-08 19:50:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
class SchemaTests(PostgreSQLTestCase):
|
|
|
|
|
2016-08-25 15:12:17 +08:00
|
|
|
def get_constraints(self, table):
|
2016-08-08 19:50:25 +08:00
|
|
|
"""
|
|
|
|
Get the indexes on the table using a new cursor.
|
|
|
|
"""
|
|
|
|
with connection.cursor() as cursor:
|
2016-08-25 15:12:17 +08:00
|
|
|
return connection.introspection.get_constraints(cursor, table)
|
2016-08-08 19:50:25 +08:00
|
|
|
|
|
|
|
def test_gin_index(self):
|
|
|
|
# Ensure the table is there and doesn't have an index.
|
2016-08-25 15:12:17 +08:00
|
|
|
self.assertNotIn('field', self.get_constraints(IntegerArrayModel._meta.db_table))
|
2016-08-08 19:50:25 +08:00
|
|
|
# Add the index
|
2016-08-25 15:12:17 +08:00
|
|
|
index_name = 'integer_array_model_field_gin'
|
|
|
|
index = GinIndex(fields=['field'], name=index_name)
|
2016-08-08 19:50:25 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.add_index(IntegerArrayModel, index)
|
2016-08-25 15:12:17 +08:00
|
|
|
constraints = self.get_constraints(IntegerArrayModel._meta.db_table)
|
2016-08-25 15:12:17 +08:00
|
|
|
# Check gin index was added
|
2017-02-16 02:33:55 +08:00
|
|
|
self.assertEqual(constraints[index_name]['type'], GinIndex.suffix)
|
2016-08-08 19:50:25 +08:00
|
|
|
# Drop the index
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.remove_index(IntegerArrayModel, index)
|
2016-08-25 15:12:17 +08:00
|
|
|
self.assertNotIn(index_name, self.get_constraints(IntegerArrayModel._meta.db_table))
|
2016-10-13 20:39:44 +08:00
|
|
|
|
2017-05-31 05:17:32 +08:00
|
|
|
def test_gin_fastupdate(self):
|
|
|
|
index_name = 'integer_array_gin_fastupdate'
|
|
|
|
index = GinIndex(fields=['field'], name=index_name, fastupdate=False)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.add_index(IntegerArrayModel, index)
|
|
|
|
constraints = self.get_constraints(IntegerArrayModel._meta.db_table)
|
|
|
|
self.assertEqual(constraints[index_name]['type'], 'gin')
|
|
|
|
self.assertEqual(constraints[index_name]['options'], ['fastupdate=off'])
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.remove_index(IntegerArrayModel, index)
|
|
|
|
self.assertNotIn(index_name, self.get_constraints(IntegerArrayModel._meta.db_table))
|
|
|
|
|
|
|
|
@skipUnlessDBFeature('has_gin_pending_list_limit')
|
|
|
|
def test_gin_parameters(self):
|
|
|
|
index_name = 'integer_array_gin_params'
|
|
|
|
index = GinIndex(fields=['field'], name=index_name, fastupdate=True, gin_pending_list_limit=64)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.add_index(IntegerArrayModel, index)
|
|
|
|
constraints = self.get_constraints(IntegerArrayModel._meta.db_table)
|
|
|
|
self.assertEqual(constraints[index_name]['type'], 'gin')
|
|
|
|
self.assertEqual(constraints[index_name]['options'], ['gin_pending_list_limit=64', 'fastupdate=on'])
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.remove_index(IntegerArrayModel, index)
|
|
|
|
self.assertNotIn(index_name, self.get_constraints(IntegerArrayModel._meta.db_table))
|
|
|
|
|
2016-10-13 20:39:44 +08:00
|
|
|
@skipUnlessDBFeature('has_brin_index_support')
|
|
|
|
def test_brin_index(self):
|
|
|
|
index_name = 'char_field_model_field_brin'
|
|
|
|
index = BrinIndex(fields=['field'], name=index_name, pages_per_range=4)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.add_index(CharFieldModel, index)
|
|
|
|
constraints = self.get_constraints(CharFieldModel._meta.db_table)
|
2017-02-16 02:33:55 +08:00
|
|
|
self.assertEqual(constraints[index_name]['type'], BrinIndex.suffix)
|
2016-10-13 20:39:44 +08:00
|
|
|
self.assertEqual(constraints[index_name]['options'], ['pages_per_range=4'])
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.remove_index(CharFieldModel, index)
|
|
|
|
self.assertNotIn(index_name, self.get_constraints(CharFieldModel._meta.db_table))
|