mongoid-report 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,8 +8,9 @@ describe Mongoid::Report do
8
8
 
9
9
  describe '.aggregate_for' do
10
10
  it 'aggregates fields by app' do
11
- Report = Class.new do
11
+ report_klass = Class.new do
12
12
  include Mongoid::Report
13
+ def self.name ; 'report-klass' ; end
13
14
 
14
15
  attach_to Model do
15
16
  column :field1
@@ -20,10 +21,11 @@ describe Mongoid::Report do
20
21
  klass.create!(field1: 1)
21
22
  klass.create!(field1: 1)
22
23
 
23
- example = Report.new
24
- report = example.aggregate_for(klass)
25
- report = report.all
26
- rows = report.rows
24
+ example = report_klass.new
25
+ report = example.aggregate_for('report-klass', 'models')
26
+ scoped = report.all
27
+
28
+ rows = scoped.rows
27
29
 
28
30
  expect(rows.size).to eq(1)
29
31
  expect(rows[0]['field1']).to eq(3)
@@ -34,14 +36,16 @@ describe Mongoid::Report do
34
36
  klass.create!(day: today , field1: 1)
35
37
  klass.create!(day: yesterday , field1: 1)
36
38
 
37
- Report = Class.new do
39
+ report_klass = Class.new do
38
40
  include Mongoid::Report
39
- group_by :day, for: Model
40
- column :field1, for: Model
41
+ def self.name ; 'report-klass' ; end
42
+
43
+ group_by :day, collection: Model
44
+ column :field1, collection: Model
41
45
  end
42
- example = Report.new
46
+ example = report_klass.new
43
47
 
44
- report = example.aggregate_for(klass)
48
+ report = example.aggregate_for('report-klass', 'models')
45
49
  report = report.all
46
50
 
47
51
  rows = report.rows
@@ -60,14 +64,16 @@ describe Mongoid::Report do
60
64
  klass.create(day: two_days_ago , field1: 1 , field2: 2)
61
65
  klass.create(day: today , field1: 1 , field2: 3)
62
66
 
63
- Report = Class.new do
67
+ report_klass = Class.new do
64
68
  include Mongoid::Report
65
- group_by :day, for: Model
66
- column :field1, for: Model
69
+ def self.name ; 'report-klass' ; end
70
+
71
+ group_by :day, collection: Model
72
+ column :field1, collection: Model
67
73
  end
68
- example = Report.new
74
+ example = report_klass.new
69
75
 
70
- scope = example.aggregate_for(Model)
76
+ scope = example.aggregate_for('report-klass', 'models')
71
77
  scope = scope.query('$match' => { :day => { '$gte' => yesterday.mongoize, '$lte' => today.mongoize } })
72
78
  scope = scope.query('$match' => { :field2 => 2 })
73
79
  scope = scope.yield
@@ -87,14 +93,16 @@ describe Mongoid::Report do
87
93
  it 'skips empty match in query' do
88
94
  klass.create(day: today , field1: 1 , field2: 2)
89
95
 
90
- Report = Class.new do
96
+ report_klass = Class.new do
91
97
  include Mongoid::Report
92
- group_by :day, for: Model
93
- column :field1, for: Model
98
+ def self.name ; 'report-klass' ; end
99
+
100
+ group_by :day, collection: Model
101
+ column :field1, collection: Model
94
102
  end
95
- example = Report.new
103
+ example = report_klass.new
96
104
 
97
- scope = example.aggregate_for(Model)
105
+ scope = example.aggregate_for('report-klass', 'models')
98
106
  scope = scope.query()
99
107
  scope = scope.query({})
100
108
 
@@ -115,8 +123,9 @@ describe Mongoid::Report do
115
123
  klass.create(day: yesterday , field1: 1 , field2: 2)
116
124
  klass.create(day: two_days_ago , field1: 1 , field2: 2)
117
125
 
118
- Report = Class.new do
126
+ report_klass = Class.new do
119
127
  include Mongoid::Report
128
+ def self.name ; 'report-klass' ; end
120
129
 
121
130
  attach_to Model, as: 'example1' do
122
131
  group_by :day
