Permalink
Browse files

Pulling in jsmestad's aggregationg fix

  • Loading branch information...
1 parent cc154f9 commit 124e14afa94bcaa0f86afb9b52d77aff6fd3eb94 @durran durran committed Feb 10, 2010
Showing with 40 additions and 19 deletions.
  1. +1 −1 lib/mongoid/contexts/mongo.rb
  2. +39 −18 spec/integration/mongoid/criteria_spec.rb
@@ -203,7 +203,7 @@ def grouped(start, field, reduce)
reduce.gsub("[field]", field),
true
)
- collection.first[start.to_s]
+ collection.empty? ? nil : collection.first[start.to_s]
end
# Filters the field list. If no fields have been supplied, then it will be
@@ -52,44 +52,65 @@
describe "#max" do
- before do
- 10.times do |n|
- Person.create(:title => "Sir", :age => (n * 10), :aliases => ["D", "Durran"], :ssn => "#{n}")
+ context "without results" do
+ it "should return nil" do
+ Person.max(:age).should == nil
end
end
- it "provides max for the field provided" do
- Person.max(:age).should == 90.0
- end
+ context "with results" do
+ before do
+ 10.times do |n|
+ Person.create(:title => "Sir", :age => (n * 10), :aliases => ["D", "Durran"], :ssn => "#{n}")
+ end
+ end
+ it "provides max for the field provided" do
+ Person.max(:age).should == 90.0
+ end
+ end
end
describe "#min" do
- before do
- 10.times do |n|
- Person.create(:title => "Sir", :age => ((n + 1) * 10), :aliases => ["D", "Durran"], :ssn => "#{n}")
+ context "without results" do
+ it "should return nil" do
+ Person.min(:age).should == nil
end
end
- it "provides min for the field provided" do
- Person.min(:age).should == 10.0
- end
+ context "with results" do
+ before do
+ 10.times do |n|
+ Person.create(:title => "Sir", :age => ((n + 1) * 10), :aliases => ["D", "Durran"], :ssn => "#{n}")
+ end
+ end
+ it "provides min for the field provided" do
+ Person.min(:age).should == 10.0
+ end
+ end
end
describe "#sum" do
- before do
- 10.times do |n|
- Person.create(:title => "Sir", :age => 5, :aliases => ["D", "Durran"], :ssn => "#{n}")
+ context "without results" do
+ it "should return nil" do
+ Person.sum(:age).should == nil
end
end
- it "provides sum for the field provided" do
- Person.where(:age.gt => 3).sum(:age).should == 50.0
- end
+ context "with results" do
+ before do
+ 10.times do |n|
+ Person.create(:title => "Sir", :age => 5, :aliases => ["D", "Durran"], :ssn => "#{n}")
+ end
+ end
+ it "provides sum for the field provided" do
+ Person.where(:age.gt => 3).sum(:age).should == 50.0
+ end
+ end
end
describe "#where" do

0 comments on commit 124e14a

Please sign in to comment.