Sha256: 9ec3f9d4cb62e2ff7a5d5b8e3365040f8c59abfc5ed9b761d84fe1e25809e18b

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

module SearchHelper
  def stub_full_results(*results)
    count =
      if results.last.is_a?(Integer) then results.pop
      else results.length
      end
    docs = results.map do |result|
      instance = result.delete('instance')
      result.merge('id' => "#{instance.class.name} #{instance.id}")
    end
    response = {
      'response' => {
        'docs' => docs,
        'numFound' => count
      }
    }
    connection.stub!(:select).and_return(response)
    response
  end

  def stub_results(*results)
    stub_full_results(
      *results.map do |result|
        if result.is_a?(Integer)
          result
        else
          { 'instance' => result }
        end
      end
    )
  end

  def stub_facet(name, values)
    connection.stub!(:select).and_return(
      'facet_counts' => {
        'facet_fields' => {
          name.to_s => values.to_a.sort_by { |value, count| -count }.flatten
        }
      }
    )
  end

  def stub_date_facet(name, gap, values)
    connection.stub!(:select).and_return(
      'facet_counts' => {
        'facet_dates' => {
          name.to_s => { 'gap' => "+#{gap}SECONDS" }.merge(values)
        }
      }
    )
  end

  def stub_query_facet(values)
    connection.stub!(:select).and_return(
      'facet_counts' => { 'facet_queries' => values }
    )
  end

  def facet_values(result, field_name)
    result.facet(field_name).rows.map { |row| row.value }
  end

  def facet_counts(result, field_name)
    result.facet(field_name).rows.map { |row| row.count }
  end

  def config
    @config ||= Sunspot::Configuration.build
  end

  def connection
    @connection ||= mock('connection')
  end

  def session
    @session ||= Sunspot::Session.new(config, connection)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
benjaminkrause-sunspot-0.9.7 spec/helpers/search_helper.rb