tests.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. import datetime
  2. from unittest import skipUnless
  3. from django.conf import settings
  4. from django.db import connection
  5. from django.db.models import (
  6. CASCADE,
  7. CharField,
  8. DateTimeField,
  9. ForeignKey,
  10. Index,
  11. Model,
  12. Q,
  13. )
  14. from django.db.models.functions import Lower
  15. from django.test import (
  16. TestCase,
  17. TransactionTestCase,
  18. ignore_warnings,
  19. skipIfDBFeature,
  20. skipUnlessDBFeature,
  21. )
  22. from django.test.utils import isolate_apps, override_settings
  23. from django.utils import timezone
  24. from django.utils.deprecation import RemovedInDjango51Warning
  25. from .models import Article, ArticleTranslation, IndexedArticle2
  26. class SchemaIndexesTests(TestCase):
  27. """
  28. Test index handling by the db.backends.schema infrastructure.
  29. """
  30. def test_index_name_hash(self):
  31. """
  32. Index names should be deterministic.
  33. """
  34. editor = connection.schema_editor()
  35. index_name = editor._create_index_name(
  36. table_name=Article._meta.db_table,
  37. column_names=("c1",),
  38. suffix="123",
  39. )
  40. self.assertEqual(index_name, "indexes_article_c1_a52bd80b123")
  41. def test_index_name(self):
  42. """
  43. Index names on the built-in database backends::
  44. * Are truncated as needed.
  45. * Include all the column names.
  46. * Include a deterministic hash.
  47. """
  48. long_name = "l%sng" % ("o" * 100)
  49. editor = connection.schema_editor()
  50. index_name = editor._create_index_name(
  51. table_name=Article._meta.db_table,
  52. column_names=("c1", "c2", long_name),
  53. suffix="ix",
  54. )
  55. expected = {
  56. "mysql": "indexes_article_c1_c2_looooooooooooooooooo_255179b2ix",
  57. "oracle": "indexes_a_c1_c2_loo_255179b2ix",
  58. "postgresql": "indexes_article_c1_c2_loooooooooooooooooo_255179b2ix",
  59. "sqlite": "indexes_article_c1_c2_l%sng_255179b2ix" % ("o" * 100),
  60. }
  61. if connection.vendor not in expected:
  62. self.skipTest(
  63. "This test is only supported on the built-in database backends."
  64. )
  65. self.assertEqual(index_name, expected[connection.vendor])
  66. def test_quoted_index_name(self):
  67. editor = connection.schema_editor()
  68. index_sql = [str(statement) for statement in editor._model_indexes_sql(Article)]
  69. self.assertEqual(len(index_sql), 1)
  70. # Ensure the index name is properly quoted.
  71. self.assertIn(
  72. connection.ops.quote_name(Article._meta.indexes[0].name),
  73. index_sql[0],
  74. )
  75. @ignore_warnings(category=RemovedInDjango51Warning)
  76. @isolate_apps("indexes")
  77. def test_index_together_single_list(self):
  78. class IndexTogetherSingleList(Model):
  79. headline = CharField(max_length=100)
  80. pub_date = DateTimeField()
  81. class Meta:
  82. index_together = ["headline", "pub_date"]
  83. index_sql = connection.schema_editor()._model_indexes_sql(
  84. IndexTogetherSingleList
  85. )
  86. self.assertEqual(len(index_sql), 1)
  87. def test_columns_list_sql(self):
  88. index = Index(fields=["headline"], name="whitespace_idx")
  89. editor = connection.schema_editor()
  90. self.assertIn(
  91. "(%s)" % editor.quote_name("headline"),
  92. str(index.create_sql(Article, editor)),
  93. )
  94. @skipUnlessDBFeature("supports_index_column_ordering")
  95. def test_descending_columns_list_sql(self):
  96. index = Index(fields=["-headline"], name="whitespace_idx")
  97. editor = connection.schema_editor()
  98. self.assertIn(
  99. "(%s DESC)" % editor.quote_name("headline"),
  100. str(index.create_sql(Article, editor)),
  101. )
  102. class SchemaIndexesNotPostgreSQLTests(TransactionTestCase):
  103. available_apps = ["indexes"]
  104. def test_create_index_ignores_opclasses(self):
  105. index = Index(
  106. name="test_ops_class",
  107. fields=["headline"],
  108. opclasses=["varchar_pattern_ops"],
  109. )
  110. with connection.schema_editor() as editor:
  111. # This would error if opclasses weren't ignored.
  112. editor.add_index(IndexedArticle2, index)
  113. # The `condition` parameter is ignored by databases that don't support partial
  114. # indexes.
  115. @skipIfDBFeature("supports_partial_indexes")
  116. class PartialIndexConditionIgnoredTests(TransactionTestCase):
  117. available_apps = ["indexes"]
  118. def test_condition_ignored(self):
  119. index = Index(
  120. name="test_condition_ignored",
  121. fields=["published"],
  122. condition=Q(published=True),
  123. )
  124. with connection.schema_editor() as editor:
  125. # This would error if condition weren't ignored.
  126. editor.add_index(Article, index)
  127. self.assertNotIn(
  128. "WHERE %s" % editor.quote_name("published"),
  129. str(index.create_sql(Article, editor)),
  130. )
  131. @skipUnless(connection.vendor == "postgresql", "PostgreSQL tests")
  132. class SchemaIndexesPostgreSQLTests(TransactionTestCase):
  133. available_apps = ["indexes"]
  134. get_opclass_query = """
  135. SELECT opcname, c.relname FROM pg_opclass AS oc
  136. JOIN pg_index as i on oc.oid = ANY(i.indclass)
  137. JOIN pg_class as c on c.oid = i.indexrelid
  138. WHERE c.relname = '%s'
  139. """
  140. def test_text_indexes(self):
  141. """Test creation of PostgreSQL-specific text indexes (#12234)"""
  142. from .models import IndexedArticle
  143. index_sql = [
  144. str(statement)
  145. for statement in connection.schema_editor()._model_indexes_sql(
  146. IndexedArticle
  147. )
  148. ]
  149. self.assertEqual(len(index_sql), 5)
  150. self.assertIn('("headline" varchar_pattern_ops)', index_sql[1])
  151. self.assertIn('("body" text_pattern_ops)', index_sql[3])
  152. # unique=True and db_index=True should only create the varchar-specific
  153. # index (#19441).
  154. self.assertIn('("slug" varchar_pattern_ops)', index_sql[4])
  155. def test_virtual_relation_indexes(self):
  156. """Test indexes are not created for related objects"""
  157. index_sql = connection.schema_editor()._model_indexes_sql(Article)
  158. self.assertEqual(len(index_sql), 1)
  159. def test_ops_class(self):
  160. index = Index(
  161. name="test_ops_class",
  162. fields=["headline"],
  163. opclasses=["varchar_pattern_ops"],
  164. )
  165. with connection.schema_editor() as editor:
  166. editor.add_index(IndexedArticle2, index)
  167. with editor.connection.cursor() as cursor:
  168. cursor.execute(self.get_opclass_query % "test_ops_class")
  169. self.assertEqual(
  170. cursor.fetchall(), [("varchar_pattern_ops", "test_ops_class")]
  171. )
  172. def test_ops_class_multiple_columns(self):
  173. index = Index(
  174. name="test_ops_class_multiple",
  175. fields=["headline", "body"],
  176. opclasses=["varchar_pattern_ops", "text_pattern_ops"],
  177. )
  178. with connection.schema_editor() as editor:
  179. editor.add_index(IndexedArticle2, index)
  180. with editor.connection.cursor() as cursor:
  181. cursor.execute(self.get_opclass_query % "test_ops_class_multiple")
  182. expected_ops_classes = (
  183. ("varchar_pattern_ops", "test_ops_class_multiple"),
  184. ("text_pattern_ops", "test_ops_class_multiple"),
  185. )
  186. self.assertCountEqual(cursor.fetchall(), expected_ops_classes)
  187. def test_ops_class_partial(self):
  188. index = Index(
  189. name="test_ops_class_partial",
  190. fields=["body"],
  191. opclasses=["text_pattern_ops"],
  192. condition=Q(headline__contains="China"),
  193. )
  194. with connection.schema_editor() as editor:
  195. editor.add_index(IndexedArticle2, index)
  196. with editor.connection.cursor() as cursor:
  197. cursor.execute(self.get_opclass_query % "test_ops_class_partial")
  198. self.assertCountEqual(
  199. cursor.fetchall(), [("text_pattern_ops", "test_ops_class_partial")]
  200. )
  201. def test_ops_class_partial_tablespace(self):
  202. indexname = "test_ops_class_tblspace"
  203. index = Index(
  204. name=indexname,
  205. fields=["body"],
  206. opclasses=["text_pattern_ops"],
  207. condition=Q(headline__contains="China"),
  208. db_tablespace="pg_default",
  209. )
  210. with connection.schema_editor() as editor:
  211. editor.add_index(IndexedArticle2, index)
  212. self.assertIn(
  213. 'TABLESPACE "pg_default" ',
  214. str(index.create_sql(IndexedArticle2, editor)),
  215. )
  216. with editor.connection.cursor() as cursor:
  217. cursor.execute(self.get_opclass_query % indexname)
  218. self.assertCountEqual(cursor.fetchall(), [("text_pattern_ops", indexname)])
  219. def test_ops_class_descending(self):
  220. indexname = "test_ops_class_ordered"
  221. index = Index(
  222. name=indexname,
  223. fields=["-body"],
  224. opclasses=["text_pattern_ops"],
  225. )
  226. with connection.schema_editor() as editor:
  227. editor.add_index(IndexedArticle2, index)
  228. with editor.connection.cursor() as cursor:
  229. cursor.execute(self.get_opclass_query % indexname)
  230. self.assertCountEqual(cursor.fetchall(), [("text_pattern_ops", indexname)])
  231. def test_ops_class_descending_partial(self):
  232. indexname = "test_ops_class_ordered_partial"
  233. index = Index(
  234. name=indexname,
  235. fields=["-body"],
  236. opclasses=["text_pattern_ops"],
  237. condition=Q(headline__contains="China"),
  238. )
  239. with connection.schema_editor() as editor:
  240. editor.add_index(IndexedArticle2, index)
  241. with editor.connection.cursor() as cursor:
  242. cursor.execute(self.get_opclass_query % indexname)
  243. self.assertCountEqual(cursor.fetchall(), [("text_pattern_ops", indexname)])
  244. @skipUnlessDBFeature("supports_covering_indexes")
  245. def test_ops_class_include(self):
  246. index_name = "test_ops_class_include"
  247. index = Index(
  248. name=index_name,
  249. fields=["body"],
  250. opclasses=["text_pattern_ops"],
  251. include=["headline"],
  252. )
  253. with connection.schema_editor() as editor:
  254. editor.add_index(IndexedArticle2, index)
  255. with editor.connection.cursor() as cursor:
  256. cursor.execute(self.get_opclass_query % index_name)
  257. self.assertCountEqual(cursor.fetchall(), [("text_pattern_ops", index_name)])
  258. @skipUnlessDBFeature("supports_covering_indexes")
  259. def test_ops_class_include_tablespace(self):
  260. index_name = "test_ops_class_include_tblspace"
  261. index = Index(
  262. name=index_name,
  263. fields=["body"],
  264. opclasses=["text_pattern_ops"],
  265. include=["headline"],
  266. db_tablespace="pg_default",
  267. )
  268. with connection.schema_editor() as editor:
  269. editor.add_index(IndexedArticle2, index)
  270. self.assertIn(
  271. 'TABLESPACE "pg_default"',
  272. str(index.create_sql(IndexedArticle2, editor)),
  273. )
  274. with editor.connection.cursor() as cursor:
  275. cursor.execute(self.get_opclass_query % index_name)
  276. self.assertCountEqual(cursor.fetchall(), [("text_pattern_ops", index_name)])
  277. def test_ops_class_columns_lists_sql(self):
  278. index = Index(
  279. fields=["headline"],
  280. name="whitespace_idx",
  281. opclasses=["text_pattern_ops"],
  282. )
  283. with connection.schema_editor() as editor:
  284. self.assertIn(
  285. "(%s text_pattern_ops)" % editor.quote_name("headline"),
  286. str(index.create_sql(Article, editor)),
  287. )
  288. def test_ops_class_descending_columns_list_sql(self):
  289. index = Index(
  290. fields=["-headline"],
  291. name="whitespace_idx",
  292. opclasses=["text_pattern_ops"],
  293. )
  294. with connection.schema_editor() as editor:
  295. self.assertIn(
  296. "(%s text_pattern_ops DESC)" % editor.quote_name("headline"),
  297. str(index.create_sql(Article, editor)),
  298. )
  299. @skipUnless(connection.vendor == "mysql", "MySQL tests")
  300. class SchemaIndexesMySQLTests(TransactionTestCase):
  301. available_apps = ["indexes"]
  302. def test_no_index_for_foreignkey(self):
  303. """
  304. MySQL on InnoDB already creates indexes automatically for foreign keys.
  305. (#14180). An index should be created if db_constraint=False (#26171).
  306. """
  307. with connection.cursor() as cursor:
  308. storage = connection.introspection.get_storage_engine(
  309. cursor,
  310. ArticleTranslation._meta.db_table,
  311. )
  312. if storage != "InnoDB":
  313. self.skipTest("This test only applies to the InnoDB storage engine")
  314. index_sql = [
  315. str(statement)
  316. for statement in connection.schema_editor()._model_indexes_sql(
  317. ArticleTranslation
  318. )
  319. ]
  320. self.assertEqual(
  321. index_sql,
  322. [
  323. "CREATE INDEX "
  324. "`indexes_articletranslation_article_no_constraint_id_d6c0806b` "
  325. "ON `indexes_articletranslation` (`article_no_constraint_id`)"
  326. ],
  327. )
  328. # The index also shouldn't be created if the ForeignKey is added after
  329. # the model was created.
  330. field_created = False
  331. try:
  332. with connection.schema_editor() as editor:
  333. new_field = ForeignKey(Article, CASCADE)
  334. new_field.set_attributes_from_name("new_foreign_key")
  335. editor.add_field(ArticleTranslation, new_field)
  336. field_created = True
  337. # No deferred SQL. The FK constraint is included in the
  338. # statement to add the field.
  339. self.assertFalse(editor.deferred_sql)
  340. finally:
  341. if field_created:
  342. with connection.schema_editor() as editor:
  343. editor.remove_field(ArticleTranslation, new_field)
  344. @skipUnlessDBFeature("supports_partial_indexes")
  345. # SQLite doesn't support timezone-aware datetimes when USE_TZ is False.
  346. @override_settings(USE_TZ=True)
  347. class PartialIndexTests(TransactionTestCase):
  348. # Schema editor is used to create the index to test that it works.
  349. available_apps = ["indexes"]
  350. def test_partial_index(self):
  351. with connection.schema_editor() as editor:
  352. index = Index(
  353. name="recent_article_idx",
  354. fields=["pub_date"],
  355. condition=Q(
  356. pub_date__gt=datetime.datetime(
  357. year=2015,
  358. month=1,
  359. day=1,
  360. # PostgreSQL would otherwise complain about the lookup
  361. # being converted to a mutable function (by removing
  362. # the timezone in the cast) which is forbidden.
  363. tzinfo=timezone.get_current_timezone(),
  364. ),
  365. ),
  366. )
  367. self.assertIn(
  368. "WHERE %s" % editor.quote_name("pub_date"),
  369. str(index.create_sql(Article, schema_editor=editor)),
  370. )
  371. editor.add_index(index=index, model=Article)
  372. with connection.cursor() as cursor:
  373. self.assertIn(
  374. index.name,
  375. connection.introspection.get_constraints(
  376. cursor=cursor,
  377. table_name=Article._meta.db_table,
  378. ),
  379. )
  380. editor.remove_index(index=index, model=Article)
  381. def test_integer_restriction_partial(self):
  382. with connection.schema_editor() as editor:
  383. index = Index(
  384. name="recent_article_idx",
  385. fields=["id"],
  386. condition=Q(pk__gt=1),
  387. )
  388. self.assertIn(
  389. "WHERE %s" % editor.quote_name("id"),
  390. str(index.create_sql(Article, schema_editor=editor)),
  391. )
  392. editor.add_index(index=index, model=Article)
  393. with connection.cursor() as cursor:
  394. self.assertIn(
  395. index.name,
  396. connection.introspection.get_constraints(
  397. cursor=cursor,
  398. table_name=Article._meta.db_table,
  399. ),
  400. )
  401. editor.remove_index(index=index, model=Article)
  402. def test_boolean_restriction_partial(self):
  403. with connection.schema_editor() as editor:
  404. index = Index(
  405. name="published_index",
  406. fields=["published"],
  407. condition=Q(published=True),
  408. )
  409. self.assertIn(
  410. "WHERE %s" % editor.quote_name("published"),
  411. str(index.create_sql(Article, schema_editor=editor)),
  412. )
  413. editor.add_index(index=index, model=Article)
  414. with connection.cursor() as cursor:
  415. self.assertIn(
  416. index.name,
  417. connection.introspection.get_constraints(
  418. cursor=cursor,
  419. table_name=Article._meta.db_table,
  420. ),
  421. )
  422. editor.remove_index(index=index, model=Article)
  423. @skipUnlessDBFeature("supports_functions_in_partial_indexes")
  424. def test_multiple_conditions(self):
  425. with connection.schema_editor() as editor:
  426. index = Index(
  427. name="recent_article_idx",
  428. fields=["pub_date", "headline"],
  429. condition=(
  430. Q(
  431. pub_date__gt=datetime.datetime(
  432. year=2015,
  433. month=1,
  434. day=1,
  435. tzinfo=timezone.get_current_timezone(),
  436. )
  437. )
  438. & Q(headline__contains="China")
  439. ),
  440. )
  441. sql = str(index.create_sql(Article, schema_editor=editor))
  442. where = sql.find("WHERE")
  443. self.assertIn("WHERE (%s" % editor.quote_name("pub_date"), sql)
  444. # Because each backend has different syntax for the operators,
  445. # check ONLY the occurrence of headline in the SQL.
  446. self.assertGreater(sql.rfind("headline"), where)
  447. editor.add_index(index=index, model=Article)
  448. with connection.cursor() as cursor:
  449. self.assertIn(
  450. index.name,
  451. connection.introspection.get_constraints(
  452. cursor=cursor,
  453. table_name=Article._meta.db_table,
  454. ),
  455. )
  456. editor.remove_index(index=index, model=Article)
  457. def test_is_null_condition(self):
  458. with connection.schema_editor() as editor:
  459. index = Index(
  460. name="recent_article_idx",
  461. fields=["pub_date"],
  462. condition=Q(pub_date__isnull=False),
  463. )
  464. self.assertIn(
  465. "WHERE %s IS NOT NULL" % editor.quote_name("pub_date"),
  466. str(index.create_sql(Article, schema_editor=editor)),
  467. )
  468. editor.add_index(index=index, model=Article)
  469. with connection.cursor() as cursor:
  470. self.assertIn(
  471. index.name,
  472. connection.introspection.get_constraints(
  473. cursor=cursor,
  474. table_name=Article._meta.db_table,
  475. ),
  476. )
  477. editor.remove_index(index=index, model=Article)
  478. @skipUnlessDBFeature("supports_expression_indexes")
  479. def test_partial_func_index(self):
  480. index_name = "partial_func_idx"
  481. index = Index(
  482. Lower("headline").desc(),
  483. name=index_name,
  484. condition=Q(pub_date__isnull=False),
  485. )
  486. with connection.schema_editor() as editor:
  487. editor.add_index(index=index, model=Article)
  488. sql = index.create_sql(Article, schema_editor=editor)
  489. table = Article._meta.db_table
  490. self.assertIs(sql.references_column(table, "headline"), True)
  491. sql = str(sql)
  492. self.assertIn("LOWER(%s)" % editor.quote_name("headline"), sql)
  493. self.assertIn(
  494. "WHERE %s IS NOT NULL" % editor.quote_name("pub_date"),
  495. sql,
  496. )
  497. self.assertGreater(sql.find("WHERE"), sql.find("LOWER"))
  498. with connection.cursor() as cursor:
  499. constraints = connection.introspection.get_constraints(
  500. cursor=cursor,
  501. table_name=table,
  502. )
  503. self.assertIn(index_name, constraints)
  504. if connection.features.supports_index_column_ordering:
  505. self.assertEqual(constraints[index_name]["orders"], ["DESC"])
  506. with connection.schema_editor() as editor:
  507. editor.remove_index(Article, index)
  508. with connection.cursor() as cursor:
  509. self.assertNotIn(
  510. index_name,
  511. connection.introspection.get_constraints(
  512. cursor=cursor,
  513. table_name=table,
  514. ),
  515. )
  516. @skipUnlessDBFeature("supports_covering_indexes")
  517. class CoveringIndexTests(TransactionTestCase):
  518. available_apps = ["indexes"]
  519. def test_covering_index(self):
  520. index = Index(
  521. name="covering_headline_idx",
  522. fields=["headline"],
  523. include=["pub_date", "published"],
  524. )
  525. with connection.schema_editor() as editor:
  526. self.assertIn(
  527. "(%s) INCLUDE (%s, %s)"
  528. % (
  529. editor.quote_name("headline"),
  530. editor.quote_name("pub_date"),
  531. editor.quote_name("published"),
  532. ),
  533. str(index.create_sql(Article, editor)),
  534. )
  535. editor.add_index(Article, index)
  536. with connection.cursor() as cursor:
  537. constraints = connection.introspection.get_constraints(
  538. cursor=cursor,
  539. table_name=Article._meta.db_table,
  540. )
  541. self.assertIn(index.name, constraints)
  542. self.assertEqual(
  543. constraints[index.name]["columns"],
  544. ["headline", "pub_date", "published"],
  545. )
  546. editor.remove_index(Article, index)
  547. with connection.cursor() as cursor:
  548. self.assertNotIn(
  549. index.name,
  550. connection.introspection.get_constraints(
  551. cursor=cursor,
  552. table_name=Article._meta.db_table,
  553. ),
  554. )
  555. def test_covering_partial_index(self):
  556. index = Index(
  557. name="covering_partial_headline_idx",
  558. fields=["headline"],
  559. include=["pub_date"],
  560. condition=Q(pub_date__isnull=False),
  561. )
  562. with connection.schema_editor() as editor:
  563. extra_sql = ""
  564. if settings.DEFAULT_INDEX_TABLESPACE:
  565. extra_sql = "TABLESPACE %s " % editor.quote_name(
  566. settings.DEFAULT_INDEX_TABLESPACE
  567. )
  568. self.assertIn(
  569. "(%s) INCLUDE (%s) %sWHERE %s "
  570. % (
  571. editor.quote_name("headline"),
  572. editor.quote_name("pub_date"),
  573. extra_sql,
  574. editor.quote_name("pub_date"),
  575. ),
  576. str(index.create_sql(Article, editor)),
  577. )
  578. editor.add_index(Article, index)
  579. with connection.cursor() as cursor:
  580. constraints = connection.introspection.get_constraints(
  581. cursor=cursor,
  582. table_name=Article._meta.db_table,
  583. )
  584. self.assertIn(index.name, constraints)
  585. self.assertEqual(
  586. constraints[index.name]["columns"],
  587. ["headline", "pub_date"],
  588. )
  589. editor.remove_index(Article, index)
  590. with connection.cursor() as cursor:
  591. self.assertNotIn(
  592. index.name,
  593. connection.introspection.get_constraints(
  594. cursor=cursor,
  595. table_name=Article._meta.db_table,
  596. ),
  597. )
  598. @skipUnlessDBFeature("supports_expression_indexes")
  599. def test_covering_func_index(self):
  600. index_name = "covering_func_headline_idx"
  601. index = Index(Lower("headline"), name=index_name, include=["pub_date"])
  602. with connection.schema_editor() as editor:
  603. editor.add_index(index=index, model=Article)
  604. sql = index.create_sql(Article, schema_editor=editor)
  605. table = Article._meta.db_table
  606. self.assertIs(sql.references_column(table, "headline"), True)
  607. sql = str(sql)
  608. self.assertIn("LOWER(%s)" % editor.quote_name("headline"), sql)
  609. self.assertIn("INCLUDE (%s)" % editor.quote_name("pub_date"), sql)
  610. self.assertGreater(sql.find("INCLUDE"), sql.find("LOWER"))
  611. with connection.cursor() as cursor:
  612. constraints = connection.introspection.get_constraints(
  613. cursor=cursor,
  614. table_name=table,
  615. )
  616. self.assertIn(index_name, constraints)
  617. self.assertIn("pub_date", constraints[index_name]["columns"])
  618. with connection.schema_editor() as editor:
  619. editor.remove_index(Article, index)
  620. with connection.cursor() as cursor:
  621. self.assertNotIn(
  622. index_name,
  623. connection.introspection.get_constraints(
  624. cursor=cursor,
  625. table_name=table,
  626. ),
  627. )
  628. @skipIfDBFeature("supports_covering_indexes")
  629. class CoveringIndexIgnoredTests(TransactionTestCase):
  630. available_apps = ["indexes"]
  631. def test_covering_ignored(self):
  632. index = Index(
  633. name="test_covering_ignored",
  634. fields=["headline"],
  635. include=["pub_date"],
  636. )
  637. with connection.schema_editor() as editor:
  638. editor.add_index(Article, index)
  639. self.assertNotIn(
  640. "INCLUDE (%s)" % editor.quote_name("headline"),
  641. str(index.create_sql(Article, editor)),
  642. )