def test_migrations_already_run_are_skipped(self, tmpdir): ''' :param tmpdir: Temporary directory provided by fixture. ''' # Count any existing records preexisting_count = db.session.query(Migration).count() # Then create and run 2 migrations and validate the new count m0 = '2016-04-19-14-13-00' m1 = '2016-04-19-14-13-10' name0 = self.create_migration_file(tmpdir, m0) name1 = self.create_migration_file(tmpdir, m1) run_migrations(self.logger, db) current_count = db.session.query(Migration).count() assert current_count == preexisting_count + 2 # Now create two more migrations and validate only two more were run preexisting_count = current_count m2 = '2016-04-19-14-13-20' m3 = '2016-04-19-14-13-30' name2 = self.create_migration_file(tmpdir, m2) name3 = self.create_migration_file(tmpdir, m3) run_migrations(self.logger, db) current_count = db.session.query(Migration).count() assert current_count == preexisting_count + 2
def test_empty_migrations_directory_is_ok(self, tmpdir, mocker): ''' :param tmpdir: Temporary directory provided by fixture. :param mocker: Mock object provided by fixture ''' self.ensure_migrations_directory(tmpdir) mocker.patch.object(self.logger, 'info') run_migrations(self.logger, db) message = "No migrations to process." self.logger.info.assert_called_with(message)
def test_no_migrations_directory_is_ok(self, tmpdir, mocker): ''' :param tmpdir: Temporary directory provided by fixture. :param mocker: Mock object provided by fixture ''' tmpdir.chdir() mocker.patch.object(self.logger, 'info') run_migrations(self.logger, db) message = "No migrations to process (non-existant directory ./migrations)" self.logger.info.assert_called_with(message)
def test_handling_failed_migration_file(self, tmpdir, mocker): ''' :param tmpdir: Temporary directory provided by fixture. :param mocker: Mock object provided by fixture ''' mocker.patch.object(self.logger, 'exception') filename = time.strftime(DATETIME_FORMAT, time.gmtime()) self.create_migration_file(tmpdir, filename, 'break me') run_migrations(self.logger, db) message = "Can't execute migration file ./migrations/{} due to invalid syntax ({}, line 1)".format( filename, filename) self.logger.exception.assert_called_with(message)
def test_bad_migration_file_type(self, tmpdir, mocker): ''' :param tmpdir: Temporary directory provided by fixture. :param mocker: Mock object provided by fixture ''' mocker.patch.object(self.logger, 'exception') migrations_dir = self.ensure_migrations_directory(tmpdir) migrations_subdir = "{}/im_a_directory".format( migrations_dir.__str__()) os.mkdir(migrations_subdir, 0755) run_migrations(self.logger, db) message = "Unexpected non-file found in migrations directory: ./migrations/im_a_directory" self.logger.exception.assert_called_with(message)
def test_bad_migration_filename(self, tmpdir, mocker): ''' :param tmpdir: Temporary directory provided by fixture. :param mocker: Mock object provided by fixture ''' mocker.patch.object(self.logger, 'exception') migrations_dir = self.ensure_migrations_directory(tmpdir) bad_filename = "im-a-bad-bad-filename" bad_migration_pathname = "{}/{}".format(migrations_dir.__str__(), bad_filename) self.create_empty_filename(bad_migration_pathname) run_migrations(self.logger, db) message = "Incorrect migration filename: im-a-bad-bad-filename" self.logger.exception.assert_called_with(message)
def test_migrations_are_run_in_correct_order(self, tmpdir): ''' :param tmpdir: Temporary directory provided by fixture. ''' db.session.execute("truncate {}".format(Migration.__tablename__)) m0 = '2014-04-19-14-13-00' m1 = '2014-04-19-14-13-10' m2 = '2014-04-19-14-13-20' m3 = '2014-04-19-14-13-30' name0 = self.create_migration_file(tmpdir, m0) name1 = self.create_migration_file(tmpdir, m1) name2 = self.create_migration_file(tmpdir, m2) name3 = self.create_migration_file(tmpdir, m3) run_migrations(self.logger, db) migrations0 = db.session.query(Migration).filter( Migration.name == name0).all() if len(migrations0) != 1: raise UnprocessableEntity migrations1 = db.session.query(Migration).filter( Migration.name == name1).all() if len(migrations1) != 1: raise UnprocessableEntity migrations2 = db.session.query(Migration).filter( Migration.name == name2).all() if len(migrations2) != 1: raise UnprocessableEntity migrations3 = db.session.query(Migration).filter( Migration.name == name3).all() if len(migrations3) != 1: raise UnprocessableEntity assert migrations0[0].run_at_timestamp >= migrations1[ 0].run_at_timestamp assert migrations1[0].run_at_timestamp >= migrations2[ 0].run_at_timestamp assert migrations2[0].run_at_timestamp >= migrations3[ 0].run_at_timestamp
def test_missing_migrations_table_is_created(self, tmpdir): ''' :param tmpdir: Temporary directory provided by fixture. ''' # First, create a valid migration file migration_name = self.create_migration_file( tmpdir, time.strftime(DATETIME_FORMAT, time.gmtime())) # Now ensure that no migrations table exists if Migration.__tablename__ in db.engine.table_names(): Migration.__table__.drop(db.engine) db.session.commit() # Run the migration run_migrations(self.logger, db) # Validate that a migration table exists with our filename if Migration.__tablename__ not in db.engine.table_names(): raise ResourceNotFound migrations_found = db.session.query(Migration).filter( Migration.name == migration_name) assert migrations_found.one().name == migration_name