tests.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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", "c2", "c3"),
  17. suffix="123",
  18. )
  19. self.assertEqual(index_name, "indexes_article_c1_7ce4cc86123")
  20. def test_index_together(self):
  21. editor = connection.schema_editor()
  22. index_sql = editor._model_indexes_sql(Article)
  23. self.assertEqual(len(index_sql), 1)
  24. # Ensure the index name is properly quoted
  25. self.assertIn(
  26. connection.ops.quote_name(
  27. editor._create_index_name(Article, ['headline', 'pub_date'], suffix='_idx')
  28. ),
  29. index_sql[0]
  30. )
  31. def test_index_together_single_list(self):
  32. # Test for using index_together with a single list (#22172)
  33. index_sql = connection.schema_editor()._model_indexes_sql(IndexTogetherSingleList)
  34. self.assertEqual(len(index_sql), 1)
  35. @skipUnless(connection.vendor == 'postgresql', "This is a postgresql-specific issue")
  36. def test_postgresql_text_indexes(self):
  37. """Test creation of PostgreSQL-specific text indexes (#12234)"""
  38. from .models import IndexedArticle
  39. index_sql = connection.schema_editor()._model_indexes_sql(IndexedArticle)
  40. self.assertEqual(len(index_sql), 5)
  41. self.assertIn('("headline" varchar_pattern_ops)', index_sql[2])
  42. self.assertIn('("body" text_pattern_ops)', index_sql[3])
  43. # unique=True and db_index=True should only create the varchar-specific
  44. # index (#19441).
  45. self.assertIn('("slug" varchar_pattern_ops)', index_sql[4])
  46. @skipUnless(connection.vendor == 'postgresql', "This is a postgresql-specific issue")
  47. def test_postgresql_virtual_relation_indexes(self):
  48. """Test indexes are not created for related objects"""
  49. index_sql = connection.schema_editor()._model_indexes_sql(Article)
  50. self.assertEqual(len(index_sql), 1)
  51. @skipUnless(connection.vendor == 'mysql', "This is a mysql-specific issue")
  52. def test_no_index_for_foreignkey(self):
  53. """
  54. MySQL on InnoDB already creates indexes automatically for foreign keys.
  55. (#14180). An index should be created if db_constraint=False (#26171).
  56. """
  57. storage = connection.introspection.get_storage_engine(
  58. connection.cursor(), ArticleTranslation._meta.db_table
  59. )
  60. if storage != "InnoDB":
  61. self.skip("This test only applies to the InnoDB storage engine")
  62. index_sql = connection.schema_editor()._model_indexes_sql(ArticleTranslation)
  63. self.assertEqual(index_sql, [
  64. 'CREATE INDEX `indexes_articletranslation_99fb53c2` '
  65. 'ON `indexes_articletranslation` (`article_no_constraint_id`)'
  66. ])