diff options
| author | Mads Jensen <mje@inducks.org> | 2016-10-13 14:39:44 +0200 |
|---|---|---|
| committer | Mads Jensen <mje@inducks.org> | 2017-01-15 13:37:18 +0100 |
| commit | e585c43be91fb3e5005ddb4191f64142c62a2ec3 (patch) | |
| tree | 6d4aade646ed38ed5cf94b080ec0a26bc8327a4f /tests/postgres_tests/test_indexes.py | |
| parent | 236ebe94bfe24d394d5b49f4405da445550e8aa6 (diff) | |
Fixed #25809 -- Added BrinIndex support in django.contrib.postgres.
Thanks Tim Graham and Markus Holtermann for review.
Diffstat (limited to 'tests/postgres_tests/test_indexes.py')
| -rw-r--r-- | tests/postgres_tests/test_indexes.py | 54 |
1 files changed, 52 insertions, 2 deletions
diff --git a/tests/postgres_tests/test_indexes.py b/tests/postgres_tests/test_indexes.py index 235d72c532..48b08a2737 100644 --- a/tests/postgres_tests/test_indexes.py +++ b/tests/postgres_tests/test_indexes.py @@ -1,8 +1,45 @@ -from django.contrib.postgres.indexes import GinIndex +from django.contrib.postgres.indexes import BrinIndex, GinIndex from django.db import connection +from django.test import skipUnlessDBFeature from . import PostgreSQLTestCase -from .models import IntegerArrayModel +from .models import CharFieldModel, IntegerArrayModel + + +@skipUnlessDBFeature('has_brin_index_support') +class BrinIndexTests(PostgreSQLTestCase): + + def test_repr(self): + index = BrinIndex(fields=['title'], pages_per_range=4) + another_index = BrinIndex(fields=['title']) + self.assertEqual(repr(index), "<BrinIndex: fields='title', pages_per_range=4>") + self.assertEqual(repr(another_index), "<BrinIndex: fields='title'>") + + 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) + + 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}) + + def test_deconstruction_with_pages_per_rank(self): + 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): + with self.assertRaises(ValueError): + BrinIndex(fields=['title'], name='test_title_brin', pages_per_range='Charles Babbage') + + with self.assertRaises(ValueError): + BrinIndex(fields=['title'], name='test_title_brin', pages_per_range=0) class GinIndexTests(PostgreSQLTestCase): @@ -55,3 +92,16 @@ class SchemaTests(PostgreSQLTestCase): with connection.schema_editor() as editor: editor.remove_index(IntegerArrayModel, index) self.assertNotIn(index_name, self.get_constraints(IntegerArrayModel._meta.db_table)) + + @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) + self.assertEqual(constraints[index_name]['type'], 'brin') + 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)) |
