| cleanup, more specs - reportable - Fork of reportable required by WarVox, from … | |
| Log | |
| Files | |
| Refs | |
| README | |
| --- | |
| commit a7042b3435edfcf4e0cc128e73da54b56b01125f | |
| parent 2d19c69d72d105e1ceceef35ba963c309b477fb2 | |
| Author: marcoow <[email protected]> | |
| Date: Sat, 6 Dec 2008 00:54:46 +0800 | |
| cleanup, more specs | |
| Signed-off-by: Marco Otte-Witte <[email protected]> | |
| 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(-) | |
| --- | |
| diff --git a/lib/kvlr/reports_as_sparkline/cumulated_report.rb b/lib/kvlr/repor… | |
| @@ -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 | |
| diff --git a/lib/kvlr/reports_as_sparkline/report.rb b/lib/kvlr/reports_as_spar… | |
| @@ -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… | |
| - 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_condition… | |
| + 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… | |
| elsif custom_conditions.size > 0 | |
| conditions = [(custom_conditions[0] || ''), *custom_conditions[1..… | |
| end | |
| @@ -63,8 +65,8 @@ module Kvlr #:nodoc: | |
| if options[:grouping] && !allowed_groupings.include?(options[:groupi… | |
| raise ArgumentError.new("Invalid grouping #{options[:grouping]}; u… | |
| end | |
| - if options[:conditions] && !options[:conditions].is_a?(Array) | |
| - raise ArgumentError.new("Invalid conditions: conditions must be sp… | |
| + if options[:conditions] && !options[:conditions].is_a?(Array) && !op… | |
| + raise ArgumentError.new("Invalid conditions: conditions must be sp… | |
| end | |
| end | |
| diff --git a/spec/other/cumulated_report_spec.rb b/spec/other/cumulated_report_… | |
| @@ -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, :pro… | |
| + User.create!(:login => 'test 2', :created_at => Time.now - 2.weeks, :pro… | |
| + User.create!(:login => 'test 3', :created_at => Time.now - 2.weeks, :pro… | |
| + 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, :p… | |
| - User.create!(:login => 'test 2', :created_at => Time.now - 2.weeks, :p… | |
| - User.create!(:login => 'test 3', :created_at => Time.now - 2.weeks, :p… | |
| - 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, :registrat… | |
| + result = @report.run().to_a | |
| - it 'should return correct data for :aggregation => :sum' do | |
| - @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :registr… | |
| - 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'… | |
| - it 'should return correct data with custom conditions' do | |
| - result = @report.run(:conditions => ['login IN (?)', ['test 1', 'test … | |
| + 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 | |
| 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 }) }.… | |
| - end | |
| - | |
| - it 'should raise an error if an invalid aggregation is specified' do | |
| - lambda { @report.send(:ensure_valid_options, { :aggregation => :invalid … | |
| - end | |
| - | |
| - it 'should raise an error if an invalid grouping is specified' do | |
| - lambda { @report.send(:ensure_valid_options, { :aggregation => :invalid … | |
| - end | |
| - | |
| - it 'should raise an error if malformed conditions are specified' do | |
| - lambda { @report.send(:ensure_valid_options, { :conditions => 1 }) }.sho… | |
| - lambda { @report.send(:ensure_valid_options, { :conditions => { :user_na… | |
| - 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… | |
| @report.run | |
| end | |
| - it 'should invoke the custom aggregation method on the model if one is spe… | |
| - @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations, :ag… | |
| - User.should_receive(:sum).once.and_return([]) | |
| + it 'should run a cached transaction but specify no_cache when custom condi… | |
| + Kvlr::ReportsAsSparkline::ReportCache.should_receive(:cached_transaction… | |
| - @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, :p… | |
| User.create!(:login => 'test 2', :created_at => Time.now - 2.weeks, :p… | |
| User.create!(:login => 'test 3', :created_at => Time.now - 2.weeks, :p… | |
| @@ -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 c… | |
| @@ -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 }) }.… | |
| + end | |
| + | |
| + it 'should raise an error if an invalid aggregation is specified' do | |
| + lambda { @report.send(:ensure_valid_options, { :aggregation => :invalid … | |
| + end | |
| + | |
| + it 'should raise an error if an invalid grouping is specified' do | |
| + lambda { @report.send(:ensure_valid_options, { :aggregation => :invalid … | |
| + end | |
| + | |
| + it 'should raise an error if malformed conditions are specified' do | |
| + lambda { @report.send(:ensure_valid_options, { :conditions => 1 }) }.sho… | |
| + end | |
| + | |
| + end | |
| + | |
| end |