summaryrefslogtreecommitdiff
path: root/tests/model_indexes
diff options
context:
space:
mode:
authorAkshesh <aksheshdoshi@gmail.com>2016-06-25 22:02:56 +0530
committerTim Graham <timograham@gmail.com>2016-06-27 10:41:01 -0400
commit156e2d59cf92eb252c2f6ef9bb0b65f26c707de2 (patch)
treea385db97c67f6b8504954c3df3ac88f169d13f8c /tests/model_indexes
parentc962b9104a22505a7d6c57bbec9eda163f486c7d (diff)
downloaddjango-156e2d59cf92eb252c2f6ef9bb0b65f26c707de2.tar.gz
Fixed #26709 -- Added class-based indexes.
Added the AddIndex and RemoveIndex operations to use them in migrations. Thanks markush, mjtamlyn, timgraham, and charettes for review and advice.
Diffstat (limited to 'tests/model_indexes')
-rw-r--r--tests/model_indexes/__init__.py0
-rw-r--r--tests/model_indexes/models.py7
-rw-r--r--tests/model_indexes/tests.py62
3 files changed, 69 insertions, 0 deletions
diff --git a/tests/model_indexes/__init__.py b/tests/model_indexes/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/model_indexes/__init__.py
diff --git a/tests/model_indexes/models.py b/tests/model_indexes/models.py
new file mode 100644
index 0000000000..598a4cb808
--- /dev/null
+++ b/tests/model_indexes/models.py
@@ -0,0 +1,7 @@
+from django.db import models
+
+
+class Book(models.Model):
+ title = models.CharField(max_length=50)
+ author = models.CharField(max_length=50)
+ pages = models.IntegerField(db_column='page_count')
diff --git a/tests/model_indexes/tests.py b/tests/model_indexes/tests.py
new file mode 100644
index 0000000000..71bd8c91c0
--- /dev/null
+++ b/tests/model_indexes/tests.py
@@ -0,0 +1,62 @@
+from django.db import models
+from django.test import TestCase
+
+from .models import Book
+
+
+class IndexesTests(TestCase):
+
+ def test_repr(self):
+ index = models.Index(fields=['title'])
+ multi_col_index = models.Index(fields=['title', 'author'])
+ self.assertEqual(repr(index), "<Index: fields='title'>")
+ self.assertEqual(repr(multi_col_index), "<Index: fields='title, author'>")
+
+ def test_eq(self):
+ index = models.Index(fields=['title'])
+ same_index = models.Index(fields=['title'])
+ another_index = models.Index(fields=['title', 'author'])
+ self.assertEqual(index, same_index)
+ self.assertNotEqual(index, another_index)
+
+ def test_raises_error_without_field(self):
+ msg = 'At least one field is required to define an index.'
+ with self.assertRaisesMessage(ValueError, msg):
+ models.Index()
+
+ def test_max_name_length(self):
+ msg = 'Index names cannot be longer than 30 characters.'
+ with self.assertRaisesMessage(ValueError, msg):
+ models.Index(fields=['title'], name='looooooooooooong_index_name_idx')
+
+ def test_name_constraints(self):
+ msg = 'Index names cannot start with an underscore (_).'
+ with self.assertRaisesMessage(ValueError, msg):
+ models.Index(fields=['title'], name='_name_starting_with_underscore')
+
+ msg = 'Index names cannot start with a number (0-9).'
+ with self.assertRaisesMessage(ValueError, msg):
+ models.Index(fields=['title'], name='5name_starting_with_number')
+
+ def test_name_auto_generation(self):
+ index = models.Index(fields=['author'])
+ index.model = Book
+ self.assertEqual(index.name, 'model_index_author_0f5565_idx')
+
+ # fields may be truncated in the name. db_column is used for naming.
+ long_field_index = models.Index(fields=['pages'])
+ long_field_index.model = Book
+ self.assertEqual(long_field_index.name, 'model_index_page_co_69235a_idx')
+
+ # suffix can't be longer than 3 characters.
+ long_field_index.suffix = 'suff'
+ msg = 'Index too long for multiple database support. Is self.suffix longer than 3 characters?'
+ with self.assertRaisesMessage(AssertionError, msg):
+ long_field_index.get_name()
+
+ def test_deconstruction(self):
+ index = models.Index(fields=['title'])
+ path, args, kwargs = index.deconstruct()
+ self.assertEqual(path, 'django.db.models.Index')
+ self.assertEqual(args, ())
+ self.assertEqual(kwargs, {'fields': ['title']})