2
0

tests.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from django.db import models
  2. from django.test import SimpleTestCase
  3. from .models import Book, ChildModel1, ChildModel2
  4. class IndexesTests(SimpleTestCase):
  5. def test_suffix(self):
  6. self.assertEqual(models.Index.suffix, 'idx')
  7. def test_repr(self):
  8. index = models.Index(fields=['title'])
  9. multi_col_index = models.Index(fields=['title', 'author'])
  10. self.assertEqual(repr(index), "<Index: fields='title'>")
  11. self.assertEqual(repr(multi_col_index), "<Index: fields='title, author'>")
  12. def test_eq(self):
  13. index = models.Index(fields=['title'])
  14. same_index = models.Index(fields=['title'])
  15. another_index = models.Index(fields=['title', 'author'])
  16. index.model = Book
  17. same_index.model = Book
  18. another_index.model = Book
  19. self.assertEqual(index, same_index)
  20. self.assertNotEqual(index, another_index)
  21. def test_index_fields_type(self):
  22. with self.assertRaisesMessage(ValueError, 'Index.fields must be a list.'):
  23. models.Index(fields='title')
  24. def test_raises_error_without_field(self):
  25. msg = 'At least one field is required to define an index.'
  26. with self.assertRaisesMessage(ValueError, msg):
  27. models.Index()
  28. def test_max_name_length(self):
  29. msg = 'Index names cannot be longer than 30 characters.'
  30. with self.assertRaisesMessage(ValueError, msg):
  31. models.Index(fields=['title'], name='looooooooooooong_index_name_idx')
  32. def test_name_constraints(self):
  33. msg = 'Index names cannot start with an underscore (_).'
  34. with self.assertRaisesMessage(ValueError, msg):
  35. models.Index(fields=['title'], name='_name_starting_with_underscore')
  36. msg = 'Index names cannot start with a number (0-9).'
  37. with self.assertRaisesMessage(ValueError, msg):
  38. models.Index(fields=['title'], name='5name_starting_with_number')
  39. def test_name_auto_generation(self):
  40. index = models.Index(fields=['author'])
  41. index.set_name_with_model(Book)
  42. self.assertEqual(index.name, 'model_index_author_0f5565_idx')
  43. # '-' for DESC columns should be accounted for in the index name.
  44. index = models.Index(fields=['-author'])
  45. index.set_name_with_model(Book)
  46. self.assertEqual(index.name, 'model_index_author_708765_idx')
  47. # fields may be truncated in the name. db_column is used for naming.
  48. long_field_index = models.Index(fields=['pages'])
  49. long_field_index.set_name_with_model(Book)
  50. self.assertEqual(long_field_index.name, 'model_index_page_co_69235a_idx')
  51. # suffix can't be longer than 3 characters.
  52. long_field_index.suffix = 'suff'
  53. msg = 'Index too long for multiple database support. Is self.suffix longer than 3 characters?'
  54. with self.assertRaisesMessage(AssertionError, msg):
  55. long_field_index.set_name_with_model(Book)
  56. def test_deconstruction(self):
  57. index = models.Index(fields=['title'])
  58. index.set_name_with_model(Book)
  59. path, args, kwargs = index.deconstruct()
  60. self.assertEqual(path, 'django.db.models.Index')
  61. self.assertEqual(args, ())
  62. self.assertEqual(kwargs, {'fields': ['title'], 'name': 'model_index_title_196f42_idx'})
  63. def test_clone(self):
  64. index = models.Index(fields=['title'])
  65. new_index = index.clone()
  66. self.assertIsNot(index, new_index)
  67. self.assertEqual(index.fields, new_index.fields)
  68. def test_abstract_children(self):
  69. index_names = [index.name for index in ChildModel1._meta.indexes]
  70. self.assertEqual(index_names, ['model_index_name_440998_idx'])
  71. index_names = [index.name for index in ChildModel2._meta.indexes]
  72. self.assertEqual(index_names, ['model_index_name_b6c374_idx'])