cleaned up specs - reportable - Fork of reportable required by WarVox, from hdm/reportable.
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
 (DIR) commit bbd76660d6b48d0a91aac63178aaa72b95d1a9d3
 (DIR) parent 415849fbf6a05c044df8465162a4218165f1d773
 (HTM) Author: marcoow <marco.otte-witte@simplabs.com>
       Date:   Fri, 12 Dec 2008 01:42:18 +0800
       
       cleaned up specs
       
       Signed-off-by: Marco Otte-Witte <marco.otte-witte@simplabs.com>
       Diffstat:
         M lib/kvlr/reports_as_sparkline/asse… |       2 +-
         M lib/kvlr/reports_as_sparkline/grou… |       2 +-
         M spec/models/report_method_spec.rb   |      42 ++++++++-----------------------
         M spec/other/cumulated_report_spec.rb |       8 ++++----
         M spec/other/grouping_spec.rb         |      55 +++++++++++++++++--------------
         M spec/other/report_cache_spec.rb     |      42 ++++++++++++++++++-------------
         M spec/other/report_spec.rb           |      22 +++++++++++-----------
         M spec/other/reporting_period_spec.rb |     127 +++++++++++++++----------------
       
       8 files changed, 142 insertions(+), 158 deletions(-)
       ---
 (DIR) diff --git a/lib/kvlr/reports_as_sparkline/asset_tag_helper.rb b/lib/kvlr/reports_as_sparkline/asset_tag_helper.rb
       @@ -8,7 +8,7 @@ module Kvlr #:nodoc:
              # Renders a sparkline with the given data.
              def sparkline_tag(data, options = {})
                options.reverse_merge!({:width => 300, :height => 34, :color => '0077CC'})
       -        data.collect! { |element| element[1].to_i }
       +        data.collect! { |element| element[1].to_s }
                image_tag(
                  "http://chart.apis.google.com/chart?cht=ls&chs=#{options[:width]}x#{options[:height]}&chd=t:#{data.join(',')}&chco=#{options[:color]}&chm=B,E6F2FA,0,0,0&chls=1,0,0&chds=#{data.min},#{data.max}"
                )
 (DIR) diff --git a/lib/kvlr/reports_as_sparkline/grouping.rb b/lib/kvlr/reports_as_sparkline/grouping.rb
       @@ -16,7 +16,7 @@ module Kvlr #:nodoc:
              def date_parts_from_db_string(db_string)
                if ActiveRecord::Base.connection.class.to_s == 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter'
                  if @identifier == :hour
       -            return (db_string[0..9].split('-') + db_string[11..12]).map(&:to_i)
       +            return (db_string[0..9].split('-') + [db_string[11..12]]).map(&:to_i)
                  elsif @identifier == :day
                    return db_string[0..9].split('-').map(&:to_i)
                  elsif @identifier == :week
 (DIR) diff --git a/spec/models/report_method_spec.rb b/spec/models/report_method_spec.rb
       @@ -2,49 +2,27 @@ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
        
        describe Kvlr::ReportsAsSparkline do
        
       -  describe 'generated <xyz>_report method' do
       -
       -    it 'should raise an error when called with anything else than a hash' do
       -      lambda { User.registrations_report(1) }.should         raise_error(ArgumentError)
       -      lambda { User.registrations_report('invalid') }.should raise_error(ArgumentError)
       -      lambda { User.registrations_report([1, 2]) }.should    raise_error(ArgumentError)
       -    end
       -
       -    it 'should raise an error when called with multiple arguments' do
       -      lambda { User.registrations_report({ 1 => 2 }, { 3 => 4 }) }.should raise_error(ArgumentError)
       -    end
       -
       -    it 'should not raise an error when called with a hash' do
       -      lambda { User.registrations_report({ :limit => 1 }) }.should_not raise_error(ArgumentError)
       -    end
       -
       -    it 'should not raise an error when called without a parameter' do
       -      lambda { User.registrations_report }.should_not raise_error(ArgumentError)
       -    end
       -
       -  end
       -
          describe 'for inherited models' do
        
            before(:all) do
       -      User.create!(:login => 'test 1', :created_at => Time.now - 1.week,  :profile_visits => 1)
       -      User.create!(:login => 'test 2', :created_at => Time.now - 2.weeks, :profile_visits => 2)
       -      SpecialUser.create!(:login => 'test 3', :created_at => Time.now - 2.weeks, :profile_visits => 3)
       +      User.create!(:login => 'test 1', :created_at => Time.now - 1.days,  :profile_visits => 1)
       +      User.create!(:login => 'test 2', :created_at => Time.now - 2.days, :profile_visits => 2)
       +      SpecialUser.create!(:login => 'test 3', :created_at => Time.now - 2.days, :profile_visits => 3)
            end
        
            it 'should include all data when invoked on the base model class' do
              result = User.registrations_report.to_a
        
       -      result.length.should == 20
       -      result[7][1].should  == 1
       -      result[14][1].should  == 2
       +      result.length.should == 10
       +      result[1][1].should  == 1
       +      result[2][1].should  == 2
            end
        
       -    it 'should include all data when invoked on the base model class' do
       +    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 == 20
       -      result[14][1].should  == 1
       +      result.length.should == 10
       +      result[2][1].should  == 1
            end
        
            after(:all) do
       @@ -61,7 +39,7 @@ describe Kvlr::ReportsAsSparkline do
        end
        
        class User < ActiveRecord::Base
       -  report_as_sparkline :registrations, :cumulate => true, :limit => 20
       +  report_as_sparkline :registrations, :limit => 10
        end
        
        class SpecialUser < User; end
 (DIR) diff --git a/spec/other/cumulated_report_spec.rb b/spec/other/cumulated_report_spec.rb
       @@ -48,7 +48,7 @@ describe Kvlr::ReportsAsSparkline::CumulatedReport do
        
                end
        
       -        it 'should return correct data for :aggregation => :count' do
       +        it 'should return correct data for aggregation :count' do
                  @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :registrations, :aggregation => :count, :grouping => grouping, :limit => 10)
                  result = @report.run
        
       @@ -58,7 +58,7 @@ describe Kvlr::ReportsAsSparkline::CumulatedReport do
                  result[3][1].should == 2
                end
        
       -        it 'should return correct data for :aggregation => :sum' do
       +        it 'should return correct data for aggregation :sum' do
                  @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :registrations, :aggregation => :sum, :grouping => grouping, :value_column_name => :profile_visits, :limit => 10)
                  result = @report.run()
        
       @@ -68,7 +68,7 @@ describe Kvlr::ReportsAsSparkline::CumulatedReport do
                  result[3][1].should == 5
                end
        
       -        it 'should return correct data with custom conditions for :aggregation => :count' do
       +        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']])
        
       @@ -78,7 +78,7 @@ describe Kvlr::ReportsAsSparkline::CumulatedReport do
                  result[3][1].should == 1
                end
        
       -        it 'should return correct data with custom conditions for :aggregation => :sum' do
       +        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_name => :profile_visits, :limit => 10)
                  result = @report.run(:conditions => ['login IN (?)', ['test 1', 'test 2']])
        
 (DIR) diff --git a/spec/other/grouping_spec.rb b/spec/other/grouping_spec.rb
       @@ -42,20 +42,12 @@ describe Kvlr::ReportsAsSparkline::Grouping do
                ActiveRecord::Base.connection.stub!(:class).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
       +      for grouping in [:hour, :day, :week, :month] do
        
       -      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 truncation identifier \"#{grouping.to_s}\" for grouping :#{grouping.to_s}" do
       +          Kvlr::ReportsAsSparkline::Grouping.new(grouping).send(:to_sql, 'created_at').should == "date_trunc('#{grouping.to_s}', 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
       @@ -87,21 +79,22 @@ describe Kvlr::ReportsAsSparkline::Grouping do
          end
        
          describe '#date_parts_from_db_string' do
       -=begin
       -    for grouping in [[:hour, '2008/12/31/12'], [:day, '2008/12/31'], [:month, '2008/12']] do
        
       -      it "should split the string with '/' for grouping :#{grouping[0].to_s}" do
       -        db_string = grouping[1]
       +    describe 'for SQLite3' do
        
       -        Kvlr::ReportsAsSparkline::Grouping.new(grouping[0]).date_parts_from_db_string(db_string).should == db_string.split('/').map(&:to_i)
       +      before do
       +        ActiveRecord::Base.connection.stub!(:class).and_return(ActiveRecord::ConnectionAdapters::SQLite3Adapter)
              end
        
       -    end
       -=end
       -    describe 'for SQLite3' do
       +      for grouping in [[:hour, '2008/12/31/12'], [:day, '2008/12/31'], [:month, '2008/12']] do
       +
       +        it "should split the string with '/' for grouping :#{grouping[0].to_s}" do
       +          Kvlr::ReportsAsSparkline::Grouping.new(grouping[0]).date_parts_from_db_string(grouping[1]).should == grouping[1].split('/').map(&:to_i)
       +        end
       +
       +      end
        
              it 'should split the string with "/" and increment the week by 1 for grouping :week' do
       -        ActiveRecord::Base.connection.stub!(:class).and_return(ActiveRecord::ConnectionAdapters::SQLite3Adapter)
                db_string = '2008/2'
                expected = [2008, 3]
        
       @@ -116,6 +109,10 @@ describe Kvlr::ReportsAsSparkline::Grouping do
                ActiveRecord::Base.connection.stub!(:class).and_return(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
              end
        
       +      it 'should split the date part of the string with "-" and read out the hour for grouping :hour' do
       +        Kvlr::ReportsAsSparkline::Grouping.new(:hour).date_parts_from_db_string('2008-12-03 06:00:00').should == [2008, 12, 03, 6]
       +      end
       +
              it 'should split the date part of the string with "-" for grouping :day' do
                Kvlr::ReportsAsSparkline::Grouping.new(:day).date_parts_from_db_string('2008-12-03 00:00:00').should == [2008, 12, 03]
              end
       @@ -124,16 +121,24 @@ describe Kvlr::ReportsAsSparkline::Grouping do
                Kvlr::ReportsAsSparkline::Grouping.new(:week).date_parts_from_db_string('2008-12-01 00:00:00').should == [2008, 49]
              end
        
       +      it 'should split the date part of the string with "-" and return year and month for grouping :month' do
       +        Kvlr::ReportsAsSparkline::Grouping.new(:month).date_parts_from_db_string('2008-12-01 00:00:00').should == [2008, 12]
       +      end
       +
            end
        
            describe 'for MySQL' do
        
       -      it 'should split the string with "/" for grouping :week' do
       +      before do
                ActiveRecord::Base.connection.stub!(:class).and_return(ActiveRecord::ConnectionAdapters::MysqlAdapter)
       -        db_string = '2008/2'
       -        expected = [2008, 2]
       +      end
       +
       +      for grouping in [[:hour, '2008/12/31/12'], [:day, '2008/12/31'], [:week, '2008/40'], [:month, '2008/12']] do
       +
       +        it "should split the string with '/' for grouping :#{grouping[0].to_s}" do
       +          Kvlr::ReportsAsSparkline::Grouping.new(grouping[0]).date_parts_from_db_string(grouping[1]).should == grouping[1].split('/').map(&:to_i)
       +        end
        
       -        Kvlr::ReportsAsSparkline::Grouping.new(:week).date_parts_from_db_string(db_string).should == expected
              end
        
            end
 (DIR) diff --git a/spec/other/report_cache_spec.rb b/spec/other/report_cache_spec.rb
       @@ -31,7 +31,7 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
              }.should raise_error(YieldMatchException)
            end
        
       -    it 'should read existing data for the report from cache if no_cache' do
       +    it 'should read existing data for the report from cache' do
              Kvlr::ReportsAsSparkline::ReportCache.should_receive(:find).once.with(
                :all,
                :conditions => {
       @@ -47,10 +47,14 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
              Kvlr::ReportsAsSparkline::ReportCache.process(@report, 10) { [] }
            end
        
       -    it 'should update the cache' do
       -      Kvlr::ReportsAsSparkline::ReportCache.should_receive(:prepare_result)
       +    it 'should prepare the results before it returns them' do
       +      new_data = []
       +      cached_data = []
       +      Kvlr::ReportsAsSparkline::ReportCache.stub!(:find).and_return(cached_data)
       +      last_reporting_period_to_read = Kvlr::ReportsAsSparkline::ReportingPeriod.first(@report.grouping, 10)
       +      Kvlr::ReportsAsSparkline::ReportCache.should_receive(:prepare_result).once.with(new_data, cached_data, last_reporting_period_to_read, @report, false)
        
       -      Kvlr::ReportsAsSparkline::ReportCache.process(@report, 10) { [] }
       +      Kvlr::ReportsAsSparkline::ReportCache.process(@report, 10) { new_data }
            end
        
            it 'should yield the first reporting period if the cache is empty' do
       @@ -110,12 +114,12 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
            end
        
            it 'should convert the date strings from the newly read data to reporting periods' do
       -      Kvlr::ReportsAsSparkline::ReportingPeriod.should_receive(:from_db_string).once.with(@report.grouping, '2008/12').and_return(Kvlr::ReportsAsSparkline::ReportingPeriod.new(@report.grouping))
       +      Kvlr::ReportsAsSparkline::ReportingPeriod.should_receive(:from_db_string).once.with(@report.grouping, @new_data[0][0]).and_return(Kvlr::ReportsAsSparkline::ReportingPeriod.new(@report.grouping))
        
              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
       +    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,
       @@ -128,7 +132,7 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
              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 no new_data has been read' do
       +    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,
       @@ -147,12 +151,6 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
              Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [], @last_reporting_period_to_read, @report)
            end
        
       -    it 'should not save the created Kvlr::ReportsAsSparkline::ReportCache if no_cache is specified' do
       -      @cached.should_not_receive(:save!)
       -
       -      Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [], @last_reporting_period_to_read, @report, true)
       -    end
       -
            it 'should return an array of arrays of Dates and Floats' do
              result = Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [], @last_reporting_period_to_read, @report, true)
        
       @@ -175,11 +173,21 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
              Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [@cached], @last_reporting_period_to_read, @report)
            end
        
       -    it 'should not update the last cached record if new data has been read for the last reporting period to read but no_cache is specified' do
       -      Kvlr::ReportsAsSparkline::ReportingPeriod.stub!(:from_db_string).and_return(@last_reporting_period_to_read)
       -      @cached.should_not_receive(:update_attributes!)
       +    describe 'with no_cache = true' do
       +
       +      it 'should not save the created Kvlr::ReportsAsSparkline::ReportCache' do
       +        @cached.should_not_receive(:save!)
       +
       +        Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [], @last_reporting_period_to_read, @report, true)
       +      end
       +
       +      it 'should not 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_not_receive(:update_attributes!)
       +
       +        Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [@cached], @last_reporting_period_to_read, @report, true)
       +      end
        
       -      Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [@cached], @last_reporting_period_to_read, @report, true)
            end
        
          end
 (DIR) diff --git a/spec/other/report_spec.rb b/spec/other/report_spec.rb
       @@ -36,7 +36,7 @@ describe Kvlr::ReportsAsSparkline::Report do
                  User.create!(:login => 'test 3', :created_at => Time.now - 3.send(grouping), :profile_visits => 3)
                end
        
       -        it 'should return correct data for :aggregation => :count' do
       +        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
        
       @@ -46,7 +46,7 @@ describe Kvlr::ReportsAsSparkline::Report do
                  result[3][1].should == 2
                end
        
       -        it 'should return correct data for :aggregation => :sum' do
       +        it 'should return correct data for aggregation :sum' do
                  @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations, :aggregation => :sum, :grouping => grouping, :value_column_name => :profile_visits, :limit => 10)
                  result = @report.run().to_a
        
       @@ -56,7 +56,7 @@ describe Kvlr::ReportsAsSparkline::Report do
                  result[3][1].should == 5
                end
        
       -        it 'should return correct data with custom conditions for :aggregation => :count' do
       +        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
        
       @@ -66,7 +66,7 @@ describe Kvlr::ReportsAsSparkline::Report do
                  result[3][1].should == 1
                end
        
       -        it 'should return correct data with custom conditions for :aggregation => :sum' do
       +        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_name => :profile_visits, :limit => 10)
                  result = @report.run(:conditions => ['login IN (?)', ['test 1', 'test 2']]).to_a
        
       @@ -99,7 +99,7 @@ describe Kvlr::ReportsAsSparkline::Report do
              @report.send(:read_data, Time.now)
            end
        
       -    it 'should build the conditions' do
       +    it 'should setup the conditions' do
              @report.should_receive(:setup_conditions).once.and_return([])
        
              @report.send(:read_data, Time.now)
       @@ -109,19 +109,19 @@ describe Kvlr::ReportsAsSparkline::Report do
        
          describe '.setup_conditions' do
        
       -    it 'should return conditions for date_column_name >= begin_at only if no custom conditions are specified' do
       +    it 'should return conditions for date_column_name >= begin_at only when no custom conditions are specified' do
              begin_at = Time.now
        
              @report.send(:setup_conditions, begin_at).should == ['created_at >= ?', begin_at]
            end
        
       -    it 'should return conditions for date_column_name >= begin_at only if an empty Hash of custom conditions is specified' do
       +    it 'should return conditions for date_column_name >= begin_at only when an empty Hash of custom conditions is specified' do
              begin_at = Time.now
        
              @report.send(:setup_conditions, begin_at, {}).should == ['created_at >= ?', begin_at]
            end
        
       -    it 'should return conditions for date_column_name >= begin_at only if an empty Array of custom conditions is specified' do
       +    it 'should return conditions for date_column_name >= begin_at only when an empty Array of custom conditions is specified' do
              begin_at = Time.now
        
              @report.send(:setup_conditions, begin_at, []).should == ['created_at >= ?', begin_at]
       @@ -176,8 +176,8 @@ describe Kvlr::ReportsAsSparkline::Report do
                    :limit             => 100,
                    :aggregation       => :count,
                    :grouping          => :day,
       -            :date_column_name  => 'created_at',
       -            :value_column_name => 'id',
       +            :date_column_name  => :created_at,
       +            :value_column_name => :id,
                    :conditions        => []
                  })
                }.should_not raise_error(ArgumentError)
       @@ -195,7 +195,7 @@ describe Kvlr::ReportsAsSparkline::Report do
                lambda { @report.send(:ensure_valid_options, { :grouping => :decade }) }.should raise_error(ArgumentError)
              end
        
       -      it 'should raise an error if aggregation :sum is spesicied without the name of the column holding the value to sum' do
       +      it 'should raise an error if aggregation :sum is spesicied but no :value_column_name' do
                lambda { @report.send(:ensure_valid_options, { :aggregation => :sum }) }.should raise_error(ArgumentError)
              end
        
 (DIR) diff --git a/spec/other/reporting_period_spec.rb b/spec/other/reporting_period_spec.rb
       @@ -4,69 +4,57 @@ describe Kvlr::ReportsAsSparkline::ReportingPeriod do
        
          describe '.date_time' do
        
       -    describe 'for grouping :hour' do
       -
       -      it 'should return the date and time with minutes = seconds = 0 for grouping :hour' do
       -        date_time = DateTime.now
       -        reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:hour), date_time)
       -
       -        reporting_period.date_time.should == DateTime.new(date_time.year, date_time.month, date_time.day, date_time.hour, 0, 0)
       -      end
       +    it 'should return the date and time with minutes = seconds = 0 for grouping :hour' do
       +      date_time = DateTime.now
       +      reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:hour), date_time)
        
       +      reporting_period.date_time.should == DateTime.new(date_time.year, date_time.month, date_time.day, date_time.hour, 0, 0)
            end
        
       -    describe 'for grouping :day' do
       -
       -      it 'should return the date part only for grouping :day' do
       -        date_time = DateTime.now
       -        reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:day), date_time)
       -
       -        reporting_period.date_time.should == date_time.to_date
       -      end
       +    it 'should return the date part only for grouping :day' do
       +      date_time = DateTime.now
       +      reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:day), date_time)
        
       +      reporting_period.date_time.should == date_time.to_date
            end
        
            describe 'for grouping :week' do
        
       -      it 'should return the date of the monday of the week date_time is in for any day in that the week' do
       +      it 'should return the date of the monday of the week date_time is in for any day in that week' do
                date_time = DateTime.new(2008, 11, 27)
                reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:week), date_time)
        
       -        reporting_period.date_time.should == DateTime.new(date_time.year, date_time.month, 24)
       +        reporting_period.date_time.should == Date.new(date_time.year, date_time.month, 24)
              end
        
              it 'should return the date of the monday of the week date_time is in when the specified date is a monday already' do
                date_time = DateTime.new(2008, 11, 24) #this is a monday already, should not change
                reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:week), date_time)
        
       -        reporting_period.date_time.should == DateTime.new(date_time.year, date_time.month, 24) # expect to get monday 24th again
       +        reporting_period.date_time.should == Date.new(date_time.year, date_time.month, 24) # expect to get monday 24th again
              end
        
              it 'should return the date of the monday of the week date_time is in when the monday is in a different month than the specified date' do
                date_time = DateTime.new(2008, 11, 1) #this is a saturday
                reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:week), date_time)
        
       -        reporting_period.date_time.should == DateTime.new(date_time.year, 10, 27) # expect to get the monday before the 1st, which is in october
       +        reporting_period.date_time.should == Date.new(2008, 10, 27) # expect to get the monday before the 1st, which is in october
              end
        
              it 'should return the date of the monday of the week date_time is in when the monday is in a different year than the specified date' do
                date_time = DateTime.new(2009, 1, 1) #this is a thursday
                reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:week), date_time)
        
       -        reporting_period.date_time.should == DateTime.new(2008, 12, 29) # expect to get the monday before the 1st, which is in december 2008
       +        reporting_period.date_time.should == Date.new(2008, 12, 29) # expect to get the monday before the 1st, which is in december 2008
              end
        
            end
        
       -    describe 'for grouping :month' do
       -
       -      it 'should return the date with day = 1 for grouping :month' do
       -        date_time = Time.now
       -        reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:month), date_time)
       -
       -        reporting_period.date_time.should == Date.new(date_time.year, date_time.month, 1)
       -      end
       +    it 'should return the date with day = 1 for grouping :month' do
       +      date_time = Time.now
       +      reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:month), date_time)
        
       +      reporting_period.date_time.should == Date.new(date_time.year, date_time.month, 1)
            end
        
          end
       @@ -105,52 +93,36 @@ describe Kvlr::ReportsAsSparkline::ReportingPeriod do
        
          describe '.previous' do
        
       -    describe 'for grouping :hour' do
       -
       -      it 'should return a reporting period with date and time one hour before the current period' do
       -        now = Time.now
       -        reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:hour), now)
       -        expected = now - 1.hour
       -
       -        reporting_period.previous.date_time.should == DateTime.new(expected.year, expected.month, expected.day, expected.hour)
       -      end
       +    it 'should return a reporting period with date and time one hour before 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
        
       +      reporting_period.previous.date_time.should == DateTime.new(expected.year, expected.month, expected.day, expected.hour)
            end
        
       -    describe 'for grouping :day' do
       -
       -      it 'should return a reporting period with date one day before the current period' do
       -        now = Time.now
       -        reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:day), now)
       -        expected = now - 1.day
       -
       -        reporting_period.previous.date_time.should == Date.new(expected.year, expected.month, expected.day)
       -      end
       +    it 'should return a reporting period with date one day before 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
        
       +      reporting_period.previous.date_time.should == Date.new(expected.year, expected.month, expected.day)
            end
        
       -    describe 'for grouping :week' do
       -
       -      it 'should return a reporting period with date one week before the current period' do
       -        now = DateTime.now
       -        reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:week), now)
       -        expected = reporting_period.date_time - 1.week
       -
       -        reporting_period.previous.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
       +      now = DateTime.now
       +      reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:week), now)
       +      expected = reporting_period.date_time - 1.week
        
       +      reporting_period.previous.date_time.should == Date.new(expected.year, expected.month, expected.day)
            end
        
       -    describe 'for grouping :month' do
       -
       -      it 'should return a reporting period with date one month before 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
       -
       -        reporting_period.previous.date_time.should == Date.new(expected.year, expected.month, 1)
       -      end
       +    it 'should return a reporting period with date of the first day in the month one month before 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
        
       +      reporting_period.previous.date_time.should == Date.new(expected.year, expected.month, 1)
            end
        
          end
       @@ -173,6 +145,20 @@ describe Kvlr::ReportsAsSparkline::ReportingPeriod do
              (reporting_period1 == reporting_period2).should == false
            end
        
       +    it 'should return true for 2 reporting periods with the same grouping but different date_times if the date times evaluate to the same reporting period identifier' do
       +      reporting_period1 = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:month), Time.now)
       +      reporting_period2 = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:month), Time.now + 1.day)
       +
       +      (reporting_period1 == reporting_period2).should == true
       +    end
       +
       +    it 'should return false for 2 reporting periods with the same grouping but different date_times if the date times evaluate to different reporting period identifiers' do
       +      reporting_period1 = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:month), Time.now)
       +      reporting_period2 = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:month), Time.now + 2.months)
       +
       +      (reporting_period1 == reporting_period2).should == false
       +    end
       +
          end
        
          describe '#first' do
       @@ -182,7 +168,7 @@ describe Kvlr::ReportsAsSparkline::ReportingPeriod do
              DateTime.stub!(:now).and_return(@now)
            end
        
       -    it 'should return a reporting period with the date part of (DateTime.now - limit.hours) for grouping :hour' do
       +    it 'should return a reporting period with the date part of (DateTime.now - limit.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
        
       @@ -196,7 +182,14 @@ describe Kvlr::ReportsAsSparkline::ReportingPeriod do
              reporting_period.date_time.should == Date.new(expected.year, expected.month, expected.day)
            end
        
       -    it 'should return a reporting period with the date of monday of the week at (DateTime.now - limit.weeks) for grouping :week' do
       +    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
       +      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)
       +    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
              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)