summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAndrew Godwin <andrew@aeracode.org>2014-03-04 12:33:51 -0800
committerAndrew Godwin <andrew@aeracode.org>2014-03-04 12:33:51 -0800
commit8fcc0140d075688e7b0565dea225f4380fc638c4 (patch)
tree40de7653b1fa36deb51d44d937376b2eb0984478 /tests
parent6fe22b30e007b7ac49eae48a53e7ba0a4ee79a4b (diff)
parent6436f1fad9ce51f18735106ac75aeea3d6d1f310 (diff)
Merge pull request #2396 from loic/ticket21893
Fixed #21893 -- ModelState didn't account for MTI parents inherited from abstract models.
Diffstat (limited to 'tests')
-rw-r--r--tests/migrations/test_operations.py53
-rw-r--r--tests/migrations/test_state.py15
2 files changed, 65 insertions, 3 deletions
diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py
index 51c95afd96..debfc760b5 100644
--- a/tests/migrations/test_operations.py
+++ b/tests/migrations/test_operations.py
@@ -14,7 +14,7 @@ class OperationTests(MigrationTestBase):
both forwards and backwards.
"""
- def set_up_test_model(self, app_label, second_model=False, related_model=False):
+ def set_up_test_model(self, app_label, second_model=False, related_model=False, mti_model=False):
"""
Creates a test model state and database table.
"""
@@ -38,7 +38,12 @@ class OperationTests(MigrationTestBase):
],
)]
if second_model:
- operations.append(migrations.CreateModel("Stable", [("id", models.AutoField(primary_key=True))]))
+ operations.append(migrations.CreateModel(
+ "Stable",
+ [
+ ("id", models.AutoField(primary_key=True)),
+ ]
+ ))
if related_model:
operations.append(migrations.CreateModel(
"Rider",
@@ -47,6 +52,21 @@ class OperationTests(MigrationTestBase):
("pony", models.ForeignKey("Pony")),
],
))
+ if mti_model:
+ operations.append(migrations.CreateModel(
+ "ShetlandPony",
+ fields=[
+ ('pony_ptr', models.OneToOneField(
+ auto_created=True,
+ primary_key=True,
+ to_field='id',
+ serialize=False,
+ to='Pony',
+ )),
+ ("cuteness", models.IntegerField(default=1)),
+ ],
+ bases=['%s.Pony' % app_label],
+ ))
project_state = ProjectState()
for operation in operations:
operation.state_forwards(app_label, project_state)
@@ -495,7 +515,7 @@ class OperationTests(MigrationTestBase):
Tests the RunPython operation
"""
- project_state = self.set_up_test_model("test_runpython")
+ project_state = self.set_up_test_model("test_runpython", mti_model=True)
# Create the operation
def inner_method(models, schema_editor):
@@ -533,7 +553,34 @@ class OperationTests(MigrationTestBase):
no_reverse_operation.database_forwards("test_runpython", editor, project_state, new_state)
with self.assertRaises(NotImplementedError):
no_reverse_operation.database_backwards("test_runpython", editor, new_state, project_state)
+ self.assertEqual(project_state.render().get_model("test_runpython", "Pony").objects.count(), 2)
+ def create_ponies(models, schema_editor):
+ Pony = models.get_model("test_runpython", "Pony")
+ pony1 = Pony.objects.create(pink=1, weight=3.55)
+ self.assertIsNot(pony1.pk, None)
+ pony2 = Pony.objects.create(weight=5)
+ self.assertIsNot(pony2.pk, None)
+ self.assertNotEqual(pony1.pk, pony2.pk)
+
+ operation = migrations.RunPython(create_ponies)
+ with connection.schema_editor() as editor:
+ operation.database_forwards("test_runpython", editor, project_state, new_state)
+ self.assertEqual(project_state.render().get_model("test_runpython", "Pony").objects.count(), 4)
+
+ def create_shetlandponies(models, schema_editor):
+ ShetlandPony = models.get_model("test_runpython", "ShetlandPony")
+ pony1 = ShetlandPony.objects.create(weight=4.0)
+ self.assertIsNot(pony1.pk, None)
+ pony2 = ShetlandPony.objects.create(weight=5.0)
+ self.assertIsNot(pony2.pk, None)
+ self.assertNotEqual(pony1.pk, pony2.pk)
+
+ operation = migrations.RunPython(create_shetlandponies)
+ with connection.schema_editor() as editor:
+ operation.database_forwards("test_runpython", editor, project_state, new_state)
+ self.assertEqual(project_state.render().get_model("test_runpython", "Pony").objects.count(), 6)
+ self.assertEqual(project_state.render().get_model("test_runpython", "ShetlandPony").objects.count(), 2)
class MigrateNothingRouter(object):
"""
diff --git a/tests/migrations/test_state.py b/tests/migrations/test_state.py
index 187b06b94a..10f5e7d9ab 100644
--- a/tests/migrations/test_state.py
+++ b/tests/migrations/test_state.py
@@ -166,6 +166,16 @@ class StateTests(TestCase):
app_label = "migrations"
apps = Apps()
+ class AbstractSubFooBar(FooBar):
+ class Meta:
+ abstract = True
+ apps = Apps()
+
+ class SubFooBar(AbstractSubFooBar):
+ class Meta:
+ app_label = "migrations"
+ apps = Apps()
+
apps = Apps(["migrations"])
# We shouldn't be able to render yet
@@ -175,8 +185,13 @@ class StateTests(TestCase):
# Once the parent models are in the app registry, it should be fine
ModelState.from_model(Foo).render(apps)
+ self.assertSequenceEqual(ModelState.from_model(Foo).bases, [models.Model])
ModelState.from_model(Bar).render(apps)
+ self.assertSequenceEqual(ModelState.from_model(Bar).bases, [models.Model])
ModelState.from_model(FooBar).render(apps)
+ self.assertSequenceEqual(ModelState.from_model(FooBar).bases, ['migrations.foo', 'migrations.bar'])
+ ModelState.from_model(SubFooBar).render(apps)
+ self.assertSequenceEqual(ModelState.from_model(SubFooBar).bases, ['migrations.foobar'])
def test_render_project_dependencies(self):
"""