# frozen_string_literal: true

require 'fast_spec_helper'
require 'rubocop'
require_relative '../../../../rubocop/cop/migration/add_reference'

RSpec.describe RuboCop::Cop::Migration::AddReference, type: :rubocop 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

    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}
        end
        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
    end

    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)
        def up
          #{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}
          add_reference(:projects, :users, index: false)
          ^^^^^^^^^^^^^ #{offense}
        end
        RUBY
      end

      it 'does not register an offense when using add_reference with index enabled' do
        expect_no_offenses(<<~RUBY)
        def up
          #{create_table_statement}
          add_reference(:projects, :users, index: true)
        end
        RUBY
      end

      it 'does not register an offense when the index is unique' do
        expect_no_offenses(<<~RUBY)
        def up
          #{create_table_statement}
          add_reference(:projects, :users, index: { unique: true } )
        end
        RUBY
      end
    end
  end
end
