summaryrefslogtreecommitdiff
path: root/tests/postgres_tests/test_indexes.py
diff options
context:
space:
mode:
authorAkshesh <aksheshdoshi@gmail.com>2016-08-08 17:20:25 +0530
committerTim Graham <timograham@gmail.com>2016-08-12 18:37:03 -0400
commit6e07ec3f655073bebdf1b1dfd2303e91202e14e1 (patch)
treed62f91ee2af2a9ae218087037f76d48767ebac35 /tests/postgres_tests/test_indexes.py
parent236baa0f0ec31422bc8c36b24e41112f7a564532 (diff)
Fixed #27030 -- Added contrib.postgres.indexes.GinIndex.
Diffstat (limited to 'tests/postgres_tests/test_indexes.py')
-rw-r--r--tests/postgres_tests/test_indexes.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/tests/postgres_tests/test_indexes.py b/tests/postgres_tests/test_indexes.py
new file mode 100644
index 0000000000..41bdd3beca
--- /dev/null
+++ b/tests/postgres_tests/test_indexes.py
@@ -0,0 +1,55 @@
+from django.contrib.postgres.indexes import GinIndex
+from django.db import connection
+
+from . import PostgreSQLTestCase
+from .models import IntegerArrayModel
+
+
+class GinIndexTests(PostgreSQLTestCase):
+
+ def test_repr(self):
+ index = GinIndex(fields=['title'])
+ self.assertEqual(repr(index), "<GinIndex: fields='title'>")
+
+ 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):
+ index = GinIndex(fields=['title'], name='test_title_gin')
+ path, args, kwargs = index.deconstruct()
+ self.assertEqual(path, 'django.contrib.postgres.indexes.GinIndex')
+ self.assertEqual(args, ())
+ self.assertEqual(kwargs, {'fields': ['title'], 'name': 'test_title_gin'})
+
+
+class SchemaTests(PostgreSQLTestCase):
+
+ def get_indexes(self, table):
+ """
+ Get the indexes on the table using a new cursor.
+ """
+ with connection.cursor() as cursor:
+ return connection.introspection.get_indexes(cursor, table)
+
+ def test_gin_index(self):
+ # Ensure the table is there and doesn't have an index.
+ self.assertNotIn('field', self.get_indexes(IntegerArrayModel._meta.db_table))
+ # Add the index
+ index = GinIndex(fields=['field'], name='integer_array_model_field_gin')
+ with connection.schema_editor() as editor:
+ editor.add_index(IntegerArrayModel, index)
+ self.assertIn('field', self.get_indexes(IntegerArrayModel._meta.db_table))
+ self.assertEqual(self.get_indexes(IntegerArrayModel._meta.db_table)['field']['type'], 'gin')
+ # Drop the index
+ with connection.schema_editor() as editor:
+ editor.remove_index(IntegerArrayModel, index)
+ self.assertNotIn('field', self.get_indexes(IntegerArrayModel._meta.db_table))