tests.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. from django.conf import settings
  2. from django.db import connection, models
  3. from django.test import SimpleTestCase, skipUnlessDBFeature
  4. from django.test.utils import isolate_apps
  5. from .models import Book, ChildModel1, ChildModel2
  6. class IndexesTests(SimpleTestCase):
  7. def test_suffix(self):
  8. self.assertEqual(models.Index.suffix, 'idx')
  9. def test_repr(self):
  10. index = models.Index(fields=['title'])
  11. multi_col_index = models.Index(fields=['title', 'author'])
  12. self.assertEqual(repr(index), "<Index: fields='title'>")
  13. self.assertEqual(repr(multi_col_index), "<Index: fields='title, author'>")
  14. def test_eq(self):
  15. index = models.Index(fields=['title'])
  16. same_index = models.Index(fields=['title'])
  17. another_index = models.Index(fields=['title', 'author'])
  18. index.model = Book
  19. same_index.model = Book
  20. another_index.model = Book
  21. self.assertEqual(index, same_index)
  22. self.assertNotEqual(index, another_index)
  23. def test_index_fields_type(self):
  24. with self.assertRaisesMessage(ValueError, 'Index.fields must be a list or tuple.'):
  25. models.Index(fields='title')
  26. def test_fields_tuple(self):
  27. self.assertEqual(models.Index(fields=('title',)).fields, ['title'])
  28. def test_raises_error_without_field(self):
  29. msg = 'At least one field is required to define an index.'
  30. with self.assertRaisesMessage(ValueError, msg):
  31. models.Index()
  32. def test_max_name_length(self):
  33. msg = 'Index names cannot be longer than 30 characters.'
  34. with self.assertRaisesMessage(ValueError, msg):
  35. models.Index(fields=['title'], name='looooooooooooong_index_name_idx')
  36. def test_name_constraints(self):
  37. msg = 'Index names cannot start with an underscore (_).'
  38. with self.assertRaisesMessage(ValueError, msg):
  39. models.Index(fields=['title'], name='_name_starting_with_underscore')
  40. msg = 'Index names cannot start with a number (0-9).'
  41. with self.assertRaisesMessage(ValueError, msg):
  42. models.Index(fields=['title'], name='5name_starting_with_number')
  43. def test_name_auto_generation(self):
  44. index = models.Index(fields=['author'])
  45. index.set_name_with_model(Book)
  46. self.assertEqual(index.name, 'model_index_author_0f5565_idx')
  47. # '-' for DESC columns should be accounted for in the index name.
  48. index = models.Index(fields=['-author'])
  49. index.set_name_with_model(Book)
  50. self.assertEqual(index.name, 'model_index_author_708765_idx')
  51. # fields may be truncated in the name. db_column is used for naming.
  52. long_field_index = models.Index(fields=['pages'])
  53. long_field_index.set_name_with_model(Book)
  54. self.assertEqual(long_field_index.name, 'model_index_page_co_69235a_idx')
  55. # suffix can't be longer than 3 characters.
  56. long_field_index.suffix = 'suff'
  57. msg = 'Index too long for multiple database support. Is self.suffix longer than 3 characters?'
  58. with self.assertRaisesMessage(AssertionError, msg):
  59. long_field_index.set_name_with_model(Book)
  60. @isolate_apps('model_indexes')
  61. def test_name_auto_generation_with_quoted_db_table(self):
  62. class QuotedDbTable(models.Model):
  63. name = models.CharField(max_length=50)
  64. class Meta:
  65. db_table = '"t_quoted"'
  66. index = models.Index(fields=['name'])
  67. index.set_name_with_model(QuotedDbTable)
  68. self.assertEqual(index.name, 't_quoted_name_e4ed1b_idx')
  69. def test_deconstruction(self):
  70. index = models.Index(fields=['title'], db_tablespace='idx_tbls')
  71. index.set_name_with_model(Book)
  72. path, args, kwargs = index.deconstruct()
  73. self.assertEqual(path, 'django.db.models.Index')
  74. self.assertEqual(args, ())
  75. self.assertEqual(
  76. kwargs,
  77. {'fields': ['title'], 'name': 'model_index_title_196f42_idx', 'db_tablespace': 'idx_tbls'}
  78. )
  79. def test_clone(self):
  80. index = models.Index(fields=['title'])
  81. new_index = index.clone()
  82. self.assertIsNot(index, new_index)
  83. self.assertEqual(index.fields, new_index.fields)
  84. def test_name_set(self):
  85. index_names = [index.name for index in Book._meta.indexes]
  86. self.assertCountEqual(index_names, ['model_index_title_196f42_idx', 'model_index_isbn_34f975_idx'])
  87. def test_abstract_children(self):
  88. index_names = [index.name for index in ChildModel1._meta.indexes]
  89. self.assertEqual(index_names, ['model_index_name_440998_idx'])
  90. index_names = [index.name for index in ChildModel2._meta.indexes]
  91. self.assertEqual(index_names, ['model_index_name_b6c374_idx'])
  92. @skipUnlessDBFeature('supports_tablespaces')
  93. def test_db_tablespace(self):
  94. with connection.schema_editor() as editor:
  95. # Index with db_tablespace attribute.
  96. for fields in [
  97. # Field with db_tablespace specified on model.
  98. ['shortcut'],
  99. # Field without db_tablespace specified on model.
  100. ['author'],
  101. # Multi-column with db_tablespaces specified on model.
  102. ['shortcut', 'isbn'],
  103. # Multi-column without db_tablespace specified on model.
  104. ['title', 'author'],
  105. ]:
  106. with self.subTest(fields=fields):
  107. index = models.Index(fields=fields, db_tablespace='idx_tbls2')
  108. self.assertIn('"idx_tbls2"', str(index.create_sql(Book, editor)).lower())
  109. # Indexes without db_tablespace attribute.
  110. for fields in [['author'], ['shortcut', 'isbn'], ['title', 'author']]:
  111. with self.subTest(fields=fields):
  112. index = models.Index(fields=fields)
  113. # The DEFAULT_INDEX_TABLESPACE setting can't be tested
  114. # because it's evaluated when the model class is defined.
  115. # As a consequence, @override_settings doesn't work.
  116. if settings.DEFAULT_INDEX_TABLESPACE:
  117. self.assertIn(
  118. '"%s"' % settings.DEFAULT_INDEX_TABLESPACE,
  119. str(index.create_sql(Book, editor)).lower()
  120. )
  121. else:
  122. self.assertNotIn('TABLESPACE', str(index.create_sql(Book, editor)))
  123. # Field with db_tablespace specified on the model and an index
  124. # without db_tablespace.
  125. index = models.Index(fields=['shortcut'])
  126. self.assertIn('"idx_tbls"', str(index.create_sql(Book, editor)).lower())