tests.py 6.0 KB

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