fixed specs for CumulatedReport; removed some unneccessary specs; fixed quoting of table names - reportable - Fork of reportable required by WarVox, from hdm/reportable.
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
 (DIR) commit f757bffbf6e3107a81753511f9f7dbe25b7baca1
 (DIR) parent bbec0de4cda48704427b6978a4d8080edf5d3a2d
 (HTM) Author: Marco Otte-Witte <marco.otte-witte@simplabs.com>
       Date:   Wed, 29 Apr 2009 11:11:20 +0200
       
       fixed specs for CumulatedReport; removed some unneccessary specs; fixed quoting of table names
       
       Diffstat:
         M lib/simplabs/reports_as_sparkline/… |       2 +-
         M lib/simplabs/reports_as_sparkline/… |       2 +-
         M spec/classes/cumulated_report_spec… |      51 ++++++++++++++++---------------
         M spec/classes/report_spec.rb         |      31 ++++++++++---------------------
       
       4 files changed, 38 insertions(+), 48 deletions(-)
       ---
 (DIR) diff --git a/lib/simplabs/reports_as_sparkline/cumulated_report.rb b/lib/simplabs/reports_as_sparkline/cumulated_report.rb
       @@ -22,7 +22,7 @@ module Simplabs #:nodoc:
        
              protected
        
       -        def cumulate(data, options) #:nodoc:
       +        def cumulate(data, options)
                  first_reporting_period = ReportingPeriod.first(options[:grouping], options[:limit], options[:end_date])
                  acc = initial_cumulative_value(first_reporting_period.date_time, options)
                  result = []
 (DIR) diff --git a/lib/simplabs/reports_as_sparkline/report.rb b/lib/simplabs/reports_as_sparkline/report.rb
       @@ -77,7 +77,7 @@ module Simplabs #:nodoc:
        
                def setup_conditions(begin_at, end_at, custom_conditions = [])
                  conditions = [@klass.send(:sanitize_sql_for_conditions, custom_conditions) || '']
       -          conditions[0] += "#{(conditions[0].blank? ? '' : ' AND ')}\"#{@klass.table_name}\".\"#{@date_column.to_s}\" "
       +          conditions[0] += "#{(conditions[0].blank? ? '' : ' AND ')}#{ActiveRecord::Base.connection.quote_table_name(@klass.table_name)}.#{ActiveRecord::Base.connection.quote_column_name(@date_column.to_s)} "
                  conditions[0] += if begin_at && end_at
                    'BETWEEN ? AND ?'
                  elsif begin_at
 (DIR) diff --git a/spec/classes/cumulated_report_spec.rb b/spec/classes/cumulated_report_spec.rb
       @@ -36,9 +36,10 @@ describe Simplabs::ReportsAsSparkline::CumulatedReport do
        
                    before(:all) do
                      User.delete_all
       -              User.create!(:login => 'test 1', :created_at => Time.now - 1.send(grouping), :profile_visits => 1)
       -              User.create!(:login => 'test 2', :created_at => Time.now - 3.send(grouping), :profile_visits => 2)
       -              User.create!(:login => 'test 3', :created_at => Time.now - 3.send(grouping), :profile_visits => 3)
       +              User.create!(:login => 'test 1', :created_at => Time.now - 1.send(grouping),  :profile_visits => 1)
       +              User.create!(:login => 'test 2', :created_at => Time.now - 3.send(grouping),  :profile_visits => 2)
       +              User.create!(:login => 'test 3', :created_at => Time.now - 3.send(grouping),  :profile_visits => 3)
       +              User.create!(:login => 'test 4', :created_at => Time.now - 20.send(grouping), :profile_visits => 1)
                    end
        
                    describe 'the returned result' do
       @@ -78,11 +79,11 @@ describe Simplabs::ReportsAsSparkline::CumulatedReport do
                      )
                      result = @report.run
        
       -              result[10][1].should == 3.0 if live_data
       -              result[9][1].should  == 3.0
       -              result[8][1].should  == 2.0
       -              result[7][1].should  == 2.0
       -              result[6][1].should  == 0.0
       +              result[10][1].should == 4.0 if live_data
       +              result[9][1].should  == 4.0
       +              result[8][1].should  == 3.0
       +              result[7][1].should  == 3.0
       +              result[6][1].should  == 1.0
                    end
        
                    it 'should return correct data for aggregation :sum' do
       @@ -95,11 +96,11 @@ describe Simplabs::ReportsAsSparkline::CumulatedReport do
                      )
                      result = @report.run()
        
       -              result[10][1].should == 6.0 if live_data
       -              result[9][1].should  == 6.0
       -              result[8][1].should  == 5.0
       -              result[7][1].should  == 5.0
       -              result[6][1].should  == 0.0
       +              result[10][1].should == 7.0 if live_data
       +              result[9][1].should  == 7.0
       +              result[8][1].should  == 6.0
       +              result[7][1].should  == 6.0
       +              result[6][1].should  == 1.0
                    end
        
                    it 'should return correct data for aggregation :count when custom conditions are specified' do
       @@ -109,13 +110,13 @@ describe Simplabs::ReportsAsSparkline::CumulatedReport do
                        :limit       => 10,
                        :live_data   => live_data
                      )
       -              result = @report.run(:conditions => ['login IN (?)', ['test 1', 'test 2']])
       +              result = @report.run(:conditions => ['login IN (?)', ['test 1', 'test 2', 'test 4']])
        
       -              result[10][1].should == 2.0 if live_data
       -              result[9][1].should  == 2.0
       -              result[8][1].should  == 1.0
       -              result[7][1].should  == 1.0
       -              result[6][1].should  == 0.0
       +              result[10][1].should == 3.0 if live_data
       +              result[9][1].should  == 3.0
       +              result[8][1].should  == 2.0
       +              result[7][1].should  == 2.0
       +              result[6][1].should  == 1.0
                    end
        
                    it 'should return correct data for aggregation :sum when custom conditions are specified' do
       @@ -126,13 +127,13 @@ describe Simplabs::ReportsAsSparkline::CumulatedReport do
                        :limit        => 10,
                        :live_data    => live_data
                      )
       -              result = @report.run(:conditions => ['login IN (?)', ['test 1', 'test 2']])
       +              result = @report.run(:conditions => ['login IN (?)', ['test 1', 'test 2', 'test 4']])
        
       -              result[10][1].should == 3.0 if live_data
       -              result[9][1].should  == 3.0
       -              result[8][1].should  == 2.0
       -              result[7][1].should  == 2.0
       -              result[6][1].should  == 0.0
       +              result[10][1].should == 4.0 if live_data
       +              result[9][1].should  == 4.0
       +              result[8][1].should  == 3.0
       +              result[7][1].should  == 3.0
       +              result[6][1].should  == 1.0
                    end
        
                  end
 (DIR) diff --git a/spec/classes/report_spec.rb b/spec/classes/report_spec.rb
       @@ -334,18 +334,19 @@ describe Simplabs::ReportsAsSparkline::Report do
            before do
              @begin_at = Time.now
              @end_at = 5.days.from_now
       +      @created_at_column_clause = "#{ActiveRecord::Base.connection.quote_table_name('users')}.#{ActiveRecord::Base.connection.quote_column_name('created_at')}"
            end
        
            it 'should return conditions for date_column BETWEEN begin_at and end_at only when no custom conditions are specified and both begin and end date are specified' do
       -      @report.send(:setup_conditions, @begin_at, @end_at).should == ['"users"."created_at" BETWEEN ? AND ?', @begin_at, @end_at]
       +      @report.send(:setup_conditions, @begin_at, @end_at).should == ["#{@created_at_column_clause} BETWEEN ? AND ?", @begin_at, @end_at]
            end
        
            it 'should return conditions for date_column >= begin_at when no custom conditions and a begin_at are specified' do
       -      @report.send(:setup_conditions, @begin_at, nil).should == ['"users"."created_at" >= ?', @begin_at]
       +      @report.send(:setup_conditions, @begin_at, nil).should == ["#{@created_at_column_clause} >= ?", @begin_at]
            end
            
            it 'should return conditions for date_column <= end_at when no custom conditions and a end_at are specified' do
       -      @report.send(:setup_conditions, nil, @end_at).should == ['"users"."created_at" <= ?', @end_at]
       +      @report.send(:setup_conditions, nil, @end_at).should == ["#{@created_at_column_clause} <= ?", @end_at]
            end
            
            it 'should raise an argument error when neither begin_at or end_at are specified' do
       @@ -353,11 +354,11 @@ describe Simplabs::ReportsAsSparkline::Report do
            end
        
            it 'should return conditions for date_column BETWEEN begin_at and end_date only when an empty Hash of custom conditions is specified' do
       -      @report.send(:setup_conditions, @begin_at, @end_at, {}).should == ['"users"."created_at" BETWEEN ? AND ?', @begin_at, @end_at]
       +      @report.send(:setup_conditions, @begin_at, @end_at, {}).should == ["#{@created_at_column_clause} BETWEEN ? AND ?", @begin_at, @end_at]
            end
        
            it 'should return conditions for date_column BETWEEN begin_at and end_date only when an empty Array of custom conditions is specified' do
       -      @report.send(:setup_conditions, @begin_at, @end_at, []).should == ['"users"."created_at" BETWEEN ? AND ?', @begin_at, @end_at]
       +      @report.send(:setup_conditions, @begin_at, @end_at, []).should == ["#{@created_at_column_clause} BETWEEN ? AND ?", @begin_at, @end_at]
            end
        
            it 'should correctly include custom conditions if they are specified as a Hash' do
       @@ -365,29 +366,17 @@ describe Simplabs::ReportsAsSparkline::Report do
        
              conditions = @report.send(:setup_conditions, @begin_at, @end_at, custom_conditions)
              # cannot directly check for string equqlity here since hashes are not ordered and so there is no way to now in which order the conditions are added to the SQL clause
       -      conditions[0].should =~ (/"users"."first_name" = \'first name\'/)
       -      conditions[0].should =~ (/"users"."last_name" = \'last name\'/)
       -      conditions[0].should =~ (/"users"."created_at" BETWEEN \? AND \?/)
       +      conditions[0].should =~ (/#{ActiveRecord::Base.connection.quote_table_name('users')}.#{ActiveRecord::Base.connection.quote_column_name('first_name')} = #{ActiveRecord::Base.connection.quote('first name')}/)
       +      conditions[0].should =~ (/#{ActiveRecord::Base.connection.quote_table_name('users')}.#{ActiveRecord::Base.connection.quote_column_name('last_name')} = #{ActiveRecord::Base.connection.quote('last name')}/)
       +      conditions[0].should =~ (/#{@created_at_column_clause} BETWEEN \? AND \?/)
              conditions[1].should == @begin_at
              conditions[2].should == @end_at
            end
        
       -    it 'should correctly translate { :column => nil }' do
       -      @report.send(:setup_conditions, @begin_at, @end_at, { :column => nil }).should == ['"users"."column" IS NULL AND "users"."created_at" BETWEEN ? AND ?', @begin_at, @end_at]
       -    end
       -
       -    it 'should correctly translate { :column => [1, 2] }' do
       -      @report.send(:setup_conditions, @begin_at, @end_at, { :column => [1, 2] }).should == ['"users"."column" IN (1,2) AND "users"."created_at" BETWEEN ? AND ?', @begin_at, @end_at]
       -    end
       -
       -    it 'should correctly translate { :column => (1..3) }' do
       -      @report.send(:setup_conditions, @begin_at, @end_at, { :column => (1..3) }).should == ['"users"."column" BETWEEN 1 AND 3 AND "users"."created_at" BETWEEN ? AND ?', @begin_at, @end_at]
       -    end
       -
            it 'should correctly include custom conditions if they are specified as an Array' do
              custom_conditions = ['first_name = ? AND last_name = ?', 'first name', 'last name']
        
       -      @report.send(:setup_conditions, @begin_at, @end_at, custom_conditions).should == ['first_name = \'first name\' AND last_name = \'last name\' AND "users"."created_at" BETWEEN ? AND ?', @begin_at, @end_at]
       +      @report.send(:setup_conditions, @begin_at, @end_at, custom_conditions).should == ["first_name = #{ActiveRecord::Base.connection.quote('first name')} AND last_name = #{ActiveRecord::Base.connection.quote('last name')} AND #{@created_at_column_clause} BETWEEN ? AND ?", @begin_at, @end_at]
            end
        
          end