| implemented :live_data option) - reportable - Fork of reportable required by Wa… | |
| Log | |
| Files | |
| Refs | |
| README | |
| --- | |
| commit 4bdd41ffb940013461b09f647bef8828387a199d | |
| parent a20b9eee3e4f42b987b0452c3b9c2552ff84c732 | |
| Author: Marco Otte-Witte <[email protected]> | |
| Date: Tue, 20 Jan 2009 17:40:09 +0100 | |
| implemented :live_data option) | |
| Diffstat: | |
| M lib/kvlr/reports_as_sparkline/repo… | 2 +- | |
| M lib/kvlr/reports_as_sparkline/repo… | 26 ++++++++++++++++---------- | |
| M lib/kvlr/reports_as_sparkline/repo… | 11 +++++------ | |
| M spec/classes/cumulated_report_spec… | 182 ++++++++++++++++++++-------… | |
| M spec/classes/report_cache_spec.rb | 154 ++++++++++++++++++++++-------… | |
| M spec/classes/report_spec.rb | 192 ++++++++++++++++++++++-------… | |
| M spec/classes/reporting_period_spec… | 16 ++++++++-------- | |
| M spec/other/report_method_spec.rb | 9 ++++----- | |
| 8 files changed, 396 insertions(+), 196 deletions(-) | |
| --- | |
| diff --git a/lib/kvlr/reports_as_sparkline/report.rb b/lib/kvlr/reports_as_spar… | |
| @@ -81,7 +81,7 @@ module Kvlr #:nodoc: | |
| case context | |
| when :initialize | |
| options.each_key do |k| | |
| - raise ArgumentError.new("Invalid option #{k}") unless [:limit,… | |
| + raise ArgumentError.new("Invalid option #{k}") unless [:limit,… | |
| end | |
| raise ArgumentError.new("Invalid aggregation #{options[:aggregat… | |
| raise ArgumentError.new('The name of the column holding the valu… | |
| diff --git a/lib/kvlr/reports_as_sparkline/report_cache.rb b/lib/kvlr/reports_a… | |
| @@ -8,30 +8,36 @@ module Kvlr #:nodoc: | |
| raise ArgumentError.new('A block must be given') unless block_given? | |
| self.transaction do | |
| cached_data = [] | |
| - last_reporting_period_to_read = ReportingPeriod.first(options[:group… | |
| + first_reporting_period = ReportingPeriod.first(options[:grouping], o… | |
| if cache | |
| - cached_data = find_cached_data(report, options, last_reporting_per… | |
| - last_reporting_period_to_read = ReportingPeriod.new(options[:group… | |
| + cached_data = find_cached_data(report, options, first_reporting_pe… | |
| + last_cached_reporting_period = (ReportingPeriod.new(options[:group… | |
| end | |
| - new_data = yield(last_reporting_period_to_read.date_time) | |
| - prepare_result(new_data, cached_data, last_reporting_period_to_read,… | |
| + new_data = if !options[:live_data] && last_cached_reporting_period =… | |
| + [] | |
| + else | |
| + yield((last_cached_reporting_period.next rescue first_reporting_pe… | |
| + end | |
| + prepare_result(new_data, cached_data, report, options, cache) | |
| end | |
| end | |
| private | |
| - def self.prepare_result(new_data, cached_data, last_reporting_period_t… | |
| + def self.prepare_result(new_data, cached_data, report, options, cache … | |
| new_data.map! { |data| [ReportingPeriod.from_db_string(options[:grou… | |
| result = cached_data.map { |cached| [cached.reporting_period, cached… | |
| current_reporting_period = ReportingPeriod.new(options[:grouping]) | |
| - reporting_period = last_reporting_period_to_read | |
| + reporting_period = (cached_data.last.reporting_period.next rescue Re… | |
| while reporting_period < current_reporting_period | |
| cached = build_cached_data(report, options[:grouping], reporting_p… | |
| cached.save! if cache | |
| result << [reporting_period.date_time, cached.value] | |
| reporting_period = reporting_period.next | |
| end | |
| - result << [current_reporting_period.date_time, find_value(new_data, … | |
| + if options[:live_data] | |
| + result << [current_reporting_period.date_time, find_value(new_data… | |
| + end | |
| result | |
| end | |
| @@ -51,7 +57,7 @@ module Kvlr #:nodoc: | |
| ) | |
| end | |
| - def self.find_cached_data(report, options, last_reporting_period_to_re… | |
| + def self.find_cached_data(report, options, first_reporting_period) | |
| self.find( | |
| :all, | |
| :conditions => [ | |
| @@ -60,7 +66,7 @@ module Kvlr #:nodoc: | |
| report.name.to_s, | |
| options[:grouping].identifier.to_s, | |
| report.aggregation.to_s, | |
| - last_reporting_period_to_read.date_time | |
| + first_reporting_period.date_time | |
| ], | |
| :limit => options[:limit], | |
| :order => 'reporting_period ASC' | |
| diff --git a/lib/kvlr/reports_as_sparkline/reporting_period.rb b/lib/kvlr/repor… | |
| @@ -15,8 +15,7 @@ module Kvlr #:nodoc: | |
| @date_time = parse_date_time(date_time) | |
| end | |
| - # Returns the first reporting period for a grouping and a limit; e.g. th… | |
| - # (since limit is 2, 2 reporting periods are included in the range, that… | |
| + # Returns the first reporting period for a grouping and a limit; e.g. th… | |
| # | |
| # ==== Parameters | |
| # * <tt>grouping</tt> - The Kvlr::ReportsAsSparkline::Grouping of the re… | |
| @@ -24,13 +23,13 @@ module Kvlr #:nodoc: | |
| def self.first(grouping, limit) | |
| return case grouping.identifier | |
| when :hour | |
| - self.new(grouping, DateTime.now - (limit - 1).hours) | |
| + self.new(grouping, DateTime.now - limit.hours) | |
| when :day | |
| - self.new(grouping, DateTime.now - (limit - 1).days) | |
| + self.new(grouping, DateTime.now - limit.days) | |
| when :week | |
| - self.new(grouping, DateTime.now - (limit - 1).weeks) | |
| + self.new(grouping, DateTime.now - limit.weeks) | |
| when :month | |
| - date = DateTime.now - (limit - 1).months | |
| + date = DateTime.now - limit.months | |
| self.new(grouping, Date.new(date.year, date.month, 1)) | |
| end | |
| end | |
| diff --git a/spec/classes/cumulated_report_spec.rb b/spec/classes/cumulated_rep… | |
| @@ -14,90 +14,140 @@ describe Kvlr::ReportsAsSparkline::CumulatedReport do | |
| @report.run | |
| end | |
| - it 'should return an array of the same length as the specified limit' do | |
| - @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :cumulated… | |
| + it 'should return an array of the same length as the specified limit when … | |
| + @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :cumulated… | |
| @report.run.length.should == 10 | |
| end | |
| - for grouping in [:hour, :day, :week, :month] do | |
| + it 'should return an array of the same length as the specified limit + 1 w… | |
| + @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :cumulated… | |
| - describe "for grouping #{grouping.to_s}" do | |
| + @report.run.length.should == 11 | |
| + end | |
| - before(:all) do | |
| - User.create!(:login => 'test 1', :created_at => Time.now - 1.send(gr… | |
| - User.create!(:login => 'test 2', :created_at => Time.now - 3.send(gr… | |
| - User.create!(:login => 'test 3', :created_at => Time.now - 3.send(gr… | |
| - end | |
| + for grouping in [:hour, :day, :week, :month] do | |
| - describe do | |
| + describe "for grouping #{grouping.to_s}" do | |
| - before do | |
| - @grouping = Kvlr::ReportsAsSparkline::Grouping.new(grouping) | |
| - @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :cum… | |
| - @result = @report.run | |
| - end | |
| + [true, false].each do |live_data| | |
| + | |
| + describe "with :live_data = #{live_data}" do | |
| + | |
| + before(:all) do | |
| + User.create!(:login => 'test 1', :created_at => Time.now - 1.sen… | |
| + User.create!(:login => 'test 2', :created_at => Time.now - 3.sen… | |
| + User.create!(:login => 'test 3', :created_at => Time.now - 3.sen… | |
| + end | |
| + | |
| + describe do | |
| + | |
| + before do | |
| + @grouping = Kvlr::ReportsAsSparkline::Grouping.new(grouping) | |
| + @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, … | |
| + :grouping => grouping, | |
| + :limit => 10, | |
| + :live_data => live_data | |
| + ) | |
| + @result = @report.run | |
| + end | |
| + | |
| + it "should return an array starting reporting period (Time.now -… | |
| + @result.first[0].should == Kvlr::ReportsAsSparkline::Reporting… | |
| + end | |
| + | |
| + if live_data | |
| + it "should return data ending with the current reporting perio… | |
| + @result.last[0].should == Kvlr::ReportsAsSparkline::Reportin… | |
| + end | |
| + else | |
| + it "should return data ending with the reporting period before… | |
| + @result.last[0].should == Kvlr::ReportsAsSparkline::Reportin… | |
| + end | |
| + end | |
| + | |
| + end | |
| + | |
| + it 'should return correct data for aggregation :count' do | |
| + @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :r… | |
| + :aggregation => :count, | |
| + :grouping => grouping, | |
| + :limit => 10, | |
| + :live_data => live_data | |
| + ) | |
| + result = @report.run | |
| + | |
| + result[10][1].should == 3.0 if live_data | |
| + result[9][1].should == 3.0 | |
| + result[8][1].should == 2.0 | |
| + result[7][1].should == 2.0 | |
| + result[6][1].should == 0.0 | |
| + end | |
| + | |
| + it 'should return correct data for aggregation :sum' do | |
| + @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :r… | |
| + :aggregation => :sum, | |
| + :grouping => grouping, | |
| + :value_column => :profile_visits, | |
| + :limit => 10, | |
| + :live_data => live_data | |
| + ) | |
| + result = @report.run() | |
| + | |
| + result[10][1].should == 6.0 if live_data | |
| + result[9][1].should == 6.0 | |
| + result[8][1].should == 5.0 | |
| + result[7][1].should == 5.0 | |
| + result[6][1].should == 0.0 | |
| + end | |
| + | |
| + it 'should return correct data for aggregation :count when custom … | |
| + @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :r… | |
| + :aggregation => :count, | |
| + :grouping => grouping, | |
| + :limit => 10, | |
| + :live_data => live_data | |
| + ) | |
| + result = @report.run(:conditions => ['login IN (?)', ['test 1', … | |
| + | |
| + result[10][1].should == 2.0 if live_data | |
| + result[9][1].should == 2.0 | |
| + result[8][1].should == 1.0 | |
| + result[7][1].should == 1.0 | |
| + result[6][1].should == 0.0 | |
| + end | |
| + | |
| + it 'should return correct data for aggregation :sum when custom co… | |
| + @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :r… | |
| + :aggregation => :sum, | |
| + :grouping => grouping, | |
| + :value_column => :profile_visits, | |
| + :limit => 10, | |
| + :live_data => live_data | |
| + ) | |
| + result = @report.run(:conditions => ['login IN (?)', ['test 1', … | |
| + | |
| + result[10][1].should == 3.0 if live_data | |
| + result[9][1].should == 3.0 | |
| + result[8][1].should == 2.0 | |
| + result[7][1].should == 2.0 | |
| + result[6][1].should == 0.0 | |
| + end | |
| - it "should return an array starting reporting period (Time.now - (li… | |
| - @result.first[0].should == Kvlr::ReportsAsSparkline::ReportingPeri… | |
| end | |
| - it "should return data ending with with the current reporting period… | |
| - @result.last[0].should == Kvlr::ReportsAsSparkline::ReportingPerio… | |
| + after(:all) do | |
| + User.destroy_all | |
| end | |
| end | |
| - it 'should return correct data for aggregation :count' do | |
| - @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :regis… | |
| - 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, :regis… | |
| - 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 cond… | |
| - @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :regis… | |
| - result = @report.run(:conditions => ['login IN (?)', ['test 1', 'tes… | |
| - | |
| - 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 condit… | |
| - @report = Kvlr::ReportsAsSparkline::CumulatedReport.new(User, :regis… | |
| - result = @report.run(:conditions => ['login IN (?)', ['test 1', 'tes… | |
| - | |
| - 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 | |
| + after(:each) do | |
| + Kvlr::ReportsAsSparkline::ReportCache.destroy_all | |
| end | |
| end | |
| diff --git a/spec/classes/report_cache_spec.rb b/spec/classes/report_cache_spec… | |
| @@ -25,10 +25,67 @@ describe Kvlr::ReportsAsSparkline::ReportCache do | |
| Kvlr::ReportsAsSparkline::ReportCache.process(@report, @report.options) … | |
| end | |
| - it 'should yield to the given block' do | |
| - lambda { | |
| - Kvlr::ReportsAsSparkline::ReportCache.process(@report, @report.options… | |
| - }.should raise_error(YieldMatchException) | |
| + describe 'with :live_data = true' do | |
| + | |
| + before do | |
| + @options = @report.options.merge(:live_data => true) | |
| + end | |
| + | |
| + it 'should yield to the given block' do | |
| + lambda { | |
| + Kvlr::ReportsAsSparkline::ReportCache.process(@report, @options) { r… | |
| + }.should raise_error(YieldMatchException) | |
| + end | |
| + | |
| + it 'should yield the reporting period after the last one in the cache if… | |
| + reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new( | |
| + @report.options[:grouping], | |
| + Time.now - 3.send(@report.options[:grouping].identifier) | |
| + ) | |
| + cached = Kvlr::ReportsAsSparkline::ReportCache.new | |
| + cached.stub!(:reporting_period).and_return(reporting_period) | |
| + Kvlr::ReportsAsSparkline::ReportCache.stub!(:find).and_return([cached]) | |
| + | |
| + Kvlr::ReportsAsSparkline::ReportCache.process(@report, @options) do |b… | |
| + begin_at.should == reporting_period.next.date_time | |
| + [] | |
| + end | |
| + end | |
| + | |
| + end | |
| + | |
| + describe 'with :live_data = false' do | |
| + | |
| + it 'should not yield to the block if data for the reporting period befor… | |
| + cached = Kvlr::ReportsAsSparkline::ReportCache.new | |
| + cached.stub!(:reporting_period).and_return(Kvlr::ReportsAsSparkline::R… | |
| + Kvlr::ReportsAsSparkline::ReportCache.stub!(:find).and_return([cached]) | |
| + lambda { | |
| + Kvlr::ReportsAsSparkline::ReportCache.process(@report, @report.optio… | |
| + }.should_not raise_error(YieldMatchException) | |
| + end | |
| + | |
| + it 'should yield to the block if no data for the reporting period before… | |
| + lambda { | |
| + Kvlr::ReportsAsSparkline::ReportCache.process(@report, @report.optio… | |
| + }.should raise_error(YieldMatchException) | |
| + end | |
| + | |
| + it 'should yield the reporting period after the last one in the cache if… | |
| + reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new( | |
| + @report.options[:grouping], | |
| + Time.now - 3.send(@report.options[:grouping].identifier) | |
| + ) | |
| + cached = Kvlr::ReportsAsSparkline::ReportCache.new | |
| + cached.stub!(:reporting_period).and_return(reporting_period) | |
| + Kvlr::ReportsAsSparkline::ReportCache.stub!(:find).and_return([cached]) | |
| + | |
| + Kvlr::ReportsAsSparkline::ReportCache.process(@report, @report.options… | |
| + begin_at.should == reporting_period.next.date_time | |
| + [] | |
| + end | |
| + end | |
| + | |
| end | |
| it 'should read existing data from the cache' do | |
| @@ -72,8 +129,7 @@ describe Kvlr::ReportsAsSparkline::ReportCache do | |
| new_data = [] | |
| cached_data = [] | |
| Kvlr::ReportsAsSparkline::ReportCache.stub!(:find).and_return(cached_dat… | |
| - last_reporting_period_to_read = Kvlr::ReportsAsSparkline::ReportingPerio… | |
| - Kvlr::ReportsAsSparkline::ReportCache.should_receive(:prepare_result).on… | |
| + Kvlr::ReportsAsSparkline::ReportCache.should_receive(:prepare_result).on… | |
| Kvlr::ReportsAsSparkline::ReportCache.process(@report, @report.options) … | |
| end | |
| @@ -85,24 +141,6 @@ describe Kvlr::ReportsAsSparkline::ReportCache do | |
| end | |
| end | |
| - it 'should yield the reporting period after the last one in the cache if t… | |
| - reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.new(@report… | |
| - cached = Kvlr::ReportsAsSparkline::ReportCache.new({ | |
| - :model_name => @report.klass, | |
| - :report_name => @report.name, | |
| - :grouping => @report.options[:grouping].identifier.to_s, | |
| - :aggregation => @report.aggregation.to_s, | |
| - :value => 1, | |
| - :reporting_period => reporting_period.date_time | |
| - }) | |
| - Kvlr::ReportsAsSparkline::ReportCache.stub!(:find).and_return([cached]) | |
| - | |
| - Kvlr::ReportsAsSparkline::ReportCache.process(@report, @report.options) … | |
| - begin_at.should == reporting_period.next.date_time | |
| - [] | |
| - end | |
| - end | |
| - | |
| describe 'with cache = false' do | |
| it 'should not read any data from cache' do | |
| @@ -125,9 +163,9 @@ describe Kvlr::ReportsAsSparkline::ReportCache do | |
| describe '.prepare_result' do | |
| before do | |
| - @last_reporting_period_to_read = Kvlr::ReportsAsSparkline::ReportingPeri… | |
| - @new_data = [[@last_reporting_period_to_read.date_time, 1.0]] | |
| - Kvlr::ReportsAsSparkline::ReportingPeriod.stub!(:from_db_string).and_ret… | |
| + @current_reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.ne… | |
| + @new_data = [[@current_reporting_period.previous.date_time, 1.0]] | |
| + Kvlr::ReportsAsSparkline::ReportingPeriod.stub!(:from_db_string).and_ret… | |
| @cached = Kvlr::ReportsAsSparkline::ReportCache.new | |
| @cached.stub!(:save!) | |
| Kvlr::ReportsAsSparkline::ReportCache.stub!(:build_cached_data).and_retu… | |
| @@ -136,22 +174,22 @@ describe Kvlr::ReportsAsSparkline::ReportCache do | |
| it 'should convert the date strings from the newly read data to reporting … | |
| Kvlr::ReportsAsSparkline::ReportingPeriod.should_receive(:from_db_string… | |
| - Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [… | |
| + Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [… | |
| end | |
| - it 'should create (:limit - 1) instances of Kvlr::ReportsAsSparkline::Repo… | |
| - Kvlr::ReportsAsSparkline::ReportCache.should_receive(:build_cached_data)… | |
| + it 'should create :limit instances of Kvlr::ReportsAsSparkline::ReportCach… | |
| + Kvlr::ReportsAsSparkline::ReportCache.should_receive(:build_cached_data)… | |
| @report, | |
| @report.options[:grouping], | |
| anything(), | |
| 0.0 | |
| ).and_return(@cached) | |
| - Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, [], [], @las… | |
| + Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, [], [], @rep… | |
| end | |
| it 'should create a new Kvlr::ReportsAsSparkline::ReportCache with the cor… | |
| - Kvlr::ReportsAsSparkline::ReportCache.should_receive(:build_cached_data)… | |
| + Kvlr::ReportsAsSparkline::ReportCache.should_receive(:build_cached_data)… | |
| @report, | |
| @report.options[:grouping], | |
| anything(), | |
| @@ -160,21 +198,21 @@ describe Kvlr::ReportsAsSparkline::ReportCache do | |
| Kvlr::ReportsAsSparkline::ReportCache.should_receive(:build_cached_data)… | |
| @report, | |
| @report.options[:grouping], | |
| - @last_reporting_period_to_read, | |
| + @current_reporting_period.previous, | |
| 1.0 | |
| ).and_return(@cached) | |
| - Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [… | |
| + Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [… | |
| end | |
| - it 'should save the created Kvlr::ReportsAsSparkline::ReportCache if no_ca… | |
| + it 'should save the created Kvlr::ReportsAsSparkline::ReportCache' do | |
| @cached.should_receive(:save!).once | |
| - Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [… | |
| + Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [… | |
| end | |
| it 'should return an array of arrays of Dates and Floats' do | |
| - result = Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @ne… | |
| + result = Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @ne… | |
| result.should be_kind_of(Array) | |
| result[0].should be_kind_of(Array) | |
| @@ -182,19 +220,45 @@ describe Kvlr::ReportsAsSparkline::ReportCache do | |
| result[0][1].should be_kind_of(Float) | |
| end | |
| - describe 'with cache = false' do | |
| + describe 'with :live_data = false' do | |
| - it 'should not save the created Kvlr::ReportsAsSparkline::ReportCache' do | |
| - @cached.should_not_receive(:save!) | |
| + before do | |
| + @result = Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, … | |
| + end | |
| + | |
| + it 'should return an array of length :limit' do | |
| + @result.length.should == 10 | |
| + end | |
| + | |
| + it 'should not include an entry for the current reporting period' do | |
| + @result.find { |row| row[0] == @current_reporting_period.date_time }.s… | |
| + end | |
| + | |
| + end | |
| + | |
| + describe 'with :live_data = true' do | |
| + | |
| + before do | |
| + options = @report.options.merge(:live_data => true) | |
| + @result = Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, … | |
| + end | |
| + | |
| + it 'should return an array of length (:limit + 1)' do | |
| + @result.length.should == 11 | |
| + end | |
| - Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data,… | |
| + it 'should include an entry for the current reporting period' do | |
| + @result.find { |row| row[0] == @current_reporting_period.date_time }.s… | |
| end | |
| - it 'should not update the last cached record if new data has been read f… | |
| - Kvlr::ReportsAsSparkline::ReportingPeriod.stub!(:from_db_string).and_r… | |
| - @cached.should_not_receive(:update_attributes!) | |
| + end | |
| + | |
| + describe 'with cache = false' do | |
| + | |
| + it 'should not save the created Kvlr::ReportsAsSparkline::ReportCache' do | |
| + @cached.should_not_receive(:save!) | |
| - Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data,… | |
| + Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data,… | |
| end | |
| end | |
| diff --git a/spec/classes/report_spec.rb b/spec/classes/report_spec.rb | |
| @@ -54,86 +54,167 @@ describe Kvlr::ReportsAsSparkline::Report do | |
| @report.run(:grouping => :month) | |
| end | |
| + it 'should return an array of the same length as the specified limit when … | |
| + @report = Kvlr::ReportsAsSparkline::Report.new(User, :cumulated_registra… | |
| + | |
| + @report.run.length.should == 10 | |
| + end | |
| + | |
| + it 'should return an array of the same length as the specified limit + 1 w… | |
| + @report = Kvlr::ReportsAsSparkline::Report.new(User, :cumulated_registra… | |
| + | |
| + @report.run.length.should == 11 | |
| + 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(gr… | |
| - User.create!(:login => 'test 2', :created_at => Time.now - 3.send(gr… | |
| - User.create!(:login => 'test 3', :created_at => Time.now - 3.send(gr… | |
| + | |
| end | |
| - describe do | |
| - | |
| - before do | |
| - @grouping = Kvlr::ReportsAsSparkline::Grouping.new(grouping) | |
| - @report = Kvlr::ReportsAsSparkline::Report.new(User, :registration… | |
| - @result = @report.run | |
| - end | |
| - | |
| - it "should return an array starting reporting period (Time.now - (li… | |
| - @result.first[0].should == Kvlr::ReportsAsSparkline::ReportingPeri… | |
| - end | |
| + | |
| - it "should return data ending with with the current reporting period… | |
| - @result.last[0].should == Kvlr::ReportsAsSparkline::ReportingPerio… | |
| - end | |
| + | |
| + after(:all) do | |
| + User.destroy_all | |
| end | |
| - it 'should return correct data for aggregation :count' do | |
| - @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations,… | |
| - 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 | |
| + after(:each) do | |
| + Kvlr::ReportsAsSparkline::ReportCache.destroy_all | |
| end | |
| - it 'should return correct data for aggregation :sum' do | |
| - @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations,… | |
| - result = @report.run().to_a | |
| + end | |
| - 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 | |
| + end | |
| - it 'should return correct data for aggregation :count when custom cond… | |
| - @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations,… | |
| - result = @report.run(:conditions => ['login IN (?)', ['test 1', 'tes… | |
| + for grouping in [:hour, :day, :week, :month] do | |
| - 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 | |
| + describe "for grouping #{grouping.to_s}" do | |
| - it 'should return correct data for aggregation :sum when custom condit… | |
| - @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations,… | |
| - result = @report.run(:conditions => ['login IN (?)', ['test 1', 'tes… | |
| + [true, false].each do |live_data| | |
| + | |
| + describe "with :live_data = #{live_data}" do | |
| + | |
| + before(:all) do | |
| + User.create!(:login => 'test 1', :created_at => Time.now, … | |
| + User.create!(:login => 'test 2', :created_at => Time.now - 1.sen… | |
| + User.create!(:login => 'test 3', :created_at => Time.now - 3.sen… | |
| + User.create!(:login => 'test 4', :created_at => Time.now - 3.sen… | |
| + end | |
| + | |
| + describe do | |
| + | |
| + before do | |
| + @grouping = Kvlr::ReportsAsSparkline::Grouping.new(grouping) | |
| + @report = Kvlr::ReportsAsSparkline::Report.new(User, :registra… | |
| + :grouping => grouping, | |
| + :limit => 10, | |
| + :live_data => live_data | |
| + ) | |
| + @result = @report.run | |
| + end | |
| + | |
| + it "should return an array starting reporting period (Time.now -… | |
| + @result.first[0].should == Kvlr::ReportsAsSparkline::Reporting… | |
| + end | |
| + | |
| + if live_data | |
| + it "should return data ending with the current reporting perio… | |
| + @result.last[0].should == Kvlr::ReportsAsSparkline::Reportin… | |
| + end | |
| + else | |
| + it "should return data ending with the reporting period before… | |
| + @result.last[0].should == Kvlr::ReportsAsSparkline::Reportin… | |
| + end | |
| + end | |
| + | |
| + end | |
| + | |
| + it 'should return correct data for aggregation :count' do | |
| + @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrati… | |
| + :aggregation => :count, | |
| + :grouping => grouping, | |
| + :limit => 10, | |
| + :live_data => live_data | |
| + ) | |
| + result = @report.run.to_a | |
| + | |
| + result[10][1].should == 1.0 if live_data | |
| + result[9][1].should == 1.0 | |
| + result[8][1].should == 0.0 | |
| + result[7][1].should == 2.0 | |
| + result[6][1].should == 0.0 | |
| + end | |
| + | |
| + it 'should return correct data for aggregation :sum' do | |
| + @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrati… | |
| + :aggregation => :sum, | |
| + :grouping => grouping, | |
| + :value_column => :profile_visits, | |
| + :limit => 10, | |
| + :live_data => live_data | |
| + ) | |
| + result = @report.run().to_a | |
| + | |
| + result[10][1].should == 2.0 if live_data | |
| + result[9][1].should == 1.0 | |
| + result[8][1].should == 0.0 | |
| + result[7][1].should == 5.0 | |
| + result[6][1].should == 0.0 | |
| + end | |
| + | |
| + it 'should return correct data for aggregation :count when custom … | |
| + @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrati… | |
| + :aggregation => :count, | |
| + :grouping => grouping, | |
| + :limit => 10, | |
| + :live_data => live_data | |
| + ) | |
| + result = @report.run(:conditions => ['login IN (?)', ['test 1', … | |
| + | |
| + result[10][1].should == 1.0 if live_data | |
| + result[9][1].should == 1.0 | |
| + result[8][1].should == 0.0 | |
| + result[7][1].should == 1.0 | |
| + result[6][1].should == 0.0 | |
| + end | |
| + | |
| + it 'should return correct data for aggregation :sum when custom co… | |
| + @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrati… | |
| + :aggregation => :sum, | |
| + :grouping => grouping, | |
| + :value_column => :profile_visits, | |
| + :limit => 10, | |
| + :live_data => live_data | |
| + ) | |
| + result = @report.run(:conditions => ['login IN (?)', ['test 1', … | |
| + | |
| + result[10][1].should == 2.0 if live_data | |
| + result[9][1].should == 1.0 | |
| + result[8][1].should == 0.0 | |
| + result[7][1].should == 3.0 | |
| + result[6][1].should == 0.0 | |
| + end | |
| - 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 | |
| + end | |
| - after(:all) do | |
| - User.destroy_all | |
| - end | |
| + after(:all) do | |
| + User.destroy_all | |
| + end | |
| - after(:each) do | |
| - Kvlr::ReportsAsSparkline::ReportCache.destroy_all | |
| end | |
| end | |
| end | |
| + after(:each) do | |
| + Kvlr::ReportsAsSparkline::ReportCache.destroy_all | |
| + end | |
| + | |
| end | |
| describe '#read_data' do | |
| @@ -228,7 +309,8 @@ describe Kvlr::ReportsAsSparkline::Report do | |
| :grouping => :day, | |
| :date_column => :created_at, | |
| :value_column => :id, | |
| - :conditions => [] | |
| + :conditions => [], | |
| + :live_data => true | |
| }) | |
| }.should_not raise_error(ArgumentError) | |
| end | |
| diff --git a/spec/classes/reporting_period_spec.rb b/spec/classes/reporting_per… | |
| @@ -204,32 +204,32 @@ 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 -… | |
| + it 'should return a reporting period with the date part of (DateTime.now -… | |
| reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.first(Kvlr:… | |
| - expected = @now - 2.hours | |
| + expected = @now - 3.hours | |
| reporting_period.date_time.should == DateTime.new(expected.year, expecte… | |
| end | |
| - it 'should return a reporting period with the date part of (DateTime.now -… | |
| + it 'should return a reporting period with the date part of (DateTime.now -… | |
| reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.first(Kvlr:… | |
| - expected = @now - 2.days | |
| + expected = @now - 3.days | |
| reporting_period.date_time.should == Date.new(expected.year, expected.mo… | |
| end | |
| - it 'should return a reporting period with the date of the first day of the… | |
| + it 'should return a reporting period with the date of the first day of the… | |
| DateTime.stub!(:now).and_return(DateTime.new(2008, 12, 31, 0, 0, 0)) | |
| reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.first(Kvlr:… | |
| - reporting_period.date_time.should == DateTime.new(2008, 10, 1) | |
| + 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 we… | |
| + it 'should return a reporting period with the date of the monday of the we… | |
| DateTime.stub!(:now).and_return(DateTime.new(2008, 12, 31, 0, 0, 0)) #we… | |
| reporting_period = Kvlr::ReportsAsSparkline::ReportingPeriod.first(Kvlr:… | |
| - reporting_period.date_time.should == DateTime.new(2008, 12, 15) #the mon… | |
| + reporting_period.date_time.should == DateTime.new(2008, 12, 8) #the mond… | |
| end | |
| end | |
| diff --git a/spec/other/report_method_spec.rb b/spec/other/report_method_spec.rb | |
| @@ -13,16 +13,15 @@ describe Kvlr::ReportsAsSparkline do | |
| 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 | |
| + result[9][1].should == 1.0 | |
| + result[8][1].should == 2.0 | |
| end | |
| it 'should include only data for instances of the inherited model when inv… | |
| result = SpecialUser.registrations_report.to_a | |
| - result.length.should == 10 | |
| - result[7][1].should == 1.0 | |
| + result[9][1].should == 0.0 | |
| + result[8][1].should == 1.0 | |
| end | |
| after(:all) do |