spec/classes/report_spec.rb in reportable-1.2.0 vs spec/classes/report_spec.rb in reportable-1.3.0

- old
+ new

@@ -19,20 +19,20 @@ describe '#run' do it 'should process the data with the report cache' do Saulabs::Reportable::ReportCache.should_receive(:process).once.with( @report, - { :limit => 100, :grouping => @report.options[:grouping], :conditions => [], :live_data => false, :end_date => false } + { :limit => 100, :grouping => @report.options[:grouping], :conditions => [], :include => [], :live_data => false, :end_date => false, :distinct => false } ) @report.run end it 'should process the data with the report cache when custom conditions are given' do Saulabs::Reportable::ReportCache.should_receive(:process).once.with( @report, - { :limit => 100, :grouping => @report.options[:grouping], :conditions => { :some => :condition }, :live_data => false, :end_date => false } + { :limit => 100, :grouping => @report.options[:grouping], :conditions => { :some => :condition }, :include => [], :live_data => false, :end_date => false, :distinct => false } ) @report.run(:conditions => { :some => :condition }) end @@ -45,11 +45,11 @@ it 'should use a custom grouping if one is specified' do grouping = Saulabs::Reportable::Grouping.new(:month) Saulabs::Reportable::Grouping.should_receive(:new).once.with(:month).and_return(grouping) Saulabs::Reportable::ReportCache.should_receive(:process).once.with( @report, - { :limit => 100, :grouping => grouping, :conditions => [], :live_data => false, :end_date => false } + { :limit => 100, :grouping => grouping, :conditions => [], :live_data => false, :end_date => false, :distinct => false, :include => [] } ) @report.run(:grouping => :month) end @@ -63,21 +63,158 @@ @report = Saulabs::Reportable::Report.new(User, :cumulated_registrations, :limit => 10, :live_data => true) @report.run.to_a.length.should == 11 end - for grouping in [:hour, :day, :week, :month] do + %w(hour day week month).each do |grouping| + grouping = grouping.to_sym describe "for grouping :#{grouping.to_s}" do before(:all) do - User.create!(:login => 'test 1', :created_at => Time.now, :profile_visits => 2) - User.create!(:login => 'test 2', :created_at => Time.now - 1.send(grouping), :profile_visits => 1) - User.create!(:login => 'test 3', :created_at => Time.now - 3.send(grouping), :profile_visits => 2) - User.create!(:login => 'test 4', :created_at => Time.now - 3.send(grouping), :profile_visits => 3) + User.create!(:login => 'test 1', :created_at => Time.now, :profile_visits => 2, :sub_type => "red") + User.create!(:login => 'test 2', :created_at => Time.now - 1.send(grouping), :profile_visits => 1, :sub_type => "red") + User.create!(:login => 'test 3', :created_at => Time.now - 3.send(grouping), :profile_visits => 2, :sub_type => "blue") + User.create!(:login => 'test 4', :created_at => Time.now - 3.send(grouping), :profile_visits => 3, :sub_type => "blue") end + describe 'optimized querying with contiguously cached data' do + it "should be optimized with specified end_date" do + @end_date = DateTime.now - 1.send(grouping) + @report = Saulabs::Reportable::Report.new(User, :registrations, + :grouping => grouping, + :limit => 10, + :end_date => @end_date + ) + @result = @report.run + + Saulabs::Reportable::ReportCache.last.delete + + grouping_instance = Saulabs::Reportable::Grouping.new(grouping) + reporting_period = Saulabs::Reportable::ReportingPeriod.new(grouping_instance, @end_date) + + @report.should_receive(:read_data) do |begin_at, end_at, options| + begin_at.should == reporting_period.date_time + end_at.should == reporting_period.last_date_time + [] # without this rspec whines about an ambiguous return value + end + + @result = @report.run + end + + it "should be optimized without specific end_date and live_data" do + @report = Saulabs::Reportable::Report.new(User, :registrations, + :grouping => grouping, + :limit => 10, + :live_data => true + ) + @result = @report.run.to_a + + Saulabs::Reportable::ReportCache.last.delete + + grouping_instance = Saulabs::Reportable::Grouping.new(grouping) + reporting_period = Saulabs::Reportable::ReportingPeriod.new(grouping_instance, DateTime.now).previous + + @report.should_receive(:read_data) do |begin_at, end_at, options| + begin_at.should == reporting_period.date_time + end_at.should == nil + [] # without this rspec whines about an ambiguous return value + end + + @result = @report.run + end + + it "should be optimized without specific end_date and without live_data requested" do + @report = Saulabs::Reportable::Report.new(User, :registrations, + :grouping => grouping, + :limit => 10 + ) + @result = @report.run.to_a + + Saulabs::Reportable::ReportCache.last.delete + + grouping_instance = Saulabs::Reportable::Grouping.new(grouping) + reporting_period = Saulabs::Reportable::ReportingPeriod.new(grouping_instance, DateTime.now).previous + + @report.should_receive(:read_data) do |begin_at, end_at, options| + begin_at.should == reporting_period.date_time + end_at.should == nil + [] # without this rspec whines about an ambiguous return value + end + + @result = @report.run + end + end + + describe 'non optimized querying when gaps present in cached data' do + it "should not be optimized with specified end_date" do + @end_date = DateTime.now - 1.send(grouping) + @report = Saulabs::Reportable::Report.new(User, :registrations, + :grouping => grouping, + :limit => 10, + :end_date => @end_date + ) + @result = @report.run + + Saulabs::Reportable::ReportCache.first.delete + + grouping_instance = Saulabs::Reportable::Grouping.new(grouping) + reporting_period = Saulabs::Reportable::ReportingPeriod.new(grouping_instance, @end_date) + + @report.should_receive(:read_data) do |begin_at, end_at, options| + begin_at.should == reporting_period.offset(-9).date_time + end_at.should == reporting_period.last_date_time + [] # without this rspec whines about an ambiguous return value + end + + @result = @report.run + end + + it "should not be optimized without specific end_date and live_data" do + @report = Saulabs::Reportable::Report.new(User, :registrations, + :grouping => grouping, + :limit => 10, + :live_data => true + ) + @result = @report.run.to_a + + Saulabs::Reportable::ReportCache.first.delete + + grouping_instance = Saulabs::Reportable::Grouping.new(grouping) + reporting_period = Saulabs::Reportable::ReportingPeriod.new(grouping_instance, DateTime.now).previous + + @report.should_receive(:read_data) do |begin_at, end_at, options| + begin_at.should == reporting_period.offset(-9).date_time + end_at.should == nil + [] # without this rspec whines about an ambiguous return value + end + + @result = @report.run + end + + it "should not be optimized without specific end_date and without live_data requested" do + @report = Saulabs::Reportable::Report.new(User, :registrations, + :grouping => grouping, + :limit => 10 + ) + @result = @report.run.to_a + + Saulabs::Reportable::ReportCache.first.delete + + grouping_instance = Saulabs::Reportable::Grouping.new(grouping) + reporting_period = Saulabs::Reportable::ReportingPeriod.new(grouping_instance, DateTime.now).previous + + @report.should_receive(:read_data) do |begin_at, end_at, options| + begin_at.should == reporting_period.offset(-9).date_time + end_at.should == nil + [] # without this rspec whines about an ambiguous return value + end + + @result = @report.run + end + end + describe 'when :end_date is specified' do it 'should not raise a SQL duplicate key error after multiple runs' do @report = Saulabs::Reportable::Report.new(User, :registrations, :limit => 2, @@ -160,10 +297,28 @@ 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 :count with distinct: true' do + @report = Saulabs::Reportable::Report.new(User, :registrations, + :aggregation => :count, + :grouping => grouping, + :value_column => :sub_type, + :distinct => true, + :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 == 1.0 + result[6][1].should == 0.0 + end + it 'should return correct data for aggregation :sum' do @report = Saulabs::Reportable::Report.new(User, :registrations, :aggregation => :sum, :grouping => grouping, :value_column => :profile_visits, @@ -424,10 +579,10 @@ end describe '#read_data' do - it 'should invoke the aggregation method on the model' do + xit 'should invoke the aggregation method on the model' do @report = Saulabs::Reportable::Report.new(User, :registrations, :aggregation => :count) User.should_receive(:count).once.and_return([]) @report.send(:read_data, Time.now, 5.days.from_now, { :grouping => @report.options[:grouping], :conditions => [] }) end