0
@@ -7,33 +7,40 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
0
# A simplistic example, using with an Integer property
0
include DataMapper::Resource
0
- property :name, String
0
- property :is_fire_breathing, TrueClass
0
- property :toes_on_claw, Integer
0
- auto_migrate!(:default)
0
+ property :name, String
0
+ property :is_fire_breathing, TrueClass
0
+ property :toes_on_claw, Integer
0
+ property :birth_at, DateTime
0
+ property :birth_on, Date
0
+ property :birth_time, Time
0
- Dragon.create(:name => 'George', :is_fire_breathing => false, :toes_on_claw => 3)
0
- Dragon.create(:name => 'Puff', :is_fire_breathing => true, :toes_on_claw => 4)
0
- Dragon.create(:name => nil, :is_fire_breathing => true, :toes_on_claw => 5)
0
# A more complex example, with BigDecimal and Float properties
0
# Statistics taken from CIA World Factbook:
0
# https://www.cia.gov/library/publications/the-world-factbook/
0
include DataMapper::Resource
0
- property :id, Integer, :serial => true
0
- property :name, String, :nullable => false
0
+ property :name, String, :nullable => false
0
property :population, Integer
0
property :birth_rate, Float, :precision => 4, :scale => 2
0
property :gold_reserve_tonnes, Float, :precision => 6, :scale => 2
0
property :gold_reserve_value, BigDecimal, :precision => 15, :scale => 1 # approx. value in USD
0
- auto_migrate!(:default)
0
+ [ Dragon, Country ].each { |m| m.auto_migrate! }
0
+ @birth_at = DateTime.now
0
+ @birth_on = Date.parse(@birth_at.to_s)
0
+ @birth_time = Time.parse(@birth_at.to_s)
0
+ Dragon.create(:name => 'George', :is_fire_breathing => false, :toes_on_claw => 3, :birth_at => @birth_at, :birth_on => @birth_on, :birth_time => @birth_time)
0
+ Dragon.create(:name => 'Puff', :is_fire_breathing => true, :toes_on_claw => 4, :birth_at => @birth_at, :birth_on => @birth_on, :birth_time => @birth_time)
0
+ Dragon.create(:name => nil, :is_fire_breathing => true, :toes_on_claw => 5, :birth_at => nil, :birth_on => nil, :birth_time => nil)
0
gold_kilo_price = 277738.70
0
@gold_tonne_price = gold_kilo_price * 10000
0
@@ -144,6 +151,21 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
0
target(Country, target_type).min(:gold_reserve_value).should == BigDecimal('1217050983400.0')
0
+ it 'should provide the lowest value of a DateTime property' do
0
+ target(Dragon, target_type).min(:birth_at).should be_kind_of(DateTime)
0
+ target(Dragon, target_type).min(:birth_at).to_s.should == @birth_at.to_s
0
+ it 'should provide the lowest value of a Date property' do
0
+ target(Dragon, target_type).min(:birth_on).should be_kind_of(Date)
0
+ target(Dragon, target_type).min(:birth_on).to_s.should == @birth_on.to_s
0
+ it 'should provide the lowest value of a Time property' do
0
+ target(Dragon, target_type).min(:birth_time).should be_kind_of(Time)
0
+ target(Dragon, target_type).min(:birth_time).to_s.should == @birth_time.to_s
0
it 'should provide the lowest value when conditions provided' do
0
target(Dragon, target_type).min(:toes_on_claw, :is_fire_breathing => true).should == 4
0
target(Dragon, target_type).min(:toes_on_claw, :is_fire_breathing => false).should == 3
0
@@ -174,6 +196,21 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
0
target(Country, target_type).max(:gold_reserve_value).should == BigDecimal('22589877164500.0')
0
+ it 'should provide the highest value of a DateTime property' do
0
+ target(Dragon, target_type).min(:birth_at).should be_kind_of(DateTime)
0
+ target(Dragon, target_type).min(:birth_at).to_s.should == @birth_at.to_s
0
+ it 'should provide the highest value of a Date property' do
0
+ target(Dragon, target_type).min(:birth_on).should be_kind_of(Date)
0
+ target(Dragon, target_type).min(:birth_on).to_s.should == @birth_on.to_s
0
+ it 'should provide the highest value of a Time property' do
0
+ target(Dragon, target_type).min(:birth_time).should be_kind_of(Time)
0
+ target(Dragon, target_type).min(:birth_time).to_s.should == @birth_time.to_s
0
it 'should provide the highest value when conditions provided' do
0
target(Dragon, target_type).max(:toes_on_claw, :is_fire_breathing => true).should == 5
0
target(Dragon, target_type).max(:toes_on_claw, :is_fire_breathing => false).should == 3
0
@@ -247,6 +284,28 @@ if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
0
+ describe ".aggregate on a #{target_type}" do
0
+ describe 'with no arguments' do
0
+ it 'should raise an error' do
0
+ lambda { target(Dragon, target_type).aggregate }.should raise_error(ArgumentError)
0
+ describe 'with only aggregate fields specified' do
0
+ it 'should provide aggregate results' do
0
+ results = target(Dragon, target_type).aggregate(:all.count, :name.count, :toes_on_claw.min, :toes_on_claw.max, :toes_on_claw.avg, :toes_on_claw.sum)
0
+ results.should == [ 3, 2, 3, 5, 4.0, 12 ]
0
+ describe 'with aggregate fields and a property to group by' do
0
+ it 'should provide aggregate results' do
0
+ results = target(Dragon, target_type).aggregate(:all.count, :name.count, :toes_on_claw.min, :toes_on_claw.max, :toes_on_claw.avg, :toes_on_claw.sum, :is_fire_breathing)
0
+ results.should == [ [ 1, 1, 3, 3, 3.0, 3, false ], [ 2, 1, 4, 5, 4.5, 9, true ] ]
BTW: many thanks to dbussink for the initial code contributed that made this commit possible.