Permalink
Browse files

modify parse_query to return nil if the query string was empty becaus…

…e it was causing a Solr error; modify parse_results to return an empty SearchResult if the solr_data is nil
  • Loading branch information...
1 parent 77a3f5a commit 6d9812e9feee332d920e9bacd1e56396434d5caf Luke Francl committed Dec 17, 2008
Showing with 23 additions and 9 deletions.
  1. +5 −2 lib/parser_methods.rb
  2. +18 −7 test/unit/parser_methods_shoulda.rb
View
@@ -6,7 +6,9 @@ module ParserMethods
def parse_query(query=nil, options={}, models=nil)
valid_options = [:offset, :limit, :facets, :models, :results_format, :order, :scores, :operator, :include, :lazy]
query_options = {}
- return if query.nil?
+
+ return nil if (query.nil? || query.strip == '')
+
raise "Invalid parameters: #{(options.keys - valid_options).join(',')}" unless (options.keys - valid_options).empty?
begin
Deprecation.validate_query(options)
@@ -62,11 +64,12 @@ def parse_results(solr_data, options = {})
:docs => [],
:total => 0
}
+
configuration = {
:format => :objects
}
results.update(:facets => {'facet_fields' => []}) if options[:facets]
- return SearchResults.new(results) if solr_data.total_hits == 0
+ return SearchResults.new(results) if (solr_data.nil? || solr_data.total_hits == 0)
configuration.update(options) if options.is_a?(Hash)
@@ -100,6 +100,12 @@ class ParserMethodsTest < Test::Unit::TestCase
assert_equal [], @parser.parse_results(@results).docs
end
end
+
+ context "with a nil result set" do
+ should "return an empty search results set" do
+ assert_equal 0, @parser.parse_results(nil).total
+ end
+ end
context "with the scores option" do
should "add the scores" do
@@ -163,30 +169,35 @@ class ParserMethodsTest < Test::Unit::TestCase
10 == request.to_hash[:rows]
20 == request.to_hash[:start]
}
- @parser.parse_query "", :limit => 10, :offset => 20
+ @parser.parse_query "foo", :limit => 10, :offset => 20
end
should "set the operator" do
ActsAsSolr::Post.expects(:execute).with {|request|
"OR" == request.to_hash["q.op"]
}
- @parser.parse_query "", :operator => :or
+ @parser.parse_query "foo", :operator => :or
end
- should "not execute anything if the query is empty" do
+ should "not execute anything if the query is nil" do
+ ActsAsSolr::Post.expects(:execute).never
+ assert_nil @parser.parse_query(nil)
+ end
+
+ should "not execute anything if the query is ''" do
ActsAsSolr::Post.expects(:execute).never
- @parser.parse_query(nil)
+ assert_nil @parser.parse_query('')
end
should "raise an error if invalid options where specified" do
- assert_raise(RuntimeError) {@parser.parse_query "", :invalid => true}
+ assert_raise(RuntimeError) {@parser.parse_query "foo", :invalid => true}
end
should "add the type" do
ActsAsSolr::Post.expects(:execute).with {|request|
request.to_hash[:q].include?("(type:ParserMethodsTest)")
}
- @parser.parse_query ""
+ @parser.parse_query "foo"
end
should "append the field types for the specified fields" do
@@ -208,7 +219,7 @@ class ParserMethodsTest < Test::Unit::TestCase
ActsAsSolr::Post.expects(:execute).with {|request|
request.to_hash[:fl] == ('id,score')
}
- @parser.parse_query ""
+ @parser.parse_query "foo"
end
context "with the order option" do

0 comments on commit 6d9812e

Please sign in to comment.