spec/unit/mongoid/collection_spec.rb in mongoid-1.2.3 vs spec/unit/mongoid/collection_spec.rb in mongoid-1.2.4

- old
+ new

@@ -9,11 +9,11 @@ let(:slaves) do stub.quacks_like(Mongoid::Collections::Slaves.allocate) end let(:collection) do - Mongoid::Collection.new("people") + Mongoid::Collection.new(Person, "people") end before do collection.instance_variable_set(:@master, master) collection.instance_variable_set(:@slaves, slaves) @@ -38,76 +38,104 @@ collection.should respond_to(name) end end end - describe "#directed" do + describe "#find" do - context "when the counter is less than the maximum" do + before do + @cursor = stub.quacks_like(Mongoid::Cursor.allocate) + Mongoid::Cursor.expects(:new).with(Person, collection, @mongo_cursor).returns(@cursor) + end + context "when no block supplied" do + before do - collection.instance_variable_set(:@counter, 0) + master.expects(:find).with({ :test => "value" }, {}).returns(@mongo_cursor) end - it "delegates to the master" do - collection.directed.should == master + it "finds return a cursor" do + collection.find({ :test => "value"}).should == @cursor end - it "increments the counter" do - collection.directed - collection.counter.should == 1 + end + + context "when a block is supplied" do + + before do + master.expects(:find).with({ :test => "value" }, {}).returns(@mongo_cursor) end + + it "yields to the cursor and closes it" do + @cursor.expects(:close).returns(true) + collection.find({ :test => "value" }) do |cur| + cur.should == @cursor + end + end end - context "when the counter is at the max" do + context "when an enslave option exists" do before do + @options = { :enslave => true } slaves.expects(:empty?).returns(false) - collection.instance_variable_set(:@counter, 10) + slaves.expects(:find).with({ :test => "value" }, {}).returns(@mongo_cursor) end - it "delegates to the slave" do - collection.directed.should == slaves + it "sends the query to the slave pool" do + collection.find({ :test => "value"}, @options).should == @cursor end - it "resets the counter" do - collection.directed - collection.counter.should == 0 + it "deletes the enslave option" do + collection.find({ :test => "value"}, @options) + @options[:enslave].should be_nil end end - context "when the slave does not exist" do + context "when an enslave option does not exist" do before do - collection.instance_variable_set(:@counter, 10) - slaves.expects(:empty?).returns(true) + master.expects(:find).with({ :test => "value" }, {}).returns(@mongo_cursor) end - it "delegates to the master" do - collection.directed.should == master + it "sends the query to the master" do + collection.find({ :test => "value"}).should == @cursor end end end - describe "#find" do + describe "#find_one" do before do - @cursor = stub.quacks_like(Mongoid::Cursor.allocate) - master.expects(:find).with({ :test => "value" }, {}).returns(@mongo_cursor) - Mongoid::Cursor.expects(:new).with(collection, @mongo_cursor).returns(@cursor) + @person = stub end - it "finds are returns a cursor" do - collection.find({ :test => "value"}).should == @cursor + context "when an enslave option exists" do + + before do + @options = { :enslave => true } + slaves.expects(:empty?).returns(false) + slaves.expects(:find_one).with({ :test => "value" }, {}).returns(@person) + end + + it "sends the query to the slave pool" do + collection.find_one({ :test => "value"}, @options).should == @person + end + + it "deletes the enslave option" do + collection.find_one({ :test => "value"}, @options) + @options[:enslave].should be_nil + end end - context "when a block is supplied" do + context "when an enslave option does not exist" do - it "yields to the cursor and closes it" do - @cursor.expects(:close).returns(true) - collection.find({ :test => "value" }) do |cur| - cur.should == @cursor - end + before do + master.expects(:find_one).with({ :test => "value" }, {}).returns(@person) + end + + it "sends the query to the master" do + collection.find_one({ :test => "value"}).should == @person end end end end