2013-06-07 22:28:38 +08:00
|
|
|
# encoding: utf8
|
|
|
|
import datetime
|
2013-06-08 00:56:43 +08:00
|
|
|
from django.utils import six
|
2013-06-19 22:36:02 +08:00
|
|
|
from django.test import TestCase
|
2013-06-07 22:28:38 +08:00
|
|
|
from django.db.migrations.writer import MigrationWriter
|
2013-06-07 22:36:31 +08:00
|
|
|
from django.db import models, migrations
|
2013-06-07 22:28:38 +08:00
|
|
|
|
|
|
|
|
2013-06-19 22:36:02 +08:00
|
|
|
class WriterTests(TestCase):
|
2013-06-07 22:28:38 +08:00
|
|
|
"""
|
|
|
|
Tests the migration writer (makes migration files from Migration instances)
|
|
|
|
"""
|
|
|
|
|
2013-06-08 00:56:43 +08:00
|
|
|
def safe_exec(self, string, value=None):
|
2013-06-07 22:28:38 +08:00
|
|
|
l = {}
|
|
|
|
try:
|
2013-06-08 00:56:43 +08:00
|
|
|
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))
|
2013-06-07 22:28:38 +08:00
|
|
|
return l
|
|
|
|
|
2013-06-08 00:56:43 +08:00
|
|
|
def serialize_round_trip(self, value):
|
2013-06-07 22:28:38 +08:00
|
|
|
string, imports = MigrationWriter.serialize(value)
|
2013-06-08 00:56:43 +08:00
|
|
|
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)
|
2013-06-07 22:28:38 +08:00
|
|
|
|
|
|
|
def assertSerializedIs(self, value):
|
2013-06-08 00:56:43 +08:00
|
|
|
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)
|
2013-06-07 22:28:38 +08:00
|
|
|
|
|
|
|
def test_serialize(self):
|
|
|
|
"""
|
|
|
|
Tests various different forms of the serializer.
|
|
|
|
This does not care about formatting, just that the parsed result is
|
|
|
|
correct, so we always exec() the result and check that.
|
|
|
|
"""
|
|
|
|
# Basic values
|
|
|
|
self.assertSerializedEqual(1)
|
|
|
|
self.assertSerializedEqual(None)
|
|
|
|
self.assertSerializedEqual("foobar")
|
|
|
|
self.assertSerializedEqual(u"föobár")
|
|
|
|
self.assertSerializedEqual({1: 2})
|
|
|
|
self.assertSerializedEqual(["a", 2, True, None])
|
|
|
|
self.assertSerializedEqual(set([2, 3, "eighty"]))
|
|
|
|
self.assertSerializedEqual({"lalalala": ["yeah", "no", "maybe"]})
|
|
|
|
# Datetime stuff
|
|
|
|
self.assertSerializedEqual(datetime.datetime.utcnow())
|
|
|
|
self.assertSerializedEqual(datetime.datetime.utcnow)
|
|
|
|
self.assertSerializedEqual(datetime.date.today())
|
|
|
|
self.assertSerializedEqual(datetime.date.today)
|
2013-06-08 00:56:43 +08:00
|
|
|
# Django fields
|
|
|
|
self.assertSerializedFieldEqual(models.CharField(max_length=255))
|
|
|
|
self.assertSerializedFieldEqual(models.TextField(null=True, blank=True))
|
2013-06-07 22:28:38 +08:00
|
|
|
|
|
|
|
def test_simple_migration(self):
|
|
|
|
"""
|
|
|
|
Tests serializing a simple migration.
|
|
|
|
"""
|
|
|
|
migration = type("Migration", (migrations.Migration,), {
|
|
|
|
"operations": [
|
|
|
|
migrations.DeleteModel("MyModel"),
|
2013-06-07 22:36:31 +08:00
|
|
|
migrations.AddField("OtherModel", "field_name", models.DateTimeField(default=datetime.datetime.utcnow))
|
2013-06-07 22:28:38 +08:00
|
|
|
],
|
|
|
|
"dependencies": [("testapp", "some_other_one")],
|
|
|
|
})
|
|
|
|
writer = MigrationWriter(migration)
|
|
|
|
output = writer.as_string()
|
2013-06-08 00:56:43 +08:00
|
|
|
# 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)
|