Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Fixed problem where invalid queries would be executed
Browse files Browse the repository at this point in the history
[#1085 state:resolved]
  • Loading branch information
dkubb committed Oct 8, 2009
1 parent a4c306a commit 58eeeb3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 11 deletions.
5 changes: 3 additions & 2 deletions dm-aggregates/lib/dm-aggregates/aggregate_functions.rb
Expand Up @@ -35,7 +35,7 @@ def count(*args)
assert_kind_of 'property', property_by_name(property_name), Property
end

aggregate(query.merge(:fields => [ property_name ? property_name.count : :all.count ]))
aggregate(query.merge(:fields => [ property_name ? property_name.count : :all.count ])).to_i
end

# Get the lowest value of a property
Expand Down Expand Up @@ -174,7 +174,8 @@ def assert_property_type(name, *types)
raise ArgumentError, 'property name must not be nil'
end

type = property_by_name(name).type
property = property_by_name(name)
type = property.custom? ? property.primitive : property.type

unless types.include?(type)
raise ArgumentError, "#{name} must be #{types * ' or '}, but was #{type}"
Expand Down
1 change: 1 addition & 0 deletions dm-aggregates/lib/dm-aggregates/repository.rb
Expand Up @@ -2,6 +2,7 @@ module DataMapper
module Aggregates
module Repository
def aggregate(query)
return [] unless query.valid?
adapter.aggregate(query)
end
end
Expand Down
44 changes: 44 additions & 0 deletions dm-aggregates/spec/public/collection_spec.rb
Expand Up @@ -10,5 +10,49 @@
end

it_should_behave_like 'An Aggregatable Class'

describe 'ignore invalid query' do
before :all do
@dragons = @dragons.all(:id => [])
end

[ :size, :count ].each do |method|
describe "##{method}" do
it 'should return 0' do
@dragons.send(method).should == 0
end
end
end

describe '#min' do
it 'should return nil' do
@dragons.min(:id).should == nil
end
end

describe '#max' do
it 'should return nil' do
@dragons.max(:id).should == nil
end
end

describe '#avg' do
it 'should return nil' do
@dragons.avg(:id).should == nil
end
end

describe '#sum' do
it 'should return nil' do
@dragons.sum(:id).should == nil
end
end

describe '#aggregate' do
it 'should return nil' do
@dragons.aggregate(:id).should == []
end
end
end
end
end
18 changes: 9 additions & 9 deletions dm-aggregates/spec/public/shared/aggregate_shared_spec.rb
Expand Up @@ -38,9 +38,9 @@ class ::Country

DataMapper.auto_migrate!

@birth_at = DateTime.now
@birth_on = Date.parse(@birth_at.to_s)
@birth_time = Time.parse(@birth_at.to_s)
@birth_time = Time.now
@birth_at = @birth_time.to_datetime
@birth_on = @birth_time.to_date

@chuck = Knight.create(:name => 'Chuck')
@larry = Knight.create(:name => 'Larry')
Expand Down Expand Up @@ -141,17 +141,17 @@ class ::Country

it 'should provide the lowest value of a DateTime property' do
@dragons.min(:birth_at).should be_kind_of(DateTime)
@dragons.min(:birth_at).to_s.should == @birth_at.to_s
@dragons.min(:birth_at).should == @birth_at
end

it 'should provide the lowest value of a Date property' do
@dragons.min(:birth_on).should be_kind_of(Date)
@dragons.min(:birth_on).to_s.should == @birth_on.to_s
@dragons.min(:birth_on).should == @birth_on
end

it 'should provide the lowest value of a Time property' do
@dragons.min(:birth_time).should be_kind_of(Time)
@dragons.min(:birth_time).to_s.should == @birth_time.to_s
@dragons.min(:birth_time).should == @birth_time
end

it 'should provide the lowest value when conditions provided' do
Expand Down Expand Up @@ -186,17 +186,17 @@ class ::Country

it 'should provide the highest value of a DateTime property' do
@dragons.min(:birth_at).should be_kind_of(DateTime)
@dragons.min(:birth_at).to_s.should == @birth_at.to_s
@dragons.min(:birth_at).should == @birth_at
end

it 'should provide the highest value of a Date property' do
@dragons.min(:birth_on).should be_kind_of(Date)
@dragons.min(:birth_on).to_s.should == @birth_on.to_s
@dragons.min(:birth_on).should == @birth_on
end

it 'should provide the highest value of a Time property' do
@dragons.min(:birth_time).should be_kind_of(Time)
@dragons.min(:birth_time).to_s.should == @birth_time.to_s
@dragons.min(:birth_time).should == @birth_time
end

it 'should provide the highest value when conditions provided' do
Expand Down

0 comments on commit 58eeeb3

Please sign in to comment.