tests.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from unittest import skipUnless
  2. from django.core.management.color import no_style
  3. from django.db import connection
  4. from django.test import TestCase
  5. from .models import Article, IndexTogetherSingleList
  6. class IndexesTests(TestCase):
  7. def test_index_together(self):
  8. index_sql = connection.creation.sql_indexes_for_model(Article, no_style())
  9. self.assertEqual(len(index_sql), 1)
  10. def test_index_together_single_list(self):
  11. # Test for using index_together with a single list (#22172)
  12. index_sql = connection.creation.sql_indexes_for_model(IndexTogetherSingleList, no_style())
  13. self.assertEqual(len(index_sql), 1)
  14. @skipUnless(connection.vendor == 'postgresql',
  15. "This is a postgresql-specific issue")
  16. def test_postgresql_text_indexes(self):
  17. """Test creation of PostgreSQL-specific text indexes (#12234)"""
  18. from .models import IndexedArticle
  19. index_sql = connection.creation.sql_indexes_for_model(IndexedArticle, no_style())
  20. self.assertEqual(len(index_sql), 5)
  21. self.assertIn('("headline" varchar_pattern_ops)', index_sql[1])
  22. self.assertIn('("body" text_pattern_ops)', index_sql[3])
  23. # unique=True and db_index=True should only create the varchar-specific
  24. # index (#19441).
  25. self.assertIn('("slug" varchar_pattern_ops)', index_sql[4])
  26. @skipUnless(connection.vendor == 'postgresql',
  27. "This is a postgresql-specific issue")
  28. def test_postgresql_virtual_relation_indexes(self):
  29. """Test indexes are not created for related objects"""
  30. index_sql = connection.creation.sql_indexes_for_model(Article, no_style())
  31. self.assertEqual(len(index_sql), 1)