Sha256: fca1f6e2c4c24e0309fd322cd1ce75b527101131e875bdba04bada309c0d3201

Contents?: true

Size: 1.53 KB

Versions: 19

Compression:

Stored size: 1.53 KB

Contents

module SchemaPlus::Matchers

  class HaveIndex

    def initialize(expectation, options = {})
      set_required_columns(expectation)
      @unique = options.delete(:unique)
      @name   = options.delete(:name)
    end

    def matches?(model)
      @too_many = nil
      @model = model
      indexes = @model.indexes.select { |index| index.columns.to_set == @required_columns }
      if indexes.length > 1
        @too_many = indexes.length
        return false
      end
      index = indexes.first
      return index && (@unique ? index.unique : true) && (@name ? index.name == @name.to_s : true)
    end

    def failure_message(should_not = false)
      invert = should_not ? "not to" : "to"
      what = ""
      what += "unique " if @unique
      what += "named '{@name}'" if @name
      msg = "Expected #{@model.table_name} #{invert} contain one #{what}index on #{@required_columns.entries.inspect}"
      msg += "; found #{@too_many} indexes" if @too_many
      msg
    end

    def failure_message_when_negated
      failure_message(true)
    end

    def on(expectation)
      set_required_columns(expectation)
      self
    end

    private
    def set_required_columns(expectation)
      @required_columns = Array(expectation).collect(&:to_s).to_set
    end

  end

  def have_index(*expectation)
    options = expectation.extract_options!
    HaveIndex.new(expectation, options)
  end

  def have_unique_index(*expectation)
    options = expectation.extract_options!
    options[:unique] = true
    HaveIndex.new(expectation, options)
  end

end

Version data entries

19 entries across 19 versions & 2 rubygems

Version Path
schema_auto_foreign_keys-1.1.0 spec/support/matchers/have_index.rb
schema_auto_foreign_keys-1.0.0 spec/support/matchers/have_index.rb
schema_auto_foreign_keys-0.1.3 spec/support/matchers/have_index.rb
schema_auto_foreign_keys-0.1.2 spec/support/matchers/have_index.rb
schema_auto_foreign_keys-0.1.1 spec/support/matchers/have_index.rb
schema_plus-2.0.1 spec/support/matchers/have_index.rb
schema_plus-2.0.0 spec/support/matchers/have_index.rb
schema_auto_foreign_keys-0.1.0 spec/support/matchers/have_index.rb
schema_plus-2.0.0.pre16 spec/support/matchers/have_index.rb
schema_plus-2.0.0.pre15 spec/support/matchers/have_index.rb
schema_plus-2.0.0.pre14 spec/support/matchers/have_index.rb
schema_plus-2.0.0.pre13 spec/support/matchers/have_index.rb
schema_plus-2.0.0.pre12 spec/support/matchers/have_index.rb
schema_plus-2.0.0.pre11 spec/support/matchers/have_index.rb
schema_plus-2.0.0.pre10 spec/support/matchers/have_index.rb
schema_plus-2.0.0.pre9 spec/support/matchers/have_index.rb
schema_plus-2.0.0.pre8 spec/support/matchers/have_index.rb
schema_plus-2.0.0.pre7 spec/support/matchers/have_index.rb
schema_plus-2.0.0.pre6 spec/support/matchers/have_index.rb