tests.py 3.1 KB

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