diff options
author | Hannes Ljungberg <hannes@5monkeys.se> | 2019-10-10 20:04:17 +0200 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-01-13 11:47:50 +0100 |
commit | 83fcfc9ec8610540948815e127101f1206562ead (patch) | |
tree | aca51a61ef2d7397ee170fc74c564d242c441147 /tests/indexes | |
parent | e3ece0144a988bc522c4bd551baecaf2139ce4ed (diff) | |
download | django-83fcfc9ec8610540948815e127101f1206562ead.tar.gz |
Fixed #26167 -- Added support for functional indexes.
Thanks Simon Charette, Mads Jensen, and Mariusz Felisiak for reviews.
Co-authored-by: Markus Holtermann <info@markusholtermann.eu>
Diffstat (limited to 'tests/indexes')
-rw-r--r-- | tests/indexes/tests.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/indexes/tests.py b/tests/indexes/tests.py index 6d01e3b52f..ae68113c75 100644 --- a/tests/indexes/tests.py +++ b/tests/indexes/tests.py @@ -3,6 +3,7 @@ from unittest import skipUnless from django.db import connection from django.db.models import CASCADE, ForeignKey, Index, Q +from django.db.models.functions import Lower from django.test import ( TestCase, TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature, ) @@ -452,6 +453,40 @@ class PartialIndexTests(TransactionTestCase): )) editor.remove_index(index=index, model=Article) + @skipUnlessDBFeature('supports_expression_indexes') + def test_partial_func_index(self): + index_name = 'partial_func_idx' + index = Index( + Lower('headline').desc(), + name=index_name, + condition=Q(pub_date__isnull=False), + ) + with connection.schema_editor() as editor: + editor.add_index(index=index, model=Article) + sql = index.create_sql(Article, schema_editor=editor) + table = Article._meta.db_table + self.assertIs(sql.references_column(table, 'headline'), True) + sql = str(sql) + self.assertIn('LOWER(%s)' % editor.quote_name('headline'), sql) + self.assertIn( + 'WHERE %s IS NOT NULL' % editor.quote_name('pub_date'), + sql, + ) + self.assertGreater(sql.find('WHERE'), sql.find('LOWER')) + with connection.cursor() as cursor: + constraints = connection.introspection.get_constraints( + cursor=cursor, table_name=table, + ) + self.assertIn(index_name, constraints) + if connection.features.supports_index_column_ordering: + self.assertEqual(constraints[index_name]['orders'], ['DESC']) + with connection.schema_editor() as editor: + editor.remove_index(Article, index) + with connection.cursor() as cursor: + self.assertNotIn(index_name, connection.introspection.get_constraints( + cursor=cursor, table_name=table, + )) + @skipUnlessDBFeature('supports_covering_indexes') class CoveringIndexTests(TransactionTestCase): @@ -520,6 +555,31 @@ class CoveringIndexTests(TransactionTestCase): cursor=cursor, table_name=Article._meta.db_table, )) + def test_covering_func_index(self): + index_name = 'covering_func_headline_idx' + index = Index(Lower('headline'), name=index_name, include=['pub_date']) + with connection.schema_editor() as editor: + editor.add_index(index=index, model=Article) + sql = index.create_sql(Article, schema_editor=editor) + table = Article._meta.db_table + self.assertIs(sql.references_column(table, 'headline'), True) + sql = str(sql) + self.assertIn('LOWER(%s)' % editor.quote_name('headline'), sql) + self.assertIn('INCLUDE (%s)' % editor.quote_name('pub_date'), sql) + self.assertGreater(sql.find('INCLUDE'), sql.find('LOWER')) + with connection.cursor() as cursor: + constraints = connection.introspection.get_constraints( + cursor=cursor, table_name=table, + ) + self.assertIn(index_name, constraints) + self.assertIn('pub_date', constraints[index_name]['columns']) + with connection.schema_editor() as editor: + editor.remove_index(Article, index) + with connection.cursor() as cursor: + self.assertNotIn(index_name, connection.introspection.get_constraints( + cursor=cursor, table_name=table, + )) + @skipIfDBFeature('supports_covering_indexes') class CoveringIndexIgnoredTests(TransactionTestCase): |