Skip to content

Commit

Permalink
Completed ThinkingSphinx::ActiveRecord spec
Browse files Browse the repository at this point in the history
  • Loading branch information
pat committed Apr 20, 2008
1 parent f9127b1 commit 6f205f4
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
75 changes: 75 additions & 0 deletions spec/unit/thinking_sphinx/active_record_spec.rb
@@ -0,0 +1,75 @@
require 'spec/spec_helper'

describe "ThinkingSphinx::ActiveRecord" do
describe "define_index method" do
before :each do
module TestModule
class TestModel < ActiveRecord::Base; end
end

TestModule::TestModel.stub_methods(
:before_save => true,
:after_commit => true
)

@index = ThinkingSphinx::Index.stub_instance(:delta? => false)
ThinkingSphinx::Index.stub_method(:new => @index)
end

after :each do
TestModule.send(:remove_const, :TestModel)
end

it "should add a new index to the model" do
TestModule::TestModel.define_index do; end

TestModule::TestModel.indexes.length.should == 1
end

it "should add to ThinkingSphinx.indexed_models if the model doesn't already exist in the array" do
TestModule::TestModel.define_index do; end

ThinkingSphinx.indexed_models.should include("TestModule::TestModel")
end

it "shouldn't add to ThinkingSphinx.indexed_models if the model already exists in the array" do
TestModule::TestModel.define_index do; end

ThinkingSphinx.indexed_models.select { |model|
model == "TestModule::TestModel"
}.length.should == 1

TestModule::TestModel.define_index do; end

ThinkingSphinx.indexed_models.select { |model|
model == "TestModule::TestModel"
}.length.should == 1
end

it "should add before_save and after_commit hooks to the model if delta indexing is enabled" do
@index.stub_method(:delta? => true)

TestModule::TestModel.define_index do; end

TestModule::TestModel.should have_received(:before_save)
TestModule::TestModel.should have_received(:after_commit)
end

it "should not add before_save and after_commit hooks to the model if delta indexing is disabled" do
TestModule::TestModel.define_index do; end

TestModule::TestModel.should_not have_received(:before_save)
TestModule::TestModel.should_not have_received(:after_commit)
end

it "should return the new index" do
TestModule::TestModel.define_index.should == @index
end
end

describe "to_crc32 method" do
it "should return an integer" do
Person.to_crc32.should be_a_kind_of(Integer)
end
end
end
13 changes: 6 additions & 7 deletions spec/unit/thinking_sphinx_spec.rb
Expand Up @@ -40,37 +40,36 @@
end

describe "use_group_by_shortcut? method" do
before :each do
@connection = ::ActiveRecord::ConnectionAdapters::MysqlAdapter.stub_instance
::ActiveRecord::Base.stub_method(:connection => @connection)
after :each do
::ActiveRecord::Base.connection.unstub_method(:select_all)
end

it "should return true if no ONLY_FULL_GROUP_BY" do
@connection.stub_method(
::ActiveRecord::Base.connection.stub_method(
:select_all => {:a => "OTHER SETTINGS"}
)

ThinkingSphinx.use_group_by_shortcut?.should be_true
end

it "should return true if NULL value" do
@connection.stub_method(
::ActiveRecord::Base.connection.stub_method(
:select_all => {:a => nil}
)

ThinkingSphinx.use_group_by_shortcut?.should be_true
end

it "should return false if ONLY_FULL_GROUP_BY is set" do
@connection.stub_method(
::ActiveRecord::Base.connection.stub_method(
:select_all => {:a => "OTHER SETTINGS,ONLY_FULL_GROUP_BY,blah"}
)

ThinkingSphinx.use_group_by_shortcut?.should be_false
end

it "should return false if ONLY_FULL_GROUP_BY is set in any of the values" do
@connection.stub_method(
::ActiveRecord::Base.connection.stub_method(
:select_all => {
:a => "OTHER SETTINGS",
:b => "ONLY_FULL_GROUP_BY"
Expand Down

0 comments on commit 6f205f4

Please sign in to comment.