tests.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from unittest import skipUnless
  2. from django.db import connection
  3. from django.test import TestCase
  4. from .models import Article, ArticleTranslation, IndexTogetherSingleList
  5. class SchemaIndexesTests(TestCase):
  6. """
  7. Test index handling by the db.backends.schema infrastructure.
  8. """
  9. def test_index_name_hash(self):
  10. """
  11. Index names should be deterministic.
  12. """
  13. with connection.schema_editor() as editor:
  14. index_name = editor._create_index_name(
  15. model=Article,
  16. column_names=("c1",),
  17. suffix="123",
  18. )
  19. self.assertEqual(index_name, "indexes_article_c1_a52bd80b123")
  20. def test_index_name(self):
  21. """
  22. Index names on the built-in database backends::
  23. * Are truncated as needed.
  24. * Include all the column names.
  25. * Include a deterministic hash.
  26. """
  27. long_name = 'l%sng' % ('o' * 100)
  28. with connection.schema_editor() as editor:
  29. index_name = editor._create_index_name(
  30. model=Article,
  31. column_names=('c1', 'c2', long_name),
  32. suffix='ix',
  33. )
  34. expected = {
  35. 'mysql': 'indexes_article_c1_c2_looooooooooooooooooo_255179b2ix',
  36. 'oracle': 'indexes_a_c1_c2_loo_255179b2ix',
  37. 'postgresql': 'indexes_article_c1_c2_loooooooooooooooooo_255179b2ix',
  38. 'sqlite': 'indexes_article_c1_c2_l%sng_255179b2ix' % ('o' * 100),
  39. }
  40. if connection.vendor not in expected:
  41. self.skipTest('This test is only supported on the built-in database backends.')
  42. self.assertEqual(index_name, expected[connection.vendor])
  43. def test_index_together(self):
  44. editor = connection.schema_editor()
  45. index_sql = editor._model_indexes_sql(Article)
  46. self.assertEqual(len(index_sql), 1)
  47. # Ensure the index name is properly quoted
  48. self.assertIn(
  49. connection.ops.quote_name(
  50. editor._create_index_name(Article, ['headline', 'pub_date'], suffix='_idx')
  51. ),
  52. index_sql[0]
  53. )
  54. def test_index_together_single_list(self):
  55. # Test for using index_together with a single list (#22172)
  56. index_sql = connection.schema_editor()._model_indexes_sql(IndexTogetherSingleList)
  57. self.assertEqual(len(index_sql), 1)
  58. @skipUnless(connection.vendor == 'postgresql', "This is a postgresql-specific issue")
  59. def test_postgresql_text_indexes(self):
  60. """Test creation of PostgreSQL-specific text indexes (#12234)"""
  61. from .models import IndexedArticle
  62. index_sql = connection.schema_editor()._model_indexes_sql(IndexedArticle)
  63. self.assertEqual(len(index_sql), 5)
  64. self.assertIn('("headline" varchar_pattern_ops)', index_sql[1])
  65. self.assertIn('("body" text_pattern_ops)', index_sql[3])
  66. # unique=True and db_index=True should only create the varchar-specific
  67. # index (#19441).
  68. self.assertIn('("slug" varchar_pattern_ops)', index_sql[4])
  69. @skipUnless(connection.vendor == 'postgresql', "This is a postgresql-specific issue")
  70. def test_postgresql_virtual_relation_indexes(self):
  71. """Test indexes are not created for related objects"""
  72. index_sql = connection.schema_editor()._model_indexes_sql(Article)
  73. self.assertEqual(len(index_sql), 1)
  74. @skipUnless(connection.vendor == 'mysql', "This is a mysql-specific issue")
  75. def test_no_index_for_foreignkey(self):
  76. """
  77. MySQL on InnoDB already creates indexes automatically for foreign keys.
  78. (#14180). An index should be created if db_constraint=False (#26171).
  79. """
  80. storage = connection.introspection.get_storage_engine(
  81. connection.cursor(), ArticleTranslation._meta.db_table
  82. )
  83. if storage != "InnoDB":
  84. self.skip("This test only applies to the InnoDB storage engine")
  85. index_sql = connection.schema_editor()._model_indexes_sql(ArticleTranslation)
  86. self.assertEqual(index_sql, [
  87. 'CREATE INDEX `indexes_articletranslation_article_no_constraint_id_d6c0806b` '
  88. 'ON `indexes_articletranslation` (`article_no_constraint_id`)'
  89. ])