summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAndrew Godwin <andrew@aeracode.org>2014-06-17 17:45:38 -0700
committerAndrew Godwin <andrew@aeracode.org>2014-06-17 17:46:52 -0700
commitf355d253f81c78b595e25e84309b648c24208d73 (patch)
tree2b09d8db03cc3c8409ae89c4c98c0d6d3fadc398 /tests
parentf16554f440a9640dd619ac01278565e4e3e1e2c6 (diff)
[1.7.x] Fixed #22853: Swapped models are now ignored for migration operations.
Diffstat (limited to 'tests')
-rw-r--r--tests/migrations/test_operations.py117
1 files changed, 110 insertions, 7 deletions
diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py
index 0b82d56f05..48eedd193c 100644
--- a/tests/migrations/test_operations.py
+++ b/tests/migrations/test_operations.py
@@ -8,6 +8,7 @@ except ImportError:
sqlparse = None
from django import test
+from django.test import override_settings
from django.db import connection, migrations, models, router
from django.db.migrations.migration import Migration
from django.db.migrations.state import ProjectState
@@ -18,11 +19,9 @@ from django.db.utils import IntegrityError, DatabaseError
from .test_base import MigrationTestBase
-class OperationTests(MigrationTestBase):
+class OperationTestBase(MigrationTestBase):
"""
- Tests running the operations and making sure they do what they say they do.
- Each test looks at their state changing, and then their database operation -
- both forwards and backwards.
+ Common functions to help test operations.
"""
def apply_operations(self, app_label, project_state, operations):
@@ -37,6 +36,16 @@ class OperationTests(MigrationTestBase):
with connection.schema_editor() as editor:
return migration.unapply(project_state, editor)
+ def make_test_state(self, app_label, operation, **kwargs):
+ """
+ Makes a test state using set_up_test_model and returns the
+ original state and the state after the migration is applied.
+ """
+ project_state = self.set_up_test_model(app_label, **kwargs)
+ new_state = project_state.clone()
+ operation.state_forwards(app_label, new_state)
+ return project_state, new_state
+
def set_up_test_model(self, app_label, second_model=False, third_model=False, related_model=False, mti_model=False, proxy_model=False):
"""
Creates a test model state and database table.
@@ -74,6 +83,9 @@ class OperationTests(MigrationTestBase):
("pink", models.IntegerField(default=3)),
("weight", models.FloatField()),
],
+ options = {
+ "swappable": "TEST_SWAP_MODEL",
+ },
)]
if second_model:
operations.append(migrations.CreateModel(
@@ -122,6 +134,15 @@ class OperationTests(MigrationTestBase):
return self.apply_operations(app_label, ProjectState(), operations)
+
+class OperationTests(OperationTestBase):
+ """
+ Tests running the operations and making sure they do what they say they do.
+ Each test looks at their state changing, and then their database operation -
+ both forwards and backwards.
+ """
+
+
def test_create_model(self):
"""
Tests the CreateModel operation.
@@ -382,15 +403,13 @@ class OperationTests(MigrationTestBase):
"""
Tests the AddField operation.
"""
- project_state = self.set_up_test_model("test_adfl")
# Test the state alteration
operation = migrations.AddField(
"Pony",
"height",
models.FloatField(null=True, default=5),
)
- new_state = project_state.clone()
- operation.state_forwards("test_adfl", new_state)
+ project_state, new_state = self.make_test_state("test_adfl", operation)
self.assertEqual(len(new_state.models["test_adfl", "pony"].fields), 4)
field = [
f for n, f in new_state.models["test_adfl", "pony"].fields
@@ -1117,3 +1136,87 @@ class MultiDBOperationTests(MigrationTestBase):
with connection.schema_editor() as editor:
operation.database_backwards("test_crmo", editor, new_state, project_state)
self.assertTableNotExists("test_crmo_pony")
+
+
+class SwappableOperationTests(OperationTestBase):
+ """
+ Tests that key operations ignore swappable models
+ (we don't want to replicate all of them here, as the functionality
+ is in a common base class anyway)
+ """
+
+ available_apps = [
+ "migrations",
+ "django.contrib.auth",
+ ]
+
+ @override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
+ def test_create_ignore_swapped(self):
+ """
+ Tests that the CreateTable operation ignores swapped models.
+ """
+ operation = migrations.CreateModel(
+ "Pony",
+ [
+ ("id", models.AutoField(primary_key=True)),
+ ("pink", models.IntegerField(default=1)),
+ ],
+ options = {
+ "swappable": "TEST_SWAP_MODEL",
+ },
+ )
+ # Test the state alteration (it should still be there!)
+ project_state = ProjectState()
+ new_state = project_state.clone()
+ operation.state_forwards("test_crigsw", new_state)
+ self.assertEqual(new_state.models["test_crigsw", "pony"].name, "Pony")
+ self.assertEqual(len(new_state.models["test_crigsw", "pony"].fields), 2)
+ # Test the database alteration
+ self.assertTableNotExists("test_crigsw_pony")
+ with connection.schema_editor() as editor:
+ operation.database_forwards("test_crigsw", editor, project_state, new_state)
+ self.assertTableNotExists("test_crigsw_pony")
+ # And test reversal
+ with connection.schema_editor() as editor:
+ operation.database_backwards("test_crigsw", editor, new_state, project_state)
+ self.assertTableNotExists("test_crigsw_pony")
+
+ @override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
+ def test_delete_ignore_swapped(self):
+ """
+ Tests the DeleteModel operation ignores swapped models.
+ """
+ operation = migrations.DeleteModel("Pony")
+ project_state, new_state = self.make_test_state("test_dligsw", operation)
+ # Test the database alteration
+ self.assertTableNotExists("test_dligsw_pony")
+ with connection.schema_editor() as editor:
+ operation.database_forwards("test_dligsw", editor, project_state, new_state)
+ self.assertTableNotExists("test_dligsw_pony")
+ # And test reversal
+ with connection.schema_editor() as editor:
+ operation.database_backwards("test_dligsw", editor, new_state, project_state)
+ self.assertTableNotExists("test_dligsw_pony")
+
+ @override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
+ def test_add_field_ignore_swapped(self):
+ """
+ Tests the AddField operation.
+ """
+ # Test the state alteration
+ operation = migrations.AddField(
+ "Pony",
+ "height",
+ models.FloatField(null=True, default=5),
+ )
+ project_state, new_state = self.make_test_state("test_adfligsw", operation)
+ # Test the database alteration
+ self.assertTableNotExists("test_adfligsw_pont")
+ self.assertColumnNotExists("test_adfligsw_pony", "height")
+ with connection.schema_editor() as editor:
+ operation.database_forwards("test_adfligsw", editor, project_state, new_state)
+ self.assertColumnNotExists("test_adfligsw_pony", "height")
+ # And test reversal
+ with connection.schema_editor() as editor:
+ operation.database_backwards("test_adfligsw", editor, new_state, project_state)
+ self.assertColumnNotExists("test_adfligsw_pony", "height")