How to use run method of Reportable Package

Best Minitest_ruby code snippet using Reportable.run

report_spec.rb

Source:report_spec.rb Github

copy

Full Screen

...9 it 'should be frozen' do10 @report.options.should be_frozen11 end12 end13 describe '#run' do14 it 'should process the data with the report cache' do15 Saulabs::Reportable::ReportCache.should_receive(:process).once.with(16 @report,17 { :limit => 100, :grouping => @report.options[:grouping], :conditions => [], :include => [], :live_data => false, :end_date => false, :distinct => false }18 )19 @report.run20 end21 it 'should process the data with the report cache when custom conditions are given' do22 Saulabs::Reportable::ReportCache.should_receive(:process).once.with(23 @report,24 { :limit => 100, :grouping => @report.options[:grouping], :conditions => { :some => :condition }, :include => [], :live_data => false, :end_date => false, :distinct => false }25 )26 @report.run(:conditions => { :some => :condition })27 end28 it 'should validate the specified options for the :run context' do29 @report.should_receive(:ensure_valid_options).once.with({ :limit => 3 }, :run)30 result = @report.run(:limit => 3)31 end32 it 'should use a custom grouping if one is specified' do33 grouping = Saulabs::Reportable::Grouping.new(:month)34 Saulabs::Reportable::Grouping.should_receive(:new).once.with(:month).and_return(grouping)35 Saulabs::Reportable::ReportCache.should_receive(:process).once.with(36 @report,37 { :limit => 100, :grouping => grouping, :conditions => [], :live_data => false, :end_date => false, :distinct => false, :include => [] }38 )39 @report.run(:grouping => :month)40 end41 it 'should return an array of the same length as the specified limit when :live_data is false' do42 @report = Saulabs::Reportable::Report.new(User, :cumulated_registrations, :limit => 10, :live_data => false)43 @report.run.to_a.length.should == 1044 end45 it 'should return an array of the same length as the specified limit + 1 when :live_data is true' do46 @report = Saulabs::Reportable::Report.new(User, :cumulated_registrations, :limit => 10, :live_data => true)47 @report.run.to_a.length.should == 1148 end49 %w(hour day week month).each do |grouping|50 grouping = grouping.to_sym51 describe "for grouping :#{grouping.to_s}" do52 before(:all) do53 User.create!(:login => 'test 1', :created_at => Time.now, :profile_visits => 2, :sub_type => "red")54 User.create!(:login => 'test 2', :created_at => Time.now - 1.send(grouping), :profile_visits => 1, :sub_type => "red")55 User.create!(:login => 'test 3', :created_at => Time.now - 3.send(grouping), :profile_visits => 2, :sub_type => "blue")56 User.create!(:login => 'test 4', :created_at => Time.now - 3.send(grouping), :profile_visits => 3, :sub_type => "blue")57 end58 describe 'optimized querying with contiguously cached data' do59 it "should be optimized with specified end_date" do60 @end_date = DateTime.now - 1.send(grouping)61 @report = Saulabs::Reportable::Report.new(User, :registrations,62 :grouping => grouping,63 :limit => 10,64 :end_date => @end_date65 )66 @result = @report.run67 Saulabs::Reportable::ReportCache.last.delete68 grouping_instance = Saulabs::Reportable::Grouping.new(grouping)69 reporting_period = Saulabs::Reportable::ReportingPeriod.new(grouping_instance, @end_date)70 @report.should_receive(:read_data) do |begin_at, end_at, options|71 begin_at.should == reporting_period.date_time72 end_at.should == reporting_period.last_date_time73 [] # without this rspec whines about an ambiguous return value74 end75 @result = @report.run76 end77 it "should be optimized without specific end_date and live_data" do78 @report = Saulabs::Reportable::Report.new(User, :registrations,79 :grouping => grouping,80 :limit => 10,81 :live_data => true82 )83 @result = @report.run.to_a84 Saulabs::Reportable::ReportCache.last.delete85 grouping_instance = Saulabs::Reportable::Grouping.new(grouping)86 reporting_period = Saulabs::Reportable::ReportingPeriod.new(grouping_instance, DateTime.now).previous87 @report.should_receive(:read_data) do |begin_at, end_at, options|88 begin_at.should == reporting_period.date_time89 end_at.should == nil90 [] # without this rspec whines about an ambiguous return value91 end92 @result = @report.run93 end94 it "should be optimized without specific end_date and without live_data requested" do95 @report = Saulabs::Reportable::Report.new(User, :registrations,96 :grouping => grouping,97 :limit => 1098 )99 @result = @report.run.to_a100 Saulabs::Reportable::ReportCache.last.delete101 grouping_instance = Saulabs::Reportable::Grouping.new(grouping)102 reporting_period = Saulabs::Reportable::ReportingPeriod.new(grouping_instance, DateTime.now).previous103 @report.should_receive(:read_data) do |begin_at, end_at, options|104 begin_at.should == reporting_period.date_time105 end_at.should == nil106 [] # without this rspec whines about an ambiguous return value107 end108 @result = @report.run109 end110 end111 describe 'non optimized querying when gaps present in cached data' do112 it "should not be optimized with specified end_date" do113 @end_date = DateTime.now - 1.send(grouping)114 @report = Saulabs::Reportable::Report.new(User, :registrations,115 :grouping => grouping,116 :limit => 10,117 :end_date => @end_date118 )119 @result = @report.run120 Saulabs::Reportable::ReportCache.first.delete121 grouping_instance = Saulabs::Reportable::Grouping.new(grouping)122 reporting_period = Saulabs::Reportable::ReportingPeriod.new(grouping_instance, @end_date)123 @report.should_receive(:read_data) do |begin_at, end_at, options|124 begin_at.should == reporting_period.offset(-9).date_time125 end_at.should == reporting_period.last_date_time126 [] # without this rspec whines about an ambiguous return value127 end128 @result = @report.run129 end130 it "should not be optimized without specific end_date and live_data" do131 @report = Saulabs::Reportable::Report.new(User, :registrations,132 :grouping => grouping,133 :limit => 10,134 :live_data => true135 )136 @result = @report.run.to_a137 Saulabs::Reportable::ReportCache.first.delete138 grouping_instance = Saulabs::Reportable::Grouping.new(grouping)139 reporting_period = Saulabs::Reportable::ReportingPeriod.new(grouping_instance, DateTime.now).previous140 @report.should_receive(:read_data) do |begin_at, end_at, options|141 begin_at.should == reporting_period.offset(-9).date_time142 end_at.should == nil143 [] # without this rspec whines about an ambiguous return value144 end145 @result = @report.run146 end147 it "should not be optimized without specific end_date and without live_data requested" do148 @report = Saulabs::Reportable::Report.new(User, :registrations,149 :grouping => grouping,150 :limit => 10151 )152 @result = @report.run.to_a153 Saulabs::Reportable::ReportCache.first.delete154 grouping_instance = Saulabs::Reportable::Grouping.new(grouping)155 reporting_period = Saulabs::Reportable::ReportingPeriod.new(grouping_instance, DateTime.now).previous156 @report.should_receive(:read_data) do |begin_at, end_at, options|157 begin_at.should == reporting_period.offset(-9).date_time158 end_at.should == nil159 [] # without this rspec whines about an ambiguous return value160 end161 @result = @report.run162 end163 end164 describe 'when :end_date is specified' do165 it 'should not raise a SQL duplicate key error after multiple runs' do166 @report = Saulabs::Reportable::Report.new(User, :registrations,167 :limit => 2,168 :grouping => grouping,169 :end_date => Date.yesterday.to_datetime170 )171 @report.run172 lambda { @report.run }.should_not raise_error173 end174 describe 'the returned result' do175 before do176 @end_date = DateTime.now - 1.send(grouping)177 @grouping = Saulabs::Reportable::Grouping.new(grouping)178 @report = Saulabs::Reportable::Report.new(User, :registrations,179 :grouping => grouping,180 :limit => 10,181 :end_date => @end_date182 )183 @result = @report.run.to_a184 end185 it "should start with the reporting period (end_date - limit.#{grouping.to_s})" do186 @result.first[0].should == Saulabs::Reportable::ReportingPeriod.new(@grouping, @end_date - 9.send(grouping)).date_time187 end188 it "should end with the reporting period of the specified end date" do189 @result.last[0].should == Saulabs::Reportable::ReportingPeriod.new(@grouping, @end_date).date_time190 end191 end192 end193 [true, false].each do |live_data|194 describe "with :live_data = #{live_data}" do195 describe 'the returned result' do196 before do197 Saulabs::Reportable::ReportCache.delete_all198 @grouping = Saulabs::Reportable::Grouping.new(grouping)199 @report = Saulabs::Reportable::Report.new(User, :registrations,200 :grouping => grouping,201 :limit => 10,202 :live_data => live_data203 )204 @result = @report.run.to_a205 end206 it "should be an array starting reporting period (Time.now - limit.#{grouping.to_s})" do207 @result.first[0].should == Saulabs::Reportable::ReportingPeriod.new(@grouping, Time.now - 10.send(grouping)).date_time208 end209 if live_data210 it "should be data ending with the current reporting period" do211 @result.last[0].should == Saulabs::Reportable::ReportingPeriod.new(@grouping).date_time212 end213 else214 it "should be data ending with the reporting period before the current" do215 @result.last[0].should == Saulabs::Reportable::ReportingPeriod.new(@grouping).previous.date_time216 end217 end218 end219 it 'should return correct data for aggregation :count' do220 @report = Saulabs::Reportable::Report.new(User, :registrations,221 :aggregation => :count,222 :grouping => grouping,223 :limit => 10,224 :live_data => live_data225 )226 result = @report.run.to_a227 result[10][1].should == 1.0 if live_data228 result[9][1].should == 1.0229 result[8][1].should == 0.0230 result[7][1].should == 2.0231 result[6][1].should == 0.0232 end233 it 'should return correct data for aggregation :count with distinct: true' do234 @report = Saulabs::Reportable::Report.new(User, :registrations,235 :aggregation => :count,236 :grouping => grouping,237 :value_column => :sub_type,238 :distinct => true,239 :limit => 10,240 :live_data => live_data241 )242 result = @report.run.to_a243 result[10][1].should == 1.0 if live_data244 result[9][1].should == 1.0245 result[8][1].should == 0.0246 result[7][1].should == 1.0247 result[6][1].should == 0.0248 end249 it 'should return correct data for aggregation :sum' do250 @report = Saulabs::Reportable::Report.new(User, :registrations,251 :aggregation => :sum,252 :grouping => grouping,253 :value_column => :profile_visits,254 :limit => 10,255 :live_data => live_data256 )257 result = @report.run().to_a258 result[10][1].should == 2.0 if live_data259 result[9][1].should == 1.0260 result[8][1].should == 0.0261 result[7][1].should == 5.0262 result[6][1].should == 0.0263 end264 it 'should return correct data for aggregation :maximum' do265 @report = Saulabs::Reportable::Report.new(User, :registrations,266 :aggregation => :maximum,267 :grouping => grouping,268 :value_column => :profile_visits,269 :limit => 10,270 :live_data => live_data271 )272 result = @report.run().to_a273 result[10][1].should == 2.0 if live_data274 result[9][1].should == 1.0275 result[8][1].should == 0.0276 result[7][1].should == 3.0277 result[6][1].should == 0.0278 end279 it 'should return correct data for aggregation :minimum' do280 @report = Saulabs::Reportable::Report.new(User, :registrations,281 :aggregation => :minimum,282 :grouping => grouping,283 :value_column => :profile_visits,284 :limit => 10,285 :live_data => live_data286 )287 result = @report.run().to_a288 result[10][1].should == 2.0 if live_data289 result[9][1].should == 1.0290 result[8][1].should == 0.0291 result[7][1].should == 2.0292 result[6][1].should == 0.0293 end294 it 'should return correct data for aggregation :average' do295 @report = Saulabs::Reportable::Report.new(User, :registrations,296 :aggregation => :average,297 :grouping => grouping,298 :value_column => :profile_visits,299 :limit => 10,300 :live_data => live_data301 )302 result = @report.run().to_a303 result[10][1].should == 2.0 if live_data304 result[9][1].should == 1.0305 result[8][1].should == 0.0306 result[7][1].should == 2.5307 result[6][1].should == 0.0308 end309 it 'should return correct data for aggregation :count when custom conditions are specified' do310 @report = Saulabs::Reportable::Report.new(User, :registrations,311 :aggregation => :count,312 :grouping => grouping,313 :limit => 10,314 :live_data => live_data315 )316 result = @report.run(:conditions => ['login IN (?)', ['test 1', 'test 2', 'test 4']]).to_a317 result[10][1].should == 1.0 if live_data318 result[9][1].should == 1.0319 result[8][1].should == 0.0320 result[7][1].should == 1.0321 result[6][1].should == 0.0322 end323 it 'should return correct data for aggregation :sum when custom conditions are specified' do324 @report = Saulabs::Reportable::Report.new(User, :registrations,325 :aggregation => :sum,326 :grouping => grouping,327 :value_column => :profile_visits,328 :limit => 10,329 :live_data => live_data330 )331 result = @report.run(:conditions => ['login IN (?)', ['test 1', 'test 2', 'test 4']]).to_a332 result[10][1].should == 2.0 if live_data333 result[9][1].should == 1.0334 result[8][1].should == 0.0335 result[7][1].should == 3.0336 result[6][1].should == 0.0337 end338 it 'should return correct results when run twice in a row with a higher limit on the second run' do339 @report = Saulabs::Reportable::Report.new(User, :registrations,340 :aggregation => :count,341 :grouping => grouping,342 :limit => 10,343 :live_data => live_data344 )345 result = @report.run(:limit => 2).to_a346 result[2][1].should == 1.0 if live_data347 result[1][1].should == 1.0348 result[0][1].should == 0.0349 result = @report.run(:limit => 10).to_a350 result[10][1].should == 1.0 if live_data351 result[9][1].should == 1.0352 result[8][1].should == 0.0353 result[7][1].should == 2.0354 result[6][1].should == 0.0355 end356 unless live_data357 it 'should return correct data for aggregation :count when :end_date is specified' do358 @report = Saulabs::Reportable::Report.new(User, :registrations,359 :aggregation => :count,360 :grouping => grouping,361 :limit => 10,362 :end_date => Time.now - 3.send(grouping)363 )364 result = @report.run.to_a365 result[9][1].should == 2.0366 result[8][1].should == 0.0367 result[7][1].should == 0.0368 result[6][1].should == 0.0369 end370 it 'should return correct data for aggregation :sum when :end_date is specified' do371 @report = Saulabs::Reportable::Report.new(User, :registrations,372 :aggregation => :sum,373 :grouping => grouping,374 :value_column => :profile_visits,375 :limit => 10,376 :end_date => Time.now - 3.send(grouping)377 )378 result = @report.run.to_a379 result[9][1].should == 5.0380 result[8][1].should == 0.0381 result[7][1].should == 0.0382 result[6][1].should == 0.0383 end384 it 'should return correct results when run twice in a row with an end date further in the past on the second run' do385 @report = Saulabs::Reportable::Report.new(User, :registrations,386 :aggregation => :count,387 :grouping => grouping,388 :limit => 10,389 :live_data => live_data390 )391 result = @report.run(:end_date => Time.now - 1.send(grouping)).to_a392 result[9][1].should == 1.0393 result[8][1].should == 0.0394 result[7][1].should == 2.0395 result = @report.run(:end_date => Time.now - 3.send(grouping)).to_a396 result[9][1].should == 2.0397 result[8][1].should == 0.0398 result[7][1].should == 0.0399 end400 end401 end402 end403 after(:all) do404 User.destroy_all405 end406 end407 end408 describe 'for grouping week with data ranging over two years' do409 describe 'with the first week of the second year belonging to the first year' do410 before(:all) do411 User.create!(:login => 'test 1', :created_at => DateTime.new(2008, 12, 22))412 User.create!(:login => 'test 2', :created_at => DateTime.new(2008, 12, 29))413 User.create!(:login => 'test 3', :created_at => DateTime.new(2009, 1, 4))414 User.create!(:login => 'test 4', :created_at => DateTime.new(2009, 1, 5))415 User.create!(:login => 'test 5', :created_at => DateTime.new(2009, 1, 12))416 Time.stub!(:now).and_return(DateTime.new(2009, 1, 25))417 end418 it 'should return correct data for aggregation :count' do419 @report = Saulabs::Reportable::Report.new(User, :registrations,420 :aggregation => :count,421 :grouping => :week,422 :limit => 10423 )424 result = @report.run.to_a425 result[9][1].should == 0.0426 result[8][1].should == 1.0427 result[7][1].should == 1.0428 result[6][1].should == 2.0429 result[5][1].should == 1.0430 end431 end432 describe 'with the first week of the second year belonging to the second year' do433 before(:all) do434 User.create!(:login => 'test 1', :created_at => DateTime.new(2009, 12, 21))435 User.create!(:login => 'test 2', :created_at => DateTime.new(2009, 12, 28))436 User.create!(:login => 'test 3', :created_at => DateTime.new(2010, 1, 3))437 User.create!(:login => 'test 4', :created_at => DateTime.new(2010, 1, 4))438 User.create!(:login => 'test 5', :created_at => DateTime.new(2010, 1, 11))439 Time.stub!(:now).and_return(DateTime.new(2010, 1, 25))440 end441 it 'should return correct data for aggregation :count' do442 @report = Saulabs::Reportable::Report.new(User, :registrations,443 :aggregation => :count,444 :grouping => :week,445 :limit => 10446 )447 result = @report.run.to_a448 result[9][1].should == 0.0449 result[8][1].should == 1.0450 result[7][1].should == 1.0451 result[6][1].should == 2.0452 result[5][1].should == 1.0453 end454 end455 end456 after do457 Saulabs::Reportable::ReportCache.destroy_all458 end459 after(:all) do460 User.destroy_all461 end462 end463 describe '#read_data' do464 xit 'should invoke the aggregation method on the model' do465 @report = Saulabs::Reportable::Report.new(User, :registrations, :aggregation => :count)466 User.should_receive(:count).once.and_return([])467 @report.send(:read_data, Time.now, 5.days.from_now, { :grouping => @report.options[:grouping], :conditions => [] })468 end469 it 'should setup the conditions' do470 @report.should_receive(:setup_conditions).once.and_return([])471 @report.send(:read_data, Time.now, 5.days.from_now, { :grouping => @report.options[:grouping], :conditions => [] })472 end473 end474 describe '#setup_conditions' do475 before do476 @begin_at = Time.now477 @end_at = 5.days.from_now478 @created_at_column_clause = "#{ActiveRecord::Base.connection.quote_table_name('users')}.#{ActiveRecord::Base.connection.quote_column_name('created_at')}"479 end480 it 'should return conditions for date_column BETWEEN begin_at and end_at only when no custom conditions are specified and both begin and end date are specified' do481 @report.send(:setup_conditions, @begin_at, @end_at).should == ["#{@created_at_column_clause} BETWEEN ? AND ?", @begin_at, @end_at]482 end483 it 'should return conditions for date_column >= begin_at when no custom conditions and a begin_at are specified' do484 @report.send(:setup_conditions, @begin_at, nil).should == ["#{@created_at_column_clause} >= ?", @begin_at]485 end486 it 'should return conditions for date_column <= end_at when no custom conditions and a end_at are specified' do487 @report.send(:setup_conditions, nil, @end_at).should == ["#{@created_at_column_clause} <= ?", @end_at]488 end489 it 'should raise an argument error when neither begin_at or end_at are specified' do490 lambda {@report.send(:setup_conditions, nil, nil)}.should raise_error(ArgumentError)491 end492 it 'should return conditions for date_column BETWEEN begin_at and end_date only when an empty Hash of custom conditions is specified' do493 @report.send(:setup_conditions, @begin_at, @end_at, {}).should == ["#{@created_at_column_clause} BETWEEN ? AND ?", @begin_at, @end_at]494 end495 it 'should return conditions for date_column BETWEEN begin_at and end_date only when an empty Array of custom conditions is specified' do496 @report.send(:setup_conditions, @begin_at, @end_at, []).should == ["#{@created_at_column_clause} BETWEEN ? AND ?", @begin_at, @end_at]497 end498 it 'should correctly include custom conditions if they are specified as a Hash' do499 custom_conditions = { :first_name => 'first name', :last_name => 'last name' }500 conditions = @report.send(:setup_conditions, @begin_at, @end_at, custom_conditions)501 # cannot directly check for string equqlity here since hashes are not ordered and so there is no way to now in which order the conditions are added to the SQL clause502 conditions[0].should =~ (/#{ActiveRecord::Base.connection.quote_table_name('users')}.#{ActiveRecord::Base.connection.quote_column_name('first_name')} = #{ActiveRecord::Base.connection.quote('first name')}/)503 conditions[0].should =~ (/#{ActiveRecord::Base.connection.quote_table_name('users')}.#{ActiveRecord::Base.connection.quote_column_name('last_name')} = #{ActiveRecord::Base.connection.quote('last name')}/)504 conditions[0].should =~ (/#{@created_at_column_clause} BETWEEN \? AND \?/)505 conditions[1].should == @begin_at506 conditions[2].should == @end_at507 end508 it 'should correctly include custom conditions if they are specified as an Array' do509 custom_conditions = ['first_name = ? AND last_name = ?', 'first name', 'last name']510 @report.send(:setup_conditions, @begin_at, @end_at, custom_conditions).should == ["first_name = #{ActiveRecord::Base.connection.quote('first name')} AND last_name = #{ActiveRecord::Base.connection.quote('last name')} AND #{@created_at_column_clause} BETWEEN ? AND ?", @begin_at, @end_at]511 end512 end513 describe '#ensure_valid_options' do514 it 'should raise an error if malformed conditions are specified' do515 lambda { @report.send(:ensure_valid_options, { :conditions => 1 }) }.should raise_error(ArgumentError)516 end517 it 'should not raise an error if conditions are specified as an Array' do518 lambda { @report.send(:ensure_valid_options, { :conditions => ['first_name = ?', 'first name'] }) }.should_not raise_error(ArgumentError)519 end520 it 'should not raise an error if conditions are specified as a Hash' do521 lambda { @report.send(:ensure_valid_options, { :conditions => { :first_name => 'first name' } }) }.should_not raise_error(ArgumentError)522 end523 it 'should raise an error if an invalid grouping is specified' do524 lambda { @report.send(:ensure_valid_options, { :grouping => :decade }) }.should raise_error(ArgumentError)525 end526 it 'should raise an error if an end date is specified that is not a DateTime' do527 lambda { @report.send(:ensure_valid_options, { :end_date => 'today' }) }.should raise_error(ArgumentError)528 end529 it 'should raise an error if an end date is specified that is in the future' do530 lambda { @report.send(:ensure_valid_options, { :end_date => (DateTime.now + 1.month) }) }.should raise_error(ArgumentError)531 end532 it 'should raise an error if both an end date and :live_data = true are specified' do533 lambda { @report.send(:ensure_valid_options, { :end_date => DateTime.now, :live_data => true }) }.should raise_error(ArgumentError)534 end535 it 'should not raise an error if both an end date and :live_data = false are specified' do536 lambda { @report.send(:ensure_valid_options, { :end_date => DateTime.now, :live_data => false }) }.should_not raise_error537 end538 describe 'for context :initialize' do539 it 'should not raise an error if valid options are specified' do540 lambda { @report.send(:ensure_valid_options, {541 :limit => 100,542 :aggregation => :count,543 :grouping => :day,544 :date_column => :created_at,545 :value_column => :id,546 :conditions => [],547 :live_data => true548 })549 }.should_not raise_error(ArgumentError)550 end551 it 'should raise an error if an unsupported option is specified' do552 lambda { @report.send(:ensure_valid_options, { :invalid => :option }) }.should raise_error(ArgumentError)553 end554 it 'should raise an error if an invalid aggregation is specified' do555 lambda { @report.send(:ensure_valid_options, { :aggregation => :invalid }) }.should raise_error(ArgumentError)556 end557 it 'should raise an error if aggregation :sum is spesicied but no :value_column' do558 lambda { @report.send(:ensure_valid_options, { :aggregation => :sum }) }.should raise_error(ArgumentError)559 end560 end561 describe 'for context :run' do562 it 'should not raise an error if valid options are specified' do563 lambda { @report.send(:ensure_valid_options, { :limit => 100, :conditions => [], :grouping => :week, :live_data => true }, :run)564 }.should_not raise_error(ArgumentError)565 end566 it 'should raise an error if an unsupported option is specified' do567 lambda { @report.send(:ensure_valid_options, { :aggregation => :sum }, :run) }.should raise_error(ArgumentError)568 end569 end570 end571end...

