2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
require 'rubocop_spec_helper'
|
2018-03-17 18:26:18 +05:30
|
|
|
require_relative '../../../../rubocop/cop/migration/safer_boolean_column'
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
RSpec.describe RuboCop::Cop::Migration::SaferBooleanColumn do
|
2018-03-17 18:26:18 +05:30
|
|
|
context 'in migration' do
|
|
|
|
before do
|
|
|
|
allow(cop).to receive(:in_migration?).and_return(true)
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
described_class::SMALL_TABLES.each do |table|
|
2018-03-17 18:26:18 +05:30
|
|
|
context "for the #{table} table" do
|
|
|
|
sources_and_offense = [
|
|
|
|
["add_column :#{table}, :column, :boolean, default: true", 'should disallow nulls'],
|
|
|
|
["add_column :#{table}, :column, :boolean, default: false", 'should disallow nulls'],
|
|
|
|
["add_column :#{table}, :column, :boolean, default: nil", 'should have a default and should disallow nulls'],
|
|
|
|
["add_column :#{table}, :column, :boolean, null: false", 'should have a default'],
|
|
|
|
["add_column :#{table}, :column, :boolean, null: true", 'should have a default and should disallow nulls'],
|
|
|
|
["add_column :#{table}, :column, :boolean", 'should have a default and should disallow nulls'],
|
|
|
|
["add_column :#{table}, :column, :boolean, default: nil, null: false", 'should have a default'],
|
|
|
|
["add_column :#{table}, :column, :boolean, default: nil, null: true", 'should have a default and should disallow nulls'],
|
|
|
|
["add_column :#{table}, :column, :boolean, default: false, null: true", 'should disallow nulls']
|
|
|
|
]
|
|
|
|
|
|
|
|
sources_and_offense.each do |source, offense|
|
|
|
|
context "given the source \"#{source}\"" do
|
|
|
|
it "registers the offense matching \"#{offense}\"" do
|
2021-04-17 20:07:23 +05:30
|
|
|
expect_offense(<<~RUBY, node: source, msg: offense)
|
|
|
|
%{node}
|
|
|
|
^{node} Boolean columns on the `#{table}` table %{msg}.[...]
|
|
|
|
RUBY
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
inoffensive_sources = [
|
|
|
|
"add_column :#{table}, :column, :boolean, default: true, null: false",
|
|
|
|
"add_column :#{table}, :column, :boolean, default: false, null: false"
|
|
|
|
]
|
|
|
|
|
|
|
|
inoffensive_sources.each do |source|
|
|
|
|
context "given the source \"#{source}\"" do
|
|
|
|
it "registers no offense" do
|
2021-04-17 20:07:23 +05:30
|
|
|
expect_no_offenses(source)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
it 'registers no offense for tables not listed in SMALL_TABLES' do
|
2021-04-17 20:07:23 +05:30
|
|
|
expect_no_offenses("add_column :large_table, :column, :boolean")
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'registers no offense for non-boolean columns' do
|
2020-11-24 15:15:51 +05:30
|
|
|
table = described_class::SMALL_TABLES.sample
|
2021-04-17 20:07:23 +05:30
|
|
|
expect_no_offenses("add_column :#{table}, :column, :string")
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'outside of migration' do
|
|
|
|
it 'registers no offense' do
|
2020-11-24 15:15:51 +05:30
|
|
|
table = described_class::SMALL_TABLES.sample
|
2021-04-17 20:07:23 +05:30
|
|
|
expect_no_offenses("add_column :#{table}, :column, :boolean")
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|