Class: BentoSearch::MockEngine
- Inherits:
-
Object
- Object
- BentoSearch::MockEngine
- 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
- :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.
Constant Summary
Constants included from SearchEngine
Instance Attribute Summary collapse
-
#last_args ⇒ Object
used for testing what the engine received as args.
Class Method Summary collapse
Instance Method Summary collapse
- #search(*args) ⇒ Object
- #search_field_definitions ⇒ Object
- #search_implementation(args) ⇒ Object
- #sort_definitions ⇒ Object
Methods included from SearchEngine
#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_args ⇒ Object
used for testing what the engine received as args
23 24 25 |
# File 'app/search_engines/bento_search/mock_engine.rb', line 23 def last_args @last_args end |
Class Method Details
.default_configuration ⇒ Object
52 53 54 55 56 57 58 |
# File 'app/search_engines/bento_search/mock_engine.rb', line 52 def self.default_configuration { :num_results => nil, :total_items => 1000, :link => "http://example.org", :error => nil, :timing => nil} end |
Instance Method Details
#search(*args) ⇒ Object
46 47 48 49 50 |
# File 'app/search_engines/bento_search/mock_engine.rb', line 46 def search(*args) results = super(*args) results.timing = configuration.timing if configuration.timing return results end |
#search_field_definitions ⇒ Object
64 65 66 |
# File 'app/search_engines/bento_search/mock_engine.rb', line 64 def search_field_definitions configuration.search_field_definitions || {} end |
#search_implementation(args) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/search_engines/bento_search/mock_engine.rb', line 25 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_definitions ⇒ Object
60 61 62 |
# File 'app/search_engines/bento_search/mock_engine.rb', line 60 def sort_definitions configuration.sort_definitions || {} end |