2020-05-24 23:13:21 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
require 'fast_spec_helper'
|
2020-05-24 23:13:21 +05:30
|
|
|
require_relative '../../../../rubocop/cop/migration/add_limit_to_text_columns'
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
RSpec.describe RuboCop::Cop::Migration::AddLimitToTextColumns do
|
2020-05-24 23:13:21 +05:30
|
|
|
subject(:cop) { described_class.new }
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
context 'when in migration' do
|
|
|
|
let(:msg) { 'Text columns should always have a limit set (255 is suggested)[...]' }
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
before do
|
|
|
|
allow(cop).to receive(:in_migration?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when text columns are defined without a limit' do
|
|
|
|
it 'registers an offense' do
|
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
class TestTextLimits < ActiveRecord::Migration[6.0]
|
|
|
|
DOWNTIME = false
|
|
|
|
disable_ddl_transaction!
|
|
|
|
|
|
|
|
def up
|
|
|
|
create_table :test_text_limits, id: false do |t|
|
|
|
|
t.integer :test_id, null: false
|
|
|
|
t.text :name
|
2021-04-17 20:07:23 +05:30
|
|
|
^^^^ #{msg}
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
create_table_with_constraints :test_text_limits_create do |t|
|
|
|
|
t.integer :test_id, null: false
|
|
|
|
t.text :title
|
|
|
|
t.text :description
|
2021-04-17 20:07:23 +05:30
|
|
|
^^^^ #{msg}
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
t.text_limit :title, 100
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
add_column :test_text_limits, :email, :text
|
2021-04-17 20:07:23 +05:30
|
|
|
^^^^^^^^^^ #{msg}
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
add_column_with_default :test_text_limits, :role, :text, default: 'default'
|
2021-04-17 20:07:23 +05:30
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
change_column_type_concurrently :test_text_limits, :test_id, :text
|
2021-04-17 20:07:23 +05:30
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when text columns are defined with a limit' do
|
|
|
|
it 'registers no offense' do
|
|
|
|
expect_no_offenses(<<~RUBY)
|
|
|
|
class TestTextLimits < ActiveRecord::Migration[6.0]
|
|
|
|
DOWNTIME = false
|
|
|
|
disable_ddl_transaction!
|
|
|
|
|
|
|
|
def up
|
|
|
|
create_table :test_text_limits, id: false do |t|
|
|
|
|
t.integer :test_id, null: false
|
|
|
|
t.text :name
|
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
create_table_with_constraints :test_text_limits_create do |t|
|
|
|
|
t.integer :test_id, null: false
|
|
|
|
t.text :title
|
|
|
|
t.text :description
|
|
|
|
|
|
|
|
t.text_limit :title, 100
|
|
|
|
t.text_limit :description, 255
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
add_column :test_text_limits, :email, :text
|
|
|
|
add_column_with_default :test_text_limits, :role, :text, default: 'default'
|
|
|
|
change_column_type_concurrently :test_text_limits, :test_id, :text
|
|
|
|
|
|
|
|
add_text_limit :test_text_limits, :name, 255
|
|
|
|
add_text_limit :test_text_limits, :email, 255
|
|
|
|
add_text_limit :test_text_limits, :role, 255
|
|
|
|
add_text_limit :test_text_limits, :test_id, 255
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
context 'when text array columns are defined without a limit' do
|
|
|
|
it 'registers no offense' do
|
|
|
|
expect_no_offenses(<<~RUBY)
|
|
|
|
class TestTextLimits < ActiveRecord::Migration[6.0]
|
|
|
|
DOWNTIME = false
|
|
|
|
|
|
|
|
def up
|
|
|
|
create_table :test_text_limits, id: false do |t|
|
|
|
|
t.integer :test_id, null: false
|
|
|
|
t.text :name, array: true, default: [], null: false
|
|
|
|
end
|
|
|
|
|
|
|
|
add_column :test_text_limits, :email, :text, array: true
|
|
|
|
add_column_with_default :test_text_limits, :role, :text, default: [], array: true
|
|
|
|
change_column_type_concurrently :test_text_limits, :test_id, :text, array: true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
# Make sure that the cop is properly checking for an `add_text_limit`
|
2021-04-17 20:07:23 +05:30
|
|
|
# over the same {table, attribute} as the one that triggered the offense
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'when the limit is defined for a same name attribute but different table' do
|
|
|
|
it 'registers an offense' do
|
|
|
|
expect_offense(<<~RUBY)
|
|
|
|
class TestTextLimits < ActiveRecord::Migration[6.0]
|
|
|
|
DOWNTIME = false
|
|
|
|
disable_ddl_transaction!
|
|
|
|
|
|
|
|
def up
|
|
|
|
create_table :test_text_limits, id: false do |t|
|
|
|
|
t.integer :test_id, null: false
|
|
|
|
t.text :name
|
2021-04-17 20:07:23 +05:30
|
|
|
^^^^ #{msg}
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
add_column :test_text_limits, :email, :text
|
2021-04-17 20:07:23 +05:30
|
|
|
^^^^^^^^^^ #{msg}
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
add_column_with_default :test_text_limits, :role, :text, default: 'default'
|
2021-04-17 20:07:23 +05:30
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
change_column_type_concurrently :test_text_limits, :test_id, :text
|
2021-04-17 20:07:23 +05:30
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
add_text_limit :wrong_table, :name, 255
|
|
|
|
add_text_limit :wrong_table, :email, 255
|
|
|
|
add_text_limit :wrong_table, :role, 255
|
|
|
|
add_text_limit :wrong_table, :test_id, 255
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
context 'when text columns are used for encryption' do
|
|
|
|
it 'registers no offenses' do
|
|
|
|
expect_no_offenses(<<~RUBY)
|
|
|
|
class TestTextLimits < ActiveRecord::Migration[6.0]
|
|
|
|
DOWNTIME = false
|
|
|
|
disable_ddl_transaction!
|
|
|
|
|
|
|
|
def up
|
|
|
|
create_table :test_text_limits, id: false do |t|
|
|
|
|
t.integer :test_id, null: false
|
|
|
|
t.text :encrypted_name
|
|
|
|
end
|
|
|
|
|
|
|
|
add_column :encrypted_test_text_limits, :encrypted_email, :text
|
|
|
|
add_column_with_default :encrypted_test_text_limits, :encrypted_role, :text, default: 'default'
|
|
|
|
change_column_type_concurrently :encrypted_test_text_limits, :encrypted_test_id, :text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'on down' do
|
|
|
|
it 'registers no offense' do
|
|
|
|
expect_no_offenses(<<~RUBY)
|
|
|
|
class TestTextLimits < ActiveRecord::Migration[6.0]
|
|
|
|
DOWNTIME = false
|
|
|
|
|
|
|
|
def up
|
2021-04-17 20:07:23 +05:30
|
|
|
drop_table :no_offense_on_down
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
2021-04-17 20:07:23 +05:30
|
|
|
create_table :no_offense_on_down, id: false do |t|
|
2020-05-24 23:13:21 +05:30
|
|
|
t.integer :test_id, null: false
|
|
|
|
t.text :name
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
add_column :no_offense_on_down, :email, :text
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
add_column_with_default :no_offense_on_down, :role, :text, default: 'default'
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
context 'when outside of migration' do
|
2020-05-24 23:13:21 +05:30
|
|
|
it 'registers no offense' do
|
|
|
|
expect_no_offenses(<<~RUBY)
|
|
|
|
class TestTextLimits < ActiveRecord::Migration[6.0]
|
|
|
|
DOWNTIME = false
|
|
|
|
disable_ddl_transaction!
|
|
|
|
|
|
|
|
def up
|
|
|
|
create_table :test_text_limits, id: false do |t|
|
|
|
|
t.integer :test_id, null: false
|
|
|
|
t.text :name
|
|
|
|
end
|
|
|
|
|
|
|
|
add_column :test_text_limits, :email, :text
|
|
|
|
add_column_with_default :test_text_limits, :role, :text, default: 'default'
|
|
|
|
change_column_type_concurrently :test_text_limits, :test_id, :text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|