spec/lib/query/allocation_spec.rb in picky-4.0.0pre1 vs spec/lib/query/allocation_spec.rb in picky-4.0.0pre2
- old
+ new
@@ -1,32 +1,24 @@
require 'spec_helper'
describe Picky::Query::Allocation do
-
+
before(:each) do
@backend = stub :backend
@index = stub :index, :result_identifier => :some_result_identifier, :backend => @backend
@combinations = stub :combinations, :empty? => false
@allocation = described_class.new @index, @combinations
end
-
- describe "eql?" do
- # TODO This works, but is not acceptable.
- #
- it "returns true" do
- @allocation.eql?(:anything).should == true
- end
- end
-
+
describe "hash" do
it "delegates to the combinations" do
@combinations.should_receive(:hash).once.with
-
+
@allocation.hash
end
end
-
+
describe "to_s" do
before(:each) do
@combinations.stub! :to_result => 'combinations_result'
end
context "allocation.count > 0" do
@@ -38,19 +30,19 @@
it "represents correctly" do
@allocation.to_s.should == "Allocation([:some_result_identifier, :score, 10, \"combinations_result\", :ids])"
end
end
end
-
+
describe 'remove' do
it 'should delegate to the combinations' do
@combinations.should_receive(:remove).once.with [:some_categories]
-
+
@allocation.remove [:some_categories]
end
end
-
+
describe 'process!' do
context 'no ids' do
before(:each) do
@allocation.stub! :calculate_ids => []
end
@@ -128,11 +120,11 @@
@allocation.instance_variable_set :@score, :some_score
end
context 'with ids' do
it 'should output an array of information' do
@backend.stub! :ids => [1,2,3]
-
+
@allocation.process! 20, 0
@allocation.to_result.should == [:some_result_identifier, :some_score, 3, [:some_result], [1, 2, 3]]
end
end
@@ -146,11 +138,11 @@
@allocation.instance_variable_set :@score, :some_score
end
context 'with ids' do
it 'should output an array of information' do
@backend.stub! :ids => [1,2,3]
-
+
@allocation.process! 20, 0
@allocation.to_result.should == [:some_result_identifier, :some_score, 3, [:some_result1, :some_result2], [1, 2, 3]]
end
end
@@ -160,11 +152,11 @@
@allocation = described_class.new @index, stub(:combinations, :empty? => true)
@allocation.instance_variable_set :@score, :some_score
end
it 'should return nil' do
@backend.stub! :ids => []
-
+
@allocation.process! 20, 0
@allocation.to_result.should == nil
end
end
@@ -175,21 +167,32 @@
@allocation = described_class.new @index, stub(:combination, :empty? => false, :to_result => [:some_result1, :some_result2])
@allocation.instance_variable_set :@score, :some_score
end
it 'should output the correct json string' do
@backend.stub! :ids => [1,2,3,4,5,6,7]
-
+
@allocation.process! 20, 0
@allocation.to_json.should == '["some_result_identifier","some_score",7,["some_result1","some_result2"],[1,2,3,4,5,6,7]]'
end
end
describe "calculate_score" do
- it 'should delegate to the combinations' do
- @combinations.should_receive(:calculate_score).once.with :some_weights
+ context 'non-empty combinations' do
+ it 'should delegate to backend and combinations' do
+ @backend.should_receive(:weight).once.with(@combinations).and_return 1
+ @combinations.should_receive(:weighted_score).once.with(:some_weights).and_return 2
- @allocation.calculate_score :some_weights
+ @allocation.calculate_score(:some_weights).should == 3
+ end
+ end
+ context 'empty combinations' do
+ before(:each) do
+ @combinations.stub! :empty? => true
+ end
+ it 'should just be zero' do
+ @allocation.calculate_score(:some_weights).should == 0
+ end
end
end
describe "<=>" do
it "should sort higher first" do
\ No newline at end of file