Sha256: 51aeee0e179288e52a5a5d222119dec24c8bad114dd5cd0a85f4fd6a11605c5c

Contents?: true

Size: 1.63 KB

Versions: 5

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

class ActiveRecordDoctor::Detectors::MissingForeignKeysTest < Minitest::Test
  def test_missing_foreign_key_is_reported
    create_table(:companies)
    create_table(:users) do |t|
      t.references :company, foreign_key: false
    end

    assert_problems(<<~OUTPUT)
      create a foreign key on users.company_id - looks like an association without a foreign key constraint
    OUTPUT
  end

  def test_present_foreign_key_is_not_reported
    create_table(:companies)
    create_table(:users) do |t|
      t.references :company, foreign_key: true
    end

    refute_problems
  end

  def test_config_ignore_models
    create_table(:companies)
    create_table(:users) do |t|
      t.references :company, foreign_key: false
    end

    config_file(<<-CONFIG)
      ActiveRecordDoctor.configure do |config|
        config.detector :missing_foreign_keys,
          ignore_tables: ["users"]
      end
    CONFIG

    refute_problems
  end

  def test_global_ignore_models
    create_table(:companies)
    create_table(:users) do |t|
      t.references :company, foreign_key: false
    end

    config_file(<<-CONFIG)
      ActiveRecordDoctor.configure do |config|
        config.global :ignore_tables, ["users"]
      end
    CONFIG

    refute_problems
  end

  def test_config_ignore_columns
    create_table(:companies)
    create_table(:users) do |t|
      t.references :company, foreign_key: false
    end

    config_file(<<-CONFIG)
      ActiveRecordDoctor.configure do |config|
        config.detector :missing_foreign_keys,
          ignore_columns: ["users.company_id"]
      end
    CONFIG

    refute_problems
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
active_record_doctor-1.12.0 test/active_record_doctor/detectors/missing_foreign_keys_test.rb
active_record_doctor-1.11.0 test/active_record_doctor/detectors/missing_foreign_keys_test.rb
active_record_doctor-1.10.0 test/active_record_doctor/detectors/missing_foreign_keys_test.rb
active_record_doctor-1.9.0 test/active_record_doctor/detectors/missing_foreign_keys_test.rb
active_record_doctor-1.9.0.rc1 test/active_record_doctor/detectors/missing_foreign_keys_test.rb