2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2018-11-20 20:47:30 +05:30
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/migration/add_reference'
describe RuboCop :: Cop :: Migration :: AddReference do
include CopHelper
let ( :cop ) { described_class . new }
context 'outside of a migration' do
it 'does not register any offenses' do
expect_no_offenses ( << ~ RUBY )
def up
add_reference ( :projects , :users )
end
RUBY
end
end
context 'in a migration' do
before do
allow ( cop ) . to receive ( :in_migration? ) . and_return ( true )
end
2019-12-04 20:38:33 +05:30
let ( :offense ) { '`add_reference` requires downtime for existing tables, use `add_concurrent_foreign_key` instead. When used for new tables, `index: true` or `index: { options... } is required.`' }
context 'when the table existed before' do
it 'registers an offense when using add_reference' do
expect_offense ( << ~ RUBY )
def up
add_reference ( :projects , :users )
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ #{offense}
end
RUBY
end
it 'registers an offense when using add_reference with index enabled' do
expect_offense ( << ~ RUBY )
def up
add_reference ( :projects , :users , index : true )
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ #{offense}
2018-11-20 20:47:30 +05:30
end
2019-12-04 20:38:33 +05:30
RUBY
end
it 'registers an offense if only a different table was created' do
expect_offense ( << ~ RUBY )
def up
create_table ( :foo ) do | t |
t . string :name
end
add_reference ( :projects , :users , index : true )
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ #{offense}
end
RUBY
end
2018-11-20 20:47:30 +05:30
end
2019-12-04 20:38:33 +05:30
context 'when creating the table at the same time' do
let ( :create_table_statement ) do
<< ~ RUBY
create_table ( :projects ) do | t |
t . string :name
end
RUBY
end
it 'registers an offense when using add_reference without index' do
expect_offense ( << ~ RUBY )
2018-11-20 20:47:30 +05:30
def up
2019-12-04 20:38:33 +05:30
#{create_table_statement}
add_reference ( :projects , :users )
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ #{offense}
end
RUBY
end
it 'registers an offense when using add_reference index disabled' do
expect_offense ( << ~ RUBY )
def up
#{create_table_statement}
2018-11-20 20:47:30 +05:30
add_reference ( :projects , :users , index : false )
2019-12-04 20:38:33 +05:30
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ #{offense}
2018-11-20 20:47:30 +05:30
end
2019-12-04 20:38:33 +05:30
RUBY
end
2018-11-20 20:47:30 +05:30
2019-12-04 20:38:33 +05:30
it 'does not register an offense when using add_reference with index enabled' do
expect_no_offenses ( << ~ RUBY )
2018-11-20 20:47:30 +05:30
def up
2019-12-04 20:38:33 +05:30
#{create_table_statement}
2018-11-20 20:47:30 +05:30
add_reference ( :projects , :users , index : true )
end
2019-12-04 20:38:33 +05:30
RUBY
end
2019-02-15 15:39:39 +05:30
2019-12-04 20:38:33 +05:30
it 'does not register an offense when the index is unique' do
expect_no_offenses ( << ~ RUBY )
2019-02-15 15:39:39 +05:30
def up
2019-12-04 20:38:33 +05:30
#{create_table_statement}
2019-02-15 15:39:39 +05:30
add_reference ( :projects , :users , index : { unique : true } )
end
2019-12-04 20:38:33 +05:30
RUBY
end
2019-02-15 15:39:39 +05:30
end
2018-11-20 20:47:30 +05:30
end
end