cleaned up specs - reportable - Fork of reportable required by WarVox, from hdm/reportable.
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) commit 302078c74c8857b4e0aab259fc7d2c79489d7d73
(DIR) parent 1a5f067f3b860469357f8a05db8efcbc2ca3ca0a
(HTM) Author: Marco Otte-Witte <marco.otte-witte@simplabs.com>
Date: Tue, 13 Jan 2009 23:34:05 +0800
cleaned up specs
Signed-off-by: Marco Otte-Witte <marco.otte-witte@simplabs.com>
Diffstat:
A spec/classes/cumulated_report_spec… | 117 +++++++++++++++++++++++++++++++
A spec/classes/date_time_spec.rb | 16 ++++++++++++++++
A spec/classes/grouping_spec.rb | 152 +++++++++++++++++++++++++++++++
R spec/other/report_cache_spec.rb ->… | 0
A spec/classes/report_spec.rb | 237 +++++++++++++++++++++++++++++++
A spec/classes/reporting_period_spec… | 201 ++++++++++++++++++++++++++++++
D spec/models/report_method_spec.rb | 45 -------------------------------
D spec/other/cumulated_report_spec.rb | 117 -------------------------------
D spec/other/date_time_spec.rb | 16 ----------------
D spec/other/grouping_spec.rb | 152 -------------------------------
A spec/other/report_method_spec.rb | 45 +++++++++++++++++++++++++++++++
D spec/other/report_spec.rb | 237 -------------------------------
D spec/other/reporting_period_spec.rb | 201 ------------------------------
13 files changed, 768 insertions(+), 768 deletions(-)
---
(DIR) diff --git a/spec/classes/cumulated_report_spec.rb b/spec/classes/cumulated_report_spec.rb
@@ -0,0 +1,117 @@
+require File.join(File.dirname(__FILE__), '..', 'spec_helper')
+
+describe Kvlr::ReportsAsSparkline::CumulatedReport do
+
+ before do
+ @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :cumulated_registrations)
+ end
+
+ describe '#run' do
+
+ it 'should cumulate the data' do
+ @report.should_receive(:cumulate).once
+
+ @report.run
+ end
+
+ it 'should return an array of the same length as the specified limit' do
+ @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :cumulated_registrations, :limit => 10)
+
+ @report.run.length.should == 10
+ end
+
+ for grouping in [:hour, :day, :week, :month] do
+
+ describe "for grouping #{grouping.to_s}" do
+
+ before(:all) do
+ 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)
+ end
+
+ describe do
+
+ before do
+ @grouping = Kvlr::ReportsAsSparkline::Grouping.new(grouping)
+ @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :cumulated_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::CumulatedReport.new(User, :registrations, :aggregation => :count, :grouping => grouping, :limit => 10)
+ result = @report.run
+
+ 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[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[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[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
+ User.destroy_all
+ end
+
+ end
+
+ after(:each) do
+ Kvlr::ReportsAsSparkline::ReportCache.destroy_all
+ end
+
+ end
+
+ end
+
+ describe '#cumulate' do
+
+ it 'should correctly cumulate the given data' do
+ first = (Time.now - 1.week).to_s
+ second = Time.now.to_s
+ data = [[first, 1], [second, 2]]
+
+ @report.send(:cumulate, data).should == [[first, 1.0], [second, 3.0]]
+ end
+
+ end
+
+end
(DIR) diff --git a/spec/classes/date_time_spec.rb b/spec/classes/date_time_spec.rb
@@ -0,0 +1,16 @@
+require File.join(File.dirname(__FILE__), '..', 'spec_helper')
+
+describe DateTime do
+
+ describe '#to_reporting_period' do
+
+ it 'should return a reporting period for the specified grouping and instance of DateTime' do
+ date_time = DateTime.now
+ grouping = Kvlr::ReportsAsSparkline::Grouping.new(:hour)
+
+ date_time.to_reporting_period(grouping).should == Kvlr::ReportsAsSparkline::ReportingPeriod.new(grouping, date_time)
+ end
+
+ end
+
+end
(DIR) diff --git a/spec/classes/grouping_spec.rb b/spec/classes/grouping_spec.rb
@@ -0,0 +1,152 @@
+require File.join(File.dirname(__FILE__), '..', 'spec_helper')
+
+describe Kvlr::ReportsAsSparkline::Grouping do
+
+ describe '#new' do
+
+ it 'should raise an error if an unsupported grouping is specified' do
+ lambda { Kvlr::ReportsAsSparkline::Grouping.new(:unsupported) }.should raise_error(ArgumentError)
+ end
+
+ end
+
+ describe '#to_sql' do
+
+ describe 'for MySQL' do
+
+ before do
+ ActiveRecord::Base.connection.stub!(:class).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.stub!(:class).and_return(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
+ end
+
+ for grouping in [:hour, :day, :week, :month] do
+
+ 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
+
+ end
+
+ end
+
+ describe 'for SQLite3' do
+
+ before do
+ ActiveRecord::Base.connection.stub!(:class).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
+
+ describe '#date_parts_from_db_string' do
+
+ describe 'for SQLite3' do
+
+ before do
+ ActiveRecord::Base.connection.stub!(:class).and_return(ActiveRecord::ConnectionAdapters::SQLite3Adapter)
+ end
+
+ 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
+ db_string = '2008/2'
+ expected = [2008, 3]
+
+ Kvlr::ReportsAsSparkline::Grouping.new(:week).date_parts_from_db_string(db_string).should == expected
+ end
+
+ end
+
+ describe 'for PostgreSQL' do
+
+ before 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
+
+ it 'should split the date part of the string with "-" for grouping :week' 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
+
+ before do
+ ActiveRecord::Base.connection.stub!(:class).and_return(ActiveRecord::ConnectionAdapters::MysqlAdapter)
+ 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
+
+ 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/classes/report_cache_spec.rb
(DIR) diff --git a/spec/classes/report_spec.rb b/spec/classes/report_spec.rb
@@ -0,0 +1,237 @@
+require File.join(File.dirname(__FILE__), '..', 'spec_helper')
+
+describe Kvlr::ReportsAsSparkline::Report do
+
+ before do
+ @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations)
+ end
+
+ describe '#run' do
+
+ it 'should process the data with the report cache' do
+ Kvlr::ReportsAsSparkline::ReportCache.should_receive(:process).once.with(@report, 100, false)
+
+ @report.run
+ end
+
+ it 'should process the data with the report cache and specify no_cache when custom conditions are given' do
+ Kvlr::ReportsAsSparkline::ReportCache.should_receive(:process).once.with(@report, 100, true)
+
+ @report.run(:conditions => { :some => :condition })
+ end
+
+ it 'should validate the specified options for the :run context' do
+ @report.should_receive(:ensure_valid_options).once.with({ :limit => 3 }, :run)
+
+ result = @report.run(:limit => 3)
+ end
+
+ for grouping in [:hour, :day, :week, :month] do
+
+ describe "for grouping #{grouping.to_s}" do
+
+ before(:all) do
+ 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)
+ 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[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[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[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[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
+ User.destroy_all
+ end
+
+ after(:each) do
+ Kvlr::ReportsAsSparkline::ReportCache.destroy_all
+ end
+
+ end
+
+ end
+
+ end
+
+ describe '#read_data' do
+
+ it 'should invoke the aggregation method on the model' do
+ @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations, :aggregation => :count)
+ User.should_receive(:count).once.and_return([])
+
+ @report.send(:read_data, Time.now)
+ end
+
+ it 'should setup the conditions' do
+ @report.should_receive(:setup_conditions).once.and_return([])
+
+ @report.send(:read_data, Time.now)
+ end
+
+ end
+
+ describe '#setup_conditions' do
+
+ it 'should return conditions for date_column >= 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 >= 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 >= 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]
+ end
+
+ it 'should correctly include custom conditions if they are specified as a Hash' do
+ begin_at = Time.now
+ custom_conditions = { :first_name => 'first name', :last_name => 'last name' }
+
+ conditions = @report.send(:setup_conditions, begin_at, custom_conditions)
+ #cannot check for equality of complete conditions array since hashes are not ordered (thus it is unknown whether first_name or last_name comes first)
+ conditions[0].should include('first_name = ?')
+ conditions[0].should include('last_name = ?')
+ conditions[0].should include('created_at >= ?')
+ conditions.should include('first name')
+ conditions.should include('last name')
+ conditions.should include(begin_at)
+ end
+
+ it 'should correctly include custom conditions if they are specified as an Array' do
+ begin_at = Time.now
+ custom_conditions = ['first_name = ? AND last_name = ?', 'first name', 'last name']
+
+ @report.send(:setup_conditions, begin_at, custom_conditions).should == [
+ 'first_name = ? AND last_name = ? AND created_at >= ?',
+ 'first name',
+ 'last name',
+ begin_at
+ ]
+ end
+
+ end
+
+ describe '#ensure_valid_options' do
+
+ it 'should raise an error if malformed conditions are specified' do
+ lambda { @report.send(:ensure_valid_options, { :conditions => 1 }) }.should raise_error(ArgumentError)
+ end
+
+ it 'should not raise an error if conditions are specified as an Array' do
+ lambda { @report.send(:ensure_valid_options, { :conditions => ['first_name = ?', 'first name'] }) }.should_not raise_error(ArgumentError)
+ end
+
+ it 'should not raise an error if conditions are specified as a Hash' do
+ lambda { @report.send(:ensure_valid_options, { :conditions => { :first_name => 'first name' } }) }.should_not raise_error(ArgumentError)
+ end
+
+ describe 'for context :initialize' 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,
+ :date_column => :created_at,
+ :value_column => :id,
+ :conditions => []
+ })
+ }.should_not raise_error(ArgumentError)
+ end
+
+ it 'should raise an error if an unsupported option is specified' do
+ lambda { @report.send(:ensure_valid_options, { :invalid => :option }) }.should raise_error(ArgumentError)
+ end
+
+ it 'should raise an error if an invalid aggregation is specified' do
+ lambda { @report.send(:ensure_valid_options, { :aggregation => :invalid }) }.should raise_error(ArgumentError)
+ end
+
+ it 'should raise an error if an invalid grouping is specified' do
+ lambda { @report.send(:ensure_valid_options, { :grouping => :decade }) }.should raise_error(ArgumentError)
+ end
+
+ it 'should raise an error if aggregation :sum is spesicied but no :value_column' do
+ lambda { @report.send(:ensure_valid_options, { :aggregation => :sum }) }.should raise_error(ArgumentError)
+ end
+
+ end
+
+ describe 'for context :run' do
+
+ it 'should not raise an error if valid options are specified' do
+ lambda { @report.send(:ensure_valid_options, { :limit => 100, :conditions => [] }, :run)
+ }.should_not raise_error(ArgumentError)
+ end
+
+ it 'should raise an error if an unsupported option is specified' do
+ lambda { @report.send(:ensure_valid_options, { :aggregation => :sum }, :run) }.should raise_error(ArgumentError)
+ end
+
+ end
+
+ end
+
+end
(DIR) diff --git a/spec/classes/reporting_period_spec.rb b/spec/classes/reporting_period_spec.rb
@@ -0,0 +1,201 @@
+require File.join(File.dirname(__FILE__), '..', 'spec_helper')
+
+describe Kvlr::ReportsAsSparkline::ReportingPeriod do
+
+ describe '#date_time' 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 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 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 == 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 == 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 == 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 == Date.new(2008, 12, 29) # expect to get the monday before the 1st, which is in december 2008
+ end
+
+ 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
+
+ describe '.from_db_string' do
+
+ it 'should return a reporting period with the correct date and time and with minutes = seconds = 0 for grouping :hour' do
+ grouping = Kvlr::ReportsAsSparkline::Grouping.new(:hour)
+ grouping.stub!(:date_parts_from_db_string).and_return([2008, 1, 1, 12])
+
+ Kvlr::ReportsAsSparkline::ReportingPeriod.from_db_string(grouping, '').date_time.should == DateTime.new(2008, 1, 1, 12, 0, 0)
+ end
+
+ it 'should return a reporting period with the date part only for grouping :day' do
+ grouping = Kvlr::ReportsAsSparkline::Grouping.new(:day)
+ grouping.stub!(:date_parts_from_db_string).and_return([2008, 1, 1])
+
+ Kvlr::ReportsAsSparkline::ReportingPeriod.from_db_string(grouping, '').date_time.should == Date.new(2008, 1, 1)
+ end
+
+ it 'should return a reporting period with the date part of the monday of the week the date is in for grouping :week' do
+ grouping = Kvlr::ReportsAsSparkline::Grouping.new(:week)
+ grouping.stub!(:date_parts_from_db_string).and_return([2008, 1])
+
+ Kvlr::ReportsAsSparkline::ReportingPeriod.from_db_string(grouping, '').date_time.should == Date.new(2007, 12, 31)
+ end
+
+ it 'should return a reporting period with the correct date and with day = 1 for grouping :month' do
+ grouping = Kvlr::ReportsAsSparkline::Grouping.new(:month)
+ grouping.stub!(:date_parts_from_db_string).and_return([2008, 1])
+
+ Kvlr::ReportsAsSparkline::ReportingPeriod.from_db_string(grouping, '').date_time.should == Date.new(2008, 1, 1)
+ end
+
+ end
+
+ describe '#next' 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
+
+ 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 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
+
+ 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 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
+
+ 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 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
+
+ reporting_period.next.date_time.should == Date.new(expected.year, expected.month, 1)
+ end
+
+ end
+
+ describe '#==' do
+
+ it 'should return true for 2 reporting periods with the same date_time and grouping' do
+ now = DateTime.now
+ reporting_period1 = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:month), now)
+ reporting_period2 = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:month), now)
+
+ (reporting_period1 == reporting_period2).should == true
+ end
+
+ it 'should return false for 2 reporting periods with the same date_time but different groupings' do
+ now = Time.now
+ reporting_period1 = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:month), now)
+ reporting_period2 = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:day), now)
+
+ (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
+
+ 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 - 1).hours) with minutes = seconds = 0 for grouping :hour' do
+ reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.first(Kvlr::ReportsAsSparkline::Grouping.new(:hour), 3)
+ 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 - 1).days) for grouping :day' do
+ reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.first(Kvlr::ReportsAsSparkline::Grouping.new(:day), 3)
+ 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 - 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, 10, 1)
+ end
+
+ 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, 15) #the monday 2 weeks earlier
+ end
+
+ end
+
+end
(DIR) diff --git a/spec/models/report_method_spec.rb b/spec/models/report_method_spec.rb
@@ -1,45 +0,0 @@
-require File.join(File.dirname(__FILE__), '..', 'spec_helper')
-
-describe Kvlr::ReportsAsSparkline do
-
- describe 'for inherited models' do
-
- before(:all) do
- 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
-puts result.inspect
- result.length.should == 10
- 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[7][1].should == 1.0
- end
-
- after(:all) do
- User.destroy_all
- SpecialUser.destroy_all
- end
-
- end
-
- after do
- Kvlr::ReportsAsSparkline::ReportCache.destroy_all
- end
-
-end
-
-class User < ActiveRecord::Base
- reports_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
@@ -1,117 +0,0 @@
-require File.join(File.dirname(__FILE__), '..', 'spec_helper')
-
-describe Kvlr::ReportsAsSparkline::CumulatedReport do
-
- before do
- @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :cumulated_registrations)
- end
-
- describe '.run' do
-
- it 'should cumulate the data' do
- @report.should_receive(:cumulate).once
-
- @report.run
- end
-
- it 'should return an array of the same length as the specified limit' do
- @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :cumulated_registrations, :limit => 10)
-
- @report.run.length.should == 10
- end
-
- for grouping in [:hour, :day, :week, :month] do
-
- describe "for grouping #{grouping.to_s}" do
-
- before(:all) do
- 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)
- end
-
- describe do
-
- before do
- @grouping = Kvlr::ReportsAsSparkline::Grouping.new(grouping)
- @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :cumulated_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::CumulatedReport.new(User, :registrations, :aggregation => :count, :grouping => grouping, :limit => 10)
- result = @report.run
-
- 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[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[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[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
- User.destroy_all
- end
-
- end
-
- after(:each) do
- Kvlr::ReportsAsSparkline::ReportCache.destroy_all
- end
-
- end
-
- end
-
- describe '.cumulate' do
-
- it 'should correctly cumulate the given data' do
- first = (Time.now - 1.week).to_s
- second = Time.now.to_s
- data = [[first, 1], [second, 2]]
-
- @report.send(:cumulate, data).should == [[first, 1.0], [second, 3.0]]
- end
-
- end
-
-end
(DIR) diff --git a/spec/other/date_time_spec.rb b/spec/other/date_time_spec.rb
@@ -1,16 +0,0 @@
-require File.join(File.dirname(__FILE__), '..', 'spec_helper')
-
-describe DateTime do
-
- describe '.to_reporting_period' do
-
- it 'should return a reporting period for the specified grouping and instance of DateTime' do
- date_time = DateTime.now
- grouping = Kvlr::ReportsAsSparkline::Grouping.new(:hour)
-
- date_time.to_reporting_period(grouping).should == Kvlr::ReportsAsSparkline::ReportingPeriod.new(grouping, date_time)
- end
-
- end
-
-end
(DIR) diff --git a/spec/other/grouping_spec.rb b/spec/other/grouping_spec.rb
@@ -1,152 +0,0 @@
-require File.join(File.dirname(__FILE__), '..', 'spec_helper')
-
-describe Kvlr::ReportsAsSparkline::Grouping do
-
- describe '#new' do
-
- it 'should raise an error if an unsupported grouping is specified' do
- lambda { Kvlr::ReportsAsSparkline::Grouping.new(:unsupported) }.should raise_error(ArgumentError)
- end
-
- end
-
- describe '.to_sql' do
-
- describe 'for MySQL' do
-
- before do
- ActiveRecord::Base.connection.stub!(:class).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.stub!(:class).and_return(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
- end
-
- for grouping in [:hour, :day, :week, :month] do
-
- 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
-
- end
-
- end
-
- describe 'for SQLite3' do
-
- before do
- ActiveRecord::Base.connection.stub!(:class).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
-
- describe '#date_parts_from_db_string' do
-
- describe 'for SQLite3' do
-
- before do
- ActiveRecord::Base.connection.stub!(:class).and_return(ActiveRecord::ConnectionAdapters::SQLite3Adapter)
- end
-
- 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
- db_string = '2008/2'
- expected = [2008, 3]
-
- Kvlr::ReportsAsSparkline::Grouping.new(:week).date_parts_from_db_string(db_string).should == expected
- end
-
- end
-
- describe 'for PostgreSQL' do
-
- before 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
-
- it 'should split the date part of the string with "-" for grouping :week' 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
-
- before do
- ActiveRecord::Base.connection.stub!(:class).and_return(ActiveRecord::ConnectionAdapters::MysqlAdapter)
- 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
-
- 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_method_spec.rb b/spec/other/report_method_spec.rb
@@ -0,0 +1,45 @@
+require File.join(File.dirname(__FILE__), '..', 'spec_helper')
+
+describe Kvlr::ReportsAsSparkline do
+
+ describe 'for inherited models' do
+
+ before(:all) do
+ 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 == 10
+ 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[7][1].should == 1.0
+ end
+
+ after(:all) do
+ User.destroy_all
+ SpecialUser.destroy_all
+ end
+
+ end
+
+ after do
+ Kvlr::ReportsAsSparkline::ReportCache.destroy_all
+ end
+
+end
+
+class User < ActiveRecord::Base
+ reports_as_sparkline :registrations, :limit => 10
+end
+
+class SpecialUser < User; end
(DIR) diff --git a/spec/other/report_spec.rb b/spec/other/report_spec.rb
@@ -1,237 +0,0 @@
-require File.join(File.dirname(__FILE__), '..', 'spec_helper')
-
-describe Kvlr::ReportsAsSparkline::Report do
-
- before do
- @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations)
- end
-
- describe '.run' do
-
- it 'should process the data with the report cache' do
- Kvlr::ReportsAsSparkline::ReportCache.should_receive(:process).once.with(@report, 100, false)
-
- @report.run
- end
-
- it 'should process the data with the report cache and specify no_cache when custom conditions are given' do
- Kvlr::ReportsAsSparkline::ReportCache.should_receive(:process).once.with(@report, 100, true)
-
- @report.run(:conditions => { :some => :condition })
- end
-
- it 'should validate the specified options for the :run context' do
- @report.should_receive(:ensure_valid_options).once.with({ :limit => 3 }, :run)
-
- result = @report.run(:limit => 3)
- end
-
- for grouping in [:hour, :day, :week, :month] do
-
- describe "for grouping #{grouping.to_s}" do
-
- before(:all) do
- 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)
- 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[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[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[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[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
- User.destroy_all
- end
-
- after(:each) do
- Kvlr::ReportsAsSparkline::ReportCache.destroy_all
- end
-
- end
-
- end
-
- end
-
- describe '.read_data' do
-
- it 'should invoke the aggregation method on the model' do
- @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations, :aggregation => :count)
- User.should_receive(:count).once.and_return([])
-
- @report.send(:read_data, Time.now)
- end
-
- it 'should setup the conditions' do
- @report.should_receive(:setup_conditions).once.and_return([])
-
- @report.send(:read_data, Time.now)
- end
-
- end
-
- describe '.setup_conditions' do
-
- it 'should return conditions for date_column >= 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 >= 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 >= 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]
- end
-
- it 'should correctly include custom conditions if they are specified as a Hash' do
- begin_at = Time.now
- custom_conditions = { :first_name => 'first name', :last_name => 'last name' }
-
- conditions = @report.send(:setup_conditions, begin_at, custom_conditions)
- #cannot check for equality of complete conditions array since hashes are not ordered (thus it is unknown whether first_name or last_name comes first)
- conditions[0].should include('first_name = ?')
- conditions[0].should include('last_name = ?')
- conditions[0].should include('created_at >= ?')
- conditions.should include('first name')
- conditions.should include('last name')
- conditions.should include(begin_at)
- end
-
- it 'should correctly include custom conditions if they are specified as an Array' do
- begin_at = Time.now
- custom_conditions = ['first_name = ? AND last_name = ?', 'first name', 'last name']
-
- @report.send(:setup_conditions, begin_at, custom_conditions).should == [
- 'first_name = ? AND last_name = ? AND created_at >= ?',
- 'first name',
- 'last name',
- begin_at
- ]
- end
-
- end
-
- describe '.ensure_valid_options' do
-
- it 'should raise an error if malformed conditions are specified' do
- lambda { @report.send(:ensure_valid_options, { :conditions => 1 }) }.should raise_error(ArgumentError)
- end
-
- it 'should not raise an error if conditions are specified as an Array' do
- lambda { @report.send(:ensure_valid_options, { :conditions => ['first_name = ?', 'first name'] }) }.should_not raise_error(ArgumentError)
- end
-
- it 'should not raise an error if conditions are specified as a Hash' do
- lambda { @report.send(:ensure_valid_options, { :conditions => { :first_name => 'first name' } }) }.should_not raise_error(ArgumentError)
- end
-
- describe 'for context :initialize' 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,
- :date_column => :created_at,
- :value_column => :id,
- :conditions => []
- })
- }.should_not raise_error(ArgumentError)
- end
-
- it 'should raise an error if an unsupported option is specified' do
- lambda { @report.send(:ensure_valid_options, { :invalid => :option }) }.should raise_error(ArgumentError)
- end
-
- it 'should raise an error if an invalid aggregation is specified' do
- lambda { @report.send(:ensure_valid_options, { :aggregation => :invalid }) }.should raise_error(ArgumentError)
- end
-
- it 'should raise an error if an invalid grouping is specified' do
- lambda { @report.send(:ensure_valid_options, { :grouping => :decade }) }.should raise_error(ArgumentError)
- end
-
- it 'should raise an error if aggregation :sum is spesicied but no :value_column' do
- lambda { @report.send(:ensure_valid_options, { :aggregation => :sum }) }.should raise_error(ArgumentError)
- end
-
- end
-
- describe 'for context :run' do
-
- it 'should not raise an error if valid options are specified' do
- lambda { @report.send(:ensure_valid_options, { :limit => 100, :conditions => [] }, :run)
- }.should_not raise_error(ArgumentError)
- end
-
- it 'should raise an error if an unsupported option is specified' do
- lambda { @report.send(:ensure_valid_options, { :aggregation => :sum }, :run) }.should raise_error(ArgumentError)
- end
-
- end
-
- end
-
-end
(DIR) diff --git a/spec/other/reporting_period_spec.rb b/spec/other/reporting_period_spec.rb
@@ -1,201 +0,0 @@
-require File.join(File.dirname(__FILE__), '..', 'spec_helper')
-
-describe Kvlr::ReportsAsSparkline::ReportingPeriod do
-
- describe '.date_time' 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 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 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 == 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 == 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 == 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 == Date.new(2008, 12, 29) # expect to get the monday before the 1st, which is in december 2008
- end
-
- 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
-
- describe '#from_db_string' do
-
- it 'should return a reporting period with the correct date and time and with minutes = seconds = 0 for grouping :hour' do
- grouping = Kvlr::ReportsAsSparkline::Grouping.new(:hour)
- grouping.stub!(:date_parts_from_db_string).and_return([2008, 1, 1, 12])
-
- Kvlr::ReportsAsSparkline::ReportingPeriod.from_db_string(grouping, '').date_time.should == DateTime.new(2008, 1, 1, 12, 0, 0)
- end
-
- it 'should return a reporting period with the date part only for grouping :day' do
- grouping = Kvlr::ReportsAsSparkline::Grouping.new(:day)
- grouping.stub!(:date_parts_from_db_string).and_return([2008, 1, 1])
-
- Kvlr::ReportsAsSparkline::ReportingPeriod.from_db_string(grouping, '').date_time.should == Date.new(2008, 1, 1)
- end
-
- it 'should return a reporting period with the date part of the monday of the week the date is in for grouping :week' do
- grouping = Kvlr::ReportsAsSparkline::Grouping.new(:week)
- grouping.stub!(:date_parts_from_db_string).and_return([2008, 1])
-
- Kvlr::ReportsAsSparkline::ReportingPeriod.from_db_string(grouping, '').date_time.should == Date.new(2007, 12, 31)
- end
-
- it 'should return a reporting period with the correct date and with day = 1 for grouping :month' do
- grouping = Kvlr::ReportsAsSparkline::Grouping.new(:month)
- grouping.stub!(:date_parts_from_db_string).and_return([2008, 1])
-
- Kvlr::ReportsAsSparkline::ReportingPeriod.from_db_string(grouping, '').date_time.should == Date.new(2008, 1, 1)
- end
-
- end
-
- describe '.next' 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
-
- 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 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
-
- 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 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
-
- 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 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
-
- reporting_period.next.date_time.should == Date.new(expected.year, expected.month, 1)
- end
-
- end
-
- describe '.==' do
-
- it 'should return true for 2 reporting periods with the same date_time and grouping' do
- now = DateTime.now
- reporting_period1 = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:month), now)
- reporting_period2 = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:month), now)
-
- (reporting_period1 == reporting_period2).should == true
- end
-
- it 'should return false for 2 reporting periods with the same date_time but different groupings' do
- now = Time.now
- reporting_period1 = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:month), now)
- reporting_period2 = Kvlr::ReportsAsSparkline::ReportingPeriod.new(Kvlr::ReportsAsSparkline::Grouping.new(:day), now)
-
- (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
-
- 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 - 1).hours) with minutes = seconds = 0 for grouping :hour' do
- reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.first(Kvlr::ReportsAsSparkline::Grouping.new(:hour), 3)
- 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 - 1).days) for grouping :day' do
- reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.first(Kvlr::ReportsAsSparkline::Grouping.new(:day), 3)
- 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 - 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, 10, 1)
- end
-
- 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, 15) #the monday 2 weeks earlier
- end
-
- end
-
-end