spec/unit/mongoid/criteria_spec.rb in mongoid-pre-2.0.0.beta1 vs spec/unit/mongoid/criteria_spec.rb in mongoid-pre-2.0.0.pre

- old
+ new

@@ -118,45 +118,32 @@ @criteria.aggregate end end - describe "#avg" do - - before do - @context = stub.quacks_like(Mongoid::Contexts::Mongo.allocate) - @criteria.instance_variable_set(:@context, @context) - end - - it "delegates to the context" do - @context.expects(:avg).with(:age) - @criteria.avg(:age) - end - end - describe "#blank?" do before do @context = stub.quacks_like(Mongoid::Contexts::Mongo.allocate) @criteria.instance_variable_set(:@context, @context) end - context "when the context is blank" do + context "when the count is 0" do before do - @context.expects(:blank?).returns(true) + @context.expects(:count).returns(0) end it "returns true" do @criteria.blank?.should be_true end end - context "when the context is not blank" do + context "when the count is greater than 0" do before do - @context.expects(:blank?).returns(false) + @context.expects(:count).returns(10) end it "returns false" do @criteria.blank?.should be_false end @@ -183,11 +170,13 @@ before do @context = stub end it "creates a new context" do - Mongoid::Contexts::Mongo.expects(:new).with(@criteria).returns(@context) + Mongoid::Contexts::Mongo.expects(:new).with( + @criteria.selector, @criteria.options, @criteria.klass + ).returns(@context) @criteria.context.should == @context end end @@ -211,23 +200,10 @@ end end - describe "#distinct" do - - before do - @context = stub.quacks_like(Mongoid::Contexts::Mongo.allocate) - @criteria.instance_variable_set(:@context, @context) - end - - it "delegates to the context" do - @context.expects(:distinct).with(:title) - @criteria.distinct(:title) - end - end - describe "#entries" do context "filtering" do before do @@ -267,11 +243,11 @@ it "calls find on the collection with the selector and options" do criteria = Mongoid::Criteria.new(Person) collection = mock Person.expects(:collection).returns(collection) - collection.expects(:find).with(criteria.selector, criteria.options).returns([]) + collection.expects(:find).with(@criteria.selector, @criteria.options).returns([]) criteria.entries.should == [] end end @@ -298,17 +274,10 @@ @collection = stub @person = Person.new(:title => "Sir") @cursor = stub(:count => 10) end - it "delegates to the context#iterate" do - @context = stub('context') - @criteria.stubs(:context).returns(@context) - @context.expects(:iterate) - @criteria.each - end - context "when the criteria has not been executed" do before do Person.expects(:collection).returns(@collection) @collection.expects(:find).with({ :_type => { "$in" => ["Doctor", "Person"] }, :title => "Sir" }, {}).returns(@cursor) @@ -349,16 +318,11 @@ context "when caching" do before do Person.expects(:collection).returns(@collection) - @collection.expects(:find).with( - { :_type => { "$in" => ["Doctor", "Person"] }, - :title => "Sir" - }, - { :cache => true } - ).returns(@cursor) + @collection.expects(:find).with({ :_type => { "$in" => ["Doctor", "Person"] }, :title => "Sir" }, {}).returns(@cursor) @cursor.expects(:each).yields(@person) @criteria.cache @criteria.each do |doc| doc.should == @person end @@ -406,26 +370,26 @@ end describe "#initialize" do - let(:criteria) { Mongoid::Criteria.new(Person) } + context "when class is hereditary" do - it "sets the selector to an empty hash" do - criteria.selector.should == {} - end + it "sets the _type value on the selector" do + criteria = Mongoid::Criteria.new(Person) + criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] } } + end - it "sets the options to an empty hash" do - criteria.options.should == {} end - it "sets the documents to an empty array" do - criteria.documents.should == [] - end + context "when class is not hereditary" do - it "sets the klass to the given class" do - criteria.klass.should == Person + it "sets no _type value on the selector" do + criteria = Mongoid::Criteria.new(Game) + criteria.selector.should == {} + end + end end describe "#last" do @@ -467,11 +431,11 @@ context "when the other has a selector and options" do before do @other = Mongoid::Criteria.new(Person) @other.where(:name => "Chloe").order_by([[:name, :asc]]) - @selector = { :title => "Sir", :age => 30, :name => "Chloe" } + @selector = { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Sir", :age => 30, :name => "Chloe" } @options = { :skip => 40, :limit => 20, :sort => [[:name, :asc]] } end it "merges the selector and options hashes together" do @criteria.merge(@other) @@ -483,11 +447,11 @@ context "when the other has no selector or options" do before do @other = Mongoid::Criteria.new(Person) - @selector = { :title => "Sir", :age => 30 } + @selector = { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Sir", :age => 30 } @options = { :skip => 40, :limit => 20 } end it "merges the selector and options hashes together" do @criteria.merge(@other) @@ -523,21 +487,22 @@ @criteria.where(:title => "Sir") end it "merges the criteria with the next one" do @new_criteria = @criteria.accepted - @new_criteria.selector.should == { :title => "Sir", :terms => true } + @new_criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Sir", :terms => true } end context "chaining more than one scope" do before do @criteria = Person.accepted.old.knight end it "returns the final merged criteria" do - @criteria.selector.should == { :title => "Sir", :terms => true, :age => { "$gt" => 50 } } + @criteria.selector.should == + { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Sir", :terms => true, :age => { "$gt" => 50 } } end end context "when expecting behaviour of an array" do @@ -649,11 +614,12 @@ before do @criteria = Person.where(:title => "Sir").skip(20) end it "returns the selector plus the options" do - @criteria.scoped.should == { :where => { :title => "Sir" }, :skip => 20 } + @criteria.scoped.should == + { :where => { :title => "Sir", :_type=>{ "$in" => [ "Doctor", "Person" ] } }, :skip => 20 } end end describe "#sum" do @@ -672,39 +638,33 @@ describe ".translate" do context "with a single argument" do - context "when the arg is a string" do + before do + @id = Mongo::ObjectID.new.to_s + @document = stub + @criteria = mock + Mongoid::Criteria.expects(:new).returns(@criteria) + @criteria.expects(:id).with(@id).returns(@criteria) + end - before do - @id = Mongo::ObjectID.new.to_s - @document = stub - @criteria = mock - Mongoid::Criteria.expects(:new).returns(@criteria) - end - - it "delegates to #id_criteria" do - @criteria.expects(:id_criteria).with(@id).returns(@document) - Mongoid::Criteria.translate(Person, @id).should == @document - end + it "creates a criteria for a string" do + @criteria.expects(:one).returns(@document) + @document.expects(:blank? => false) + Mongoid::Criteria.translate(Person, @id) end - context "when the arg is an object id" do + context "when the document is not found" do - before do - @id = Mongo::ObjectID.new - @document = stub - @criteria = mock - Mongoid::Criteria.expects(:new).returns(@criteria) + it "raises an error" do + @criteria.expects(:one).returns(nil) + lambda { Mongoid::Criteria.translate(Person, @id) }.should raise_error end - it "delegates to #id_criteria" do - @criteria.expects(:id_criteria).with(@id).returns(@document) - Mongoid::Criteria.translate(Person, @id).should == @document - end end + end context "multiple arguments" do context "when an array of ids" do @@ -714,45 +674,56 @@ @documents = [] 3.times do @ids << Mongo::ObjectID.new.to_s @documents << stub end - @criteria = mock - Mongoid::Criteria.expects(:new).returns(@criteria) + @collection = stub + Person.expects(:collection).returns(@collection) end - it "delegates to #id_criteria" do - @criteria.expects(:id_criteria).with(@ids).returns(@documents) - Mongoid::Criteria.translate(Person, @ids).should == @documents - end + context "when documents are found" do - end + it "returns an ids criteria" do + @collection.expects(:find).with( + { :_type => + { "$in" => + ["Doctor", "Person"] + }, + :_id => + { "$in" => @ids } + }, {}).returns([{ "_id" => "4", "title" => "Sir", "_type" => "Person" }]) + @criteria = Mongoid::Criteria.translate(Person, @ids) + end - context "when Person, :conditions => {}" do - - before do - @criteria = Mongoid::Criteria.translate(Person, :conditions => { :title => "Test" }) end - it "returns a criteria with a selector from the conditions" do - @criteria.selector.should == { :title => "Test" } - end + context "when documents are not found" do - it "returns a criteria with klass Person" do - @criteria.klass.should == Person + it "returns an ids criteria" do + @collection.expects(:find).with( + { :_type => + { "$in" => + ["Doctor", "Person"] + }, + :_id => + { "$in" => @ids } + }, {}).returns([]) + lambda { Mongoid::Criteria.translate(Person, @ids) }.should raise_error + end + end end - context "when Person, :conditions => {:id => id}" do + context "when Person, :conditions => {}" do before do - @criteria = Mongoid::Criteria.translate(Person, :conditions => { :id => "1234e567" }) + @criteria = Mongoid::Criteria.translate(Person, :conditions => { :title => "Test" }) end it "returns a criteria with a selector from the conditions" do - @criteria.selector.should == { :_id => "1234e567" } + @criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Test" } end it "returns a criteria with klass Person" do @criteria.klass.should == Person end @@ -764,11 +735,11 @@ before do @criteria = Mongoid::Criteria.translate(Person, :conditions => { :title => "Test" }) end it "returns a criteria with a selector from the conditions" do - @criteria.selector.should == { :title => "Test" } + @criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Test" } end it "returns a criteria with klass Person" do @criteria.klass.should == Person end @@ -780,11 +751,11 @@ before do @criteria = Mongoid::Criteria.translate(Person, :conditions => { :title => "Test" }) end it "returns a criteria with a selector from the conditions" do - @criteria.selector.should == { :title => "Test" } + @criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Test" } end it "returns a criteria with klass Person" do @criteria.klass.should == Person end @@ -795,10 +766,10 @@ before do @criteria = Mongoid::Criteria.translate(Person, :conditions => { :title => "Test" }, :skip => 10) end it "adds the criteria and the options" do - @criteria.selector.should == { :title => "Test" } + @criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Test" } @criteria.options.should == { :skip => 10 } end end