cumulated_report_spec.rb - reportable - Fork of reportable required by WarVox, from hdm/reportable.
 (HTM) git clone git://jay.scot/reportable
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
       cumulated_report_spec.rb (6135B)
       ---
            1 require File.join(File.dirname(File.dirname(File.expand_path(__FILE__))),'spec_helper')
            2 
            3 describe Saulabs::Reportable::CumulatedReport do
            4 
            5   before do
            6     @report = Saulabs::Reportable::CumulatedReport.new(User, :cumulated_registrations)
            7   end
            8 
            9   describe '#run' do
           10 
           11     it 'should cumulate the data' do
           12       @report.should_receive(:cumulate).once
           13 
           14       @report.run
           15     end
           16 
           17     it 'should return an array of the same length as the specified limit when :live_data is false' do
           18       @report = Saulabs::Reportable::CumulatedReport.new(User, :cumulated_registrations, :limit => 10, :live_data => false)
           19 
           20       @report.run.length.should == 10
           21     end
           22 
           23     it 'should return an array of the same length as the specified limit + 1 when :live_data is true' do
           24       @report = Saulabs::Reportable::CumulatedReport.new(User, :cumulated_registrations, :limit => 10, :live_data => true)
           25 
           26       @report.run.length.should == 11
           27     end
           28     
           29     for grouping in [:hour, :day, :week, :month] do
           30 
           31       describe "for grouping #{grouping.to_s}" do
           32 
           33         [true, false].each do |live_data|
           34 
           35           describe "with :live_data = #{live_data}" do
           36 
           37             before(:all) do
           38               User.delete_all
           39               User.create!(:login => 'test 1', :created_at => Time.now,                    :profile_visits => 2)
           40               User.create!(:login => 'test 2', :created_at => Time.now - 1.send(grouping), :profile_visits => 1)
           41               User.create!(:login => 'test 3', :created_at => Time.now - 3.send(grouping), :profile_visits => 2)
           42               User.create!(:login => 'test 4', :created_at => Time.now - 3.send(grouping), :profile_visits => 3)
           43             end
           44 
           45             describe 'the returned result' do
           46 
           47               before do
           48                 @grouping = Saulabs::Reportable::Grouping.new(grouping)
           49                 @report = Saulabs::Reportable::CumulatedReport.new(User, :cumulated_registrations,
           50                   :grouping  => grouping,
           51                   :limit     => 10,
           52                   :live_data => live_data
           53                 )
           54                 @result = @report.run
           55               end
           56 
           57               it "should be an array starting reporting period (Time.now - limit.#{grouping.to_s})" do
           58                 @result.first[0].should == Saulabs::Reportable::ReportingPeriod.new(@grouping, Time.now - 10.send(grouping)).date_time
           59               end
           60 
           61               if live_data
           62                 it "should be data ending with the current reporting period" do
           63                   @result.last[0].should == Saulabs::Reportable::ReportingPeriod.new(@grouping).date_time
           64                 end
           65               else
           66                 it "should be data ending with the reporting period before the current" do
           67                   @result.last[0].should == Saulabs::Reportable::ReportingPeriod.new(@grouping).previous.date_time
           68                 end
           69               end
           70 
           71             end
           72 
           73             it 'should return correct data for aggregation :count' do
           74               @report = Saulabs::Reportable::CumulatedReport.new(User, :registrations,
           75                 :aggregation => :count,
           76                 :grouping    => grouping,
           77                 :limit       => 10,
           78                 :live_data   => live_data
           79               )
           80               result = @report.run
           81 
           82               result[10][1].should == 4.0 if live_data
           83               result[9][1].should  == 3.0
           84               result[8][1].should  == 2.0
           85               result[7][1].should  == 2.0
           86               result[6][1].should  == 0.0
           87             end
           88 
           89             it 'should return correct data for aggregation :sum' do
           90               @report = Saulabs::Reportable::CumulatedReport.new(User, :registrations,
           91                 :aggregation  => :sum,
           92                 :grouping     => grouping,
           93                 :value_column => :profile_visits,
           94                 :limit        => 10,
           95                 :live_data    => live_data
           96               )
           97               result = @report.run()
           98 
           99               result[10][1].should == 8.0 if live_data
          100               result[9][1].should  == 6.0
          101               result[8][1].should  == 5.0
          102               result[7][1].should  == 5.0
          103               result[6][1].should  == 0.0
          104             end
          105 
          106             it 'should return correct data for aggregation :count when custom conditions are specified' do
          107               @report = Saulabs::Reportable::CumulatedReport.new(User, :registrations,
          108                 :aggregation => :count,
          109                 :grouping    => grouping,
          110                 :limit       => 10,
          111                 :live_data   => live_data
          112               )
          113               result = @report.run(:conditions => ['login IN (?)', ['test 1', 'test 2', 'test 4']])
          114 
          115               result[10][1].should == 3.0 if live_data
          116               result[9][1].should  == 2.0
          117               result[8][1].should  == 1.0
          118               result[7][1].should  == 1.0
          119               result[6][1].should  == 0.0
          120             end
          121 
          122             it 'should return correct data for aggregation :sum when custom conditions are specified' do
          123               @report = Saulabs::Reportable::CumulatedReport.new(User, :registrations,
          124                 :aggregation  => :sum,
          125                 :grouping     => grouping,
          126                 :value_column => :profile_visits,
          127                 :limit        => 10,
          128                 :live_data    => live_data
          129               )
          130               result = @report.run(:conditions => ['login IN (?)', ['test 1', 'test 2', 'test 4']])
          131 
          132               result[10][1].should == 6.0 if live_data
          133               result[9][1].should  == 4.0
          134               result[8][1].should  == 3.0
          135               result[7][1].should  == 3.0
          136               result[6][1].should  == 0.0
          137             end
          138 
          139           end
          140 
          141           after(:all) do
          142             User.destroy_all
          143           end
          144 
          145         end
          146 
          147       end
          148 
          149     end
          150 
          151     after(:each) do
          152       Saulabs::Reportable::ReportCache.destroy_all
          153     end
          154 
          155   end
          156 
          157   describe '#cumulate' do
          158 
          159     it 'should correctly cumulate the given data' do
          160       first = (Time.now - 1.week).to_s
          161       second = Time.now.to_s
          162       data = [[first, 1], [second, 2]]
          163 
          164       @report.send(:cumulate, data, @report.send(:options_for_run, {})).should == [[first, 1.0], [second, 3.0]]
          165     end
          166 
          167   end
          168 
          169 end