spec/mongoid/report/aggregation_spec.rb in mongoid-report-0.0.8 vs spec/mongoid/report/aggregation_spec.rb in mongoid-report-0.0.9

- old
+ new

@@ -1,13 +1,14 @@ require 'spec_helper' describe Mongoid::Report do - describe '.aggregate_for' do - let(:klass) { Model } - let(:yesterday) { Date.parse("19-12-2004") } - let(:today) { Date.parse("20-12-2004") } + let(:klass) { Model } + let(:yesterday) { Date.parse("19-12-2004") } + let(:today) { Date.parse("20-12-2004") } + let(:two_days_ago) { Date.parse("18-12-2004") } + describe '.aggregate_for' do it 'aggregates fields by default group _id as well' do instance1 = klass.create!(day: today , field1: 1) instance2 = klass.create!(day: today , field1: 1) instance3 = klass.create!(day: yesterday , field1: 1) @@ -36,12 +37,10 @@ expect(rows[0]['day']).to eq(yesterday) expect(rows[1]['field1']).to eq(2) expect(rows[1]['day']).to eq(today) end - let(:two_days_ago) { Date.parse("18-12-2004") } - it 'wraps group query by extra match queries' do klass.create(day: today , field1: 1 , field2: 2) klass.create(day: today , field1: 1 , field2: 2) klass.create(day: yesterday , field1: 1 , field2: 2) klass.create(day: two_days_ago , field1: 1 , field2: 2) @@ -76,6 +75,52 @@ expect(rows.size).to eq(1) expect(rows[0]['field1']).to eq(1) expect(rows[0]['day']).to eq(today) end end + + class Report7 + include Mongoid::Report + + attach_to Model, as: 'example1' do + group_by :day + aggregation_field :field1 + end + + attach_to Model, as: 'example2' do + group_by :day + aggregation_field :field2 + end + end + + describe '.aggregate' do + it 'aggregates all defined groups in the report class' do + klass.create(day: today , field1: 1 , field2: 2) + klass.create(day: today , field1: 1 , field2: 2) + klass.create(day: yesterday , field1: 1 , field2: 2) + klass.create(day: two_days_ago , field1: 1 , field2: 2) + + example = Report7.new + scope = example.aggregate + scope + .query('$match' => { :day => { '$gte' => yesterday.mongoize, '$lte' => today.mongoize } }) + .yield + .query('$sort' => { day: -1 }) + scope = scope.all + + rows = scope['example1'] + expect(rows.size).to eq(2) + expect(rows[0]['field1']).to eq(2) + expect(rows[0]['day']).to eq(today) + expect(rows[1]['field1']).to eq(1) + expect(rows[1]['day']).to eq(yesterday) + + rows = scope['example2'] + expect(rows.size).to eq(2) + expect(rows[0]['field2']).to eq(4) + expect(rows[0]['day']).to eq(today) + expect(rows[1]['field2']).to eq(2) + expect(rows[1]['day']).to eq(yesterday) + end + end + end