spec/mongoid/report/aggregation_spec.rb in mongoid-report-0.0.6 vs spec/mongoid/report/aggregation_spec.rb in mongoid-report-0.0.7
- old
+ new
@@ -11,10 +11,11 @@
instance2 = klass.create!(day: today , field1: 1)
instance3 = klass.create!(day: yesterday , field1: 1)
example = Report2.new
rows = example.aggregate_for(klass)
+ rows = rows.all
expect(rows.size).to eq(3)
expect(rows[0]['field1']).to eq(1)
expect(rows[1]['field1']).to eq(1)
expect(rows[2]['field1']).to eq(1)
@@ -24,11 +25,13 @@
klass.create!(day: today , field1: 1)
klass.create!(day: today , field1: 1)
klass.create!(day: yesterday , field1: 1)
example = Report3.new
+
rows = example.aggregate_for(klass)
+ rows = rows.all
expect(rows.size).to eq(2)
expect(rows[0]['field1']).to eq(1)
expect(rows[0]['day']).to eq(yesterday)
expect(rows[1]['field1']).to eq(2)
@@ -43,17 +46,16 @@
klass.create(day: yesterday , field1: 1 , field2: 2)
klass.create(day: two_days_ago , field1: 1 , field2: 2)
klass.create(day: today , field1: 1 , field2: 3)
example = Report3.new
- rows = example.aggregate_for(Model) do |queries|
- # adding extra queries before the main
- queries.unshift({ '$match' => { :day => { '$gte' => yesterday.mongoize, '$lte' => today.mongoize } } })
- queries.unshift({ '$match' => { :field2 => 2 } })
- # adding sort to the end of aggregation query
- queries.concat([{ '$sort' => { day: -1 } }])
- queries
- end
+ scope = example.aggregate_for(Model)
+ scope = scope.query('$match' => { :day => { '$gte' => yesterday.mongoize, '$lte' => today.mongoize } })
+ scope = scope.query('$match' => { :field2 => 2 })
+ scope = scope.yield
+ scope = scope.query('$sort' => { day: -1 })
+
+ rows = scope.all
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)