2013-09-05 20:51:31 +00:00
|
|
|
from unittest import skipIf
|
|
|
|
|
2013-12-23 15:01:13 +00:00
|
|
|
from django.test import TestCase, override_settings
|
2013-05-18 11:49:56 +00:00
|
|
|
from django.db import connection
|
2013-07-22 18:43:58 +00:00
|
|
|
from django.db.migrations.loader import MigrationLoader, AmbiguityError
|
2013-05-18 11:49:56 +00:00
|
|
|
from django.db.migrations.recorder import MigrationRecorder
|
2013-09-05 20:51:31 +00:00
|
|
|
from django.utils import six
|
2013-05-18 11:49:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RecorderTests(TestCase):
|
|
|
|
"""
|
2013-05-29 16:47:10 +00:00
|
|
|
Tests recording migrations as applied or not.
|
2013-05-18 11:49:56 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def test_apply(self):
|
|
|
|
"""
|
|
|
|
Tests marking migrations as applied/unapplied.
|
|
|
|
"""
|
|
|
|
recorder = MigrationRecorder(connection)
|
|
|
|
self.assertEqual(
|
|
|
|
recorder.applied_migrations(),
|
|
|
|
set(),
|
|
|
|
)
|
|
|
|
recorder.record_applied("myapp", "0432_ponies")
|
|
|
|
self.assertEqual(
|
|
|
|
recorder.applied_migrations(),
|
|
|
|
set([("myapp", "0432_ponies")]),
|
|
|
|
)
|
|
|
|
recorder.record_unapplied("myapp", "0432_ponies")
|
|
|
|
self.assertEqual(
|
|
|
|
recorder.applied_migrations(),
|
|
|
|
set(),
|
|
|
|
)
|
2013-05-29 16:47:10 +00:00
|
|
|
|
|
|
|
|
2013-06-19 14:36:02 +00:00
|
|
|
class LoaderTests(TestCase):
|
2013-05-29 16:47:10 +00:00
|
|
|
"""
|
|
|
|
Tests the disk and database loader, and running through migrations
|
|
|
|
in memory.
|
|
|
|
"""
|
|
|
|
|
2013-06-19 14:36:02 +00:00
|
|
|
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
|
2013-05-29 16:47:10 +00:00
|
|
|
def test_load(self):
|
|
|
|
"""
|
|
|
|
Makes sure the loader can load the migrations for the test apps,
|
2013-12-24 11:25:17 +00:00
|
|
|
and then render them out to a new Apps.
|
2013-05-29 16:47:10 +00:00
|
|
|
"""
|
|
|
|
# Load and test the plan
|
|
|
|
migration_loader = MigrationLoader(connection)
|
|
|
|
self.assertEqual(
|
|
|
|
migration_loader.graph.forwards_plan(("migrations", "0002_second")),
|
2014-01-08 13:00:12 +00:00
|
|
|
[
|
|
|
|
("migrations", "0001_initial"),
|
|
|
|
("migrations", "0002_second"),
|
|
|
|
],
|
2013-05-29 16:47:10 +00:00
|
|
|
)
|
|
|
|
# Now render it out!
|
|
|
|
project_state = migration_loader.graph.project_state(("migrations", "0002_second"))
|
|
|
|
self.assertEqual(len(project_state.models), 2)
|
|
|
|
|
|
|
|
author_state = project_state.models["migrations", "author"]
|
|
|
|
self.assertEqual(
|
|
|
|
[x for x, y in author_state.fields],
|
2013-05-30 17:08:58 +00:00
|
|
|
["id", "name", "slug", "age", "rating"]
|
2013-05-29 16:47:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
book_state = project_state.models["migrations", "book"]
|
|
|
|
self.assertEqual(
|
|
|
|
[x for x, y in book_state.fields],
|
|
|
|
["id", "author"]
|
|
|
|
)
|
2013-07-22 18:43:58 +00:00
|
|
|
|
2014-01-08 13:00:12 +00:00
|
|
|
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_unmigdep"})
|
|
|
|
def test_load_unmigrated_dependency(self):
|
|
|
|
"""
|
|
|
|
Makes sure the loader can load migrations with a dependency on an unmigrated app.
|
|
|
|
"""
|
|
|
|
# Load and test the plan
|
|
|
|
migration_loader = MigrationLoader(connection)
|
|
|
|
self.assertEqual(
|
|
|
|
migration_loader.graph.forwards_plan(("migrations", "0001_initial")),
|
|
|
|
[
|
|
|
|
("auth", "__first__"),
|
|
|
|
("migrations", "0001_initial"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
# Now render it out!
|
|
|
|
project_state = migration_loader.graph.project_state(("migrations", "0001_initial"))
|
2014-01-08 21:20:29 +00:00
|
|
|
self.assertEqual(len([m for a, m in project_state.models if a == "migrations"]), 1)
|
2014-01-08 13:00:12 +00:00
|
|
|
|
|
|
|
book_state = project_state.models["migrations", "book"]
|
|
|
|
self.assertEqual(
|
|
|
|
[x for x, y in book_state.fields],
|
|
|
|
["id", "user"]
|
|
|
|
)
|
|
|
|
|
2013-07-22 18:43:58 +00:00
|
|
|
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
|
|
|
|
def test_name_match(self):
|
|
|
|
"Tests prefix name matching"
|
|
|
|
migration_loader = MigrationLoader(connection)
|
|
|
|
self.assertEqual(
|
|
|
|
migration_loader.get_migration_by_prefix("migrations", "0001").name,
|
|
|
|
"0001_initial",
|
|
|
|
)
|
|
|
|
with self.assertRaises(AmbiguityError):
|
|
|
|
migration_loader.get_migration_by_prefix("migrations", "0")
|
|
|
|
with self.assertRaises(KeyError):
|
|
|
|
migration_loader.get_migration_by_prefix("migrations", "blarg")
|
2013-09-05 20:51:11 +00:00
|
|
|
|
|
|
|
def test_load_import_error(self):
|
|
|
|
with override_settings(MIGRATION_MODULES={"migrations": "migrations.faulty_migrations.import_error"}):
|
|
|
|
with self.assertRaises(ImportError):
|
2013-10-23 21:56:54 +00:00
|
|
|
MigrationLoader(connection)
|
2013-09-05 20:51:31 +00:00
|
|
|
|
|
|
|
def test_load_module_file(self):
|
|
|
|
with override_settings(MIGRATION_MODULES={"migrations": "migrations.faulty_migrations.file"}):
|
2013-10-23 21:56:54 +00:00
|
|
|
MigrationLoader(connection)
|
2013-09-05 20:51:31 +00:00
|
|
|
|
|
|
|
@skipIf(six.PY2, "PY2 doesn't load empty dirs.")
|
|
|
|
def test_load_empty_dir(self):
|
|
|
|
with override_settings(MIGRATION_MODULES={"migrations": "migrations.faulty_migrations.namespace"}):
|
2013-10-23 21:56:54 +00:00
|
|
|
MigrationLoader(connection)
|
|
|
|
|
|
|
|
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"})
|
|
|
|
def test_loading_squashed(self):
|
|
|
|
"Tests loading a squashed migration"
|
|
|
|
migration_loader = MigrationLoader(connection)
|
|
|
|
recorder = MigrationRecorder(connection)
|
|
|
|
# Loading with nothing applied should just give us the one node
|
|
|
|
self.assertEqual(
|
|
|
|
len(migration_loader.graph.nodes),
|
|
|
|
1,
|
|
|
|
)
|
|
|
|
# However, fake-apply one migration and it should now use the old two
|
|
|
|
recorder.record_applied("migrations", "0001_initial")
|
|
|
|
migration_loader.build_graph()
|
|
|
|
self.assertEqual(
|
|
|
|
len(migration_loader.graph.nodes),
|
|
|
|
2,
|
|
|
|
)
|
|
|
|
recorder.flush()
|