tests.py 1.5 KB

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