Full Screen

Full Screen

cumulated_report_spec.rb

Source:cumulated_report_spec.rb Github

copy

Full Screen

2describe Saulabs::Reportable::CumulatedReport do3 before do4 @report = Saulabs::Reportable::CumulatedReport.new(User, :cumulated_registrations)5 end6 describe '#run' do7 it 'should cumulate the data' do8 @report.should_receive(:cumulate).once9 @report.run10 end11 it 'should return an array of the same length as the specified limit when :live_data is false' do12 @report = Saulabs::Reportable::CumulatedReport.new(User, :cumulated_registrations, :limit => 10, :live_data => false)13 @report.run.length.should == 1014 end15 it 'should return an array of the same length as the specified limit + 1 when :live_data is true' do16 @report = Saulabs::Reportable::CumulatedReport.new(User, :cumulated_registrations, :limit => 10, :live_data => true)17 @report.run.length.should == 1118 end19 20 for grouping in [:hour, :day, :week, :month] do21 describe "for grouping #{grouping.to_s}" do22 [true, false].each do |live_data|23 describe "with :live_data = #{live_data}" do24 before(:all) do25 User.delete_all26 User.create!(:login => 'test 1', :created_at => Time.now, :profile_visits => 2)27 User.create!(:login => 'test 2', :created_at => Time.now - 1.send(grouping), :profile_visits => 1)28 User.create!(:login => 'test 3', :created_at => Time.now - 3.send(grouping), :profile_visits => 2)29 User.create!(:login => 'test 4', :created_at => Time.now - 3.send(grouping), :profile_visits => 3)30 end31 describe 'the returned result' do32 before do33 @grouping = Saulabs::Reportable::Grouping.new(grouping)34 @report = Saulabs::Reportable::CumulatedReport.new(User, :cumulated_registrations,35 :grouping => grouping,36 :limit => 10,37 :live_data => live_data38 )39 @result = @report.run40 end41 it "should be an array starting reporting period (Time.now - limit.#{grouping.to_s})" do42 @result.first[0].should == Saulabs::Reportable::ReportingPeriod.new(@grouping, Time.now - 10.send(grouping)).date_time43 end44 if live_data45 it "should be data ending with the current reporting period" do46 @result.last[0].should == Saulabs::Reportable::ReportingPeriod.new(@grouping).date_time47 end48 else49 it "should be data ending with the reporting period before the current" do50 @result.last[0].should == Saulabs::Reportable::ReportingPeriod.new(@grouping).previous.date_time51 end52 end53 end54 it 'should return correct data for aggregation :count' do55 @report = Saulabs::Reportable::CumulatedReport.new(User, :registrations,56 :aggregation => :count,57 :grouping => grouping,58 :limit => 10,59 :live_data => live_data60 )61 result = @report.run62 result[10][1].should == 4.0 if live_data63 result[9][1].should == 3.064 result[8][1].should == 2.065 result[7][1].should == 2.066 result[6][1].should == 0.067 end68 it 'should return correct data for aggregation :sum' do69 @report = Saulabs::Reportable::CumulatedReport.new(User, :registrations,70 :aggregation => :sum,71 :grouping => grouping,72 :value_column => :profile_visits,73 :limit => 10,74 :live_data => live_data75 )76 result = @report.run()77 result[10][1].should == 8.0 if live_data78 result[9][1].should == 6.079 result[8][1].should == 5.080 result[7][1].should == 5.081 result[6][1].should == 0.082 end83 it 'should return correct data for aggregation :count when custom conditions are specified' do84 @report = Saulabs::Reportable::CumulatedReport.new(User, :registrations,85 :aggregation => :count,86 :grouping => grouping,87 :limit => 10,88 :live_data => live_data89 )90 result = @report.run(:conditions => ['login IN (?)', ['test 1', 'test 2', 'test 4']])91 result[10][1].should == 3.0 if live_data92 result[9][1].should == 2.093 result[8][1].should == 1.094 result[7][1].should == 1.095 result[6][1].should == 0.096 end97 it 'should return correct data for aggregation :sum when custom conditions are specified' do98 @report = Saulabs::Reportable::CumulatedReport.new(User, :registrations,99 :aggregation => :sum,100 :grouping => grouping,101 :value_column => :profile_visits,102 :limit => 10,103 :live_data => live_data104 )105 result = @report.run(:conditions => ['login IN (?)', ['test 1', 'test 2', 'test 4']])106 result[10][1].should == 6.0 if live_data107 result[9][1].should == 4.0108 result[8][1].should == 3.0109 result[7][1].should == 3.0110 result[6][1].should == 0.0111 end112 end113 after(:all) do114 User.destroy_all115 end116 end117 end118 end119 after(:each) do120 Saulabs::Reportable::ReportCache.destroy_all121 end122 end123 describe '#cumulate' do124 it 'should correctly cumulate the given data' do125 first = (Time.now - 1.week).to_s126 second = Time.now.to_s127 data = [[first, 1], [second, 2]]128 @report.send(:cumulate, data, @report.send(:options_for_run, {})).should == [[first, 1.0], [second, 3.0]]129 end130 end131end...

