|
| 1 | +require 'spec_helper' |
| 2 | +require 'rubocop' |
| 3 | +require 'rubocop/rspec/support' |
| 4 | +require_relative '../../../../rubocop/cop/migration/update_column_in_batches' |
| 5 | + |
| 6 | +describe RuboCop::Cop::Migration::UpdateColumnInBatches do |
| 7 | + let(:cop) { described_class.new } |
| 8 | + let(:tmp_rails_root) { Rails.root.join('tmp', 'rails_root') } |
| 9 | + let(:migration_code) do |
| 10 | + <<-END |
| 11 | + def up |
| 12 | + update_column_in_batches(:projects, :name, "foo") do |table, query| |
| 13 | + query.where(table[:name].eq(nil)) |
| 14 | + end |
| 15 | + end |
| 16 | + END |
| 17 | + end |
| 18 | + |
| 19 | + before do |
| 20 | + allow(cop).to receive(:rails_root).and_return(tmp_rails_root) |
| 21 | + end |
| 22 | + after do |
| 23 | + FileUtils.rm_rf(tmp_rails_root) |
| 24 | + end |
| 25 | + |
| 26 | + context 'outside of a migration' do |
| 27 | + it 'does not register any offenses' do |
| 28 | + inspect_source(cop, migration_code) |
| 29 | + |
| 30 | + expect(cop.offenses).to be_empty |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + let(:spec_filepath) { tmp_rails_root.join('spec', 'migrations', 'my_super_migration_spec.rb') } |
| 35 | + |
| 36 | + shared_context 'with a migration file' do |
| 37 | + before do |
| 38 | + FileUtils.mkdir_p(File.dirname(migration_filepath)) |
| 39 | + @migration_file = File.new(migration_filepath, 'w+') |
| 40 | + end |
| 41 | + after do |
| 42 | + @migration_file.close |
| 43 | + end |
| 44 | + end |
| 45 | + |
| 46 | + shared_examples 'a migration file with no spec file' do |
| 47 | + include_context 'with a migration file' |
| 48 | + |
| 49 | + let(:relative_spec_filepath) { Pathname.new(spec_filepath).relative_path_from(tmp_rails_root) } |
| 50 | + |
| 51 | + it 'registers an offense when using update_column_in_batches' do |
| 52 | + inspect_source(cop, migration_code, @migration_file) |
| 53 | + |
| 54 | + aggregate_failures do |
| 55 | + expect(cop.offenses.size).to eq(1) |
| 56 | + expect(cop.offenses.map(&:line)).to eq([2]) |
| 57 | + expect(cop.offenses.first.message). |
| 58 | + to include("`#{relative_spec_filepath}`") |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + shared_examples 'a migration file with a spec file' do |
| 64 | + include_context 'with a migration file' |
| 65 | + |
| 66 | + before do |
| 67 | + FileUtils.mkdir_p(File.dirname(spec_filepath)) |
| 68 | + @spec_file = File.new(spec_filepath, 'w+') |
| 69 | + end |
| 70 | + after do |
| 71 | + @spec_file.close |
| 72 | + end |
| 73 | + |
| 74 | + it 'does not register any offenses' do |
| 75 | + inspect_source(cop, migration_code, @migration_file) |
| 76 | + |
| 77 | + expect(cop.offenses).to be_empty |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + context 'in a migration' do |
| 82 | + let(:migration_filepath) { tmp_rails_root.join('db', 'migrate', '20121220064453_my_super_migration.rb') } |
| 83 | + |
| 84 | + it_behaves_like 'a migration file with no spec file' |
| 85 | + it_behaves_like 'a migration file with a spec file' |
| 86 | + end |
| 87 | + |
| 88 | + context 'in a post migration' do |
| 89 | + let(:migration_filepath) { tmp_rails_root.join('db', 'post_migrate', '20121220064453_my_super_migration.rb') } |
| 90 | + |
| 91 | + it_behaves_like 'a migration file with no spec file' |
| 92 | + it_behaves_like 'a migration file with a spec file' |
| 93 | + end |
| 94 | +end |
0 commit comments