2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
require 'fast_spec_helper'
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
require 'rubocop'
|
|
|
|
require 'rubocop/rspec/support'
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
require_relative '../../../../rubocop/cop/migration/update_column_in_batches'
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
RSpec.describe RuboCop::Cop::Migration::UpdateColumnInBatches, type: :rubocop do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:cop) { described_class.new }
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:tmp_rails_root) { rails_root_join('tmp', 'rails_root') }
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:migration_code) do
|
|
|
|
<<-END
|
|
|
|
def up
|
|
|
|
update_column_in_batches(:projects, :name, "foo") do |table, query|
|
|
|
|
query.where(table[:name].eq(nil))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
END
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(cop).to receive(:rails_root).and_return(tmp_rails_root)
|
|
|
|
end
|
|
|
|
after do
|
|
|
|
FileUtils.rm_rf(tmp_rails_root)
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:spec_filepath) { File.join(tmp_rails_root, 'spec', 'migrations', 'my_super_migration_spec.rb') }
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'outside of a migration' do
|
|
|
|
it 'does not register any offenses' do
|
2018-03-17 18:26:18 +05:30
|
|
|
inspect_source(migration_code)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
expect(cop.offenses).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_context 'with a migration file' do
|
|
|
|
before do
|
|
|
|
FileUtils.mkdir_p(File.dirname(migration_filepath))
|
|
|
|
@migration_file = File.new(migration_filepath, 'w+')
|
|
|
|
end
|
|
|
|
after do
|
|
|
|
@migration_file.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'a migration file with no spec file' do
|
|
|
|
include_context 'with a migration file'
|
|
|
|
|
|
|
|
let(:relative_spec_filepath) { Pathname.new(spec_filepath).relative_path_from(tmp_rails_root) }
|
|
|
|
|
|
|
|
it 'registers an offense when using update_column_in_batches' do
|
2018-03-17 18:26:18 +05:30
|
|
|
inspect_source(migration_code, @migration_file)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
aggregate_failures do
|
|
|
|
expect(cop.offenses.size).to eq(1)
|
|
|
|
expect(cop.offenses.map(&:line)).to eq([2])
|
|
|
|
expect(cop.offenses.first.message)
|
|
|
|
.to include("`#{relative_spec_filepath}`")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'a migration file with a spec file' do
|
|
|
|
include_context 'with a migration file'
|
|
|
|
|
|
|
|
before do
|
|
|
|
FileUtils.mkdir_p(File.dirname(spec_filepath))
|
|
|
|
@spec_file = File.new(spec_filepath, 'w+')
|
|
|
|
end
|
|
|
|
after do
|
|
|
|
@spec_file.close
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not register any offenses' do
|
2018-03-17 18:26:18 +05:30
|
|
|
inspect_source(migration_code, @migration_file)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
expect(cop.offenses).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'in a migration' do
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:migration_filepath) { File.join(tmp_rails_root, 'db', 'migrate', '20121220064453_my_super_migration.rb') }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
it_behaves_like 'a migration file with no spec file'
|
|
|
|
it_behaves_like 'a migration file with a spec file'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'in a post migration' do
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:migration_filepath) { File.join(tmp_rails_root, 'db', 'post_migrate', '20121220064453_my_super_migration.rb') }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
it_behaves_like 'a migration file with no spec file'
|
|
|
|
it_behaves_like 'a migration file with a spec file'
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
context 'EE migrations' do
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:spec_filepath) { File.join(tmp_rails_root, 'ee', 'spec', 'migrations', 'my_super_migration_spec.rb') }
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
context 'in a migration' do
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:migration_filepath) { File.join(tmp_rails_root, 'ee', 'db', 'migrate', '20121220064453_my_super_migration.rb') }
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
it_behaves_like 'a migration file with no spec file'
|
|
|
|
it_behaves_like 'a migration file with a spec file'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'in a post migration' do
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:migration_filepath) { File.join(tmp_rails_root, 'ee', 'db', 'post_migrate', '20121220064453_my_super_migration.rb') }
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
it_behaves_like 'a migration file with no spec file'
|
|
|
|
it_behaves_like 'a migration file with a spec file'
|
|
|
|
end
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|