Skip to content

Commit

Permalink
* add ActiveRecord::Base.logger for specs
Browse files Browse the repository at this point in the history
* Add ThinkingSphinx::Search#count and ActiveRecord::Base.search_count for
  returnings counts of Sphinx searches
  • Loading branch information
technoweenie authored and pat committed Aug 14, 2008
1 parent 7e809a9 commit badb797
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/thinking_sphinx/active_record/search.rb
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion lib/thinking_sphinx/search.rb
Expand Up @@ -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:
#
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -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

Expand Down
26 changes: 26 additions & 0 deletions spec/unit/thinking_sphinx/active_record/search_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
27 changes: 27 additions & 0 deletions spec/unit/thinking_sphinx/search_spec.rb
Expand Up @@ -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

0 comments on commit badb797

Please sign in to comment.