@@ -129,7 +138,7 @@ describe Mongoid::Report do
129
138
  end
130
139
  end
131
140
 
132
- example = Report.new
141
+ example = report_klass.new
133
142
  scope = example.aggregate
134
143
  scope
135
144
  .query('$match' => { :day => { '$gte' => yesterday.mongoize, '$lte' => today.mongoize } })
@@ -137,14 +146,14 @@ describe Mongoid::Report do
137
146
  .query('$sort' => { day: -1 })
138
147
  scope = scope.all
139
148
 
140
- rows = scope['example1'].rows
149
+ rows = scope['report-klass']['example1'].rows
141
150
  expect(rows.size).to eq(2)
142
151
  expect(rows[0]['field1']).to eq(2)
143
152
  expect(rows[0]['day']).to eq(today)
144
153
  expect(rows[1]['field1']).to eq(1)
145
154
  expect(rows[1]['day']).to eq(yesterday)
146
155
 
147
- rows = scope['example2'].rows
156
+ rows = scope['report-klass']['example2'].rows
148
157
  expect(rows.size).to eq(2)
149
158
  expect(rows[0]['field2']).to eq(4)
150
159
  expect(rows[0]['day']).to eq(today)
@@ -158,8 +167,9 @@ describe Mongoid::Report do
158
167
  klass.create(day: yesterday , field1: 1 , field2: 2)
159
168
  klass.create(day: two_days_ago , field1: 1 , field2: 2)
160
169
 
161
- Report = Class.new do
170
+ report_klass = Class.new do
162
171
  include Mongoid::Report
172
+ def self.name ; 'report-klass' ; end
163
173
 
164
174
  report 'example' do
165
175
  attach_to Model, as: 'model1' do
@@ -173,7 +183,7 @@ describe Mongoid::Report do
173
183
  end
174
184
  end
175
185
  end
176
- example = Report.new
186
+ example = report_klass.new
177
187
 
178
188
  scope = example.aggregate
179
189
  scope
@@ -182,14 +192,14 @@ describe Mongoid::Report do
182
192
  .query('$sort' => { day: -1 })
183
193
  scope = scope.all
184
194
 
185
- rows = scope['example-model1'].rows
195
+ rows = scope['example']['model1'].rows
186
196
  expect(rows.size).to eq(2)
187
197
  expect(rows[0]['field1']).to eq(2)
188
198
  expect(rows[0]['day']).to eq(today)
189
199
  expect(rows[1]['field1']).to eq(1)
190
200
  expect(rows[1]['day']).to eq(yesterday)
191
201
 
192
- rows = scope['example-model2'].rows
202
+ rows = scope['example']['model2'].rows
193
203
  expect(rows.size).to eq(2)
194
204
  expect(rows[0]['field2']).to eq(4)
195
205
  expect(rows[0]['day']).to eq(today)
@@ -203,13 +213,14 @@ describe Mongoid::Report do
203
213
  klass.create(day: yesterday , field1: 1 , field2: 2)
204
214
  klass.create(day: two_days_ago , field1: 1 , field2: 2)
205
215
 
206
- Report = Class.new do
216
+ report_klass = Class.new do
207
217
  include Mongoid::Report
218
+ def self.name ; 'report-klass' ; end
208
219
 
209
220
  report 'example' do
210
221
  attach_to Model, as: 'model1' do
211
222
  group_by :day
212
- column :field1, as: 'new-field1'
223
+ column :field1
213
224
  end
214
225
 
215
226
  attach_to Model, as: 'model2' do
@@ -218,7 +229,7 @@ describe Mongoid::Report do
218
229
  end
219
230
  end
220
231
  end
221
- example = Report.new
232
+ example = report_klass.new
222
233
 
223
234
  scope = example.aggregate
224
235
  scope
@@ -227,14 +238,14 @@ describe Mongoid::Report do
227
238
  .query('$sort' => { day: -1 })
228
239
  scope = scope.all
229
240
 
230
- rows = scope['example-model1'].rows
241
+ rows = scope['example']['model1'].rows
231
242
  expect(rows.size).to eq(2)
