spec/unit/mongoid/contexts/mongo_spec.rb in mongoid-1.2.13 vs spec/unit/mongoid/contexts/mongo_spec.rb in mongoid-1.2.14

- old
+ new

@@ -20,13 +20,34 @@ it "calls group on the collection with the aggregate js" do @collection.expects(:group).with([:field1], {:_type => {'$in' => ['Doctor', 'Person']}}, {:count => 0}, @reduce, true) @context.aggregate end + end + end + describe "#avg" do + + before do + @reduce = Mongoid::Javascript.sum.gsub("[field]", "age") + @collection = mock + Person.expects(:collection).twice.returns(@collection) + @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']}}, + {:sum => "start"}, + @reduce + ).returns([{"sum" => 100.0}]) + @cursor = mock(:count => 10) + @collection.expects(:find).returns(@cursor) + @context.avg(:age).should == 10 + end end describe "blank?" do before do @@ -100,10 +121,26 @@ end end + describe "#distinct" do + + before do + @criteria = Mongoid::Criteria.new(Person) + @criteria.where(:test => 'Testing') + @context = Mongoid::Contexts::Mongo.new(@criteria) + @collection = mock + Person.expects(:collection).returns(@collection) + end + + it "returns delegates to distinct on the collection" do + @collection.expects(:distinct).with(:title, @criteria.selector).returns(["Sir"]) + @context.distinct(:title).should == ["Sir"] + end + end + describe "#execute" do let(:selector) { { :field => "value" } } let(:options) { { :skip => 20 } } @@ -177,12 +214,11 @@ it "calls group on the collection with the aggregate js" do @collection.expects(:group).with( [:field1], {:_type => { "$in" => ["Doctor", "Person"] }}, {:group => []}, - @reduce, - true + @reduce ).returns(@grouping) @context.group end end @@ -321,11 +357,14 @@ before do @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( + @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 @@ -338,11 +377,14 @@ before do @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) + @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 @@ -353,11 +395,14 @@ before do @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( + @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 @@ -381,12 +426,11 @@ it "calls group on the collection with the aggregate js" do @collection.expects(:group).with( nil, {:_type => {'$in' => ['Doctor', 'Person']}}, {:max => "start"}, - @reduce, - true + @reduce ).returns([{"max" => 200.0}]) @context.max(:age).should == 200.0 end end @@ -404,12 +448,11 @@ it "calls group on the collection with the aggregate js" do @collection.expects(:group).with( nil, {:_type => {'$in' => ['Doctor', 'Person']}}, {:min => "start"}, - @reduce, - true + @reduce ).returns([{"min" => 4.0}]) @context.min(:age).should == 4.0 end end @@ -516,11 +559,10 @@ it "calls group on the collection with the aggregate js" do @collection.expects(:group).with( nil, {:_type => {'$in' => ['Doctor', 'Person']}}, {:sum => "start"}, - @reduce, - true + @reduce ).returns([{"sum" => 50.0}]) @context.sum(:age).should == 50.0 end end