added specs for Grouping.to_sql - reportable - Fork of reportable required by WarVox, from hdm/reportable.
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
 (DIR) commit 7f02d5870811625ca57691a29a3c9f749203f550
 (DIR) parent 627ddcc78052dbd58ea36f1d405deff5c6f8801d
 (HTM) Author: marcoow <marco.otte-witte@simplabs.com>
       Date:   Mon,  8 Dec 2008 22:48:15 +0800
       
       added specs for Grouping.to_sql
       
       Signed-off-by: Marco Otte-Witte <marco.otte-witte@simplabs.com>
       Diffstat:
         M lib/kvlr/reports_as_sparkline/grou… |      26 ++++++++++++--------------
         M lib/kvlr/reports_as_sparkline/repo… |      23 ++++++++++++++++-------
         M spec/other/grouping_spec.rb         |      80 +++++++++++++++++++++++++++++++
         M spec/other/report_cache_spec.rb     |       3 +++
       
       4 files changed, 111 insertions(+), 21 deletions(-)
       ---
 (DIR) diff --git a/lib/kvlr/reports_as_sparkline/grouping.rb b/lib/kvlr/reports_as_sparkline/grouping.rb
       @@ -4,10 +4,10 @@ module Kvlr #:nodoc:
        
            class Grouping
        
       -      @@allowed_groupings = [:month, :week, :day, :hour]
       +      @@allowed_groupings = 
        
              def initialize(grouping)
       -        raise ArgumentError.new("Argument grouping must be one of #{@@allowed_groupings.map(&:to_s).join(', ')}") unless @@allowed_groupings.include?(grouping)
       +        raise ArgumentError.new("Invalid grouping #{grouping}") unless [:hour, :day, :week, :month].include?(grouping)
                @identifier = grouping
              end
        
       @@ -17,6 +17,8 @@ module Kvlr #:nodoc:
        
              def to_reporting_period(date_time)
                return case @identifier
       +          when :hour
       +            DateTime.new(date_time.year, date_time.month, date_time.day, date_time.hour)
                  when :day
                    date_time.to_date
                  when :week
       @@ -24,13 +26,13 @@ module Kvlr #:nodoc:
                    Date.new(date_time.year, date_time.month, date_time.day)
                  when :month
                    Date.new(date_time.year, date_time.month)
       -          when :hour
       -            DateTime.new(date_time.year, date_time.month, date_time.day, date_time.hour)
                end
              end
        
              def first_reporting_period(limit)
                return case @identifier
       +          when :hour
       +            to_reporting_period(Time.now - limit.hours)
                  when :day
                    to_reporting_period(Time.now - limit.days)
                  when :week
       @@ -38,14 +40,10 @@ module Kvlr #:nodoc:
                  when :month
                    date = Time.now - limit.months
                    Date.new(date.year, date.month, 1)
       -          when :hour
       -            to_reporting_period(Time.now - limit.hours)
                end
              end
        
              def to_sql(date_column_name)
       -        #TODO: DATE_FORMAT's format string is different on different RDBMs => custom format string for all supported RDBMs needed!
       -        # => this can be implemented using ActiveRecord::Base.connection.class
                return case ActiveRecord::Base.connection.class.to_s
                  when 'ActiveRecord::ConnectionAdapters::MysqlAdapter'
                    mysql_format(date_column_name)
       @@ -60,40 +58,40 @@ module Kvlr #:nodoc:
        
                def mysql_format(date_column_name)
                  return case @identifier
       +            when :hour
       +              "DATE_FORMAT(#{date_column_name}, '%Y/%m/%d/%H')"
                    when :day
                      "DATE_FORMAT(#{date_column_name}, '%Y/%m/%d')"
                    when :week
                      "DATE_FORMAT(#{date_column_name}, '%Y-%u')"
                    when :month
                      "DATE_FORMAT(#{date_column_name}, '%Y/%m')"
       -            when :hour
       -              "DATE_FORMAT(#{date_column_name}, '%Y/%m/%d/%H')"
                  end
                end
        
                def sqlite_format(date_column_name)
                  return case @identifier
       +            when :hour
       +              "strftime('%Y/%m/%d/%H', #{date_column_name})"
                    when :day
                      "strftime('%Y/%m/%d', #{date_column_name})"
                    when :week
                      "strftime('%Y-%W', #{date_column_name})"
                    when :month
                      "strftime('%Y/%m', #{date_column_name})"
       -            when :hour
       -              "strftime('%Y/%m/%d/%H', #{date_column_name})"
                  end
                end
        
                def postgresql_format(date_column_name)
                  return case @identifier
       +            when :hour
       +              "date_trunc('hour', #{date_column_name})"
                    when :day
                      "date_trunc('day', #{date_column_name})"
                    when :week
                      "date_trunc('week', #{date_column_name})"
                    when :month
                      "date_trunc('month', #{date_column_name})"
       -            when :hour
       -              "date_trunc('hour', #{date_column_name})"
                  end
                end
        
 (DIR) diff --git a/lib/kvlr/reports_as_sparkline/report_cache.rb b/lib/kvlr/reports_as_sparkline/report_cache.rb
       @@ -10,11 +10,20 @@ module Kvlr #:nodoc:
                self.transaction do
                  cached_data = self.find(
                    :all,
       -            :conditions => { :model_name => report.klass.to_s, :report_name => report.name.to_s, :grouping => report.grouping.identifier.to_s, :aggregation => report.aggregation.to_s },
       +            :conditions => {
       +              :model_name  => report.klass.to_s,
       +              :report_name => report.name.to_s,
       +              :grouping    => report.grouping.identifier.to_s,
       +              :aggregation => report.aggregation.to_s
       +            },
                    :limit => limit,
                    :order => 'reporting_period DESC'
                  )
       -          last_reporting_period_to_read = cached_data.empty? ? report.grouping.first_reporting_period(limit) : report.grouping.to_reporting_period(cached_data.last.reporting_period)
       +          last_reporting_period_to_read = if cached_data.empty? 
       +              report.grouping.first_reporting_period(limit)
       +            else
       +              report.grouping.to_reporting_period(cached_data.last.reporting_period)
       +            end
                  new_data = yield(last_reporting_period_to_read)
                  return update_cache(new_data, cached_data, report)
                end
       @@ -30,12 +39,12 @@ module Kvlr #:nodoc:
                  end
                  for row in (new_data[rows_to_write] || [])
                    self.create!(
       -              :model_name => report.klass.to_s,
       -              :report_name => report.name.to_s,
       -              :grouping => report.grouping.identifier.to_s,
       -              :aggregation => report.aggregation.to_s,
       +              :model_name       => report.klass.to_s,
       +              :report_name      => report.name.to_s,
       +              :grouping         => report.grouping.identifier.to_s,
       +              :aggregation      => report.aggregation.to_s,
                      :reporting_period => report.grouping.to_reporting_period(DateTime.parse(row[0])),
       -              :value => row[1]
       +              :value            => row[1]
                    )
                  end
                  result = cached_data.map { |cached| [cached.reporting_period, cached.value] }
 (DIR) diff --git a/spec/other/grouping_spec.rb b/spec/other/grouping_spec.rb
       @@ -51,4 +51,84 @@ describe Kvlr::ReportsAsSparkline::Grouping do
        
          end
        
       +  describe '.to_sql' do
       +
       +    describe 'for MySQL' do
       +
       +      before do
       +        ActiveRecord::Base.connection.class.stub!(:to_s).and_return('ActiveRecord::ConnectionAdapters::MysqlAdapter')
       +      end
       +
       +      it 'should use DATE_FORMAT with format string "%Y/%m/%d/%H" for grouping :hour' do
       +        Kvlr::ReportsAsSparkline::Grouping.new(:hour).send(:to_sql, 'created_at').should == "DATE_FORMAT(created_at, '%Y/%m/%d/%H')"
       +      end
       +
       +      it 'should use DATE_FORMAT with format string "%Y/%m/%d" for grouping :day' do
       +        Kvlr::ReportsAsSparkline::Grouping.new(:day).send(:to_sql, 'created_at').should == "DATE_FORMAT(created_at, '%Y/%m/%d')"
       +      end
       +
       +      it 'should use DATE_FORMAT with format string "%Y-%u" for grouping :week' do
       +        Kvlr::ReportsAsSparkline::Grouping.new(:week).send(:to_sql, 'created_at').should == "DATE_FORMAT(created_at, '%Y-%u')"
       +      end
       +
       +      it 'should use DATE_FORMAT with format string "%Y/%m" for grouping :month' do
       +        Kvlr::ReportsAsSparkline::Grouping.new(:month).send(:to_sql, 'created_at').should == "DATE_FORMAT(created_at, '%Y/%m')"
       +      end
       +
       +    end
       +
       +    describe 'for PostgreSQL' do
       +
       +      before do
       +        ActiveRecord::Base.connection.class.stub!(:to_s).and_return('ActiveRecord::ConnectionAdapters::PostgreSQLAdapter')
       +      end
       +
       +      it 'should use date_trunc with trunc "hour" for grouping :hour' do
       +        Kvlr::ReportsAsSparkline::Grouping.new(:hour).send(:to_sql, 'created_at').should == "date_trunc('hour', created_at)"
       +      end
       +
       +      it 'should use date_trunc with trunc "day" for grouping :day' do
       +        Kvlr::ReportsAsSparkline::Grouping.new(:day).send(:to_sql, 'created_at').should == "date_trunc('day', created_at)"
       +      end
       +
       +      it 'should use date_trunc with trunc "week" for grouping :week' do
       +        Kvlr::ReportsAsSparkline::Grouping.new(:week).send(:to_sql, 'created_at').should == "date_trunc('week', created_at)"
       +      end
       +
       +      it 'should use date_trunc with trunc "month" for grouping :month' do
       +        Kvlr::ReportsAsSparkline::Grouping.new(:month).send(:to_sql, 'created_at').should == "date_trunc('month', created_at)"
       +      end
       +
       +    end
       +
       +    describe 'for SQLite3' do
       +
       +      before do
       +        ActiveRecord::Base.connection.class.stub!(:to_s).and_return('ActiveRecord::ConnectionAdapters::SQLite3Adapter')
       +      end
       +
       +      it 'should use strftime with format string "%Y/%m/%d/%H" for grouping :hour' do
       +        Kvlr::ReportsAsSparkline::Grouping.new(:hour).send(:to_sql, 'created_at').should == "strftime('%Y/%m/%d/%H', created_at)"
       +      end
       +
       +      it 'should use strftime with format string "%Y/%m/%d" for grouping :day' do
       +        Kvlr::ReportsAsSparkline::Grouping.new(:day).send(:to_sql, 'created_at').should == "strftime('%Y/%m/%d', created_at)"
       +      end
       +
       +      it 'should use strftime with format string "%Y-%W" for grouping :week' do
       +        Kvlr::ReportsAsSparkline::Grouping.new(:week).send(:to_sql, 'created_at').should == "strftime('%Y-%W', created_at)"
       +      end
       +
       +      it 'should use strftime with format string "%Y/%m" for grouping :month' do
       +        Kvlr::ReportsAsSparkline::Grouping.new(:month).send(:to_sql, 'created_at').should == "strftime('%Y/%m', created_at)"
       +      end
       +
       +    end
       +
       +  end
       +
        end
       +
       +class ActiveRecord::ConnectionAdapters::MysqlAdapter; end
       +class ActiveRecord::ConnectionAdapters::SQLite3Adapter; end
       +class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter; end
 (DIR) diff --git a/spec/other/report_cache_spec.rb b/spec/other/report_cache_spec.rb
       @@ -98,4 +98,7 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
        
          end
        
       +  describe '#update_cache' do
       +  end
       +
        end