def setUp(self): class Migrations(migopy.MigrationsManager): MONGO_DATABASE = 'migopy_db_test' self.Migrations = Migrations self.migr_mng = Migrations() self.tmp_dir = TestDirectory() self.tmp_dir.__enter__() self.tmp_dir.mkdir('mongomigrations') for path in files: self.tmp_dir.create_file(path, files[path])
def test_it_returns_unregistered_migrations_in_order(self): with TestDirectory() as test_dir: test_dir.mkdir('mongomigrations') test_dir.touch('mongomigrations/1_test.py') test_dir.touch('mongomigrations/12_test.py') test_dir.touch('mongomigrations/3_test.py') self.migr_mng.collection = \ MigrationsCollectionMock(['1_test.py']) unregistered = self.migr_mng.unregistered() self.assertEqual(unregistered, ['3_test.py', '12_test.py']) # when migrations directory is not python module, # creates __init__.py self.assertTrue(os.path.exists('mongomigrations/__init__.py')) # when no migrations directory founded, raise exception test_dir.clear() with self.assertRaises(migopy.MigopyException) as cm: self.migr_mng.unregistered() self.assertTrue(cm.exception.message.startswith("Migrations dir"))
def test_it_prints_status_of_migrations(self): # given test directory with TestDirectory() as test_dir: self.migr_mng.collection = MigrationsCollectionMock() test_dir.mkdir('mongomigrations') # when no migrations files found, show 'all registered' self.migr_mng.show_status() self.assertEqual(self.migr_mng.logger.green.call_count, 1, "Not logged with green message") # when some files found, check them and show status test_dir.touch('mongomigrations/1_test.py') test_dir.touch('mongomigrations/002_test.py') self.migr_mng.logger.reset_mock() self.migr_mng.show_status() self.assertEqual(self.migr_mng.logger.white_bold.call_count, 1, "Not logged with white message") self.migr_mng.logger.red.\ assert_has_calls([mock.call('1_test.py'), mock.call('002_test.py')])
class MongoMigrationsIntegratedBehavior(unittest.TestCase): def setUp(self): class Migrations(migopy.MigrationsManager): MONGO_DATABASE = 'migopy_db_test' self.Migrations = Migrations self.migr_mng = Migrations() self.tmp_dir = TestDirectory() self.tmp_dir.__enter__() self.tmp_dir.mkdir('mongomigrations') for path in files: self.tmp_dir.create_file(path, files[path]) def tearDown(self): self.migr_mng.mongo_client.drop_database(self.migr_mng.MONGO_DATABASE) self.tmp_dir.__exit__(None, None, None) def test_it_connects_with_mongo_database(self): self.migr_mng.db.migo_coll.insert({'name': 'migo_test'}) self.assertFalse( self.migr_mng.db.migo_coll.find_one({'name': 'test_migo'})) self.assertTrue( self.migr_mng.db.migo_coll.find_one({'name': 'migo_test'})) # when user data given to connection self.migr_mng.db.add_user('migopy', 'migopy_pass') class Migrations(migopy.MigrationsManager): MONGO_DATABASE = 'migopy_db_test' MONGO_USER = '******' MONGO_USER_PASS = '******' Migrations() self.migr_mng.db.remove_user('migopy') def test_it_do_fab_migrations(self): self.migr_mng.collection.insert({'name': '001_test.py'}) msg = call('fab migrations') self.assertIn('2_test.py', msg) self.assertNotIn('001_test.py', msg) def test_it_do_fab_migrations_execute(self): call('fab migrations:execute') msg = call('fab migrations') self.assertNotIn('001_test.py', msg) self.assertNotIn('2_test.py', msg) self.assertTrue( self.migr_mng.db.test_collection.find_one( {'test_key': 'test_content'})) self.assertIn('All migrations', msg) def test_it_do_fab_migrations_execute_001_test(self): call('fab migrations:execute,001_test.py') msg = call('fab migrations') self.assertNotIn('001_test.py', msg) self.assertIn('2_test.py', msg) def test_it_do_fab_migrations_ignore(self): call('fab migrations:ignore') msg = call('fab migrations') self.assertNotIn('001_test.py', msg) self.assertNotIn('2_test.py', msg) self.assertIn('All migrations', msg) def test_it_do_fab_migrations_ignore_001_test(self): call('fab migrations:ignore,001_test.py') msg = call('fab migrations') self.assertNotIn('001_test.py', msg) self.assertIn('2_test.py', msg) def test_it_do_fab_migrations_dbdump(self): self.assertEqual(len(glob('mongodumps/*')), 0) call('fab migrations:dbdump') self.assertEqual(len(glob('mongodumps/*')), 1) def test_it_do_dbdump_during_fab_migrations_execute(self): self.assertEqual(len(glob('mongodumps/*')), 0) self.tmp_dir.create_file('fabfile.py', fabfile_with_mongodump) call('fab migrations:execute') self.assertEqual(len(glob('mongodumps/*')), 1) def test_it_works_with_fabric_task_decorator(self): self.tmp_dir.create_file('fabfile.py', fabfile_with_task) msg = call('fab migrations') self.assertNotIn('not found', msg, msg) self.assertIn('2_test.py', msg) self.assertIn('001_test.py', msg)
class MongoMigrationsIntegratedBehavior(unittest.TestCase): def setUp(self): class Migrations(migopy.MigrationsManager): MONGO_DATABASE = 'migopy_db_test' self.Migrations = Migrations self.migr_mng = Migrations() self.tmp_dir = TestDirectory() self.tmp_dir.__enter__() self.tmp_dir.mkdir('mongomigrations') for path in files: self.tmp_dir.create_file(path, files[path]) def tearDown(self): self.migr_mng.mongo_client.drop_database(self.migr_mng.MONGO_DATABASE) self.tmp_dir.__exit__(None, None, None) def test_it_connects_with_mongo_database(self): self.migr_mng.db.migo_coll.insert({'name': 'migo_test'}) self.assertFalse( self.migr_mng.db.migo_coll.find_one({'name': 'test_migo'})) self.assertTrue( self.migr_mng.db.migo_coll.find_one({'name': 'migo_test'})) # when user data given to connection self.migr_mng.db.add_user('migopy', 'migopy_pass') class Migrations(migopy.MigrationsManager): MONGO_DATABASE = 'migopy_db_test' MONGO_USER = '******' MONGO_USER_PASS = '******' Migrations() self.migr_mng.db.remove_user('migopy') def test_it_do_fab_migrations(self): self.migr_mng.collection.insert({'name': '001_test.py'}) msg = call('fab migrations') self.assertIn('2_test.py', msg) self.assertNotIn('001_test.py', msg) def test_it_do_fab_migrations_execute(self): call('fab migrations:execute') msg = call('fab migrations') self.assertNotIn('001_test.py', msg) self.assertNotIn('2_test.py', msg) self.assertTrue(self.migr_mng.db.test_collection.find_one( {'test_key': 'test_content'})) self.assertIn('All migrations', msg) def test_it_do_fab_migrations_execute_001_test(self): call('fab migrations:execute,001_test.py') msg = call('fab migrations') self.assertNotIn('001_test.py', msg) self.assertIn('2_test.py', msg) def test_it_do_fab_migrations_ignore(self): call('fab migrations:ignore') msg = call('fab migrations') self.assertNotIn('001_test.py', msg) self.assertNotIn('2_test.py', msg) self.assertIn('All migrations', msg) def test_it_do_fab_migrations_ignore_001_test(self): call('fab migrations:ignore,001_test.py') msg = call('fab migrations') self.assertNotIn('001_test.py', msg) self.assertIn('2_test.py', msg) def test_it_do_fab_migrations_dbdump(self): self.assertEqual(len(glob('mongodumps/*')), 0) call('fab migrations:dbdump') self.assertEqual(len(glob('mongodumps/*')), 1) def test_it_do_dbdump_during_fab_migrations_execute(self): self.assertEqual(len(glob('mongodumps/*')), 0) self.tmp_dir.create_file('fabfile.py', fabfile_with_mongodump) call('fab migrations:execute') self.assertEqual(len(glob('mongodumps/*')), 1) def test_it_works_with_fabric_task_decorator(self): self.tmp_dir.create_file('fabfile.py', fabfile_with_task) msg = call('fab migrations') self.assertNotIn('not found', msg, msg) self.assertIn('2_test.py', msg) self.assertIn('001_test.py', msg)