cleanup, more specs - reportable - Fork of reportable required by WarVox, from hdm/reportable.
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
---
(DIR) commit a7042b3435edfcf4e0cc128e73da54b56b01125f
(DIR) parent 2d19c69d72d105e1ceceef35ba963c309b477fb2
(HTM) Author: marcoow <marco.otte-witte@simplabs.com>
Date: Sat, 6 Dec 2008 00:54:46 +0800
cleanup, more specs
Signed-off-by: Marco Otte-Witte <marco.otte-witte@simplabs.com>
Diffstat:
M lib/kvlr/reports_as_sparkline/cumu… | 4 ++--
M lib/kvlr/reports_as_sparkline/repo… | 28 +++++++++++++++-------------
M spec/other/cumulated_report_spec.rb | 66 +++++++++++++++++++------------
M spec/other/report_spec.rb | 90 +++++++++++++++++---------------
4 files changed, 105 insertions(+), 83 deletions(-)
---
(DIR) diff --git a/lib/kvlr/reports_as_sparkline/cumulated_report.rb b/lib/kvlr/reports_as_sparkline/cumulated_report.rb
@@ -5,12 +5,12 @@ module Kvlr #:nodoc:
class CumulatedReport < Report
def run(options = {})
- CumulatedReport.cumulate!(super)
+ cumulate(super)
end
protected
- def self.cumulate!(data)
+ def cumulate(data)
acc = 0
data.collect do |element|
acc += element[1].to_i
(DIR) diff --git a/lib/kvlr/reports_as_sparkline/report.rb b/lib/kvlr/reports_as_sparkline/report.rb
@@ -16,16 +16,24 @@ module Kvlr #:nodoc:
@grouping = Grouping.new(options[:grouping] || :day)
@options = {
:limit => options[:limit] || 100,
- :conditions => options[:conditions] || ['']
+ :conditions => options[:conditions] || []
}
@options.merge!(options)
end
def run(options = {})
ensure_valid_options(options)
- ReportCache.cached_transaction(self, options, options.key?(:conditions)) do |begin_at|
- options = @options.merge(options)
- conditions = setup_conditions(begin_at, options[:conditions])
+ custom_conditions = options.key?(:conditions)
+ options.reverse_merge!(@options)
+ ReportCache.cached_transaction(self, options[:limit], custom_conditions) do |begin_at|
+ read_data(begin_at, options[:conditions])
+ end
+ end
+
+ private
+
+ def read_data(begin_at, conditions = [])
+ conditions = setup_conditions(begin_at, conditions)
@klass.send(@aggregation,
@value_column_name,
:conditions => conditions,
@@ -33,17 +41,11 @@ module Kvlr #:nodoc:
:order => "#{@grouping.to_sql(@date_column_name)} DESC"
)
end
- end
-
- private
def setup_conditions(begin_at, custom_conditions = [])
conditions = ['']
if custom_conditions.is_a?(Hash)
- conditions = [
- custom_conditions.map{ |k, v| "#{k.to_s} = ?" }.join(' AND '),
- *custom_conditions.map{ |k, v| v }
- ]
+ conditions = [custom_conditions.map{ |k, v| "#{k.to_s} = ?" }.join(' AND '), *custom_conditions.map{ |k, v| v }]
elsif custom_conditions.size > 0
conditions = [(custom_conditions[0] || ''), *custom_conditions[1..-1]]
end
@@ -63,8 +65,8 @@ module Kvlr #:nodoc:
if options[:grouping] && !allowed_groupings.include?(options[:grouping])
raise ArgumentError.new("Invalid grouping #{options[:grouping]}; use one of #{allowed_groupings.join(', ')}")
end
- if options[:conditions] && !options[:conditions].is_a?(Array)
- raise ArgumentError.new("Invalid conditions: conditions must be specified as an array like ['user_name = ?', 'username']")
+ if options[:conditions] && !options[:conditions].is_a?(Array) && !options[:conditions].is_a?(Hash)
+ raise ArgumentError.new("Invalid conditions: conditions must be specified as an Array or a Hash")
end
end
(DIR) diff --git a/spec/other/cumulated_report_spec.rb b/spec/other/cumulated_report_spec.rb
@@ -8,41 +8,55 @@ describe Kvlr::ReportsAsSparkline::CumulatedReport do
describe '.run' do
- describe 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)
+ User.create!(:login => 'test 3', :created_at => Time.now - 2.weeks, :profile_visits => 3)
+ end
+
+ it 'should cumulate the data' do
+ @report.should_receive(:cumulate).once
+
+ @report.run
+ end
- before 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)
- User.create!(:login => 'test 3', :created_at => Time.now - 2.weeks, :profile_visits => 3)
- end
+ it 'should return correct data for :aggregation => :count' do
+ result = @report.run.to_a
- it 'should return correct data for :aggregation => :count' do
- result = @report.run.to_a
+ result[0][1].should == 1
+ result[1][1].should == 3
+ end
- result[0][1].should == 1
- result[1][1].should == 3
- end
+ it 'should return correct data for :aggregation => :sum' do
+ @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :registrations, :aggregation => :sum, :value_column_name => :profile_visits)
+ result = @report.run().to_a
- it 'should return correct data for :aggregation => :sum' do
- @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :registrations, :aggregation => :sum, :value_column_name => :profile_visits)
- result = @report.run().to_a
+ result[0][1].should == 1
+ result[1][1].should == 6
+ end
- result[0][1].should == 1
- result[1][1].should == 6
- end
+ it 'should return correct data with custom conditions' do
+ result = @report.run(:conditions => ['login IN (?)', ['test 1', 'test 2']]).to_a
- it 'should return correct data with custom conditions' do
- result = @report.run(:conditions => ['login IN (?)', ['test 1', 'test 2']]).to_a
+ result[0][1].should == 1
+ result[1][1].should == 2
+ end
+
+ after(:all) do
+ User.destroy_all
+ Kvlr::ReportsAsSparkline::ReportCache.destroy_all
+ end
+
+ end
- result[0][1].should == 1
- result[1][1].should == 2
- end
+ describe '.cumulate' do
- after do
- User.destroy_all
- Kvlr::ReportsAsSparkline::ReportCache.destroy_all
- end
+ it 'should correctly cumulate the given data' do
+ first = Time.now.to_s
+ second = (Time.now - 1.week).to_s
+ data = [[first, 1], [second, 2]]
+ @report.send(:cumulate, data).should == [[first, 1], [second, 3]]
end
end
(DIR) diff --git a/spec/other/report_spec.rb b/spec/other/report_spec.rb
@@ -6,58 +6,23 @@ describe Kvlr::ReportsAsSparkline::Report do
@report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations)
end
- share_as :OptionValidation 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_name => 'created_at',
- :value_column_name => '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, { :aggregation => :invalid }) }.should raise_error(ArgumentError)
- end
-
- it 'should raise an error if malformed conditions are specified' do
- lambda { @report.send(:ensure_valid_options, { :conditions => 1 }) }.should raise_error(ArgumentError)
- lambda { @report.send(:ensure_valid_options, { :conditions => { :user_name => 'username' } }) }.should raise_error(ArgumentError)
- end
-
- end
-
describe '.run' do
- include OptionValidation
-
- it 'should invoke the default aggregation method on the model' do
- User.should_receive(:count).once.and_return([])
+ it 'should run a cached transaction' do
+ Kvlr::ReportsAsSparkline::ReportCache.should_receive(:cached_transaction).once.with(@report, 100, false)
@report.run
end
- it 'should invoke the custom aggregation method on the model if one is specified' do
- @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations, :aggregation => :sum)
- User.should_receive(:sum).once.and_return([])
+ it 'should run a cached transaction but specify no_cache when custom conditions are given' do
+ Kvlr::ReportsAsSparkline::ReportCache.should_receive(:cached_transaction).once.with(@report, 100, true)
- @report.run
+ @report.run(:conditions => { :some => :condition })
end
describe do
- before 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)
User.create!(:login => 'test 3', :created_at => Time.now - 2.weeks, :profile_visits => 3)
@@ -91,7 +56,7 @@ describe Kvlr::ReportsAsSparkline::Report do
result[1][1].should == 1
end
- after do
+ after(:all) do
User.destroy_all
Kvlr::ReportsAsSparkline::ReportCache.destroy_all
end
@@ -100,6 +65,16 @@ describe Kvlr::ReportsAsSparkline::Report do
end
+ describe '.read_data' do
+
+ it 'should invoke the aggregation method on the model' do
+ User.should_receive(:count).once.and_return([])
+
+ @report.send(:read_data, Time.now)
+ end
+
+ end
+
describe '.setup_conditions' do
it 'should return conditions for date_column_name >= begin_at only if no custom conditions are specified' do
@@ -148,4 +123,35 @@ describe Kvlr::ReportsAsSparkline::Report do
end
+ describe '.ensure_valid_options' 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_name => 'created_at',
+ :value_column_name => '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, { :aggregation => :invalid }) }.should raise_error(ArgumentError)
+ end
+
+ it 'should raise an error if malformed conditions are specified' do
+ lambda { @report.send(:ensure_valid_options, { :conditions => 1 }) }.should raise_error(ArgumentError)
+ end
+
+ end
+
end