232
- expect(rows[0]['new-field1']).to eq(2)
243
+ expect(rows[0]['field1']).to eq(2)
233
244
  expect(rows[0]['day']).to eq(today)
234
- expect(rows[1]['new-field1']).to eq(1)
245
+ expect(rows[1]['field1']).to eq(1)
235
246
  expect(rows[1]['day']).to eq(yesterday)
236
247
 
237
- rows = scope['example-model2'].rows
248
+ rows = scope['example']['model2'].rows
238
249
  expect(rows.size).to eq(2)
239
250
  expect(rows[0]['field2']).to eq(4)
240
251
  expect(rows[0]['day']).to eq(today)
@@ -243,85 +254,89 @@ describe Mongoid::Report do
243
254
  end
244
255
  end
245
256
 
246
- describe '.filter' do
247
- it 'creates filter' do
257
+ describe '.match' do
258
+ it 'creates match' do
248
259
  klass.create(field1: 1, field2: 2)
249
260
  klass.create(field1: 3, field2: 4)
250
261
 
251
- Report = Class.new do
262
+ report_klass = Class.new do
252
263
  include Mongoid::Report
264
+ def self.name ; 'report-klass' ; end
253
265
 
254
- filter field2: 2, for: Model
255
- column :field1, for: Model
266
+ match field2: 2, collection: Model
267
+ column :field1, collection: Model
256
268
  end
257
- example = Report.new
269
+ example = report_klass.new
258
270
 
259
271
  scope = example.aggregate
260
272
  scope = scope.all
261
273
 
262
- rows = scope[Model].rows
274
+ rows = scope['report-klass']['models'].rows
263
275
  expect(rows.size).to eq(1)
264
276
  expect(rows[0]['field1']).to eq(1)
265
277
  end
266
278
 
267
- it 'creates filter in report scope' do
279
+ it 'creates match in report scope' do
268
280
  klass.create(field1: 1, field2: 2)
269
281
  klass.create(field1: 3, field2: 4)
270
282
 
271
- Report = Class.new do
283
+ report_klass = Class.new do
272
284
  include Mongoid::Report
285
+ def self.name ; 'report-klass' ; end
273
286
 
274
287
  report 'example' do
275
288
  attach_to Model do
276
- filter field2: 2
289
+ match field2: 2
277
290
  column :field1
278
291
  end
279
292
  end
280
293
  end
281
294
 
282
- example = Report.new
295
+ example = report_klass.new
283
296
  scope = example.aggregate
284
297
  scope = scope.all
285
298
 
286
- rows = scope['example-models'].rows
299
+ rows = scope['example']['models'].rows
287
300
  expect(rows.size).to eq(1)
288
301
  expect(rows[0]['field1']).to eq(1)
289
302
  end
290
303
 
291
- it 'creates filter in report scope' do
304
+ it 'creates match in report scope' do
292
305
  klass.create(day: today , field1: 1 , field2: 2)
293
306
  klass.create(day: yesterday , field1: 1 , field2: 2)
294
307
  klass.create(day: today , field1: 3 , field2: 4)
295
308
 
296
- Report = Class.new do
309
+ report_klass = Class.new do
297
310
  include Mongoid::Report
311
+ def self.name ; 'report-klass' ; end
298
312
 
299
313
  report 'example' do
300
314
  attach_to Model do
301
- filter field2: 2,
315
+ match field2: 2,
302
316
  day: ->(context) { Date.parse("20-12-2004").mongoize }
303
317
  column :field1
304
318
  end
305
319
  end
306
320
  end
307
- example = Report.new
321
+ example = report_klass.new
308
322
 
309
323
  scope = example.aggregate
310
324
  scope = scope.all
311
325
 
312
- rows = scope['example-models'].rows
326
+ rows = scope['example']['models'].rows
313
327
  expect(rows.size).to eq(1)
314
328
  expect(rows[0]['field1']).to eq(1)
315
329
  end
316
330
 
317
- it 'creates filter in report scope' do
331
+ it 'creates match in report scope' do
318
332
  klass.create(day: today , field1: 1 , field2: 2)
319
333
  klass.create(day: today , field1: 1 , field2: 2)
320
334
  klass.create(day: yesterday , field1: 1 , field2: 2)
