test_writer.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import datetime
  4. import os
  5. import tokenize
  6. import unittest
  7. from django.core.validators import RegexValidator, EmailValidator
  8. from django.db import models, migrations
  9. from django.db.migrations.writer import MigrationWriter, SettingsReference
  10. from django.test import TestCase
  11. from django.conf import settings
  12. from django.utils import datetime_safe, six
  13. from django.utils.deconstruct import deconstructible
  14. from django.utils.translation import ugettext_lazy as _
  15. from django.utils.timezone import get_default_timezone
  16. import custom_migration_operations.operations
  17. import custom_migration_operations.more_operations
  18. class TestModel1(object):
  19. def upload_to(self):
  20. return "somewhere dynamic"
  21. thing = models.FileField(upload_to=upload_to)
  22. class WriterTests(TestCase):
  23. """
  24. Tests the migration writer (makes migration files from Migration instances)
  25. """
  26. def safe_exec(self, string, value=None):
  27. l = {}
  28. try:
  29. exec(string, globals(), l)
  30. except Exception as e:
  31. if value:
  32. self.fail("Could not exec %r (from value %r): %s" % (string.strip(), value, e))
  33. else:
  34. self.fail("Could not exec %r: %s" % (string.strip(), e))
  35. return l
  36. def serialize_round_trip(self, value):
  37. string, imports = MigrationWriter.serialize(value)
  38. return self.safe_exec("%s\ntest_value_result = %s" % ("\n".join(imports), string), value)['test_value_result']
  39. def assertSerializedEqual(self, value):
  40. self.assertEqual(self.serialize_round_trip(value), value)
  41. def assertSerializedResultEqual(self, value, target):
  42. self.assertEqual(MigrationWriter.serialize(value), target)
  43. def assertSerializedFieldEqual(self, value):
  44. new_value = self.serialize_round_trip(value)
  45. self.assertEqual(value.__class__, new_value.__class__)
  46. self.assertEqual(value.max_length, new_value.max_length)
  47. self.assertEqual(value.null, new_value.null)
  48. self.assertEqual(value.unique, new_value.unique)
  49. def test_serialize(self):
  50. """
  51. Tests various different forms of the serializer.
  52. This does not care about formatting, just that the parsed result is
  53. correct, so we always exec() the result and check that.
  54. """
  55. # Basic values
  56. self.assertSerializedEqual(1)
  57. self.assertSerializedEqual(None)
  58. self.assertSerializedEqual(b"foobar")
  59. string, imports = MigrationWriter.serialize(b"foobar")
  60. self.assertEqual(string, "b'foobar'")
  61. self.assertSerializedEqual("föobár")
  62. string, imports = MigrationWriter.serialize("foobar")
  63. self.assertEqual(string, "'foobar'")
  64. self.assertSerializedEqual({1: 2})
  65. self.assertSerializedEqual(["a", 2, True, None])
  66. self.assertSerializedEqual(set([2, 3, "eighty"]))
  67. self.assertSerializedEqual({"lalalala": ["yeah", "no", "maybe"]})
  68. self.assertSerializedEqual(_('Hello'))
  69. # Functions
  70. with six.assertRaisesRegex(self, ValueError, 'Cannot serialize function: lambda'):
  71. self.assertSerializedEqual(lambda x: 42)
  72. self.assertSerializedEqual(models.SET_NULL)
  73. string, imports = MigrationWriter.serialize(models.SET(42))
  74. self.assertEqual(string, 'models.SET(42)')
  75. self.serialize_round_trip(models.SET(42))
  76. # Datetime stuff
  77. self.assertSerializedEqual(datetime.datetime.utcnow())
  78. self.assertSerializedEqual(datetime.datetime.utcnow)
  79. self.assertSerializedEqual(datetime.datetime.today())
  80. self.assertSerializedEqual(datetime.datetime.today)
  81. self.assertSerializedEqual(datetime.date.today())
  82. self.assertSerializedEqual(datetime.date.today)
  83. with self.assertRaises(ValueError):
  84. self.assertSerializedEqual(datetime.datetime(2012, 1, 1, 1, 1, tzinfo=get_default_timezone()))
  85. safe_date = datetime_safe.date(2014, 3, 31)
  86. string, imports = MigrationWriter.serialize(safe_date)
  87. self.assertEqual(string, repr(datetime.date(2014, 3, 31)))
  88. self.assertEqual(imports, {'import datetime'})
  89. safe_datetime = datetime_safe.datetime(2014, 3, 31, 16, 4, 31)
  90. string, imports = MigrationWriter.serialize(safe_datetime)
  91. self.assertEqual(string, repr(datetime.datetime(2014, 3, 31, 16, 4, 31)))
  92. self.assertEqual(imports, {'import datetime'})
  93. # Classes
  94. validator = RegexValidator(message="hello")
  95. string, imports = MigrationWriter.serialize(validator)
  96. self.assertEqual(string, "django.core.validators.RegexValidator(message='hello')")
  97. self.serialize_round_trip(validator)
  98. validator = EmailValidator(message="hello") # Test with a subclass.
  99. string, imports = MigrationWriter.serialize(validator)
  100. self.assertEqual(string, "django.core.validators.EmailValidator(message='hello')")
  101. self.serialize_round_trip(validator)
  102. validator = deconstructible(path="custom.EmailValidator")(EmailValidator)(message="hello")
  103. string, imports = MigrationWriter.serialize(validator)
  104. self.assertEqual(string, "custom.EmailValidator(message='hello')")
  105. # Django fields
  106. self.assertSerializedFieldEqual(models.CharField(max_length=255))
  107. self.assertSerializedFieldEqual(models.TextField(null=True, blank=True))
  108. # Setting references
  109. self.assertSerializedEqual(SettingsReference(settings.AUTH_USER_MODEL, "AUTH_USER_MODEL"))
  110. self.assertSerializedResultEqual(
  111. SettingsReference("someapp.model", "AUTH_USER_MODEL"),
  112. (
  113. "settings.AUTH_USER_MODEL",
  114. set(["from django.conf import settings"]),
  115. )
  116. )
  117. self.assertSerializedResultEqual(
  118. ((x, x * x) for x in range(3)),
  119. (
  120. "((0, 0), (1, 1), (2, 4))",
  121. set(),
  122. )
  123. )
  124. def test_serialize_empty_nonempty_tuple(self):
  125. """
  126. Ticket #22679: makemigrations generates invalid code for (an empty
  127. tuple) default_permissions = ()
  128. """
  129. empty_tuple = ()
  130. one_item_tuple = ('a',)
  131. many_items_tuple = ('a', 'b', 'c')
  132. self.assertSerializedEqual(empty_tuple)
  133. self.assertSerializedEqual(one_item_tuple)
  134. self.assertSerializedEqual(many_items_tuple)
  135. @unittest.skipUnless(six.PY2, "Only applies on Python 2")
  136. def test_serialize_direct_function_reference(self):
  137. """
  138. Ticket #22436: You cannot use a function straight from its body
  139. (e.g. define the method and use it in the same body)
  140. """
  141. with self.assertRaises(ValueError):
  142. self.serialize_round_trip(TestModel1.thing)
  143. def test_serialize_local_function_reference(self):
  144. """
  145. Neither py2 or py3 can serialize a reference in a local scope.
  146. """
  147. class TestModel2(object):
  148. def upload_to(self):
  149. return "somewhere dynamic"
  150. thing = models.FileField(upload_to=upload_to)
  151. with self.assertRaises(ValueError):
  152. self.serialize_round_trip(TestModel2.thing)
  153. def test_serialize_local_function_reference_message(self):
  154. """
  155. Make sure user is seeing which module/function is the issue
  156. """
  157. class TestModel2(object):
  158. def upload_to(self):
  159. return "somewhere dynamic"
  160. thing = models.FileField(upload_to=upload_to)
  161. with six.assertRaisesRegex(self, ValueError,
  162. '^Could not find function upload_to in migrations.test_writer'):
  163. self.serialize_round_trip(TestModel2.thing)
  164. def test_simple_migration(self):
  165. """
  166. Tests serializing a simple migration.
  167. """
  168. fields = {
  169. 'charfield': models.DateTimeField(default=datetime.datetime.utcnow),
  170. 'datetimefield': models.DateTimeField(default=datetime.datetime.utcnow),
  171. }
  172. options = {
  173. 'verbose_name': 'My model',
  174. 'verbose_name_plural': 'My models',
  175. }
  176. migration = type(str("Migration"), (migrations.Migration,), {
  177. "operations": [
  178. migrations.CreateModel("MyModel", tuple(fields.items()), options, (models.Model,)),
  179. migrations.CreateModel("MyModel2", tuple(fields.items()), bases=(models.Model,)),
  180. migrations.CreateModel(name="MyModel3", fields=tuple(fields.items()), options=options, bases=(models.Model,)),
  181. migrations.DeleteModel("MyModel"),
  182. migrations.AddField("OtherModel", "datetimefield", fields["datetimefield"]),
  183. ],
  184. "dependencies": [("testapp", "some_other_one")],
  185. })
  186. writer = MigrationWriter(migration)
  187. output = writer.as_string()
  188. # It should NOT be unicode.
  189. self.assertIsInstance(output, six.binary_type, "Migration as_string returned unicode")
  190. # We don't test the output formatting - that's too fragile.
  191. # Just make sure it runs for now, and that things look alright.
  192. result = self.safe_exec(output)
  193. self.assertIn("Migration", result)
  194. # In order to preserve compatibility with Python 3.2 unicode literals
  195. # prefix shouldn't be added to strings.
  196. tokens = tokenize.generate_tokens(six.StringIO(str(output)).readline)
  197. for token_type, token_source, (srow, scol), __, line in tokens:
  198. if token_type == tokenize.STRING:
  199. self.assertFalse(
  200. token_source.startswith('u'),
  201. "Unicode literal prefix found at %d:%d: %r" % (
  202. srow, scol, line.strip()
  203. )
  204. )
  205. def test_migration_path(self):
  206. test_apps = [
  207. 'migrations.migrations_test_apps.normal',
  208. 'migrations.migrations_test_apps.with_package_model',
  209. 'migrations.migrations_test_apps.without_init_file',
  210. ]
  211. base_dir = os.path.dirname(os.path.dirname(__file__))
  212. for app in test_apps:
  213. with self.modify_settings(INSTALLED_APPS={'append': app}):
  214. migration = migrations.Migration('0001_initial', app.split('.')[-1])
  215. expected_path = os.path.join(base_dir, *(app.split('.') + ['migrations', '0001_initial.py']))
  216. writer = MigrationWriter(migration)
  217. self.assertEqual(writer.path, expected_path)
  218. def test_custom_operation(self):
  219. migration = type(str("Migration"), (migrations.Migration,), {
  220. "operations": [
  221. custom_migration_operations.operations.TestOperation(),
  222. custom_migration_operations.operations.CreateModel(),
  223. migrations.CreateModel("MyModel", (), {}, (models.Model,)),
  224. custom_migration_operations.more_operations.TestOperation()
  225. ],
  226. "dependencies": []
  227. })
  228. writer = MigrationWriter(migration)
  229. output = writer.as_string()
  230. result = self.safe_exec(output)
  231. self.assertIn("custom_migration_operations", result)
  232. self.assertNotEqual(
  233. result['custom_migration_operations'].operations.TestOperation,
  234. result['custom_migration_operations'].more_operations.TestOperation
  235. )