Sha256: 844428ea99b1a27a25f8a2d507ef4d3b7986dfba70c4c05651a8fbadfbd49160

Contents?: true

Size: 1.86 KB

Versions: 2

Compression:

Stored size: 1.86 KB

Contents

# A fake engine that simply makes up it's results, used in testing. 
#
# Used in BentoSearch's own automated tests, but exposed publically because
# can be useful to use in your tests for software that uses BentoSearch too. 
#
# The nature of the fake results can be controlled by config variables:
#
# [:total_items]  total_items to report
# [:sort_definitions] hash for #sort_definitions
# [:link]             link to give to each item in results
# [:error]            set to an error value hash and results returned
#                     will all be failed? with that error hash. 
# [:timing]           in seconds, fill out the results as if they took
#                     this long. 
class BentoSearch::MockEngine
    include BentoSearch::SearchEngine
    
    # used for testing what the engine received as args
    attr_accessor :last_args
    
    def search_implementation(args)
      self.last_args = args
      
      results = BentoSearch::Results.new
      
      if configuration.error
        results.error = configuration.error
        return results
      end
      
      1.upto(configuration.num_results || args[:per_page] ) do |i|
        results << BentoSearch::ResultItem.new(:title => "Item #{i}: #{args[:query]}", :link => configuration.link)
      end
      results.total_items = configuration.total_items      
      return results
    end    
    
    def search(*args)
      results = super(*args)
      results.timing = configuration.timing if configuration.timing
      return results
    end
    
    def self.default_configuration
      { :num_results => nil,
        :total_items => 1000, 
        :link => "http://example.org",
        :error => nil,
        :timing => nil}
    end
    
    def sort_definitions
      configuration.sort_definitions || {}
    end
    
    def search_field_definitions
      configuration.search_field_definitions || {}
    end
    
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bento_search-0.8.0 app/search_engines/bento_search/mock_engine.rb
bento_search-0.7.0 app/search_engines/bento_search/mock_engine.rb