2019-10-29 08:06:40 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-22 17:08:42 -04:00
|
|
|
require 'fast_spec_helper'
|
2017-12-11 11:34:51 -05:00
|
|
|
require_relative '../../../../rubocop/cop/migration/remove_column'
|
|
|
|
|
2021-01-07 13:10:38 -05:00
|
|
|
RSpec.describe RuboCop::Cop::Migration::RemoveColumn do
|
2017-12-11 11:34:51 -05:00
|
|
|
subject(:cop) { described_class.new }
|
|
|
|
|
|
|
|
def source(meth = 'change')
|
|
|
|
"def #{meth}; remove_column :table, :column; end"
|
|
|
|
end
|
|
|
|
|
2021-02-17 16:09:06 -05:00
|
|
|
context 'when in a regular migration' do
|
2017-12-11 11:34:51 -05:00
|
|
|
before do
|
|
|
|
allow(cop).to receive(:in_migration?).and_return(true)
|
|
|
|
allow(cop).to receive(:in_post_deployment_migration?).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers an offense when remove_column is used in the change method' do
|
2021-02-17 16:09:06 -05:00
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
def change
|
|
|
|
remove_column :table, :column
|
|
|
|
^^^^^^^^^^^^^ `remove_column` must only be used in post-deployment migrations
|
|
|
|
end
|
|
|
|
RUBY
|
2017-12-11 11:34:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers an offense when remove_column is used in the up method' do
|
2021-02-17 16:09:06 -05:00
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
def up
|
|
|
|
remove_column :table, :column
|
|
|
|
^^^^^^^^^^^^^ `remove_column` must only be used in post-deployment migrations
|
|
|
|
end
|
|
|
|
RUBY
|
2017-12-11 11:34:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers no offense when remove_column is used in the down method' do
|
2021-02-17 16:09:06 -05:00
|
|
|
expect_no_offenses(source('down'))
|
2017-12-11 11:34:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-17 16:09:06 -05:00
|
|
|
context 'when in a post-deployment migration' do
|
2017-12-11 11:34:51 -05:00
|
|
|
before do
|
|
|
|
allow(cop).to receive(:in_migration?).and_return(true)
|
|
|
|
allow(cop).to receive(:in_post_deployment_migration?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers no offense' do
|
2021-02-17 16:09:06 -05:00
|
|
|
expect_no_offenses(source)
|
2017-12-11 11:34:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-17 16:09:06 -05:00
|
|
|
context 'when outside of a migration' do
|
2017-12-11 11:34:51 -05:00
|
|
|
it 'registers no offense' do
|
2021-02-17 16:09:06 -05:00
|
|
|
expect_no_offenses(source)
|
2017-12-11 11:34:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|