refactored ReportCache#prepare_values - reportable - Fork of reportable required by WarVox, from hdm/reportable.
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
 (DIR) commit 1a5f067f3b860469357f8a05db8efcbc2ca3ca0a
 (DIR) parent 0080b2cf29f53a7f20991bab2056766fd6d5bf27
 (HTM) Author: Marco Otte-Witte <marco.otte-witte@simplabs.com>
       Date:   Tue, 13 Jan 2009 23:29:27 +0800
       
       refactored ReportCache#prepare_values
       
       Signed-off-by: Marco Otte-Witte <marco.otte-witte@simplabs.com>
       Diffstat:
         M lib/kvlr/reports_as_sparkline/cumu… |       3 +--
         M lib/kvlr/reports_as_sparkline/repo… |       2 +-
         M lib/kvlr/reports_as_sparkline/repo… |      31 +++++++++++++------------------
         M lib/kvlr/reports_as_sparkline/repo… |      30 +++++++++++++++++++-----------
         M spec/models/report_method_spec.rb   |       8 ++++----
         M spec/other/cumulated_report_spec.rb |      46 ++++++++++++++++----------------
         M spec/other/report_cache_spec.rb     |      71 +++++++++++++++++--------------
         M spec/other/report_spec.rb           |      58 ++++++++++++++++++++-----------
         M spec/other/reporting_period_spec.rb |      44 ++++++++++++++++----------------
       
       9 files changed, 160 insertions(+), 133 deletions(-)
       ---
 (DIR) diff --git a/lib/kvlr/reports_as_sparkline/cumulated_report.rb b/lib/kvlr/reports_as_sparkline/cumulated_report.rb
       @@ -25,11 +25,10 @@ module Kvlr #:nodoc:
                def cumulate(data) #:nodoc:
                  acc = 0.0
                  result = []
       -          data.reverse_each do |element|
       +          data.each do |element|
                    acc += element[1].to_f
                    result << [element[0], acc]
                  end
       -          result.reverse!
                  result
                end
        
 (DIR) diff --git a/lib/kvlr/reports_as_sparkline/report.rb b/lib/kvlr/reports_as_sparkline/report.rb
       @@ -56,7 +56,7 @@ module Kvlr #:nodoc:
                    @value_column,
                    :conditions => conditions,
                    :group => @grouping.to_sql(@date_column),
       -            :order => "#{@grouping.to_sql(@date_column)} DESC"
       +            :order => "#{@grouping.to_sql(@date_column)} ASC"
                  )
                end
        
 (DIR) diff --git a/lib/kvlr/reports_as_sparkline/report_cache.rb b/lib/kvlr/reports_as_sparkline/report_cache.rb
       @@ -23,7 +23,7 @@ module Kvlr #:nodoc:
                      :limit => limit,
                      :order => 'reporting_period ASC'
                    )
       -            last_reporting_period_to_read = ReportingPeriod.new(report.grouping, cached_data.last.reporting_period) unless cached_data.empty?
       +            last_reporting_period_to_read = ReportingPeriod.new(report.grouping, cached_data.last.reporting_period).next unless cached_data.empty?
                  end
                  new_data = yield(last_reporting_period_to_read.date_time)
                  prepare_result(new_data, cached_data, last_reporting_period_to_read, report, no_cache)[0..(limit - 1)]
       @@ -34,29 +34,24 @@ module Kvlr #:nodoc:
        
                def self.prepare_result(new_data, cached_data, last_reporting_period_to_read, report, no_cache = false)
                  new_data.map! { |data| [ReportingPeriod.from_db_string(report.grouping, data[0]), data[1]] }
       -          result = []
       -          reporting_period = ReportingPeriod.new(report.grouping)
       -          while reporting_period != last_reporting_period_to_read
       -            data = new_data.detect { |data| data[0] == reporting_period }
       -            cached = build_cached_data(report, reporting_period, data ? data[1] : 0.0)
       +          result = cached_data.map { |cached| [cached.reporting_period, cached.value] }
       +          current_reporting_period = ReportingPeriod.new(report.grouping)
       +          reporting_period = last_reporting_period_to_read
       +          while reporting_period < current_reporting_period
       +            cached = build_cached_data(report, reporting_period, find_value(new_data, reporting_period))
                    cached.save! unless no_cache
                    result << [reporting_period.date_time, cached.value]
       -            reporting_period = reporting_period.previous
       +            reporting_period = reporting_period.next
                  end
       -          data = (new_data.first && new_data.first[0] == last_reporting_period_to_read) ? new_data.first : nil
       -          unless no_cache
       -            if data && cached = cached_data.last
       -              cached.update_attributes!(:value => data[1])
       -            else
       -              cached = build_cached_data(report, last_reporting_period_to_read, data ? data[1] : 0.0)
       -              cached.save!
       -              result << [last_reporting_period_to_read.date_time, cached.value]
       -            end
       -          end
       -          result += (cached_data.map { |cached| [cached.reporting_period, cached.value] }).reverse
       +          result << [current_reporting_period.date_time, find_value(new_data, current_reporting_period)]
                  result
                end
        
       +        def self.find_value(data, reporting_period)
       +          data = data.detect { |d| d[0] == reporting_period }
       +          data ? data[1] : 0.0
       +        end
       +
                def self.build_cached_data(report, reporting_period, value)
                  self.new(
                    :model_name       => report.klass.to_s,
 (DIR) diff --git a/lib/kvlr/reports_as_sparkline/reporting_period.rb b/lib/kvlr/reports_as_sparkline/reporting_period.rb
       @@ -15,7 +15,8 @@ module Kvlr #:nodoc:
                @date_time = parse_date_time(date_time)
              end
        
       -      # Returns the first reporting period for a grouping and a limit; e.g. the first reporting period for Grouping :day and limit 2 would be Time.now - 2.days
       +      # Returns the first reporting period for a grouping and a limit; e.g. the first reporting period for Grouping :day and limit 2 would be Time.now - 1.days
       +      # (since limit is 2, 2 reporting periods are included in the range, that is yesterday and today)
              #
              # ==== Parameters
              # * <tt>grouping</tt> - The Kvlr::ReportsAsSparkline::Grouping of the reporting period
       @@ -23,13 +24,13 @@ module Kvlr #:nodoc:
              def self.first(grouping, limit)
                return case grouping.identifier
                  when :hour
       -            self.new(grouping, DateTime.now - limit.hours)
       +            self.new(grouping, DateTime.now - (limit - 1).hours)
                  when :day
       -            self.new(grouping, DateTime.now - limit.days)
       +            self.new(grouping, DateTime.now - (limit - 1).days)
                  when :week
       -            self.new(grouping, DateTime.now - limit.weeks)
       +            self.new(grouping, DateTime.now - (limit - 1).weeks)
                  when :month
       -            date = DateTime.now - limit.months
       +            date = DateTime.now - (limit - 1).months
                    self.new(grouping, Date.new(date.year, date.month, 1))
                end
              end
       @@ -49,17 +50,17 @@ module Kvlr #:nodoc:
                result
              end
        
       -      # Returns the previous reporting period
       -      def previous
       +      # Returns the next reporting period (that is next hour/day/month/year)
       +      def next
                return case @grouping.identifier
                  when :hour
       -            self.class.new(@grouping, @date_time - 1.hour)
       +            self.class.new(@grouping, @date_time + 1.hour)
                  when :day
       -            self.class.new(@grouping, @date_time - 1.day)
       +            self.class.new(@grouping, @date_time + 1.day)
                  when :week
       -            self.class.new(@grouping, @date_time - 1.week)
       +            self.class.new(@grouping, @date_time + 1.week)
                  when :month
       -            self.class.new(@grouping, @date_time - 1.month)
       +            self.class.new(@grouping, @date_time + 1.month)
                end
              end
        
       @@ -70,6 +71,13 @@ module Kvlr #:nodoc:
                false
              end
        
       +      def <(other) #:nodoc:
       +        if other.class == Kvlr::ReportsAsSparkline::ReportingPeriod
       +          return @date_time < other.date_time
       +        end
       +        raise ArgumentError.new("Can only compare instances of #{Kvlr::ReportsAsSparkline::ReportingPeriod.klass}")
       +      end
       +
              private
        
                def parse_date_time(date_time)
 (DIR) diff --git a/spec/models/report_method_spec.rb b/spec/models/report_method_spec.rb
       @@ -12,17 +12,17 @@ describe Kvlr::ReportsAsSparkline do
        
            it 'should include all data when invoked on the base model class' do
              result = User.registrations_report.to_a
       -
       +puts result.inspect
              result.length.should == 10
       -      result[1][1].should  == 1
       -      result[2][1].should  == 2
       +      result[8][1].should  == 1.0
       +      result[7][1].should  == 2.0
            end
        
            it 'should include only data for instances of the inherited model when invoked on the inherited model class' do
              result = SpecialUser.registrations_report.to_a
        
              result.length.should == 10
       -      result[2][1].should  == 1
       +      result[7][1].should  == 1.0
            end
        
            after(:all) do
 (DIR) diff --git a/spec/other/cumulated_report_spec.rb b/spec/other/cumulated_report_spec.rb
       @@ -38,12 +38,12 @@ describe Kvlr::ReportsAsSparkline::CumulatedReport do
                    @result = @report.run
                  end
        
       -          it "should return data starting with the current reporting period" do
       -            @result.first[0].should == Kvlr::ReportsAsSparkline::ReportingPeriod.new(@grouping).date_time
       +          it "should return an array starting reporting period (Time.now - (limit - 1).#{grouping.to_s})" do
       +            @result.first[0].should == Kvlr::ReportsAsSparkline::ReportingPeriod.new(@grouping, Time.now - 9.send(grouping)).date_time
                  end
        
       -          it "should return data ending with reporting period (Time.now - (limit - 1).#{grouping.to_s})" do
       -            @result.last[0].should == Kvlr::ReportsAsSparkline::ReportingPeriod.new(@grouping, Time.now - 9.send(grouping)).date_time
       +          it "should return data ending with with the current reporting period" do
       +            @result.last[0].should == Kvlr::ReportsAsSparkline::ReportingPeriod.new(@grouping).date_time
                  end
        
                end
       @@ -52,40 +52,40 @@ describe Kvlr::ReportsAsSparkline::CumulatedReport do
                  @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :registrations, :aggregation => :count, :grouping => grouping, :limit => 10)
                  result = @report.run
        
       -          result[0][1].should == 3
       -          result[1][1].should == 3
       -          result[2][1].should == 2
       -          result[3][1].should == 2
       +          result[9][1].should == 3.0
       +          result[8][1].should == 3.0
       +          result[7][1].should == 2.0
       +          result[6][1].should == 2.0
                end
        
                it 'should return correct data for aggregation :sum' do
                  @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :registrations, :aggregation => :sum, :grouping => grouping, :value_column => :profile_visits, :limit => 10)
                  result = @report.run()
        
       -          result[0][1].should == 6
       -          result[1][1].should == 6
       -          result[2][1].should == 5
       -          result[3][1].should == 5
       +          result[9][1].should == 6.0
       +          result[8][1].should == 6.0
       +          result[7][1].should == 5.0
       +          result[6][1].should == 5.0
                end
        
                it 'should return correct data for aggregation :count when custom conditions are specified' do
                  @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :registrations, :aggregation => :count, :grouping => grouping, :limit => 10)
                  result = @report.run(:conditions => ['login IN (?)', ['test 1', 'test 2']])
        
       -          result[0][1].should == 2
       -          result[1][1].should == 2
       -          result[2][1].should == 1
       -          result[3][1].should == 1
       +          result[9][1].should == 2.0
       +          result[8][1].should == 2.0
       +          result[7][1].should == 1.0
       +          result[6][1].should == 1.0
                end
        
                it 'should return correct data for aggregation :sum when custom conditions are specified' do
                  @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :registrations, :aggregation => :sum, :grouping => grouping, :value_column => :profile_visits, :limit => 10)
                  result = @report.run(:conditions => ['login IN (?)', ['test 1', 'test 2']])
        
       -          result[0][1].should == 3
       -          result[1][1].should == 3
       -          result[2][1].should == 2
       -          result[3][1].should == 2
       +          result[9][1].should == 3.0
       +          result[8][1].should == 3.0
       +          result[7][1].should == 2.0
       +          result[6][1].should == 2.0
                end
        
                after(:all) do
       @@ -105,11 +105,11 @@ describe Kvlr::ReportsAsSparkline::CumulatedReport do
          describe '.cumulate' do
        
            it 'should correctly cumulate the given data' do
       -      first = Time.now.to_s
       -      second = (Time.now - 1.week).to_s
       +      first = (Time.now - 1.week).to_s
       +      second = Time.now.to_s
              data = [[first, 1], [second, 2]]
        
       -      @report.send(:cumulate, data).should == [[first, 3.0], [second, 2.0]]
       +      @report.send(:cumulate, data).should == [[first, 1.0], [second, 3.0]]
            end
        
          end
 (DIR) diff --git a/spec/other/report_cache_spec.rb b/spec/other/report_cache_spec.rb
       @@ -6,7 +6,7 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
            @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations)
          end
        
       -  describe '#process' do
       +  describe '.process' do
        
            before do
              Kvlr::ReportsAsSparkline::ReportCache.stub!(:find).and_return([])
       @@ -46,7 +46,7 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
                :order => 'reporting_period ASC'
              )
        
       -      puts Kvlr::ReportsAsSparkline::ReportCache.process(@report, 10) { [] }
       +      Kvlr::ReportsAsSparkline::ReportCache.process(@report, 10) { [] }
            end
        
            it 'should prepare the results before it returns them' do
       @@ -66,7 +66,7 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
              end
            end
        
       -    it 'should yield the last reporting period in the cache if the cache is not empty' do
       +    it 'should yield the reporting period after the last one in the cache if the cache is not empty' do
              reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(@report.grouping)
              cached = Kvlr::ReportsAsSparkline::ReportCache.new({
                :model_name       => @report.klass,
       @@ -79,7 +79,7 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
              Kvlr::ReportsAsSparkline::ReportCache.stub!(:find).and_return([cached])
        
              Kvlr::ReportsAsSparkline::ReportCache.process(@report, 10) do |begin_at|
       -        begin_at.should == reporting_period.date_time
       +        begin_at.should == reporting_period.next.date_time
                []
              end
            end
       @@ -103,16 +103,15 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
        
          end
        
       -  describe '#prepare_result' do
       +  describe '.prepare_result' do
        
            before do
              @last_reporting_period_to_read = Kvlr::ReportsAsSparkline::ReportingPeriod.first(@report.grouping, 10)
       -      @new_data = [['2008/12', 1.0]]
       -      Kvlr::ReportsAsSparkline::ReportingPeriod.stub!(:from_db_string).and_return(Kvlr::ReportsAsSparkline::ReportingPeriod.new(@report.grouping))
       +      @new_data = [[@last_reporting_period_to_read.date_time, 1.0]]
       +      Kvlr::ReportsAsSparkline::ReportingPeriod.stub!(:from_db_string).and_return(@last_reporting_period_to_read)
              @cached = Kvlr::ReportsAsSparkline::ReportCache.new
              @cached.stub!(:save!)
       -      @cached.stub!(:reporting_period).and_return(Kvlr::ReportsAsSparkline::ReportingPeriod.new(@report.grouping).date_time)
       -      Kvlr::ReportsAsSparkline::ReportCache.stub!(:new).and_return(@cached)
       +      Kvlr::ReportsAsSparkline::ReportCache.stub!(:build_cached_data).and_return(@cached)
            end
        
            it 'should convert the date strings from the newly read data to reporting periods' do
       @@ -121,27 +120,26 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
              Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [], @last_reporting_period_to_read, @report)
            end
        
       -    it 'should create a new Kvlr::ReportsAsSparkline::ReportCache with the correct data and value 0 if no new data has been read' do
       -      Kvlr::ReportsAsSparkline::ReportCache.should_receive(:new).once.with(
       -        :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 => anything(),
       -        :value            => 0
       +    it 'should create (:limit - 1) instances of Kvlr::ReportsAsSparkline::ReportCache with value 0.0 if no new data has been read and nothing was cached' do
       +      Kvlr::ReportsAsSparkline::ReportCache.should_receive(:build_cached_data).exactly(9).times.with(
       +        @report,
       +        anything(),
       +        0.0
              ).and_return(@cached)
        
              Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, [], [], @last_reporting_period_to_read, @report)
            end
        
       -    it 'should create a new Kvlr::ReportsAsSparkline::ReportCache with the correct data and value if new data has been read' do
       -      Kvlr::ReportsAsSparkline::ReportCache.should_receive(:new).once.with(
       -        :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 => anything(),
       -        :value            => 1.0
       +    it 'should create a new Kvlr::ReportsAsSparkline::ReportCache with the correct value if new data has been read' do
       +      Kvlr::ReportsAsSparkline::ReportCache.should_receive(:build_cached_data).exactly(8).times.with(
       +        @report,
       +        anything(),
       +        0.0
       +      ).and_return(@cached)
       +      Kvlr::ReportsAsSparkline::ReportCache.should_receive(:build_cached_data).once.with(
       +        @report,
       +        @last_reporting_period_to_read,
       +        1.0
              ).and_return(@cached)
        
              Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [], @last_reporting_period_to_read, @report)
       @@ -162,13 +160,6 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
              result[0][1].should be_kind_of(Float)
            end
        
       -    it 'should update the last cached record if new data has been read for the last reporting period to read' do
       -      Kvlr::ReportsAsSparkline::ReportingPeriod.stub!(:from_db_string).and_return(@last_reporting_period_to_read)
       -      @cached.should_receive(:update_attributes!).once.with(:value => 1.0)
       -
       -      Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [@cached], @last_reporting_period_to_read, @report)
       -    end
       -
            describe 'with no_cache = true' do
        
              it 'should not save the created Kvlr::ReportsAsSparkline::ReportCache' do
       @@ -188,4 +179,20 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
        
          end
        
       +  describe '.find_value' do
       +
       +    before do
       +      @data = [[Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:day)), 3.0]]
       +    end
       +
       +    it 'should return the correct value when new data has been read for the reporting period' do
       +      Kvlr::ReportsAsSparkline::ReportCache.send(:find_value, @data, @data[0][0]).should == 3.0
       +    end
       +
       +    it 'should return 0.0 when no data has been read for the reporting period' do
       +      Kvlr::ReportsAsSparkline::ReportCache.send(:find_value, @data, @data[0][0].next).should == 0.0
       +    end
       +
       +  end
       +
        end
 (DIR) diff --git a/spec/other/report_spec.rb b/spec/other/report_spec.rb
       @@ -36,44 +36,62 @@ describe Kvlr::ReportsAsSparkline::Report do
                  User.create!(:login => 'test 3', :created_at => Time.now - 3.send(grouping), :profile_visits => 3)
                end
        
       +        describe do
       +
       +          before do
       +            @grouping = Kvlr::ReportsAsSparkline::Grouping.new(grouping)
       +            @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations, :grouping => grouping, :limit => 10)
       +            @result = @report.run
       +          end
       +
       +          it "should return an array starting reporting period (Time.now - (limit - 1).#{grouping.to_s})" do
       +            @result.first[0].should == Kvlr::ReportsAsSparkline::ReportingPeriod.new(@grouping, Time.now - 9.send(grouping)).date_time
       +          end
       +
       +          it "should return data ending with with the current reporting period" do
       +            @result.last[0].should == Kvlr::ReportsAsSparkline::ReportingPeriod.new(@grouping).date_time
       +          end
       +
       +        end
       +
                it 'should return correct data for aggregation :count' do
                  @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations, :aggregation => :count, :grouping => grouping, :limit => 10)
                  result = @report.run.to_a
        
       -          result[0][1].should == 0
       -          result[1][1].should == 1
       -          result[2][1].should == 0
       -          result[3][1].should == 2
       +          result[9][1].should == 0.0
       +          result[8][1].should == 1.0
       +          result[7][1].should == 0.0
       +          result[6][1].should == 2.0
                end
        
                it 'should return correct data for aggregation :sum' do
                  @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations, :aggregation => :sum, :grouping => grouping, :value_column => :profile_visits, :limit => 10)
                  result = @report.run().to_a
        
       -          result[0][1].should == 0
       -          result[1][1].should == 1
       -          result[2][1].should == 0
       -          result[3][1].should == 5
       +          result[9][1].should == 0.0
       +          result[8][1].should == 1.0
       +          result[7][1].should == 0.0
       +          result[6][1].should == 5.0
                end
        
                it 'should return correct data for aggregation :count when custom conditions are specified' do
                  @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations, :aggregation => :count, :grouping => grouping, :limit => 10)
                  result = @report.run(:conditions => ['login IN (?)', ['test 1', 'test 2']]).to_a
        
       -          result[0][1].should == 0
       -          result[1][1].should == 1
       -          result[2][1].should == 0
       -          result[3][1].should == 1
       +          result[9][1].should == 0.0
       +          result[8][1].should == 1.0
       +          result[7][1].should == 0.0
       +          result[6][1].should == 1.0
                end
        
                it 'should return correct data for aggregation :sum when custom conditions are specified' do
                  @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations, :aggregation => :sum, :grouping => grouping, :value_column => :profile_visits, :limit => 10)
                  result = @report.run(:conditions => ['login IN (?)', ['test 1', 'test 2']]).to_a
        
       -          result[0][1].should == 0
       -          result[1][1].should == 1
       -          result[2][1].should == 0
       -          result[3][1].should == 2
       +          result[9][1].should == 0.0
       +          result[8][1].should == 1.0
       +          result[7][1].should == 0.0
       +          result[6][1].should == 2.0
                end
        
                after(:all) do
       @@ -173,12 +191,12 @@ describe Kvlr::ReportsAsSparkline::Report do
        
              it 'should not raise an error if valid options are specified' do
                lambda { @report.send(:ensure_valid_options, {
       -            :limit             => 100,
       -            :aggregation       => :count,
       -            :grouping          => :day,
       +            :limit        => 100,
       +            :aggregation  => :count,
       +            :grouping     => :day,
                    :date_column  => :created_at,
                    :value_column => :id,
       -            :conditions        => []
       +            :conditions   => []
                  })
                }.should_not raise_error(ArgumentError)
              end
 (DIR) diff --git a/spec/other/reporting_period_spec.rb b/spec/other/reporting_period_spec.rb
       @@ -91,38 +91,38 @@ describe Kvlr::ReportsAsSparkline::ReportingPeriod do
        
          end
        
       -  describe '.previous' do
       +  describe '.next' do
        
       -    it 'should return a reporting period with date and time one hour before the current period for grouping :hour' do
       +    it 'should return a reporting period with date and time one hour after the current period for grouping :hour' do
              now = Time.now
              reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:hour), now)
       -      expected = now - 1.hour
       +      expected = now + 1.hour
        
       -      reporting_period.previous.date_time.should == DateTime.new(expected.year, expected.month, expected.day, expected.hour)
       +      reporting_period.next.date_time.should == DateTime.new(expected.year, expected.month, expected.day, expected.hour)
            end
        
       -    it 'should return a reporting period with date one day before the current period for grouping :day' do
       +    it 'should return a reporting period with date one day after the current period for grouping :day' do
              now = Time.now
              reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:day), now)
       -      expected = now - 1.day
       +      expected = now + 1.day
        
       -      reporting_period.previous.date_time.should == Date.new(expected.year, expected.month, expected.day)
       +      reporting_period.next.date_time.should == Date.new(expected.year, expected.month, expected.day)
            end
        
       -    it 'should return a reporting period with date one week before the current period for grouping :week' do
       +    it 'should return a reporting period with date one week after the current period for grouping :week' do
              now = DateTime.now
              reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:week), now)
       -      expected = reporting_period.date_time - 1.week
       +      expected = reporting_period.date_time + 1.week
        
       -      reporting_period.previous.date_time.should == Date.new(expected.year, expected.month, expected.day)
       +      reporting_period.next.date_time.should == Date.new(expected.year, expected.month, expected.day)
            end
        
       -    it 'should return a reporting period with date of the first day in the month one month before the current period' do
       +    it 'should return a reporting period with date of the first day in the month one month after the current period' do
              now = Time.now
              reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:month), now)
       -      expected = reporting_period.date_time - 1.month
       +      expected = reporting_period.date_time + 1.month
        
       -      reporting_period.previous.date_time.should == Date.new(expected.year, expected.month, 1)
       +      reporting_period.next.date_time.should == Date.new(expected.year, expected.month, 1)
            end
        
          end
       @@ -161,39 +161,39 @@ describe Kvlr::ReportsAsSparkline::ReportingPeriod do
        
          end
        
       -  describe '#first' do
       +  describe '.first' do
        
            before do
              @now = DateTime.now
              DateTime.stub!(:now).and_return(@now)
            end
        
       -    it 'should return a reporting period with the date part of (DateTime.now - limit.hours) with minutes = seconds = 0 for grouping :hour' do
       +    it 'should return a reporting period with the date part of (DateTime.now - (limit - 1).hours) with minutes = seconds = 0 for grouping :hour' do
              reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.first(Kvlr::ReportsAsSparkline::Grouping.new(:hour), 3)
       -      expected = @now - 3.hours
       +      expected = @now - 2.hours
        
              reporting_period.date_time.should == DateTime.new(expected.year, expected.month, expected.day, expected.hour, 0, 0)
            end
        
       -    it 'should return a reporting period with the date part of (DateTime.now - limit.days) for grouping :day' do
       +    it 'should return a reporting period with the date part of (DateTime.now - (limit - 1).days) for grouping :day' do
              reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.first(Kvlr::ReportsAsSparkline::Grouping.new(:day), 3)
       -      expected = @now - 3.days
       +      expected = @now - 2.days
        
              reporting_period.date_time.should == Date.new(expected.year, expected.month, expected.day)
            end
        
       -    it 'should return a reporting period with the date of the first day of the month at (DateTime.now - limit.weeks) for grouping :month' do
       +    it 'should return a reporting period with the date of the first day of the month at (DateTime.now - (limit - 1).weeks) for grouping :month' do
              DateTime.stub!(:now).and_return(DateTime.new(2008, 12, 31, 0, 0, 0))
              reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.first(Kvlr::ReportsAsSparkline::Grouping.new(:month), 3)
        
       -      reporting_period.date_time.should == DateTime.new(2008, 9, 1)
       +      reporting_period.date_time.should == DateTime.new(2008, 10, 1)
            end
        
       -    it 'should return a reporting period with the date of the monday of the week at (DateTime.now - limit.weeks) for grouping :week' do
       +    it 'should return a reporting period with the date of the monday of the week at (DateTime.now - (limit - 1).weeks) for grouping :week' do
              DateTime.stub!(:now).and_return(DateTime.new(2008, 12, 31, 0, 0, 0)) #wednesday
              reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.first(Kvlr::ReportsAsSparkline::Grouping.new(:week), 3)
        
       -      reporting_period.date_time.should == DateTime.new(2008, 12, 8) #the monday 3 weeks earlier
       +      reporting_period.date_time.should == DateTime.new(2008, 12, 15) #the monday 2 weeks earlier
            end
        
          end