2 from dmigrations.migration_db import MigrationDb
6 class MigrationDbTest(TestCase):
8 self.mock_migrations_dir = os.path.join(os.path.dirname(__file__), "mock_migrations_dir")
10 def test_migrations_sorted_by_numbers_not_ascii(self):
11 db = MigrationDb(migrations=[
29 def test_migrations_can_be_initialized_from_directory(self):
30 db = MigrationDb(directory=self.mock_migrations_dir)
31 db.warn = WarningsMocker()
42 def test_dups_and_missing_numbers_ok(self):
43 db = MigrationDb(migrations = [
63 def test_nonpy_and_nonnumber_are_ignored(self):
65 db.populate_migrations_from_ls(["1_foo.py", "1_bar.pyc", "4-ohhi.py", "-9_kitty.py", "1_bar.html", ".", ".boo", "..", "123.py.gz", "hello_45.py", "__init__.py", "___init__.pyc", "hello.py", "hello.pyc"])
71 def test_dup_warnings(self):
73 db.warn = WarningsMocker()
74 db.populate_migrations_from_ls([
83 u'There are multiple migrations with the same number 17: 017_bar, 17_foo',
84 u'There are multiple migrations with the same number 18: 00018_foo, 0018_foo'
89 def test_is_dev_migration(self):
91 self.assert_equal(False, db.is_dev_migration("1_foo"))
92 self.assert_equal(False, db.is_dev_migration("1DEV_foo"))
93 self.assert_equal(False, db.is_dev_migration("1_DEVELOPER_foo"))
95 self.assert_equal(True, db.is_dev_migration("1_DEV_foo"))
96 self.assert_equal(True, db.is_dev_migration("005_DEV_bar"))
97 self.assert_equal(True, db.is_dev_migration("1__DEV__foo"))
99 def test_load_migration_object(self):
100 db = MigrationDb(directory=self.mock_migrations_dir)
101 db.warn = WarningsMocker()
103 for name in ['001_foo', '0001', '001', '01', '1', 1]:
104 self.assert_attrs(db.load_migration_object(name),
107 sql_up = "INSERT INTO mock VALUES (1)",
110 for name in ['002_bar', '0002', '002', '02', '2', 2]:
111 self.assert_attrs(db.load_migration_object(name),
114 sql_up = "INSERT INTO mock VALUES (2)",
117 for name in ['005_DEV_hello', '0005', '005', '05', '5', 5]:
118 self.assert_attrs(db.load_migration_object(name),
119 name = '005_DEV_hello',
121 sql_up = "INSERT INTO mock VALUES (5)",
124 for name in ['3', '02_foo', '2_foo', 3]:
125 self.assert_raises(NoSuchMigrationError, lambda: db.load_migration_object(name))
127 def test_load_migration_object_with_dups(self):
128 db = MigrationDb(directory=self.mock_migrations_dir)
129 db.warn = WarningsMocker()
131 self.assert_attrs(db.load_migration_object('009_one'),
134 sql_up = "INSERT INTO mock VALUES (9, 1)",
137 self.assert_attrs(db.load_migration_object('09_two'),
140 sql_up = "INSERT INTO mock VALUES (9, 2)",
143 for name in ['0009', '009', '09', '9', 9]:
144 self.assert_raises(AmbiguousMigrationNameError, lambda: db.load_migration_object(name))
146 def test_find_unique_migration_by_number(self):
147 db = MigrationDb(directory=self.mock_migrations_dir)
148 db.warn = WarningsMocker()
150 self.assert_equal("001_foo", db.find_unique_migration_by_number(1))
151 self.assert_equal("002_bar", db.find_unique_migration_by_number(2))
152 self.assert_equal(None, db.find_unique_migration_by_number(3))
153 self.assert_raises(AmbiguousMigrationNameError, lambda: db.find_unique_migration_by_number(9))
155 def test_migration_path(self):
156 db = MigrationDb(directory=self.mock_migrations_dir)
157 db.warn = WarningsMocker()
159 self.assert_equal(self.mock_migrations_dir + '/010_foo.py', db.migration_path('foo'))