Sha256: 559961c4955716ae91f51f93194e385cc385f7f716afdd7c86d0be6b1a5953a2

Contents?: true

Size: 1.7 KB

Versions: 19

Compression:

Stored size: 1.7 KB

Contents

module SearchHelper
  def stub_nil_results
    connection.response = { 'response' => nil }
  end

  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.response = 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.response = {
      '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.response = {
      'facet_counts' => {
        'facet_dates' => {
          name.to_s => { 'gap' => "+#{gap}SECONDS" }.merge(values)
        }
      }
    }
  end

  def stub_query_facet(values)
    connection.response = { '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.new
  end

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

Version data entries

19 entries across 19 versions & 7 rubygems

Version Path
pduey-sunspot-1.2.1.1 spec/helpers/search_helper.rb
erichummel-sunspot-1.2.1g spec/helpers/search_helper.rb
erichummel-sunspot-1.2.1f spec/helpers/search_helper.rb
erichummel-sunspot-1.2.1b spec/helpers/search_helper.rb
erichummel-sunspot-1.2.1a spec/helpers/search_helper.rb
erichummel-sunspot-1.2.1 spec/helpers/search_helper.rb
lisausa-sunspot-1.2.1.1 spec/helpers/search_helper.rb
lisausa-sunspot-1.2.1 spec/helpers/search_helper.rb
sunspot_rbg-1.3.1 spec/helpers/search_helper.rb
sunspot_rbg-1.3.0 spec/helpers/search_helper.rb
ruben-sunspot-1.1.4 spec/helpers/search_helper.rb
ruben-sunspot-1.1.3 spec/helpers/search_helper.rb
ruben-sunspot-1.1.2 spec/helpers/search_helper.rb
sunspot-1.2.1 spec/helpers/search_helper.rb
sunspot-1.2.0 spec/helpers/search_helper.rb
ruben-sunspot-1.1.1 spec/helpers/search_helper.rb
ruben-sunspot-1.1.0 spec/helpers/search_helper.rb
nuatt_sunspot-1.1.0.3 spec/helpers/search_helper.rb
sunspot-1.1.0 spec/helpers/search_helper.rb