summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMarkus Holtermann <info@markusholtermann.eu>2014-09-08 02:26:12 +0200
committerTim Graham <timograham@gmail.com>2014-09-25 10:28:11 -0400
commit00c2a721e20f6487571ed14b69aed620f474104c (patch)
treecbab2dcaf41ff54fdbbfc80157681454d6112a0e /tests
parentb5d60927078743b68ee13795601c694a1d837e1c (diff)
[1.7.x] Fixed #23415 -- Added fields for unmanaged and proxy model migrations.
Thanks sky-chen for the report. Backport of 215aa4f53b from master
Diffstat (limited to 'tests')
-rw-r--r--tests/migrations/test_autodetector.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py
index 0ba05d3495..c58b41de30 100644
--- a/tests/migrations/test_autodetector.py
+++ b/tests/migrations/test_autodetector.py
@@ -31,6 +31,7 @@ class AutodetectorTests(TestCase):
author_name_default = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200, default='Ada Lovelace'))])
author_name_deconstructable_1 = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200, default=DeconstructableObject()))])
author_name_deconstructable_2 = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200, default=DeconstructableObject()))])
+ author_custom_pk = ModelState("testapp", "Author", [("pk_field", models.IntegerField(primary_key=True))])
author_with_book = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("book", models.ForeignKey("otherapp.Book"))])
author_with_book_order_wrt = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("book", models.ForeignKey("otherapp.Book"))], options={"order_with_respect_to": "book"})
author_renamed_with_book = ModelState("testapp", "Writer", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("book", models.ForeignKey("otherapp.Book"))])
@@ -44,6 +45,10 @@ class AutodetectorTests(TestCase):
author_proxy_proxy = ModelState("testapp", "AAuthorProxyProxy", [], {"proxy": True}, ("testapp.authorproxy", ))
author_unmanaged = ModelState("testapp", "AuthorUnmanaged", [], {"managed": False}, ("testapp.author", ))
author_unmanaged_managed = ModelState("testapp", "AuthorUnmanaged", [], {}, ("testapp.author", ))
+ author_unmanaged_default_pk = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True))])
+ author_unmanaged_custom_pk = ModelState("testapp", "Author", [
+ ("pk_field", models.IntegerField(primary_key=True)),
+ ])
author_with_m2m = ModelState("testapp", "Author", [
("id", models.AutoField(primary_key=True)),
("publishers", models.ManyToManyField("testapp.Publisher")),
@@ -644,6 +649,24 @@ class AutodetectorTests(TestCase):
self.assertOperationAttributes(changes, "testapp", 0, 0, name="AuthorProxy")
self.assertOperationAttributes(changes, "testapp", 0, 1, name="AuthorProxy", options={})
+ def test_proxy_custom_pk(self):
+ "#23415 - The autodetector must correctly deal with custom FK on proxy models."
+ # First, we test the default pk field name
+ before = self.make_project_state([])
+ after = self.make_project_state([self.author_empty, self.author_proxy_third, self.book_proxy_fk])
+ autodetector = MigrationAutodetector(before, after)
+ changes = autodetector._detect_changes()
+ # The field name the FK on the book model points to
+ self.assertEqual(changes['otherapp'][0].operations[0].fields[2][1].rel.field_name, 'id')
+
+ # Now, we test the custom pk field name
+ before = self.make_project_state([])
+ after = self.make_project_state([self.author_custom_pk, self.author_proxy_third, self.book_proxy_fk])
+ autodetector = MigrationAutodetector(before, after)
+ changes = autodetector._detect_changes()
+ # The field name the FK on the book model points to
+ self.assertEqual(changes['otherapp'][0].operations[0].fields[2][1].rel.field_name, 'pk_field')
+
def test_unmanaged(self):
"Tests that the autodetector correctly deals with managed models"
# First, we test adding an unmanaged model
@@ -667,6 +690,24 @@ class AutodetectorTests(TestCase):
self.assertOperationAttributes(changes, 'testapp', 0, 0, name="AuthorUnmanaged")
self.assertOperationAttributes(changes, 'testapp', 0, 1, name="AuthorUnmanaged")
+ def test_unmanaged_custom_pk(self):
+ "#23415 - The autodetector must correctly deal with custom FK on unmanaged models."
+ # First, we test the default pk field name
+ before = self.make_project_state([])
+ after = self.make_project_state([self.author_unmanaged_default_pk, self.book])
+ autodetector = MigrationAutodetector(before, after)
+ changes = autodetector._detect_changes()
+ # The field name the FK on the book model points to
+ self.assertEqual(changes['otherapp'][0].operations[0].fields[2][1].rel.field_name, 'id')
+
+ # Now, we test the custom pk field name
+ before = self.make_project_state([])
+ after = self.make_project_state([self.author_unmanaged_custom_pk, self.book])
+ autodetector = MigrationAutodetector(before, after)
+ changes = autodetector._detect_changes()
+ # The field name the FK on the book model points to
+ self.assertEqual(changes['otherapp'][0].operations[0].fields[2][1].rel.field_name, 'pk_field')
+
@override_settings(AUTH_USER_MODEL="thirdapp.CustomUser")
def test_swappable(self):
before = self.make_project_state([self.custom_user])