Class: BentoSearch::MockEngine

Inherits:
Object
  • Object
show all
Includes:
SearchEngine
Defined in:
app/search_engines/bento_search/mock_engine.rb

Overview

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:

:num_results

how many items to include in returned results (default specified per_page, or 10)

:total_items

total_items to report

:sort_definitions

hash for #sort_definitions

:search_field_definitions

hash for #search_field_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.

:raise_exception_class

string name of exception class that the engine will raise and not catch,

to be caught by BentoSearch::SearchEngine wrapper possibly.
:timing

in seconds, fill out the results as if they took this long.

:supports_multi_search

true or false

Constant Summary

Constants included from SearchEngine

SearchEngine::DefaultPerPage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SearchEngine

#display_configuration, #engine_id, #fill_in_search_metadata_for, #initialize, #normalized_search_arguments, #public_settable_search_args

Methods included from SearchEngine::Capabilities

#max_per_page, #search_keys, #semantic_search_keys, #semantic_search_map, #sort_keys

Instance Attribute Details

#last_argsObject

used for testing what the engine received as args



25
26
27
# File 'app/search_engines/bento_search/mock_engine.rb', line 25

def last_args
  @last_args
end

Class Method Details

.default_configurationObject



54
55
56
57
58
59
60
# File 'app/search_engines/bento_search/mock_engine.rb', line 54

def self.default_configuration
  { :num_results => nil,
    :total_items => 1000, 
    :link => "http://example.org",
    :error => nil,
    :timing => nil}
end

Instance Method Details

#multi_field_search?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'app/search_engines/bento_search/mock_engine.rb', line 70

def multi_field_search?
  configuration.multi_field_search || false
end

#search(*args) ⇒ Object



48
49
50
51
52
# File 'app/search_engines/bento_search/mock_engine.rb', line 48

def search(*args)
  results = super(*args)
  results.timing = configuration.timing if configuration.timing
  return results
end

#search_field_definitionsObject



66
67
68
# File 'app/search_engines/bento_search/mock_engine.rb', line 66

def search_field_definitions
  configuration.search_field_definitions.try(:to_hash).try(:stringify_keys) || {}
end

#search_implementation(args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/search_engines/bento_search/mock_engine.rb', line 27

def search_implementation(args)
  self.last_args = args
  
  if configuration.raise_exception_class
    raise configuration.raise_exception_class.constantize.new("MockEngine forced raise")
  end
  
  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

#sort_definitionsObject



62
63
64
# File 'app/search_engines/bento_search/mock_engine.rb', line 62

def sort_definitions
  configuration.sort_definitions.try(:to_hash).try(:stringify_keys) || {}
end