Sha256: 05091190e6a7b351f11e9f0ae29cb3da08732310978e79078b0f532e0a96e889

Contents?: true

Size: 1.53 KB

Versions: 18

Compression:

Stored size: 1.53 KB

Contents

module SchemaPlusPgIndexesMatchers

  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

18 entries across 18 versions & 1 rubygems

Version Path
schema_plus_pg_indexes-0.3.2 spec/support/matchers/have_index.rb
schema_plus_pg_indexes-0.3.1 spec/support/matchers/have_index.rb
schema_plus_pg_indexes-0.3.0 spec/support/matchers/have_index.rb
schema_plus_pg_indexes-0.2.1 spec/support/matchers/have_index.rb
schema_plus_pg_indexes-0.2.0 spec/support/matchers/have_index.rb
schema_plus_pg_indexes-0.1.12 spec/support/matchers/have_index.rb
schema_plus_pg_indexes-0.1.11 spec/support/matchers/have_index.rb
schema_plus_pg_indexes-0.1.10 spec/support/matchers/have_index.rb
schema_plus_pg_indexes-0.1.9 spec/support/matchers/have_index.rb
schema_plus_pg_indexes-0.1.8 spec/support/matchers/have_index.rb
schema_plus_pg_indexes-0.1.7 spec/support/matchers/have_index.rb
schema_plus_pg_indexes-0.1.6 spec/support/matchers/have_index.rb
schema_plus_pg_indexes-0.1.5 spec/support/matchers/have_index.rb
schema_plus_pg_indexes-0.1.4 spec/support/matchers/have_index.rb
schema_plus_pg_indexes-0.1.3 spec/support/matchers/have_index.rb
schema_plus_pg_indexes-0.1.2 spec/support/matchers/have_index.rb
schema_plus_pg_indexes-0.1.1 spec/support/matchers/have_index.rb
schema_plus_pg_indexes-0.1.0 spec/support/matchers/have_index.rb