summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMarkus Holtermann <info@markusholtermann.eu>2013-09-01 17:42:43 +0200
committerTim Graham <timograham@gmail.com>2013-09-06 09:51:58 -0400
commitbd8e1a354cb1fde5e5411e3c729a0d179f4eb37b (patch)
tree218c93dfc5f9c7151bb690e2598d1398b84ef896 /tests
parent8625c7aab3760d16572792fea23a95e572e28ead (diff)
Fixed #20977 -- Fixed writing migrations to disk on Python 3
Diffstat (limited to 'tests')
-rw-r--r--tests/migrations/models.py20
-rw-r--r--tests/migrations/test_commands.py75
2 files changed, 93 insertions, 2 deletions
diff --git a/tests/migrations/models.py b/tests/migrations/models.py
index e69de29bb2..038c75201b 100644
--- a/tests/migrations/models.py
+++ b/tests/migrations/models.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models
+from django.db.models.loading import BaseAppCache
+from django.utils.encoding import python_2_unicode_compatible
+
+
+@python_2_unicode_compatible
+class UnicodeModel(models.Model):
+ title = models.CharField('ÚÑÍ¢ÓÐÉ', max_length=20, default='“Ðjáñgó”')
+
+ class Meta:
+ # Disable auto loading of this model as we load it on our own
+ app_cache = BaseAppCache()
+ verbose_name = 'úñí©óðé µóðéø'
+ verbose_name_plural = 'úñí©óðé µóðéøß'
+
+ def __str__(self):
+ return self.title
diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py
index d775d1eba7..7fc78f74b5 100644
--- a/tests/migrations/test_commands.py
+++ b/tests/migrations/test_commands.py
@@ -1,11 +1,23 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+import os
+import shutil
+
from django.core.management import call_command
+from django.db.models.loading import cache
from django.test.utils import override_settings
+from django.utils import six
+from django.utils._os import upath
+from django.utils.encoding import force_text
+
+from .models import UnicodeModel
from .test_base import MigrationTestBase
-class CommandTests(MigrationTestBase):
+class MigrateTests(MigrationTestBase):
"""
- Tests running the commands (migrate, makemigrations).
+ Tests running the migrate command.
"""
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
@@ -35,3 +47,62 @@ class CommandTests(MigrationTestBase):
self.assertTableNotExists("migrations_author")
self.assertTableNotExists("migrations_tribble")
self.assertTableNotExists("migrations_book")
+
+
+class MakeMigrationsTests(MigrationTestBase):
+ """
+ Tests running the makemigrations command.
+ """
+
+ def setUp(self):
+ self._cwd = os.getcwd()
+ self.test_dir = os.path.abspath(os.path.dirname(upath(__file__)))
+ self.migration_dir = os.path.join(self.test_dir, 'migrations')
+
+ def tearDown(self):
+ os.chdir(self.test_dir)
+ try:
+ self._rmrf(self.migration_dir)
+ except OSError:
+ pass
+ os.chdir(self._cwd)
+
+ def _rmrf(self, dname):
+ if os.path.commonprefix([self.test_dir, os.path.abspath(dname)]) != self.test_dir:
+ return
+ shutil.rmtree(dname)
+
+ def test_files_content(self):
+ self.assertTableNotExists("migrations_unicodemodel")
+ cache.register_models('migrations', UnicodeModel)
+ call_command("makemigrations", "migrations", verbosity=0)
+
+ init_file = os.path.join(self.migration_dir, "__init__.py")
+
+ # Check for existing __init__.py file in migrations folder
+ self.assertTrue(os.path.exists(init_file))
+
+ with open(init_file, 'r') as fp:
+ content = force_text(fp.read())
+ self.assertEqual(content, '')
+
+ initial_file = os.path.join(self.migration_dir, "0001_initial.py")
+
+ # Check for existing 0001_initial.py file in migration folder
+ self.assertTrue(os.path.exists(initial_file))
+
+ with open(initial_file, 'r') as fp:
+ content = force_text(fp.read())
+ self.assertTrue('# encoding: utf8' in content)
+ self.assertTrue('migrations.CreateModel' in content)
+
+ if six.PY3:
+ self.assertTrue('úñí©óðé µóðéø' in content) # Meta.verbose_name
+ self.assertTrue('úñí©óðé µóðéøß' in content) # Meta.verbose_name_plural
+ self.assertTrue('ÚÑÍ¢ÓÐÉ' in content) # title.verbose_name
+ self.assertTrue('“Ðjáñgó”' in content) # title.default
+ else:
+ self.assertTrue('\\xfa\\xf1\\xed\\xa9\\xf3\\xf0\\xe9 \\xb5\\xf3\\xf0\\xe9\\xf8' in content) # Meta.verbose_name
+ self.assertTrue('\\xfa\\xf1\\xed\\xa9\\xf3\\xf0\\xe9 \\xb5\\xf3\\xf0\\xe9\\xf8\\xdf' in content) # Meta.verbose_name_plural
+ self.assertTrue('\\xda\\xd1\\xcd\\xa2\\xd3\\xd0\\xc9' in content) # title.verbose_name
+ self.assertTrue('\\u201c\\xd0j\\xe1\\xf1g\\xf3\\u201d' in content) # title.default