321
335
  klass.create(day: today , field1: 3 , field2: 4)
322
336
 
323
- Report = Class.new do
337
+ report_klass = Class.new do
324
338
  include Mongoid::Report
339
+ def self.name ; 'report-klass' ; end
325
340
 
326
341
  def values
327
342
  [1, 2]
@@ -330,17 +345,17 @@ describe Mongoid::Report do
330
345
  report 'example' do
331
346
  attach_to Model do
332
347
  group_by :day
333
- filter field2: ->(context) { { '$in' => context.values } }
348
+ match field2: ->(context) { { '$in' => context.values } }
334
349
  column :field1
335
350
  end
336
351
  end
337
352
  end
338
- example = Report.new
353
+ example = report_klass.new
339
354
 
340
355
  scope = example.aggregate
341
356
  scope = scope.all
342
357
 
343
- rows = scope['example-models'].rows
358
+ rows = scope['example']['models'].rows
344
359
  expect(rows.size).to eq(2)
345
360
  expect(rows[0]['field1']).to eq(1)
346
361
  expect(rows[1]['field1']).to eq(2)
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Report do
4
+ it 'should creates settings in case of missing block' do
5
+ report_klass = Class.new do
6
+ include Mongoid::Report
7
+
8
+ report 'example' do
9
+ attach_to Model do ; end
10
+ end
11
+ end
12
+
13
+ expect(report_klass.settings['example']).to be
14
+ expect(report_klass.settings['example'][:reports]['models']).to be
15
+ end
16
+ end
@@ -5,9 +5,11 @@ describe Mongoid::Report::Collection do
5
5
 
6
6
  describe '.rows' do
7
7
  it 'use returns aggregated rows' do
8
- Report = Class.new do
8
+ report_klass = Class.new do
9
9
  include Mongoid::Report
10
10
 
11
+ def self.name ; 'report-klass' ; end
12
+
11
13
  attach_to Model do
12
14
  column :field1
13
15
  end
@@ -15,9 +17,10 @@ describe Mongoid::Report::Collection do
15
17
 
16
18
  3.times { klass.create!(field1: 1) }
17
19
 
18
- example = Report.new
19
- report = example.aggregate_for(klass)
20
- report = report.all
20
+ example = report_klass.new
21
+ report = example
22
+ .aggregate_for('report-klass', 'models')
23
+ .all
21
24
 
22
25
  rows = report.rows
23
26
  expect(rows.size).to eq(1)
@@ -27,17 +30,19 @@ describe Mongoid::Report::Collection do
27
30
 
28
31
  describe '.headers' do
29
32
  it 'returns columns for showing in the reports' do
30
- Report = Class.new do
33
+ report_klass = Class.new do
31
34
  include Mongoid::Report
32
35
 
36
+ def self.name ; 'report-klass' ; end
37
+
33
38
  attach_to Model do
34
39
  column :field1, :field3, :field2
35
40
  end
36
41
  end
37
42
 
38
- report = Report.new
43
+ report = report_klass.new
39
44
  report = report
40
- .aggregate_for(klass)
45
+ .aggregate_for('report-klass', 'models')
41
46
  .all
42
47
 
43
48
  expect(report.headers).to eq(["field1", "field3", "field2"])
@@ -28,7 +28,7 @@ describe Mongoid::Report do
28
28
  scope = report.aggregate
29
29
  scope = scope.all
30
30
 
31
- rows = scope['example-models'].rows
31
+ rows = scope['example']['models'].rows
32
32
  expect(rows.size).to eq(2)
33
33
  expect(rows[0]['field1']).to eq(1)
