summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAlexander Sosnovskiy <alecs.box@gmail.com>2015-07-02 11:43:15 +0300
committerTim Graham <timograham@gmail.com>2015-12-25 20:01:31 -0500
commit2a7ce34600d0f879e93c9a5e02215948ed3bb6ac (patch)
treed1a4770772b452513e1d64c090dc15ae287afe70 /tests
parenta1d0c60fa05cbad2e5a25ec37e0afaf1b84c9302 (diff)
Fixed #14286 -- Added models.BigAutoField.
Diffstat (limited to 'tests')
-rw-r--r--tests/introspection/models.py18
-rw-r--r--tests/introspection/tests.py8
-rw-r--r--tests/many_to_many/models.py10
-rw-r--r--tests/many_to_one/models.py18
-rw-r--r--tests/many_to_one/tests.py13
-rw-r--r--tests/migrations/test_operations.py138
6 files changed, 202 insertions, 3 deletions
diff --git a/tests/introspection/models.py b/tests/introspection/models.py
index 9ad52b8f8d..4d961b20be 100644
--- a/tests/introspection/models.py
+++ b/tests/introspection/models.py
@@ -5,6 +5,24 @@ from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
+class City(models.Model):
+ id = models.BigAutoField(primary_key=True)
+ name = models.CharField(max_length=50)
+
+ def __str__(self):
+ return self.name
+
+
+@python_2_unicode_compatible
+class District(models.Model):
+ city = models.ForeignKey(City, models.CASCADE)
+ name = models.CharField(max_length=50)
+
+ def __str__(self):
+ return self.name
+
+
+@python_2_unicode_compatible
class Reporter(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
diff --git a/tests/introspection/tests.py b/tests/introspection/tests.py
index 3cd00060db..9ce5a3f0f6 100644
--- a/tests/introspection/tests.py
+++ b/tests/introspection/tests.py
@@ -6,7 +6,7 @@ from django.db import connection
from django.db.utils import DatabaseError
from django.test import TransactionTestCase, mock, skipUnlessDBFeature
-from .models import Article, Reporter
+from .models import Article, City, Reporter
class IntrospectionTests(TransactionTestCase):
@@ -103,6 +103,12 @@ class IntrospectionTests(TransactionTestCase):
[False, nullable_by_backend, nullable_by_backend, nullable_by_backend, True, True, False]
)
+ @skipUnlessDBFeature('can_introspect_autofield')
+ def test_bigautofield(self):
+ with connection.cursor() as cursor:
+ desc = connection.introspection.get_table_description(cursor, City._meta.db_table)
+ self.assertIn('BigAutoField', [datatype(r[1], r) for r in desc])
+
# Regression test for #9991 - 'real' types in postgres
@skipUnlessDBFeature('has_real_datatype')
def test_postgresql_real_type(self):
diff --git a/tests/many_to_many/models.py b/tests/many_to_many/models.py
index 5688c853d6..3e4cdd2e70 100644
--- a/tests/many_to_many/models.py
+++ b/tests/many_to_many/models.py
@@ -24,11 +24,21 @@ class Publication(models.Model):
@python_2_unicode_compatible
+class Tag(models.Model):
+ id = models.BigAutoField(primary_key=True)
+ name = models.CharField(max_length=50)
+
+ def __str__(self):
+ return self.name
+
+
+@python_2_unicode_compatible
class Article(models.Model):
headline = models.CharField(max_length=100)
# Assign a unicode string as name to make sure the intermediary model is
# correctly created. Refs #20207
publications = models.ManyToManyField(Publication, name='publications')
+ tags = models.ManyToManyField(Tag, related_name='tags')
def __str__(self):
return self.headline
diff --git a/tests/many_to_one/models.py b/tests/many_to_one/models.py
index 57fc7a617b..05491e99f1 100644
--- a/tests/many_to_one/models.py
+++ b/tests/many_to_one/models.py
@@ -32,6 +32,24 @@ class Article(models.Model):
ordering = ('headline',)
+@python_2_unicode_compatible
+class City(models.Model):
+ id = models.BigAutoField(primary_key=True)
+ name = models.CharField(max_length=50)
+
+ def __str__(self):
+ return self.name
+
+
+@python_2_unicode_compatible
+class District(models.Model):
+ city = models.ForeignKey(City, models.CASCADE)
+ name = models.CharField(max_length=50)
+
+ def __str__(self):
+ return self.name
+
+
# If ticket #1578 ever slips back in, these models will not be able to be
# created (the field names being lower-cased versions of their opposite
# classes is important here).
diff --git a/tests/many_to_one/tests.py b/tests/many_to_one/tests.py
index 69bba0993d..79caec7177 100644
--- a/tests/many_to_one/tests.py
+++ b/tests/many_to_one/tests.py
@@ -9,8 +9,8 @@ from django.utils.deprecation import RemovedInDjango20Warning
from django.utils.translation import ugettext_lazy
from .models import (
- Article, Category, Child, First, Parent, Record, Relation, Reporter,
- School, Student, Third, ToFieldChild,
+ Article, Category, Child, City, District, First, Parent, Record, Relation,
+ Reporter, School, Student, Third, ToFieldChild,
)
@@ -569,6 +569,15 @@ class ManyToOneTests(TestCase):
self.assertIsNot(c.parent, p)
self.assertEqual(c.parent, p)
+ def test_fk_to_bigautofield(self):
+ ch = City.objects.create(name='Chicago')
+ District.objects.create(city=ch, name='Far South')
+ District.objects.create(city=ch, name='North')
+
+ ny = City.objects.create(name='New York', id=2 ** 33)
+ District.objects.create(city=ny, name='Brooklyn')
+ District.objects.create(city=ny, name='Manhattan')
+
def test_multiple_foreignkeys(self):
# Test of multiple ForeignKeys to the same model (bug #7125).
c1 = Category.objects.create(name='First')
diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py
index b624a73392..de32353aa4 100644
--- a/tests/migrations/test_operations.py
+++ b/tests/migrations/test_operations.py
@@ -1806,6 +1806,144 @@ class OperationTests(OperationTestBase):
create_old_man.state_forwards("test_books", new_state)
create_old_man.database_forwards("test_books", editor, project_state, new_state)
+ def test_model_with_bigautofield(self):
+ """
+ A model with BigAutoField can be created.
+ """
+ def create_data(models, schema_editor):
+ Author = models.get_model("test_author", "Author")
+ Book = models.get_model("test_book", "Book")
+ author1 = Author.objects.create(name="Hemingway")
+ Book.objects.create(title="Old Man and The Sea", author=author1)
+ Book.objects.create(id=2 ** 33, title="A farewell to arms", author=author1)
+
+ author2 = Author.objects.create(id=2 ** 33, name="Remarque")
+ Book.objects.create(title="All quiet on the western front", author=author2)
+ Book.objects.create(title="Arc de Triomphe", author=author2)
+
+ create_author = migrations.CreateModel(
+ "Author",
+ [
+ ("id", models.BigAutoField(primary_key=True)),
+ ("name", models.CharField(max_length=100)),
+ ],
+ options={},
+ )
+ create_book = migrations.CreateModel(
+ "Book",
+ [
+ ("id", models.BigAutoField(primary_key=True)),
+ ("title", models.CharField(max_length=100)),
+ ("author", models.ForeignKey(to="test_author.Author", on_delete=models.CASCADE))
+ ],
+ options={},
+ )
+ fill_data = migrations.RunPython(create_data)
+
+ project_state = ProjectState()
+ new_state = project_state.clone()
+ with connection.schema_editor() as editor:
+ create_author.state_forwards("test_author", new_state)
+ create_author.database_forwards("test_author", editor, project_state, new_state)
+
+ project_state = new_state
+ new_state = new_state.clone()
+ with connection.schema_editor() as editor:
+ create_book.state_forwards("test_book", new_state)
+ create_book.database_forwards("test_book", editor, project_state, new_state)
+
+ project_state = new_state
+ new_state = new_state.clone()
+ with connection.schema_editor() as editor:
+ fill_data.state_forwards("fill_data", new_state)
+ fill_data.database_forwards("fill_data", editor, project_state, new_state)
+
+ def test_autofield_foreignfield_growth(self):
+ """
+ A field may be migrated from AutoField to BigAutoField.
+ """
+ def create_initial_data(models, schema_editor):
+ Article = models.get_model("test_article", "Article")
+ Blog = models.get_model("test_blog", "Blog")
+ blog = Blog.objects.create(name="web development done right")
+ Article.objects.create(name="Frameworks", blog=blog)
+ Article.objects.create(name="Programming Languages", blog=blog)
+
+ def create_big_data(models, schema_editor):
+ Article = models.get_model("test_article", "Article")
+ Blog = models.get_model("test_blog", "Blog")
+ blog2 = Blog.objects.create(name="Frameworks", id=2 ** 33)
+ Article.objects.create(name="Django", blog=blog2)
+ Article.objects.create(id=2 ** 33, name="Django2", blog=blog2)
+
+ create_blog = migrations.CreateModel(
+ "Blog",
+ [
+ ("id", models.AutoField(primary_key=True)),
+ ("name", models.CharField(max_length=100)),
+ ],
+ options={},
+ )
+ create_article = migrations.CreateModel(
+ "Article",
+ [
+ ("id", models.AutoField(primary_key=True)),
+ ("blog", models.ForeignKey(to="test_blog.Blog", on_delete=models.CASCADE)),
+ ("name", models.CharField(max_length=100)),
+ ("data", models.TextField(default="")),
+ ],
+ options={},
+ )
+ fill_initial_data = migrations.RunPython(create_initial_data, create_initial_data)
+ fill_big_data = migrations.RunPython(create_big_data, create_big_data)
+
+ grow_article_id = migrations.AlterField("Article", "id", models.BigAutoField(primary_key=True))
+ grow_blog_id = migrations.AlterField("Blog", "id", models.BigAutoField(primary_key=True))
+
+ project_state = ProjectState()
+ new_state = project_state.clone()
+ with connection.schema_editor() as editor:
+ create_blog.state_forwards("test_blog", new_state)
+ create_blog.database_forwards("test_blog", editor, project_state, new_state)
+
+ project_state = new_state
+ new_state = new_state.clone()
+ with connection.schema_editor() as editor:
+ create_article.state_forwards("test_article", new_state)
+ create_article.database_forwards("test_article", editor, project_state, new_state)
+
+ project_state = new_state
+ new_state = new_state.clone()
+ with connection.schema_editor() as editor:
+ fill_initial_data.state_forwards("fill_initial_data", new_state)
+ fill_initial_data.database_forwards("fill_initial_data", editor, project_state, new_state)
+
+ project_state = new_state
+ new_state = new_state.clone()
+ with connection.schema_editor() as editor:
+ grow_article_id.state_forwards("test_article", new_state)
+ grow_article_id.database_forwards("test_article", editor, project_state, new_state)
+
+ state = new_state.clone()
+ article = state.apps.get_model("test_article.Article")
+ self.assertIsInstance(article._meta.pk, models.BigAutoField)
+
+ project_state = new_state
+ new_state = new_state.clone()
+ with connection.schema_editor() as editor:
+ grow_blog_id.state_forwards("test_blog", new_state)
+ grow_blog_id.database_forwards("test_blog", editor, project_state, new_state)
+
+ state = new_state.clone()
+ blog = state.apps.get_model("test_blog.Blog")
+ self.assertTrue(isinstance(blog._meta.pk, models.BigAutoField))
+
+ project_state = new_state
+ new_state = new_state.clone()
+ with connection.schema_editor() as editor:
+ fill_big_data.state_forwards("fill_big_data", new_state)
+ fill_big_data.database_forwards("fill_big_data", editor, project_state, new_state)
+
def test_run_python_noop(self):
"""
#24098 - Tests no-op RunPython operations.