summaryrefslogtreecommitdiff
path: root/tests/migrations
diff options
context:
space:
mode:
authorAndrew Godwin <andrew@aeracode.org>2013-06-07 17:56:43 +0100
committerAndrew Godwin <andrew@aeracode.org>2013-06-07 17:56:43 +0100
commit91c470def50c4de420b0c6ee7debddc5bbd53ec8 (patch)
tree61889b1f70bec7e187fdfec4b06a045fc7207e05 /tests/migrations
parentcd809619a270ad48f1a77ee8c32bb0c7d8293f63 (diff)
Auto-naming for migrations and some writer fixes
Diffstat (limited to 'tests/migrations')
-rw-r--r--tests/migrations/test_autodetector.py33
-rw-r--r--tests/migrations/test_writer.py41
2 files changed, 59 insertions, 15 deletions
diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py
index 8e6a1e4160..1fc8f7aefb 100644
--- a/tests/migrations/test_autodetector.py
+++ b/tests/migrations/test_autodetector.py
@@ -2,6 +2,7 @@
from django.test import TransactionTestCase
from django.db.migrations.autodetector import MigrationAutodetector
from django.db.migrations.state import ProjectState, ModelState
+from django.db.migrations.graph import MigrationGraph
from django.db import models
@@ -11,6 +12,8 @@ class AutodetectorTests(TransactionTestCase):
"""
author_empty = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True))])
+ other_pony = ModelState("otherapp", "Pony", [("id", models.AutoField(primary_key=True))])
+ other_stable = ModelState("otherapp", "Stable", [("id", models.AutoField(primary_key=True))])
def make_project_state(self, model_states):
"Shortcut to make ProjectStates from lists of predefined models"
@@ -19,6 +22,28 @@ class AutodetectorTests(TransactionTestCase):
project_state.add_model_state(model_state)
return project_state
+ def test_arrange_for_graph(self):
+ "Tests auto-naming of migrations for graph matching."
+ # Make a fake graph
+ graph = MigrationGraph()
+ graph.add_node(("testapp", "0001_initial"), None)
+ graph.add_node(("testapp", "0002_foobar"), None)
+ graph.add_node(("otherapp", "0001_initial"), None)
+ graph.add_dependency(("testapp", "0002_foobar"), ("testapp", "0001_initial"))
+ graph.add_dependency(("testapp", "0002_foobar"), ("otherapp", "0001_initial"))
+ # Use project state to make a new migration change set
+ before = self.make_project_state([])
+ after = self.make_project_state([self.author_empty, self.other_pony, self.other_stable])
+ autodetector = MigrationAutodetector(before, after)
+ changes = autodetector.changes()
+ # Run through arrange_for_graph
+ changes = autodetector.arrange_for_graph(changes, graph)
+ # Make sure there's a new name, deps match, etc.
+ self.assertEqual(changes["testapp"][0].name, "0003_author")
+ self.assertEqual(changes["testapp"][0].dependencies, [("testapp", "0002_foobar")])
+ self.assertEqual(changes["otherapp"][0].name, "0002_pony_stable")
+ self.assertEqual(changes["otherapp"][0].dependencies, [("otherapp", "0001_initial")])
+
def test_new_model(self):
"Tests autodetection of new models"
# Make state
@@ -27,9 +52,9 @@ class AutodetectorTests(TransactionTestCase):
autodetector = MigrationAutodetector(before, after)
changes = autodetector.changes()
# Right number of migrations?
- self.assertEqual(len(changes), 1)
+ self.assertEqual(len(changes['testapp']), 1)
# Right number of actions?
- migration = changes.pop()
+ migration = changes['testapp'][0]
self.assertEqual(len(migration.operations), 1)
# Right action?
action = migration.operations[0]
@@ -44,9 +69,9 @@ class AutodetectorTests(TransactionTestCase):
autodetector = MigrationAutodetector(before, after)
changes = autodetector.changes()
# Right number of migrations?
- self.assertEqual(len(changes), 1)
+ self.assertEqual(len(changes['testapp']), 1)
# Right number of actions?
- migration = changes.pop()
+ migration = changes['testapp'][0]
self.assertEqual(len(migration.operations), 1)
# Right action?
action = migration.operations[0]
diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py
index 0581d6a4bd..c6ca100c1a 100644
--- a/tests/migrations/test_writer.py
+++ b/tests/migrations/test_writer.py
@@ -1,5 +1,6 @@
# encoding: utf8
import datetime
+from django.utils import six
from django.test import TransactionTestCase
from django.db.migrations.writer import MigrationWriter
from django.db import models, migrations
@@ -10,23 +11,33 @@ class WriterTests(TransactionTestCase):
Tests the migration writer (makes migration files from Migration instances)
"""
- def safe_exec(self, value, string):
+ def safe_exec(self, string, value=None):
l = {}
try:
- exec(string, {}, l)
- except:
- self.fail("Could not serialize %r: failed to exec %r" % (value, string.strip()))
+ exec(string, globals(), l)
+ except Exception as e:
+ if value:
+ self.fail("Could not exec %r (from value %r): %s" % (string.strip(), value, e))
+ else:
+ self.fail("Could not exec %r: %s" % (string.strip(), e))
return l
- def assertSerializedEqual(self, value):
+ def serialize_round_trip(self, value):
string, imports = MigrationWriter.serialize(value)
- new_value = self.safe_exec(value, "%s\ntest_value_result = %s" % ("\n".join(imports), string))['test_value_result']
- self.assertEqual(new_value, value)
+ return self.safe_exec("%s\ntest_value_result = %s" % ("\n".join(imports), string), value)['test_value_result']
+
+ def assertSerializedEqual(self, value):
+ self.assertEqual(self.serialize_round_trip(value), value)
def assertSerializedIs(self, value):
- string, imports = MigrationWriter.serialize(value)
- new_value = self.safe_exec(value, "%s\ntest_value_result = %s" % ("\n".join(imports), string))['test_value_result']
- self.assertIs(new_value, value)
+ self.assertIs(self.serialize_round_trip(value), value)
+
+ def assertSerializedFieldEqual(self, value):
+ new_value = self.serialize_round_trip(value)
+ self.assertEqual(value.__class__, new_value.__class__)
+ self.assertEqual(value.max_length, new_value.max_length)
+ self.assertEqual(value.null, new_value.null)
+ self.assertEqual(value.unique, new_value.unique)
def test_serialize(self):
"""
@@ -48,6 +59,9 @@ class WriterTests(TransactionTestCase):
self.assertSerializedEqual(datetime.datetime.utcnow)
self.assertSerializedEqual(datetime.date.today())
self.assertSerializedEqual(datetime.date.today)
+ # Django fields
+ self.assertSerializedFieldEqual(models.CharField(max_length=255))
+ self.assertSerializedFieldEqual(models.TextField(null=True, blank=True))
def test_simple_migration(self):
"""
@@ -62,4 +76,9 @@ class WriterTests(TransactionTestCase):
})
writer = MigrationWriter(migration)
output = writer.as_string()
- print output
+ # It should NOT be unicode.
+ self.assertIsInstance(output, six.binary_type, "Migration as_string returned unicode")
+ # We don't test the output formatting - that's too fragile.
+ # Just make sure it runs for now, and that things look alright.
+ result = self.safe_exec(output)
+ self.assertIn("Migration", result)