blob: 890f3d91213fa7cb382549b87f380774df8fd7d7 [file] [log] [blame]
Mike Frysingerd03e6b52019-08-03 12:49:01 -04001#!/usr/bin/python2
showardf208abc2008-04-01 23:57:11 +00002
showard0e73c852008-10-03 10:15:50 +00003import unittest, tempfile, os
mbligh9b907d62008-05-13 17:56:24 +00004import common
mbligh8bcd23a2009-02-03 19:14:06 +00005import MySQLdb
mbligh9b907d62008-05-13 17:56:24 +00006from autotest_lib.client.common_lib import global_config
showard442e71e2008-10-06 10:05:20 +00007from autotest_lib.database import database_connection, migrate
showardf208abc2008-04-01 23:57:11 +00008
9# Which section of the global config to pull info from. We won't actually use
10# that DB, we'll use the corresponding test DB (test_<db name>).
11CONFIG_DB = 'AUTOTEST_WEB'
12
13NUM_MIGRATIONS = 3
14
15class DummyMigration(object):
jadmanski0afbb632008-06-06 21:10:57 +000016 """\
17 Dummy migration class that records all migrations done in a class
18 varaible.
19 """
showardf208abc2008-04-01 23:57:11 +000020
jadmanski0afbb632008-06-06 21:10:57 +000021 migrations_done = []
showardf208abc2008-04-01 23:57:11 +000022
jadmanski0afbb632008-06-06 21:10:57 +000023 def __init__(self, version):
24 self.version = version
25 self.name = '%03d_test' % version
showardf208abc2008-04-01 23:57:11 +000026
27
jadmanski0afbb632008-06-06 21:10:57 +000028 @classmethod
29 def get_migrations_done(cls):
30 return cls.migrations_done
showardf208abc2008-04-01 23:57:11 +000031
32
jadmanski0afbb632008-06-06 21:10:57 +000033 @classmethod
34 def clear_migrations_done(cls):
35 cls.migrations_done = []
showardf208abc2008-04-01 23:57:11 +000036
37
jadmanski0afbb632008-06-06 21:10:57 +000038 @classmethod
39 def do_migration(cls, version, direction):
40 cls.migrations_done.append((version, direction))
showardf208abc2008-04-01 23:57:11 +000041
42
jadmanski0afbb632008-06-06 21:10:57 +000043 def migrate_up(self, manager):
44 self.do_migration(self.version, 'up')
45 if self.version == 1:
46 manager.create_migrate_table()
showardf208abc2008-04-01 23:57:11 +000047
48
jadmanski0afbb632008-06-06 21:10:57 +000049 def migrate_down(self, manager):
50 self.do_migration(self.version, 'down')
showardf208abc2008-04-01 23:57:11 +000051
52
53MIGRATIONS = [DummyMigration(n) for n in xrange(1, NUM_MIGRATIONS + 1)]
54
55
56class TestableMigrationManager(migrate.MigrationManager):
showard0e73c852008-10-03 10:15:50 +000057 def _set_migrations_dir(self, migrations_dir=None):
58 pass
showardf208abc2008-04-01 23:57:11 +000059
60
jadmanski0afbb632008-06-06 21:10:57 +000061 def get_migrations(self, minimum_version=None, maximum_version=None):
62 minimum_version = minimum_version or 1
63 maximum_version = maximum_version or len(MIGRATIONS)
64 return MIGRATIONS[minimum_version-1:maximum_version]
showardf208abc2008-04-01 23:57:11 +000065
66
67class MigrateManagerTest(unittest.TestCase):
jadmanski0afbb632008-06-06 21:10:57 +000068 def setUp(self):
showard0e73c852008-10-03 10:15:50 +000069 self._database = (
70 database_connection.DatabaseConnection.get_test_database())
71 self._database.connect()
72 self.manager = TestableMigrationManager(self._database)
jadmanski0afbb632008-06-06 21:10:57 +000073 DummyMigration.clear_migrations_done()
showardf208abc2008-04-01 23:57:11 +000074
75
jadmanski0afbb632008-06-06 21:10:57 +000076 def tearDown(self):
showard0e73c852008-10-03 10:15:50 +000077 self._database.disconnect()
showardf208abc2008-04-01 23:57:11 +000078
79
jadmanski0afbb632008-06-06 21:10:57 +000080 def test_sync(self):
81 self.manager.do_sync_db()
82 self.assertEquals(self.manager.get_db_version(), NUM_MIGRATIONS)
83 self.assertEquals(DummyMigration.get_migrations_done(),
84 [(1, 'up'), (2, 'up'), (3, 'up')])
showardf208abc2008-04-01 23:57:11 +000085
jadmanski0afbb632008-06-06 21:10:57 +000086 DummyMigration.clear_migrations_done()
87 self.manager.do_sync_db(0)
88 self.assertEquals(self.manager.get_db_version(), 0)
89 self.assertEquals(DummyMigration.get_migrations_done(),
90 [(3, 'down'), (2, 'down'), (1, 'down')])
showardf208abc2008-04-01 23:57:11 +000091
92
jadmanski0afbb632008-06-06 21:10:57 +000093 def test_sync_one_by_one(self):
94 for version in xrange(1, NUM_MIGRATIONS + 1):
95 self.manager.do_sync_db(version)
96 self.assertEquals(self.manager.get_db_version(),
97 version)
98 self.assertEquals(
99 DummyMigration.get_migrations_done()[-1],
100 (version, 'up'))
showardf208abc2008-04-01 23:57:11 +0000101
jadmanski0afbb632008-06-06 21:10:57 +0000102 for version in xrange(NUM_MIGRATIONS - 1, -1, -1):
103 self.manager.do_sync_db(version)
104 self.assertEquals(self.manager.get_db_version(),
105 version)
106 self.assertEquals(
107 DummyMigration.get_migrations_done()[-1],
108 (version + 1, 'down'))
showardf208abc2008-04-01 23:57:11 +0000109
110
jadmanski0afbb632008-06-06 21:10:57 +0000111 def test_null_sync(self):
112 self.manager.do_sync_db()
113 DummyMigration.clear_migrations_done()
114 self.manager.do_sync_db()
115 self.assertEquals(DummyMigration.get_migrations_done(), [])
showardf208abc2008-04-01 23:57:11 +0000116
117
showardeca1f212009-05-13 20:28:12 +0000118class DummyMigrationManager(object):
119 def __init__(self):
120 self.calls = []
121
122
123 def execute_script(self, script):
124 self.calls.append(script)
125
126
127class MigrationTest(unittest.TestCase):
128 def setUp(self):
129 self.manager = DummyMigrationManager()
130
131
132 def _do_migration(self, migration_module):
133 migration = migrate.Migration('name', 1, migration_module)
134 migration.migrate_up(self.manager)
135 migration.migrate_down(self.manager)
136
137 self.assertEquals(self.manager.calls, ['foo', 'bar'])
138
139
140 def test_migration_with_methods(self):
141 class DummyMigration(object):
142 @staticmethod
143 def migrate_up(manager):
144 manager.execute_script('foo')
145
146
147 @staticmethod
148 def migrate_down(manager):
149 manager.execute_script('bar')
150
151 self._do_migration(DummyMigration)
152
153
154 def test_migration_with_strings(self):
155 class DummyMigration(object):
156 UP_SQL = 'foo'
157 DOWN_SQL = 'bar'
158
159 self._do_migration(DummyMigration)
160
161
showardf208abc2008-04-01 23:57:11 +0000162if __name__ == '__main__':
jadmanski0afbb632008-06-06 21:10:57 +0000163 unittest.main()