spec/unit/mongoid/contexts/mongo_spec.rb in mongoid-1.2.6 vs spec/unit/mongoid/contexts/mongo_spec.rb in mongoid-1.2.7

- old
+ new

@@ -3,13 +3,13 @@ describe Mongoid::Contexts::Mongo do describe "#aggregate" do before do - @selector = {} - @options = { :fields => [:field1] } - @context = Mongoid::Contexts::Mongo.new(@selector, @options, Person) + @criteria = Mongoid::Criteria.new(Person) + @criteria.only(:field1) + @context = Mongoid::Contexts::Mongo.new(@criteria) end context "when klass not provided" do before do @@ -17,24 +17,24 @@ @collection = mock Person.expects(:collection).returns(@collection) end it "calls group on the collection with the aggregate js" do - @collection.expects(:group).with([:field1], {}, {:count => 0}, @reduce, true) + @collection.expects(:group).with([:field1], {:_type => {'$in' => ['Doctor', 'Person']}}, {:count => 0}, @reduce, true) @context.aggregate end end end describe "#count" do before do - @selector = { :_type => { "$in" => ["Doctor", "Person"] }, :test => "Testing" } - @options = {} - @context = Mongoid::Contexts::Mongo.new(@selector, @options, Person) + @criteria = Mongoid::Criteria.new(Person) + @criteria.where(:test => 'Testing') + @context = Mongoid::Contexts::Mongo.new(@criteria) end context "when criteria has not been executed" do before do @@ -54,11 +54,11 @@ @cursor = mock Person.expects(:collection).returns(@collection) end it "returns the count from the cursor without creating the documents" do - @collection.expects(:find).with(@selector, {}).returns(@cursor) + @collection.expects(:find).with(@criteria.selector, {}).returns(@cursor) @cursor.expects(:count).returns(10) @context.count.should == 10 end end @@ -72,11 +72,13 @@ before do @cursor = stub(:count => 500) @collection = mock @klass = stub(:collection => @collection, :hereditary => false, :instantiate => @person) - @context = Mongoid::Contexts::Mongo.new(selector, options, @klass) + @criteria = Mongoid::Criteria.new(@klass) + @criteria.where(selector).skip(20) + @context = Mongoid::Contexts::Mongo.new(@criteria) end it "calls find on the collection" do @collection.expects(:find).with(selector, options).returns(@cursor) @context.execute.should == @cursor @@ -95,11 +97,11 @@ context "when field options are supplied" do context "when _type not in the field list" do before do - options[:fields] = [ :title ] + @criteria.only(:title) @expected_options = { :skip => 20, :fields => [ :title, :_type ] } end it "adds _type to the fields" do @collection.expects(:find).with(selector, @expected_options).returns(@cursor) @@ -113,14 +115,14 @@ end describe "#group" do before do - @selector = { :_type => { "$in" => ["Doctor", "Person"] } } - @options = { :fields => [ :field1 ] } + @criteria = Mongoid::Criteria.new(Person) + @criteria.only(:field1) @grouping = [{ "title" => "Sir", "group" => [{ "title" => "Sir", "age" => 30, "_type" => "Person" }] }] - @context = Mongoid::Contexts::Mongo.new(@selector, @options, Person) + @context = Mongoid::Contexts::Mongo.new(@criteria) end context "when klass provided" do before do @@ -128,11 +130,17 @@ @collection = mock Person.expects(:collection).returns(@collection) end it "calls group on the collection with the aggregate js" do - @collection.expects(:group).with([:field1], {:_type => { "$in" => ["Doctor", "Person"] }}, {:group => []}, @reduce, true).returns(@grouping) + @collection.expects(:group).with( + [:field1], + {:_type => { "$in" => ["Doctor", "Person"] }}, + {:group => []}, + @reduce, + true + ).returns(@grouping) @context.group end end @@ -143,25 +151,30 @@ let(:selector) { { :field => "value" } } let(:options) { { :skip => 20 } } let(:klass) { Person } before do - @context = Mongoid::Contexts::Mongo.new(selector, options, klass) + @criteria = Mongoid::Criteria.new(klass) + @criteria.where(selector).skip(20) + @context = Mongoid::Contexts::Mongo.new(@criteria) end it "sets the selector" do - @context.selector.should == selector + @context.selector.should == @criteria.selector end it "sets the options" do @context.options.should == options end it "sets the klass" do @context.klass.should == klass end + it "set the selector to query across the _type of the Criteria's klass when it is hereditary" do + @context.selector[:_type].should == {'$in' => Person._types} + end end describe "#last" do before do @@ -170,14 +183,14 @@ end context "when documents exist" do before do - @selector = {} - @options = { :sort => [[:title, :asc]] } - @context = Mongoid::Contexts::Mongo.new(@selector, @options, Person) - @collection.expects(:find_one).with(@selector, { :sort => [[:title, :desc]] }).returns( + @criteria = Mongoid::Criteria.new(Person) + @criteria.order_by([[:title, :asc]]) + @context = Mongoid::Contexts::Mongo.new(@criteria) + @collection.expects(:find_one).with({:_type => {'$in' => ['Doctor', 'Person']}}, { :sort => [[:title, :desc]] }).returns( { "title" => "Sir", "_type" => "Person" } ) end it "calls find on the collection with the selector and sort options reversed" do @@ -187,14 +200,14 @@ end context "when no documents exist" do before do - @selector = {} - @options = { :sort => [[:_id, :asc]] } - @context = Mongoid::Contexts::Mongo.new(@selector, @options, Person) - @collection.expects(:find_one).with(@selector, { :sort => [[:_id, :desc]] }).returns(nil) + @criteria = Mongoid::Criteria.new(Person) + @criteria.order_by([[:_id, :asc]]) + @context = Mongoid::Contexts::Mongo.new(@criteria) + @collection.expects(:find_one).with({:_type => {'$in' => ['Doctor', 'Person']}}, { :sort => [[:_id, :desc]] }).returns(nil) end it "returns nil" do @context.last.should be_nil end @@ -202,14 +215,14 @@ end context "when no sorting options provided" do before do - @selector = {} - @options = { :sort => [[:_id, :asc]] } - @context = Mongoid::Contexts::Mongo.new(@selector, @options, Person) - @collection.expects(:find_one).with(@selector, { :sort => [[:_id, :desc]] }).returns( + @criteria = Mongoid::Criteria.new(Person) + @criteria.order_by([[:_id, :asc]]) + @context = Mongoid::Contexts::Mongo.new(@criteria) + @collection.expects(:find_one).with({:_type => {'$in' => ['Doctor', 'Person']}}, { :sort => [[:_id, :desc]] }).returns( { "title" => "Sir", "_type" => "Person" } ) end it "defaults to sort by id" do @@ -224,17 +237,18 @@ before do @reduce = Mongoid::Contexts::Mongo::MAX_REDUCE.gsub("[field]", "age") @collection = mock Person.expects(:collection).returns(@collection) - @context = Mongoid::Contexts::Mongo.new({}, {}, Person) + @criteria = Mongoid::Criteria.new(Person) + @context = Mongoid::Contexts::Mongo.new(@criteria) end it "calls group on the collection with the aggregate js" do @collection.expects(:group).with( nil, - {}, + {:_type => {'$in' => ['Doctor', 'Person']}}, {:max => "start"}, @reduce, true ).returns([{"max" => 200.0}]) @context.max(:age).should == 200.0 @@ -246,17 +260,18 @@ before do @reduce = Mongoid::Contexts::Mongo::MIN_REDUCE.gsub("[field]", "age") @collection = mock Person.expects(:collection).returns(@collection) - @context = Mongoid::Contexts::Mongo.new({}, {}, Person) + @criteria = Mongoid::Criteria.new(Person) + @context = Mongoid::Contexts::Mongo.new(@criteria) end it "calls group on the collection with the aggregate js" do @collection.expects(:group).with( nil, - {}, + {:_type => {'$in' => ['Doctor', 'Person']}}, {:min => "start"}, @reduce, true ).returns([{"min" => 4.0}]) @context.min(:age).should == 4.0 @@ -267,14 +282,14 @@ describe "#one" do context "when documents exist" do before do - @collection = mock Person.expects(:collection).returns(@collection) - @context = Mongoid::Contexts::Mongo.new({}, {}, Person) - @collection.expects(:find_one).with({}, {}).returns( + @criteria = Mongoid::Criteria.new(Person) + @context = Mongoid::Contexts::Mongo.new(@criteria) + @collection.expects(:find_one).with({:_type => {'$in' => ['Doctor', 'Person']}}, {}).returns( { "title"=> "Sir", "_type" => "Person" } ) end it "calls find on the collection with the selector and options" do @@ -286,12 +301,13 @@ context "when no documents exist" do before do @collection = mock Person.expects(:collection).returns(@collection) - @context = Mongoid::Contexts::Mongo.new({}, {}, Person) - @collection.expects(:find_one).with({}, {}).returns(nil) + @criteria = Mongoid::Criteria.new(Person) + @context = Mongoid::Contexts::Mongo.new(@criteria) + @collection.expects(:find_one).with({:_type => {'$in' => ['Doctor', 'Person']}}, {}).returns(nil) end it "returns nil" do @context.one.should be_nil end @@ -304,11 +320,11 @@ context "when the page option exists" do before do @criteria = Mongoid::Criteria.new(Person).extras({ :page => 5 }) - @context = Mongoid::Contexts::Mongo.new({}, @criteria.options, Person) + @context = Mongoid::Contexts::Mongo.new(@criteria) end it "returns the page option" do @context.page.should == 5 end @@ -317,11 +333,11 @@ context "when the page option does not exist" do before do @criteria = Mongoid::Criteria.new(Person) - @context = Mongoid::Contexts::Mongo.new({}, @criteria.options, Person) + @context = Mongoid::Contexts::Mongo.new(@criteria) end it "returns 1" do @context.page.should == 1 end @@ -334,11 +350,11 @@ before do @collection = mock Person.expects(:collection).returns(@collection) @criteria = Person.where(:_id => "1").skip(60).limit(20) - @context = Mongoid::Contexts::Mongo.new(@criteria.selector, @criteria.options, Person) + @context = Mongoid::Contexts::Mongo.new(@criteria) @collection.expects(:find).with( {:_type => { "$in" => ["Doctor", "Person"] }, :_id => "1"}, :skip => 60, :limit => 20 ).returns([]) @results = @context.paginate end @@ -355,24 +371,134 @@ context "when klass not provided" do before do @reduce = Mongoid::Contexts::Mongo::SUM_REDUCE.gsub("[field]", "age") @collection = mock - @context = Mongoid::Contexts::Mongo.new({}, {}, Person) + @criteria = Mongoid::Criteria.new(Person) + @context = Mongoid::Contexts::Mongo.new(@criteria) Person.expects(:collection).returns(@collection) end it "calls group on the collection with the aggregate js" do @collection.expects(:group).with( nil, - {}, + {:_type => {'$in' => ['Doctor', 'Person']}}, {:sum => "start"}, @reduce, true ).returns([{"sum" => 50.0}]) @context.sum(:age).should == 50.0 end + end + + end + + context "#id_criteria" do + + let(:criteria) { Mongoid::Criteria.new(Person) } + let(:context) { criteria.context } + + context "with a single argument" do + + let(:id) { Mongo::ObjectID.new.to_s } + + before do + criteria.expects(:id).with(id).returns(criteria) + end + + context "when the document is found" do + + let(:document) { stub } + + it "returns a matching document" do + context.expects(:one).returns(document) + document.expects(:blank? => false) + context.id_criteria(id).should == document + end + + end + + context "when the document is not found" do + + it "raises an error" do + context.expects(:one).returns(nil) + lambda { context.id_criteria(id) }.should raise_error + end + + end + + end + + context "multiple arguments" do + + context "when an array of ids" do + + let(:ids) do + (0..2).inject([]) { |ary, i| ary << Mongo::ObjectID.new.to_s } + end + + context "when documents are found" do + + let(:docs) do + (0..2).inject([]) { |ary, i| ary << stub } + end + + before do + criteria.expects(:id).with(ids).returns(criteria) + end + + it "returns matching documents" do + context.expects(:execute).returns(docs) + context.id_criteria(ids).should == docs + end + + end + + context "when documents are not found" do + + it "raises an error" do + context.expects(:execute).returns([]) + lambda { context.id_criteria(ids) }.should raise_error + end + + end + + end + + context "when an array of object ids" do + + let(:ids) do + (0..2).inject([]) { |ary, i| ary << Mongo::ObjectID.new } + end + + context "when documents are found" do + + let(:docs) do + (0..2).inject([]) { |ary, i| ary << stub } + end + + before do + criteria.expects(:id).with(ids).returns(criteria) + end + + it "returns matching documents" do + context.expects(:execute).returns(docs) + context.id_criteria(ids).should == docs + end + + end + + context "when documents are not found" do + + it "raises an error" do + context.expects(:execute).returns([]) + lambda { context.id_criteria(ids) }.should raise_error + end + + end + + end end end end