34
34
  expect(rows[0]['dynamic-field1']).to eq(10)
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Report do
4
+ let(:klass) { Model }
5
+
6
+ it 'allows to specify dynamic name for attach_to option' do
7
+ report_klass = Class.new do
8
+ include Mongoid::Report
9
+
10
+ report 'example' do
11
+ attach_to 'pregenerated' do
12
+ group_by :field1
13
+ column :field3
14
+ end
15
+
16
+ attach_to Model do
17
+ group_by :field1, :field2
18
+ column :field3
19
+ end
20
+ end
21
+ end
22
+
23
+ klass.create!(field1: 1, field2: 1, field3: 1)
24
+ klass.create!(field1: 1, field2: 2, field3: 1)
25
+
26
+ report = report_klass.new
27
+ scoped = report.aggregate_for('example', 'models')
28
+ scoped = scoped
29
+ .out('new-collection')
30
+ .all
31
+
32
+ expect(scoped.rows.size).to eq(2)
33
+ expect(scoped.summary['field3']).to eq(2)
34
+
35
+ scoped = report.aggregate_for('example', 'pregenerated')
36
+ scoped = scoped
37
+ .in('new-collection')
38
+ .all
39
+
40
+ expect(scoped.rows.size).to eq(1)
41
+ expect(scoped.rows[0]['field3']).to eq(2)
42
+ expect(scoped.summary['field3']).to eq(2)
43
+ end
44
+
45
+
46
+ it 'should be possible to define column and other methods out of the attach_to scope' do
47
+ report_klass = Class.new do
48
+ include Mongoid::Report
49
+
50
+ report 'example' do
51
+ columns :field4 => ->(context, row, options) { row['field3'] }
52
+
53
+ column :field3, :field4
54
+
55
+ attach_to 'pregenerated' do
56
+ group_by :field1
57
+ end
58
+
59
+ attach_to Model do
60
+ group_by :field1, :field2
61
+ end
62
+ end
63
+ end
64
+
65
+ klass.create!(field1: 1, field2: 1, field3: 1)
66
+ klass.create!(field1: 1, field2: 2, field3: 1)
67
+
68
+ report = report_klass.new
69
+ scoped = report.aggregate_for('example', 'models')
70
+ scoped = scoped
71
+ .out('new-collection')
72
+ .all
73
+ expect(scoped.rows.size).to eq(2)
74
+ expect(scoped.summary['field3']).to eq(2)
75
+
76
+ scoped = report.aggregate_for('example', 'pregenerated')
77
+ scoped = scoped
78
+ .in('new-collection')
79
+ .all
80
+
81
+ expect(scoped.rows.size).to eq(1)
82
+ expect(scoped.rows[0]['field3']).to eq(2)
83
+ expect(scoped.rows[0]['field4']).to eq(2)
84
+ expect(scoped.summary['field3']).to eq(2)
85
+ end
86
+ end
@@ -0,0 +1,129 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Report do
4
+ it 'works fine on multiple requests' do
5
+ report_klass = Class.new do
6
+ include Mongoid::Report
7
+
8
+ report 'example' do
9
+ attach_to Model do
10
+ match field2: 2
11
+ column :field1
12
+ end
13
+ end
14
+ end
15
+
16
+ Model.create(field1: 1, field2: 1)
17
+ Model.create(field1: 1, field2: 2)
18
+
19
+ report = report_klass.new
20
+ scoped = report.aggregate_for('example', 'models').all
21
+ queries1 = report.report_module_settings['example'][:reports]['models'][:queries].deep_dup
22
+
23
+ report = report_klass.new
24
+ scoped = report.aggregate_for('example', 'models').all
25
+ queries2 = report.report_module_settings['example'][:reports]['models'][:queries].deep_dup
26
+
27
+ expect(queries1).to eq(queries2)
28
+ end
29
+
30
+ it 'works find on multiple requests with drops of their stats' do
31
+ day1 = Date.parse("2014-01-01")
32
+ 1.times { Model.create(day: day1, field1: 1, field2: 1) }
33
+
34
+ day2 = Date.parse("2014-01-02")
35
+ 2.times { Model.create(day: day2, field1: 1, field2: 2) }
36
+
37
+ day3 = Date.parse("2014-01-03")
38
+ 3.times { Model.create(day: day3, field1: 1, field2: 3) }
39
+
40
+ report_klass = Class.new do
41
+ include Mongoid::Report
42
+
43
+ report 'example' do
44
+ attach_to Model do
45
+ group_by :day
46
+ column :field1, :field2
47
+ end
48
+ end
49
+ end
50
+
51
+ # pregenerate the data
52
+ report = report_klass.new
53
+ scoped = report
54
+ .aggregate_for('example', 'models')
55
+ .query(
56
+ '$match' => {
57
+ 'day' => {
58
+ '$gte' => day1.mongoize,
59
+ '$lte' => day3.mongoize,
60
+ },
61
+ })
62
+ .yield
63
+ .out('new-models', drop: {
64
+ 'day' => {
65
+ '$gte' => day1.mongoize,
66
+ '$lte' => day3.mongoize,
67
+ },
68
+ })
69
+ .all
70
+ report = report_klass.new
71
+ # read the data from output collection
72
+ scoped = report
73
+ .aggregate_for('example', 'models')
74
+ .in('new-models')
75
+ .all
76
+
77
+ expect(scoped.rows.size).to eq(3)
78
+ row = scoped.rows.find { |row| row['day'] == day1 }
79
+ expect(row['field1']).to eq(1)
80
+ expect(row['field2']).to eq(1)
81
+ row = scoped.rows.find { |row| row['day'] == day2 }
82
+ expect(row['field1']).to eq(2)
83
+ expect(row['field2']).to eq(4)
84
+ row = scoped.rows.find { |row| row['day'] == day3 }
85
+ expect(row['field1']).to eq(3)
86
+ expect(row['field2']).to eq(9)
87
+
88
+ Model.where(day: day1).set(field1: 1, field2: 2)
89
+ Model.where(day: day2).set(field1: 2, field2: 3)
90
+ Model.where(day: day3).set(field1: 1, field2: 2)
91
+
92
+ # pregenerate the data
93
+ report = report_klass.new
94
+ scoped = report
95
+ .aggregate_for('example', 'models')
96
+ .query(
97
+ '$match' => {
98
+ 'day' => {
99
+ '$gte' => day1.mongoize,
100
+ '$lte' => day3.mongoize,
101
+ },
102
+ })
103
+ .yield
104
+ .out('new-models', drop: {
105
+ 'day' => {
106
+ '$gte' => day1.mongoize,
107
+ '$lte' => day3.mongoize,
108
+ },
109
+ })
110
+ .all
111
+ # read the data from output collection
112
+ report = report_klass.new
113
+ scoped = report
114
+ .aggregate_for('example', 'models')
115
+ .in('new-models')
116
+ .all
117
+
118
+ expect(scoped.rows.size).to eq(3)
119
+ row = scoped.rows.find { |row| row['day'] == day1 }
120
+ expect(row['field1']).to eq(1)
121
+ expect(row['field2']).to eq(2)
122
+ row = scoped.rows.find { |row| row['day'] == day2 }
123
+ expect(row['field1']).to eq(4)
124
+ expect(row['field2']).to eq(6)
125
+ row = scoped.rows.find { |row| row['day'] == day3 }
126
+ expect(row['field1']).to eq(3)
127
+ expect(row['field2']).to eq(6)
128
+ end
129
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Report do
4
+ it 'should stores global queries for each defined attach_to blocks' do
5
+ report_klass = Class.new do
6
+ include Mongoid::Report
7
+
8
+ report 'example' do
9
+ match field1: 1
10
+
11
+ attach_to Model, as: 'models1' do ; end
12
+ attach_to Model, as: 'models2' do
13
+ column :field2
14
+ end
15
+ end
16
+ end
17
+
18
+ report = report_klass.new
19
+ queries1 = report.report_module_settings['example'][:reports]['models1'][:queries]
20
+ queries2 = report.report_module_settings['example'][:reports]['models2'][:queries]
21
+
22
+ expect(queries1).to eq([
23
+ { '$match' => { :field1 => 1 }},
24
+ { '$project' => { :_id => 1 }},
25
+ { '$group' => { :_id => {} } },
26
+ { '$project' => { :_id => 0 } },
27
+ ])
28
+ expect(queries2).to eq([
29
+ { '$match' => { :field1 => 1 }},
30
+ { '$project' => { :_id => 1, 'field2' => 1 }},
31
+ { '$group' => { :_id => {}, 'field2'=>{'$sum'=>'$field2'} } },
32
+ { '$project' => { :_id => 0, 'field2' => '$field2' } },
33
+ ])
34
+ end
35
+ end