spec/mongoid/report/aggregation_spec.rb in mongoid-report-0.1.9 vs spec/mongoid/report/aggregation_spec.rb in mongoid-report-0.2.0
- old
+ new
@@ -6,44 +6,48 @@
let(:today) { Date.parse("20-12-2004") }
let(:two_days_ago) { Date.parse("18-12-2004") }
describe '.aggregate_for' do
it 'aggregates fields by app' do
- Report = Class.new do
+ report_klass = Class.new do
include Mongoid::Report
+ def self.name ; 'report-klass' ; end
attach_to Model do
column :field1
end
end
klass.create!(field1: 1)
klass.create!(field1: 1)
klass.create!(field1: 1)
- example = Report.new
- report = example.aggregate_for(klass)
- report = report.all
- rows = report.rows
+ example = report_klass.new
+ report = example.aggregate_for('report-klass', 'models')
+ scoped = report.all
+ rows = scoped.rows
+
expect(rows.size).to eq(1)
expect(rows[0]['field1']).to eq(3)
end
it 'aggregates field by defined field of the mode' do
klass.create!(day: today , field1: 1)
klass.create!(day: today , field1: 1)
klass.create!(day: yesterday , field1: 1)
- Report = Class.new do
+ report_klass = Class.new do
include Mongoid::Report
- group_by :day, for: Model
- column :field1, for: Model
+ def self.name ; 'report-klass' ; end
+
+ group_by :day, collection: Model
+ column :field1, collection: Model
end
- example = Report.new
+ example = report_klass.new
- report = example.aggregate_for(klass)
+ report = example.aggregate_for('report-klass', 'models')
report = report.all
rows = report.rows
expect(rows.size).to eq(2)
@@ -58,18 +62,20 @@
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)
klass.create(day: today , field1: 1 , field2: 3)
- Report = Class.new do
+ report_klass = Class.new do
include Mongoid::Report
- group_by :day, for: Model
- column :field1, for: Model
+ def self.name ; 'report-klass' ; end
+
+ group_by :day, collection: Model
+ column :field1, collection: Model
end
- example = Report.new
+ example = report_klass.new
- scope = example.aggregate_for(Model)
+ scope = example.aggregate_for('report-klass', 'models')
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 })
@@ -85,18 +91,20 @@
end
it 'skips empty match in query' do
klass.create(day: today , field1: 1 , field2: 2)
- Report = Class.new do
+ report_klass = Class.new do
include Mongoid::Report
- group_by :day, for: Model
- column :field1, for: Model
+ def self.name ; 'report-klass' ; end
+
+ group_by :day, collection: Model
+ column :field1, collection: Model
end
- example = Report.new
+ example = report_klass.new
- scope = example.aggregate_for(Model)
+ scope = example.aggregate_for('report-klass', 'models')
scope = scope.query()
scope = scope.query({})
scope = scope.all
@@ -113,12 +121,13 @@
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)
- Report = Class.new do
+ report_klass = Class.new do
include Mongoid::Report
+ def self.name ; 'report-klass' ; end
attach_to Model, as: 'example1' do
group_by :day
column :field1
end
@@ -127,26 +136,26 @@
group_by :day
column :field2
end
end
- example = Report.new
+ example = report_klass.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'].rows
+ rows = scope['report-klass']['example1'].rows
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'].rows
+ rows = scope['report-klass']['example2'].rows
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)
@@ -156,12 +165,13 @@
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)
- Report = Class.new do
+ report_klass = Class.new do
include Mongoid::Report
+ def self.name ; 'report-klass' ; end
report 'example' do
attach_to Model, as: 'model1' do
group_by :day
column :field1
@@ -171,27 +181,27 @@
group_by :day
column :field2
end
end
end
- example = Report.new
+ example = report_klass.new
scope = example.aggregate
scope
.query('$match' => { :day => { '$gte' => yesterday.mongoize, '$lte' => today.mongoize } })
.yield
.query('$sort' => { day: -1 })
scope = scope.all
- rows = scope['example-model1'].rows
+ rows = scope['example']['model1'].rows
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['example-model2'].rows
+ rows = scope['example']['model2'].rows
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)
@@ -201,147 +211,152 @@
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)
- Report = Class.new do
+ report_klass = Class.new do
include Mongoid::Report
+ def self.name ; 'report-klass' ; end
report 'example' do
attach_to Model, as: 'model1' do
group_by :day
- column :field1, as: 'new-field1'
+ column :field1
end
attach_to Model, as: 'model2' do
group_by :day
column :field2
end
end
end
- example = Report.new
+ example = report_klass.new
scope = example.aggregate
scope
.query('$match' => { :day => { '$gte' => yesterday.mongoize, '$lte' => today.mongoize } })
.yield
.query('$sort' => { day: -1 })
scope = scope.all
- rows = scope['example-model1'].rows
+ rows = scope['example']['model1'].rows
expect(rows.size).to eq(2)
- expect(rows[0]['new-field1']).to eq(2)
+ expect(rows[0]['field1']).to eq(2)
expect(rows[0]['day']).to eq(today)
- expect(rows[1]['new-field1']).to eq(1)
+ expect(rows[1]['field1']).to eq(1)
expect(rows[1]['day']).to eq(yesterday)
- rows = scope['example-model2'].rows
+ rows = scope['example']['model2'].rows
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
- describe '.filter' do
- it 'creates filter' do
+ describe '.match' do
+ it 'creates match' do
klass.create(field1: 1, field2: 2)
klass.create(field1: 3, field2: 4)
- Report = Class.new do
+ report_klass = Class.new do
include Mongoid::Report
+ def self.name ; 'report-klass' ; end
- filter field2: 2, for: Model
- column :field1, for: Model
+ match field2: 2, collection: Model
+ column :field1, collection: Model
end
- example = Report.new
+ example = report_klass.new
scope = example.aggregate
scope = scope.all
- rows = scope[Model].rows
+ rows = scope['report-klass']['models'].rows
expect(rows.size).to eq(1)
expect(rows[0]['field1']).to eq(1)
end
- it 'creates filter in report scope' do
+ it 'creates match in report scope' do
klass.create(field1: 1, field2: 2)
klass.create(field1: 3, field2: 4)
- Report = Class.new do
+ report_klass = Class.new do
include Mongoid::Report
+ def self.name ; 'report-klass' ; end
report 'example' do
attach_to Model do
- filter field2: 2
+ match field2: 2
column :field1
end
end
end
- example = Report.new
+ example = report_klass.new
scope = example.aggregate
scope = scope.all
- rows = scope['example-models'].rows
+ rows = scope['example']['models'].rows
expect(rows.size).to eq(1)
expect(rows[0]['field1']).to eq(1)
end
- it 'creates filter in report scope' do
+ it 'creates match in report scope' do
klass.create(day: today , field1: 1 , field2: 2)
klass.create(day: yesterday , field1: 1 , field2: 2)
klass.create(day: today , field1: 3 , field2: 4)
- Report = Class.new do
+ report_klass = Class.new do
include Mongoid::Report
+ def self.name ; 'report-klass' ; end
report 'example' do
attach_to Model do
- filter field2: 2,
+ match field2: 2,
day: ->(context) { Date.parse("20-12-2004").mongoize }
column :field1
end
end
end
- example = Report.new
+ example = report_klass.new
scope = example.aggregate
scope = scope.all
- rows = scope['example-models'].rows
+ rows = scope['example']['models'].rows
expect(rows.size).to eq(1)
expect(rows[0]['field1']).to eq(1)
end
- it 'creates filter in report scope' do
+ it 'creates match in report scope' 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: today , field1: 3 , field2: 4)
- Report = Class.new do
+ report_klass = Class.new do
include Mongoid::Report
+ def self.name ; 'report-klass' ; end
def values
[1, 2]
end
report 'example' do
attach_to Model do
group_by :day
- filter field2: ->(context) { { '$in' => context.values } }
+ match field2: ->(context) { { '$in' => context.values } }
column :field1
end
end
end
- example = Report.new
+ example = report_klass.new
scope = example.aggregate
scope = scope.all
- rows = scope['example-models'].rows
+ rows = scope['example']['models'].rows
expect(rows.size).to eq(2)
expect(rows[0]['field1']).to eq(1)
expect(rows[1]['field1']).to eq(2)
end
end