Best Python code snippet using autotest_python
migrate_unittest.py
Source:migrate_unittest.py
...20 @classmethod21 def get_migrations_done(cls):22 return cls.migrations_done23 @classmethod24 def clear_migrations_done(cls):25 cls.migrations_done = []26 @classmethod27 def do_migration(cls, version, direction):28 cls.migrations_done.append((version, direction))29 def migrate_up(self, manager):30 self.do_migration(self.version, 'up')31 if self.version == 1:32 manager.create_migrate_table()33 def migrate_down(self, manager):34 self.do_migration(self.version, 'down')35MIGRATIONS = [DummyMigration(n) for n in xrange(1, NUM_MIGRATIONS + 1)]36class TestableMigrationManager(migrate.MigrationManager):37 def _set_migrations_dir(self, migrations_dir=None):38 pass39 def get_migrations(self, minimum_version=None, maximum_version=None):40 minimum_version = minimum_version or 141 maximum_version = maximum_version or len(MIGRATIONS)42 return MIGRATIONS[minimum_version-1:maximum_version]43class MigrateManagerTest(unittest.TestCase):44 def setUp(self):45 self._database = (46 database_connection.DatabaseConnection.get_test_database())47 self._database.connect()48 self.manager = TestableMigrationManager(self._database)49 DummyMigration.clear_migrations_done()50 def tearDown(self):51 self._database.disconnect()52 def test_sync(self):53 self.manager.do_sync_db()54 self.assertEquals(self.manager.get_db_version(), NUM_MIGRATIONS)55 self.assertEquals(DummyMigration.get_migrations_done(),56 [(1, 'up'), (2, 'up'), (3, 'up')])57 DummyMigration.clear_migrations_done()58 self.manager.do_sync_db(0)59 self.assertEquals(self.manager.get_db_version(), 0)60 self.assertEquals(DummyMigration.get_migrations_done(),61 [(3, 'down'), (2, 'down'), (1, 'down')])62 def test_sync_one_by_one(self):63 for version in xrange(1, NUM_MIGRATIONS + 1):64 self.manager.do_sync_db(version)65 self.assertEquals(self.manager.get_db_version(),66 version)67 self.assertEquals(68 DummyMigration.get_migrations_done()[-1],69 (version, 'up'))70 for version in xrange(NUM_MIGRATIONS - 1, -1, -1):71 self.manager.do_sync_db(version)72 self.assertEquals(self.manager.get_db_version(),73 version)74 self.assertEquals(75 DummyMigration.get_migrations_done()[-1],76 (version + 1, 'down'))77 def test_null_sync(self):78 self.manager.do_sync_db()79 DummyMigration.clear_migrations_done()80 self.manager.do_sync_db()81 self.assertEquals(DummyMigration.get_migrations_done(), [])82class DummyMigrationManager(object):83 def __init__(self):84 self.calls = []85 def execute_script(self, script):86 self.calls.append(script)87class MigrationTest(unittest.TestCase):88 def setUp(self):89 self.manager = DummyMigrationManager()90 def _do_migration(self, migration_module):91 migration = migrate.Migration('name', 1, migration_module)92 migration.migrate_up(self.manager)93 migration.migrate_down(self.manager)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!