2014-05-15 19:41:55 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-08-23 13:56:37 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-06-07 16:28:38 +02:00
|
|
|
import datetime
|
2014-11-06 12:29:43 +01:00
|
|
|
import math
|
2013-10-19 02:24:38 +02:00
|
|
|
import os
|
2014-07-04 18:48:13 +02:00
|
|
|
import re
|
2014-03-31 21:25:08 +02:00
|
|
|
import tokenize
|
2014-06-08 02:04:58 +02:00
|
|
|
import unittest
|
2013-08-23 13:56:37 +02:00
|
|
|
|
2015-01-28 13:35:27 +01:00
|
|
|
import custom_migration_operations.more_operations
|
|
|
|
import custom_migration_operations.operations
|
|
|
|
|
2014-01-15 15:20:47 +01:00
|
|
|
from django.conf import settings
|
2015-01-28 13:35:27 +01:00
|
|
|
from django.core.validators import EmailValidator, RegexValidator
|
|
|
|
from django.db import migrations, models
|
|
|
|
from django.db.migrations.writer import (
|
|
|
|
MigrationWriter, OperationWriter, SettingsReference,
|
|
|
|
)
|
|
|
|
from django.test import SimpleTestCase, TestCase, ignore_warnings
|
2014-03-31 22:16:34 +02:00
|
|
|
from django.utils import datetime_safe, six
|
2015-02-15 01:47:07 +01:00
|
|
|
from django.utils._os import upath
|
2013-10-21 19:33:57 +02:00
|
|
|
from django.utils.deconstruct import deconstructible
|
2015-01-28 13:35:27 +01:00
|
|
|
from django.utils.timezone import FixedOffset, get_default_timezone, utc
|
2013-08-31 21:11:37 +02:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2014-06-11 15:00:52 +02:00
|
|
|
|
2015-01-28 13:35:27 +01:00
|
|
|
from .models import FoodManager, FoodQuerySet
|
2014-12-15 22:42:17 +01:00
|
|
|
|
2013-06-07 16:28:38 +02:00
|
|
|
|
2014-06-08 02:04:58 +02:00
|
|
|
class TestModel1(object):
|
|
|
|
def upload_to(self):
|
|
|
|
return "somewhere dynamic"
|
|
|
|
thing = models.FileField(upload_to=upload_to)
|
|
|
|
|
|
|
|
|
2015-01-07 21:10:56 +01:00
|
|
|
class OperationWriterTests(SimpleTestCase):
|
|
|
|
|
|
|
|
def test_empty_signature(self):
|
|
|
|
operation = custom_migration_operations.operations.TestOperation()
|
|
|
|
writer = OperationWriter(operation)
|
|
|
|
writer.indentation = 0
|
|
|
|
buff, imports = writer.serialize()
|
|
|
|
self.assertEqual(imports, {'import custom_migration_operations.operations'})
|
|
|
|
self.assertEqual(
|
|
|
|
buff,
|
|
|
|
'custom_migration_operations.operations.TestOperation(\n'
|
|
|
|
'),'
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_args_signature(self):
|
|
|
|
operation = custom_migration_operations.operations.ArgsOperation(1, 2)
|
|
|
|
writer = OperationWriter(operation)
|
|
|
|
writer.indentation = 0
|
|
|
|
buff, imports = writer.serialize()
|
|
|
|
self.assertEqual(imports, {'import custom_migration_operations.operations'})
|
|
|
|
self.assertEqual(
|
|
|
|
buff,
|
|
|
|
'custom_migration_operations.operations.ArgsOperation(\n'
|
|
|
|
' arg1=1,\n'
|
|
|
|
' arg2=2,\n'
|
|
|
|
'),'
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_kwargs_signature(self):
|
|
|
|
operation = custom_migration_operations.operations.KwargsOperation(kwarg1=1)
|
|
|
|
writer = OperationWriter(operation)
|
|
|
|
writer.indentation = 0
|
|
|
|
buff, imports = writer.serialize()
|
|
|
|
self.assertEqual(imports, {'import custom_migration_operations.operations'})
|
|
|
|
self.assertEqual(
|
|
|
|
buff,
|
|
|
|
'custom_migration_operations.operations.KwargsOperation(\n'
|
|
|
|
' kwarg1=1,\n'
|
|
|
|
'),'
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_args_kwargs_signature(self):
|
|
|
|
operation = custom_migration_operations.operations.ArgsKwargsOperation(1, 2, kwarg2=4)
|
|
|
|
writer = OperationWriter(operation)
|
|
|
|
writer.indentation = 0
|
|
|
|
buff, imports = writer.serialize()
|
|
|
|
self.assertEqual(imports, {'import custom_migration_operations.operations'})
|
|
|
|
self.assertEqual(
|
|
|
|
buff,
|
|
|
|
'custom_migration_operations.operations.ArgsKwargsOperation(\n'
|
|
|
|
' arg1=1,\n'
|
|
|
|
' arg2=2,\n'
|
|
|
|
' kwarg2=4,\n'
|
|
|
|
'),'
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_expand_args_signature(self):
|
|
|
|
operation = custom_migration_operations.operations.ExpandArgsOperation([1, 2])
|
|
|
|
writer = OperationWriter(operation)
|
|
|
|
writer.indentation = 0
|
|
|
|
buff, imports = writer.serialize()
|
|
|
|
self.assertEqual(imports, {'import custom_migration_operations.operations'})
|
|
|
|
self.assertEqual(
|
|
|
|
buff,
|
|
|
|
'custom_migration_operations.operations.ExpandArgsOperation(\n'
|
|
|
|
' arg=[\n'
|
|
|
|
' 1,\n'
|
|
|
|
' 2,\n'
|
|
|
|
' ],\n'
|
|
|
|
'),'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2013-06-19 16:36:02 +02:00
|
|
|
class WriterTests(TestCase):
|
2013-06-07 16:28:38 +02:00
|
|
|
"""
|
|
|
|
Tests the migration writer (makes migration files from Migration instances)
|
|
|
|
"""
|
|
|
|
|
2013-06-07 18:56:43 +02:00
|
|
|
def safe_exec(self, string, value=None):
|
2013-06-07 16:28:38 +02:00
|
|
|
l = {}
|
|
|
|
try:
|
2013-06-07 18:56:43 +02: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 16:28:38 +02:00
|
|
|
return l
|
|
|
|
|
2013-06-07 18:56:43 +02:00
|
|
|
def serialize_round_trip(self, value):
|
2013-06-07 16:28:38 +02:00
|
|
|
string, imports = MigrationWriter.serialize(value)
|
2013-06-07 18:56:43 +02: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 16:28:38 +02:00
|
|
|
|
2014-01-15 15:20:47 +01:00
|
|
|
def assertSerializedResultEqual(self, value, target):
|
|
|
|
self.assertEqual(MigrationWriter.serialize(value), target)
|
2013-06-07 18:56:43 +02:00
|
|
|
|
|
|
|
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 16:28:38 +02:00
|
|
|
|
2015-01-17 00:51:31 +01:00
|
|
|
def test_serialize_numbers(self):
|
2013-06-07 16:28:38 +02:00
|
|
|
self.assertSerializedEqual(1)
|
2014-11-06 12:29:43 +01:00
|
|
|
self.assertSerializedEqual(1.2)
|
|
|
|
self.assertTrue(math.isinf(self.serialize_round_trip(float("inf"))))
|
|
|
|
self.assertTrue(math.isinf(self.serialize_round_trip(float("-inf"))))
|
|
|
|
self.assertTrue(math.isnan(self.serialize_round_trip(float("nan"))))
|
2015-01-17 00:51:31 +01:00
|
|
|
|
|
|
|
def test_serialize_constants(self):
|
2013-06-07 16:28:38 +02:00
|
|
|
self.assertSerializedEqual(None)
|
2015-01-17 00:51:31 +01:00
|
|
|
self.assertSerializedEqual(True)
|
|
|
|
self.assertSerializedEqual(False)
|
|
|
|
|
|
|
|
def test_serialize_strings(self):
|
2013-08-23 13:56:37 +02:00
|
|
|
self.assertSerializedEqual(b"foobar")
|
2014-03-31 21:25:08 +02:00
|
|
|
string, imports = MigrationWriter.serialize(b"foobar")
|
|
|
|
self.assertEqual(string, "b'foobar'")
|
2013-08-23 13:56:37 +02:00
|
|
|
self.assertSerializedEqual("föobár")
|
2014-03-31 21:25:08 +02:00
|
|
|
string, imports = MigrationWriter.serialize("foobar")
|
|
|
|
self.assertEqual(string, "'foobar'")
|
2015-01-17 00:51:31 +01:00
|
|
|
|
|
|
|
def test_serialize_collections(self):
|
2013-06-07 16:28:38 +02:00
|
|
|
self.assertSerializedEqual({1: 2})
|
|
|
|
self.assertSerializedEqual(["a", 2, True, None])
|
2014-09-26 14:31:50 +02:00
|
|
|
self.assertSerializedEqual({2, 3, "eighty"})
|
2013-06-07 16:28:38 +02:00
|
|
|
self.assertSerializedEqual({"lalalala": ["yeah", "no", "maybe"]})
|
2013-08-31 21:11:37 +02:00
|
|
|
self.assertSerializedEqual(_('Hello'))
|
2015-01-17 00:51:31 +01:00
|
|
|
|
|
|
|
def test_serialize_builtin_types(self):
|
|
|
|
self.assertSerializedEqual([list, tuple, dict, set, frozenset])
|
|
|
|
self.assertSerializedResultEqual(
|
|
|
|
[list, tuple, dict, set, frozenset],
|
|
|
|
("[list, tuple, dict, set, frozenset]", set())
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_serialize_functions(self):
|
2013-09-05 05:36:31 +02:00
|
|
|
with six.assertRaisesRegex(self, ValueError, 'Cannot serialize function: lambda'):
|
|
|
|
self.assertSerializedEqual(lambda x: 42)
|
2013-09-02 08:02:07 +02:00
|
|
|
self.assertSerializedEqual(models.SET_NULL)
|
|
|
|
string, imports = MigrationWriter.serialize(models.SET(42))
|
|
|
|
self.assertEqual(string, 'models.SET(42)')
|
|
|
|
self.serialize_round_trip(models.SET(42))
|
2015-01-17 00:51:31 +01:00
|
|
|
|
|
|
|
def test_serialize_datetime(self):
|
2013-06-07 16:28:38 +02:00
|
|
|
self.assertSerializedEqual(datetime.datetime.utcnow())
|
|
|
|
self.assertSerializedEqual(datetime.datetime.utcnow)
|
2013-10-18 19:14:01 +02:00
|
|
|
self.assertSerializedEqual(datetime.datetime.today())
|
|
|
|
self.assertSerializedEqual(datetime.datetime.today)
|
2013-06-07 16:28:38 +02:00
|
|
|
self.assertSerializedEqual(datetime.date.today())
|
|
|
|
self.assertSerializedEqual(datetime.date.today)
|
2014-08-19 15:23:29 +02:00
|
|
|
self.assertSerializedEqual(datetime.datetime.now().time())
|
2014-09-06 22:42:36 +02:00
|
|
|
self.assertSerializedEqual(datetime.datetime(2014, 1, 1, 1, 1, tzinfo=get_default_timezone()))
|
2015-01-17 00:51:31 +01:00
|
|
|
self.assertSerializedEqual(datetime.datetime(2013, 12, 31, 22, 1, tzinfo=FixedOffset(180)))
|
|
|
|
self.assertSerializedResultEqual(
|
|
|
|
datetime.datetime(2014, 1, 1, 1, 1),
|
|
|
|
("datetime.datetime(2014, 1, 1, 1, 1)", {'import datetime'})
|
|
|
|
)
|
|
|
|
self.assertSerializedResultEqual(
|
|
|
|
datetime.datetime(2012, 1, 1, 1, 1, tzinfo=utc),
|
|
|
|
(
|
|
|
|
"datetime.datetime(2012, 1, 1, 1, 1, tzinfo=utc)",
|
|
|
|
{'import datetime', 'from django.utils.timezone import utc'},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_serialize_datetime_safe(self):
|
|
|
|
self.assertSerializedResultEqual(
|
|
|
|
datetime_safe.date(2014, 3, 31),
|
|
|
|
("datetime.date(2014, 3, 31)", {'import datetime'})
|
|
|
|
)
|
|
|
|
self.assertSerializedResultEqual(
|
|
|
|
datetime_safe.time(10, 25),
|
|
|
|
("datetime.time(10, 25)", {'import datetime'})
|
|
|
|
)
|
|
|
|
self.assertSerializedResultEqual(
|
|
|
|
datetime_safe.datetime(2014, 3, 31, 16, 4, 31),
|
|
|
|
("datetime.datetime(2014, 3, 31, 16, 4, 31)", {'import datetime'})
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_serialize_fields(self):
|
2013-06-07 18:56:43 +02:00
|
|
|
self.assertSerializedFieldEqual(models.CharField(max_length=255))
|
2015-01-17 00:55:41 +01:00
|
|
|
self.assertSerializedResultEqual(
|
|
|
|
models.CharField(max_length=255),
|
|
|
|
("models.CharField(max_length=255)", {"from django.db import models"})
|
|
|
|
)
|
2013-06-07 18:56:43 +02:00
|
|
|
self.assertSerializedFieldEqual(models.TextField(null=True, blank=True))
|
2015-01-17 00:55:41 +01:00
|
|
|
self.assertSerializedResultEqual(
|
|
|
|
models.TextField(null=True, blank=True),
|
|
|
|
("models.TextField(blank=True, null=True)", {'from django.db import models'})
|
|
|
|
)
|
2015-01-17 00:51:31 +01:00
|
|
|
|
|
|
|
def test_serialize_settings(self):
|
2014-01-15 15:20:47 +01:00
|
|
|
self.assertSerializedEqual(SettingsReference(settings.AUTH_USER_MODEL, "AUTH_USER_MODEL"))
|
|
|
|
self.assertSerializedResultEqual(
|
|
|
|
SettingsReference("someapp.model", "AUTH_USER_MODEL"),
|
2015-01-17 00:51:31 +01:00
|
|
|
("settings.AUTH_USER_MODEL", {"from django.conf import settings"})
|
2014-01-15 15:20:47 +01:00
|
|
|
)
|
2014-01-22 09:06:06 +01:00
|
|
|
self.assertSerializedResultEqual(
|
2014-01-22 16:21:05 +01:00
|
|
|
((x, x * x) for x in range(3)),
|
2015-01-17 00:51:31 +01:00
|
|
|
("((0, 0), (1, 1), (2, 4))", set())
|
2014-01-22 09:06:06 +01:00
|
|
|
)
|
2013-06-07 16:28:38 +02:00
|
|
|
|
2014-07-04 18:48:13 +02:00
|
|
|
def test_serialize_compiled_regex(self):
|
|
|
|
"""
|
|
|
|
Make sure compiled regex can be serialized.
|
|
|
|
"""
|
|
|
|
regex = re.compile(r'^\w+$', re.U)
|
|
|
|
self.assertSerializedEqual(regex)
|
|
|
|
|
|
|
|
def test_serialize_class_based_validators(self):
|
|
|
|
"""
|
|
|
|
Ticket #22943: Test serialization of class-based validators, including
|
|
|
|
compiled regexes.
|
|
|
|
"""
|
|
|
|
validator = RegexValidator(message="hello")
|
|
|
|
string = MigrationWriter.serialize(validator)[0]
|
|
|
|
self.assertEqual(string, "django.core.validators.RegexValidator(message='hello')")
|
|
|
|
self.serialize_round_trip(validator)
|
|
|
|
|
|
|
|
# Test with a compiled regex.
|
|
|
|
validator = RegexValidator(regex=re.compile(r'^\w+$', re.U))
|
|
|
|
string = MigrationWriter.serialize(validator)[0]
|
|
|
|
self.assertEqual(string, "django.core.validators.RegexValidator(regex=re.compile('^\\\\w+$', 32))")
|
|
|
|
self.serialize_round_trip(validator)
|
|
|
|
|
|
|
|
# Test a string regex with flag
|
|
|
|
validator = RegexValidator(r'^[0-9]+$', flags=re.U)
|
|
|
|
string = MigrationWriter.serialize(validator)[0]
|
|
|
|
self.assertEqual(string, "django.core.validators.RegexValidator('^[0-9]+$', flags=32)")
|
|
|
|
self.serialize_round_trip(validator)
|
|
|
|
|
|
|
|
# Test message and code
|
|
|
|
validator = RegexValidator('^[-a-zA-Z0-9_]+$', 'Invalid', 'invalid')
|
|
|
|
string = MigrationWriter.serialize(validator)[0]
|
|
|
|
self.assertEqual(string, "django.core.validators.RegexValidator('^[-a-zA-Z0-9_]+$', 'Invalid', 'invalid')")
|
|
|
|
self.serialize_round_trip(validator)
|
|
|
|
|
|
|
|
# Test with a subclass.
|
|
|
|
validator = EmailValidator(message="hello")
|
|
|
|
string = MigrationWriter.serialize(validator)[0]
|
|
|
|
self.assertEqual(string, "django.core.validators.EmailValidator(message='hello')")
|
|
|
|
self.serialize_round_trip(validator)
|
|
|
|
|
2014-09-08 03:01:42 +02:00
|
|
|
validator = deconstructible(path="migrations.test_writer.EmailValidator")(EmailValidator)(message="hello")
|
2014-07-04 18:48:13 +02:00
|
|
|
string = MigrationWriter.serialize(validator)[0]
|
2014-09-08 03:01:42 +02:00
|
|
|
self.assertEqual(string, "migrations.test_writer.EmailValidator(message='hello')")
|
|
|
|
|
|
|
|
validator = deconstructible(path="custom.EmailValidator")(EmailValidator)(message="hello")
|
2014-09-08 22:01:58 +02:00
|
|
|
with six.assertRaisesRegex(self, ImportError, "No module named '?custom'?"):
|
2014-09-08 03:01:42 +02:00
|
|
|
MigrationWriter.serialize(validator)
|
|
|
|
|
|
|
|
validator = deconstructible(path="django.core.validators.EmailValidator2")(EmailValidator)(message="hello")
|
|
|
|
with self.assertRaisesMessage(ValueError, "Could not find object EmailValidator2 in django.core.validators."):
|
|
|
|
MigrationWriter.serialize(validator)
|
2014-07-04 18:48:13 +02:00
|
|
|
|
2014-05-22 13:29:30 +02:00
|
|
|
def test_serialize_empty_nonempty_tuple(self):
|
|
|
|
"""
|
|
|
|
Ticket #22679: makemigrations generates invalid code for (an empty
|
|
|
|
tuple) default_permissions = ()
|
|
|
|
"""
|
|
|
|
empty_tuple = ()
|
2014-05-23 14:50:10 +02:00
|
|
|
one_item_tuple = ('a',)
|
2014-05-22 13:29:30 +02:00
|
|
|
many_items_tuple = ('a', 'b', 'c')
|
|
|
|
self.assertSerializedEqual(empty_tuple)
|
|
|
|
self.assertSerializedEqual(one_item_tuple)
|
|
|
|
self.assertSerializedEqual(many_items_tuple)
|
|
|
|
|
2014-06-08 02:04:58 +02:00
|
|
|
@unittest.skipUnless(six.PY2, "Only applies on Python 2")
|
|
|
|
def test_serialize_direct_function_reference(self):
|
|
|
|
"""
|
|
|
|
Ticket #22436: You cannot use a function straight from its body
|
|
|
|
(e.g. define the method and use it in the same body)
|
|
|
|
"""
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
self.serialize_round_trip(TestModel1.thing)
|
|
|
|
|
|
|
|
def test_serialize_local_function_reference(self):
|
|
|
|
"""
|
|
|
|
Neither py2 or py3 can serialize a reference in a local scope.
|
|
|
|
"""
|
|
|
|
class TestModel2(object):
|
|
|
|
def upload_to(self):
|
|
|
|
return "somewhere dynamic"
|
|
|
|
thing = models.FileField(upload_to=upload_to)
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
self.serialize_round_trip(TestModel2.thing)
|
|
|
|
|
2014-07-01 22:42:25 +02:00
|
|
|
def test_serialize_local_function_reference_message(self):
|
|
|
|
"""
|
|
|
|
Make sure user is seeing which module/function is the issue
|
|
|
|
"""
|
|
|
|
class TestModel2(object):
|
|
|
|
def upload_to(self):
|
|
|
|
return "somewhere dynamic"
|
|
|
|
thing = models.FileField(upload_to=upload_to)
|
|
|
|
|
|
|
|
with six.assertRaisesRegex(self, ValueError,
|
|
|
|
'^Could not find function upload_to in migrations.test_writer'):
|
|
|
|
self.serialize_round_trip(TestModel2.thing)
|
|
|
|
|
2015-01-17 00:51:31 +01:00
|
|
|
def test_serialize_managers(self):
|
|
|
|
self.assertSerializedEqual(models.Manager())
|
|
|
|
self.assertSerializedResultEqual(
|
|
|
|
FoodQuerySet.as_manager(),
|
|
|
|
('migrations.models.FoodQuerySet.as_manager()', {'import migrations.models'})
|
|
|
|
)
|
|
|
|
self.assertSerializedEqual(FoodManager('a', 'b'))
|
|
|
|
self.assertSerializedEqual(FoodManager('x', 'y', c=3, d=4))
|
|
|
|
|
2015-03-23 15:38:25 +01:00
|
|
|
def test_serialize_frozensets(self):
|
|
|
|
self.assertSerializedEqual(frozenset())
|
|
|
|
self.assertSerializedEqual(frozenset("let it go"))
|
|
|
|
|
2013-06-07 16:28:38 +02:00
|
|
|
def test_simple_migration(self):
|
|
|
|
"""
|
|
|
|
Tests serializing a simple migration.
|
|
|
|
"""
|
2013-09-26 09:25:35 +02:00
|
|
|
fields = {
|
|
|
|
'charfield': models.DateTimeField(default=datetime.datetime.utcnow),
|
|
|
|
'datetimefield': models.DateTimeField(default=datetime.datetime.utcnow),
|
|
|
|
}
|
|
|
|
|
|
|
|
options = {
|
|
|
|
'verbose_name': 'My model',
|
|
|
|
'verbose_name_plural': 'My models',
|
|
|
|
}
|
|
|
|
|
2013-08-23 13:56:37 +02:00
|
|
|
migration = type(str("Migration"), (migrations.Migration,), {
|
2013-06-07 16:28:38 +02:00
|
|
|
"operations": [
|
2013-09-26 09:25:35 +02:00
|
|
|
migrations.CreateModel("MyModel", tuple(fields.items()), options, (models.Model,)),
|
|
|
|
migrations.CreateModel("MyModel2", tuple(fields.items()), bases=(models.Model,)),
|
|
|
|
migrations.CreateModel(name="MyModel3", fields=tuple(fields.items()), options=options, bases=(models.Model,)),
|
2013-06-07 16:28:38 +02:00
|
|
|
migrations.DeleteModel("MyModel"),
|
2013-09-26 09:25:35 +02:00
|
|
|
migrations.AddField("OtherModel", "datetimefield", fields["datetimefield"]),
|
2013-06-07 16:28:38 +02:00
|
|
|
],
|
|
|
|
"dependencies": [("testapp", "some_other_one")],
|
|
|
|
})
|
|
|
|
writer = MigrationWriter(migration)
|
|
|
|
output = writer.as_string()
|
2013-06-07 18:56:43 +02: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)
|
2014-03-31 21:25:08 +02:00
|
|
|
# In order to preserve compatibility with Python 3.2 unicode literals
|
|
|
|
# prefix shouldn't be added to strings.
|
|
|
|
tokens = tokenize.generate_tokens(six.StringIO(str(output)).readline)
|
2014-04-21 12:25:43 +02:00
|
|
|
for token_type, token_source, (srow, scol), __, line in tokens:
|
2014-03-31 21:25:08 +02:00
|
|
|
if token_type == tokenize.STRING:
|
|
|
|
self.assertFalse(
|
|
|
|
token_source.startswith('u'),
|
|
|
|
"Unicode literal prefix found at %d:%d: %r" % (
|
|
|
|
srow, scol, line.strip()
|
|
|
|
)
|
|
|
|
)
|
2013-10-19 02:24:38 +02:00
|
|
|
|
2014-12-21 21:19:05 +01:00
|
|
|
# Silence warning on Python 2: Not importing directory
|
|
|
|
# 'tests/migrations/migrations_test_apps/without_init_file/migrations':
|
|
|
|
# missing __init__.py
|
|
|
|
@ignore_warnings(category=ImportWarning)
|
2013-10-19 02:24:38 +02:00
|
|
|
def test_migration_path(self):
|
|
|
|
test_apps = [
|
|
|
|
'migrations.migrations_test_apps.normal',
|
|
|
|
'migrations.migrations_test_apps.with_package_model',
|
2014-06-17 17:50:31 +02:00
|
|
|
'migrations.migrations_test_apps.without_init_file',
|
2013-10-19 02:24:38 +02:00
|
|
|
]
|
|
|
|
|
2015-02-15 01:47:07 +01:00
|
|
|
base_dir = os.path.dirname(os.path.dirname(upath(__file__)))
|
2013-10-19 02:24:38 +02:00
|
|
|
|
2013-12-19 22:33:46 +01:00
|
|
|
for app in test_apps:
|
2013-12-23 10:37:34 +01:00
|
|
|
with self.modify_settings(INSTALLED_APPS={'append': app}):
|
2013-12-19 22:33:46 +01:00
|
|
|
migration = migrations.Migration('0001_initial', app.split('.')[-1])
|
|
|
|
expected_path = os.path.join(base_dir, *(app.split('.') + ['migrations', '0001_initial.py']))
|
|
|
|
writer = MigrationWriter(migration)
|
2014-12-21 21:19:05 +01:00
|
|
|
self.assertEqual(writer.path, expected_path)
|
2014-06-11 15:00:52 +02:00
|
|
|
|
|
|
|
def test_custom_operation(self):
|
|
|
|
migration = type(str("Migration"), (migrations.Migration,), {
|
|
|
|
"operations": [
|
|
|
|
custom_migration_operations.operations.TestOperation(),
|
|
|
|
custom_migration_operations.operations.CreateModel(),
|
|
|
|
migrations.CreateModel("MyModel", (), {}, (models.Model,)),
|
|
|
|
custom_migration_operations.more_operations.TestOperation()
|
|
|
|
],
|
|
|
|
"dependencies": []
|
|
|
|
})
|
|
|
|
writer = MigrationWriter(migration)
|
|
|
|
output = writer.as_string()
|
|
|
|
result = self.safe_exec(output)
|
|
|
|
self.assertIn("custom_migration_operations", result)
|
|
|
|
self.assertNotEqual(
|
|
|
|
result['custom_migration_operations'].operations.TestOperation,
|
|
|
|
result['custom_migration_operations'].more_operations.TestOperation
|
|
|
|
)
|
2014-09-06 22:42:36 +02:00
|
|
|
|
2015-01-17 00:55:41 +01:00
|
|
|
def test_sorted_imports(self):
|
|
|
|
"""
|
|
|
|
#24155 - Tests ordering of imports.
|
|
|
|
"""
|
|
|
|
migration = type(str("Migration"), (migrations.Migration,), {
|
|
|
|
"operations": [
|
|
|
|
migrations.AddField("mymodel", "myfield", models.DateTimeField(
|
|
|
|
default=datetime.datetime(2012, 1, 1, 1, 1, tzinfo=utc),
|
|
|
|
)),
|
|
|
|
]
|
|
|
|
})
|
|
|
|
writer = MigrationWriter(migration)
|
|
|
|
output = writer.as_string().decode('utf-8')
|
|
|
|
self.assertIn(
|
|
|
|
"import datetime\n"
|
|
|
|
"from django.db import migrations, models\n"
|
|
|
|
"from django.utils.timezone import utc\n",
|
|
|
|
output
|
|
|
|
)
|
|
|
|
|
2014-12-03 02:52:58 +01:00
|
|
|
def test_deconstruct_class_arguments(self):
|
|
|
|
# Yes, it doesn't make sense to use a class as a default for a
|
|
|
|
# CharField. It does make sense for custom fields though, for example
|
|
|
|
# an enumfield that takes the enum class as an argument.
|
|
|
|
class DeconstructableInstances(object):
|
|
|
|
def deconstruct(self):
|
|
|
|
return ('DeconstructableInstances', [], {})
|
|
|
|
|
|
|
|
string = MigrationWriter.serialize(models.CharField(default=DeconstructableInstances))[0]
|
|
|
|
self.assertEqual(string, "models.CharField(default=migrations.test_writer.DeconstructableInstances)")
|