From badb797f5896fbbc5c33055d72772ff4325aeed1 Mon Sep 17 00:00:00 2001 From: rick Date: Thu, 14 Aug 2008 15:59:23 -0700 Subject: [PATCH] * add ActiveRecord::Base.logger for specs * Add ThinkingSphinx::Search#count and ActiveRecord::Base.search_count for returnings counts of Sphinx searches --- lib/thinking_sphinx/active_record/search.rb | 9 ++++++- lib/thinking_sphinx/search.rb | 7 ++++- spec/spec_helper.rb | 2 ++ .../active_record/search_spec.rb | 26 ++++++++++++++++++ spec/unit/thinking_sphinx/search_spec.rb | 27 +++++++++++++++++++ 5 files changed, 69 insertions(+), 2 deletions(-) diff --git a/lib/thinking_sphinx/active_record/search.rb b/lib/thinking_sphinx/active_record/search.rb index b0e83537f..ad175af45 100644 --- a/lib/thinking_sphinx/active_record/search.rb +++ b/lib/thinking_sphinx/active_record/search.rb @@ -28,7 +28,14 @@ def search(*args) args << options ThinkingSphinx::Search.search(*args) end - + + def search_count(*args) + options = args.extract_options! + options[:class] = self + args << options + ThinkingSphinx::Search.count(*args) + end + def search_for_id(*args) options = args.extract_options! options[:class] = self diff --git a/lib/thinking_sphinx/search.rb b/lib/thinking_sphinx/search.rb index 188146409..67a800014 100644 --- a/lib/thinking_sphinx/search.rb +++ b/lib/thinking_sphinx/search.rb @@ -203,7 +203,12 @@ def search(*args) instances_from_results(results[:matches], options, klass) end end - + + def count(*args) + results, client = search_results(*args.clone) + results[:total] || 0 + end + # Checks if a document with the given id exists within a specific index. # Expected parameters: # diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7a46b6acd..f88ad0eb2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,6 +7,8 @@ require 'lib/thinking_sphinx' require 'spec/sphinx_helper' +ActiveRecord::Base.logger = Logger.new(StringIO.new) + Spec::Runner.configure do |config| # config.mock_with NotAMock::RspecMockFrameworkAdapter diff --git a/spec/unit/thinking_sphinx/active_record/search_spec.rb b/spec/unit/thinking_sphinx/active_record/search_spec.rb index b257c64f3..49c59e171 100644 --- a/spec/unit/thinking_sphinx/active_record/search_spec.rb +++ b/spec/unit/thinking_sphinx/active_record/search_spec.rb @@ -9,6 +9,10 @@ ActiveRecord::Base.methods.should include("search") end + it "should add search_count to ActiveRecord::Base" do + ActiveRecord::Base.methods.should include("search_count") + end + it "should add search_for_id to ActiveRecord::Base" do ActiveRecord::Base.methods.should include("search_for_id") end @@ -78,4 +82,26 @@ ) end end + + describe "search_count method" do + before :each do + ThinkingSphinx::Search.stub_method(:count => true) + end + + it "should call ThinkingSphinx::Search#search with the class option set" do + Person.search_count("search") + + ThinkingSphinx::Search.should have_received(:count).with( + "search", :class => Person + ) + end + + it "should override the class option" do + Person.search_count("search", :class => Friendship) + + ThinkingSphinx::Search.should have_received(:count).with( + "search", :class => Person + ) + end + end end diff --git a/spec/unit/thinking_sphinx/search_spec.rb b/spec/unit/thinking_sphinx/search_spec.rb index e93f1afc7..f2cfa48c1 100644 --- a/spec/unit/thinking_sphinx/search_spec.rb +++ b/spec/unit/thinking_sphinx/search_spec.rb @@ -160,4 +160,31 @@ ).should == [@person_a, @person_b, @person_c] end end + + describe "count method" do + before :each do + @client = Riddle::Client.stub_instance( + :filters => [], + :filters= => true, + :id_range= => true, + :sort_mode => :asc, + :limit => 5, + :offset= => 0, + :sort_mode= => true, + :query => { + :matches => [], + :total => 50 + } + ) + + ThinkingSphinx::Search.stub_methods( + :client_from_options => @client, + :search_conditions => ["", []] + ) + end + + it "should return query total" do + ThinkingSphinx::Search.count(42, "an_index").should == 50 + end + end end