spec/mongoid/report_spec.rb in mongoid-report-0.1.9 vs spec/mongoid/report_spec.rb in mongoid-report-0.2.0
- old
+ new
@@ -3,29 +3,31 @@
describe Mongoid::Report do
describe '.column' do
let(:report_klass) do
Class.new do
+ def self.name ; 'report-klass' ; end
include Mongoid::Report
- column :field1, for: Model
+ column :field1, collection: Model
end
end
it 'defines aggegration settings' do
expect(report_klass).to be_respond_to(:settings)
end
it 'defines aggregation field for specific model to make queries' do
- fields = report_klass.fields(Model)
- expect(fields).to eq({ 'field1' => 'field1' })
+ fields = report_klass.settings['report-klass'][:reports]['models'][:fields]
+ expect(fields).to eq(['field1'])
end
end
- describe '.attach_to' do
+ describe '.attach_to' do
let(:report_klass) do
Class.new do
include Mongoid::Report
+ def self.name ; 'report-klass' ; end
attach_to Model do
column :field1
end
end
end
@@ -33,61 +35,64 @@
it 'defines method in report class to attach report to the model' do
expect(report_klass).to be_respond_to(:attach_to)
end
it 'defines field in terms of attached model' do
- fields = report_klass.fields(Model)
- expect(fields).to eq({ 'field1' => 'field1' })
+ fields = report_klass.settings['report-klass'][:reports]['models'][:fields]
+ expect(fields).to eq(['field1'])
end
end
describe '.group_by' do
let(:report_klass1) do
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
end
it 'defines group by method as class method' do
expect(report_klass1).to be_respond_to(:group_by)
end
it 'stores group by settings under report class' do
- group_by_settings = report_klass1.settings[Model][:group_by]
- expect(group_by_settings).to eq(['day'])
+ settings = report_klass1.settings['report-klass'][:reports]['models'][:group_by]
+ expect(settings).to eq(['day'])
end
let(:report_klass2) do
Class.new do
include Mongoid::Report
+ def self.name ; 'report-klass' ; end
attach_to Model do
group_by :day
column :field1
end
end
end
it 'defines groups in terms of attached model' do
- groups = report_klass2.groups(Model)
- expect(groups).to eq(['day'])
+ settings = report_klass2.settings['report-klass'][:reports]['models'][:group_by]
+ expect(settings).to eq(['day'])
end
end
describe '.as' do
let(:report_klass) do
Class.new do
include Mongoid::Report
+ def self.name ; 'report-klass' ; end
attach_to Model, as: 'example1' do
column :field1
end
end
end
it 'creates settings with "as" name' do
- expect(report_klass.settings).to have_key('example1')
+ expect(report_klass.settings['report-klass'][:reports]).to have_key('example1')
end
end
describe '.report' do
let(:report_klass) do
@@ -104,49 +109,37 @@
end
end
end
it 'creates settings with report-<attached-model-name' do
- expect(report_klass.settings).to have_key('example-model1')
- expect(report_klass.settings).to have_key("example-#{Model.collection.name}")
+ reports = report_klass.settings['example'][:reports].keys
+ expect(reports).to include('model1')
+ expect(reports).to include('models')
end
end
- describe '.column `as` option' do
- let(:report_klass) do
- Class.new do
- include Mongoid::Report
- column :field1, for: Model, as: 'field-name'
- end
- end
-
- it 'creates settings with report-<attached-model-name' do
- expect(report_klass.fields(Model).keys).to eq(['field1'])
- expect(report_klass.fields(Model).values).to eq(['field-name'])
- end
- end
-
-
describe 'two report classes' do
it 'should have different settings' do
- ReportKlass1 = Class.new do
+ report_klass1 = Class.new do
include Mongoid::Report
+ def self.name ; 'report-klass1' ; end
attach_to Model do
column :field1
end
end
- ReportKlass2 = Class.new do
+ report_klass2 = Class.new do
include Mongoid::Report
+ def self.name ; 'report-klass2' ; end
attach_to Model do
column :field2
end
end
- expect(ReportKlass1.settings).not_to eq(ReportKlass2.settings)
+ expect(report_klass1.settings).not_to eq(report_klass2.settings)
end
class ReportKlass
include Mongoid::Report
end
@@ -162,9 +155,40 @@
column :field2
end
end
it 'should have different settings for inherited classes' do
- expect(ReportKlass1.fields(Model)).not_to eq(ReportKlass2.fields(Model))
+ settings1 = ReportKlass1.settings['ReportKlass1'][:reports]['models']
+ settings2 = ReportKlass2.settings['ReportKlass2'][:reports]['models']
+ expect(settings1).not_to eq(settings2)
+ end
+ end
+
+ describe 'empty matchs' do
+ it 'should work fine with passed empty match value' do
+ report_klass = Class.new do
+ include Mongoid::Report
+
+ report 'example' do
+ attach_to 'models' do
+ match field1: {}
+ match field2: 1
+ match field3: { '$in' => [1,2,3] }
+ column :field1, :field2, :field3
+ end
+ end
+ end
+
+ Model.create(field1: 1, field2: 2, field3: 1)
+ Model.create(field1: 1, field2: 1, field3: 2)
+ Model.create(field1: 1, field2: 1, field3: 4)
+
+ report = report_klass.new
+ scope = report.aggregate_for('example', 'models').all
+
+ expect(scope.rows.size).to eq(1)
+ expect(scope.rows[0]['field1']).to eq(1)
+ expect(scope.rows[0]['field2']).to eq(1)
+ expect(scope.rows[0]['field3']).to eq(2)
end
end
end