Full Screen

Full Screen

detail.rb

Source:detail.rb Github

copy

Full Screen

1class Reports::Detail < Reports::Excel2 def initialize(opts = {})3 super(opts)4 @investigations = opts[:investigations] || Investigation.published5 # stud.id, class, school, user.id, username, student name, teachers, completed, %completed, last_run6 @common_columns = [7 Reports::ColumnDefinition.new(:title => "Student ID", :width => 10),8 Reports::ColumnDefinition.new(:title => "Class", :width => 25),9 Reports::ColumnDefinition.new(:title => "School", :width => 25),10 Reports::ColumnDefinition.new(:title => "UserID", :width => 25),11 Reports::ColumnDefinition.new(:title => "Username", :width => 25),12 Reports::ColumnDefinition.new(:title => "Student Name", :width => 25),13 Reports::ColumnDefinition.new(:title => "Teachers", :width => 50),14 Reports::ColumnDefinition.new(:title => "Assessments Completed", :width => 4, :left_border => true),15 Reports::ColumnDefinition.new(:title => "% Completed", :width => 4),16 Reports::ColumnDefinition.new(:title => "Last run", :width => 20),17 ]18 end19 def setup_sheet_for_investigation(inv)20 # Spreadhseet was dying on ":" and "/" chars. Others?21 sheet_name = inv.name.gsub /[^a-zA-z0-9 ]/,"_"22 sheet_name = sheet_name[0..30]23 @inv_sheet[inv] = @book.create_worksheet :name => sheet_name24 sheet_defs = @common_columns.clone25 answer_defs = []26 header_defs = [] # top level header: Investigation27 activities = inv.activities.student_only28 # offset the reportables counter by 229 reportable_header_counter = sheet_defs.size30 header_defs << Reports::ColumnDefinition.new(:title => sheet_name, :heading_row => 0, :col_index => reportable_header_counter)31 activities.each do |a|32 header_defs << Reports::ColumnDefinition.new(:title => a.name, :width=> 30, :heading_row => 0, :col_index => reportable_header_counter)33 reportable_header_counter += 2 # (two columns per activity header)34 end35 reportable_header_counter -= 136 # Itterate Activities37 activities.each do |a|38 sheet_defs << Reports::ColumnDefinition.new(:title => "#{a.name}\nAssessments Completed", :width => 4, :left_border => true)39 sheet_defs << Reports::ColumnDefinition.new(:title => "% Completed", :width => 4)40 reportables = a.reportable_elements.map {|re| re[:embeddable]}41 first = true42 # Itterate Reportables43 reportables.each do |r|44 reportable_header_counter += 145 header_defs << Reports::ColumnDefinition.new(:title => a.name, :heading_row => 0, :col_index => reportable_header_counter)46 answer_defs << Reports::ColumnDefinition.new(:title => clean_text((r.respond_to?(:prompt) ? r.prompt : r.name)), :width => 25, :left_border => first)47 first = false48 end # reportables49 end #activities50 col_defs = sheet_defs + answer_defs + header_defs51 write_sheet_headers(@inv_sheet[inv], col_defs)52 end53 def run_report(stream_or_path, work_book = Spreadsheet::Workbook.new)54 @book = work_book55 @inv_sheet = {}56 @investigations.sort!{|a,b| a.name <=> b.name}57 students = sorted_students_for_runnables(@investigations)58 print "Creating #{@investigations.size} worksheets for report" if @verbose59 @investigations.each do |inv|60 setup_sheet_for_investigation(inv)61 end # investigations62 puts " done." if @verbose63 @report_utils = {} # map of offerings to Report::Util objects64 print "Filling in student data" if @verbose65 iterate_with_status(students) do |student|66 sorted_learners(student).each do |l|67 inv = l.offering.runnable68 next unless @investigations.include?(inv)69 @report_utils[l.offering] ||= Report::Util.new(l.offering,false, true)70 # <=================================================>71 total_assessments = @report_utils[l.offering].embeddables.size72 assess_completed = @report_utils[l.offering].saveables({:learner => l})73 assess_completed = assess_completed.select{|s| s.answered? }.size74 # <=================================================>75 total_by_activity = inv.activities.inject(0) { |a,b| a + b.reportable_elements.size}76 assess_percent = percent(assess_completed, total_assessments)77 last_run = l.bundle_logger.bundle_contents.compact.last78 last_run = last_run.nil? ? 'never' : last_run.created_at79 sheet = @inv_sheet[inv]80 row = sheet.row(sheet.last_row_index + 1)81 assess_completed = "#{assess_completed}/#{total_assessments}(#{total_by_activity})"82 row[0, 3] = learner_info_cells(l) + [assess_completed, assess_percent, last_run]83 all_answers = []84 inv.activities.student_only.each do |a|85 # <=================================================>86 reportables = a.reportable_elements.map {|re| re[:embeddable]}87 answers = reportables.map{|r| @report_utils[l.offering].saveable(l,r)}88 #Bellow is bad, it gets the answers in the wrong order!89 #answers = @report_utils[l.offering].saveables(:learner => l, :embeddables => reportables )90 answered_answers = answers.select{|s| s.answered? }91 # <=================================================>92 # TODO: weed out answers with no length, or which are empty93 row.concat [answered_answers.size, percent(answered_answers.size, reportables.size)]94 all_answers += answers.collect{|ans| 95 if ans.answer.class == Dataservice::Blob96 url = "#{@blobs_url}/#{ans.answer.id.to_s}.blob"...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Minitest_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful