reportable 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,335 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ describe Saulabs::Reportable::ReportingPeriod do
4
+
5
+ describe '#date_time' do
6
+
7
+ it 'should return the date and time with minutes = seconds = 0 for grouping :hour' do
8
+ date_time = DateTime.now
9
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:hour), date_time)
10
+
11
+ reporting_period.date_time.should == DateTime.new(date_time.year, date_time.month, date_time.day, date_time.hour, 0, 0)
12
+ end
13
+
14
+ it 'should return the date part only for grouping :day' do
15
+ date_time = DateTime.now
16
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:day), date_time)
17
+
18
+ reporting_period.date_time.should == date_time.to_date
19
+ end
20
+
21
+ describe 'for grouping :week' do
22
+
23
+ it 'should return the date of the monday of the week date_time is in for any day in that week' do
24
+ date_time = DateTime.new(2008, 11, 27)
25
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:week), date_time)
26
+
27
+ reporting_period.date_time.should == Date.new(date_time.year, date_time.month, 24)
28
+ end
29
+
30
+ it 'should return the date of the monday of the week date_time is in when the specified date is a monday already' do
31
+ date_time = DateTime.new(2008, 11, 24)
32
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:week), date_time)
33
+
34
+ reporting_period.date_time.should == Date.new(date_time.year, date_time.month, 24)
35
+ end
36
+
37
+ it 'should return the date of the monday of the week date_time is in when the monday is in a different month than the specified date' do
38
+ date_time = DateTime.new(2008, 11, 1)
39
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:week), date_time)
40
+
41
+ reporting_period.date_time.should == Date.new(2008, 10, 27)
42
+ end
43
+
44
+ it 'should return the date of the monday of the week date_time is in when the monday is in a different year than the specified date' do
45
+ date_time = DateTime.new(2009, 1, 1)
46
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:week), date_time)
47
+
48
+ reporting_period.date_time.should == Date.new(2008, 12, 29)
49
+ end
50
+
51
+ end
52
+
53
+ it 'should return the date with day = 1 for grouping :month' do
54
+ date_time = Time.now
55
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), date_time)
56
+
57
+ reporting_period.date_time.should == Date.new(date_time.year, date_time.month, 1)
58
+ end
59
+
60
+ end
61
+
62
+ describe '#last_date_time' do
63
+
64
+ it 'should return the date and time with minutes = seconds = 59 for grouping :hour' do
65
+ date_time = DateTime.now
66
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:hour), date_time)
67
+
68
+ reporting_period.last_date_time.should == DateTime.new(date_time.year, date_time.month, date_time.day, date_time.hour, 59, 59)
69
+ end
70
+
71
+ it 'should return the date part with hour = 23 and minute = seconds = 59 for grouping :day' do
72
+ date_time = DateTime.now
73
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:day), date_time)
74
+
75
+ reporting_period.last_date_time.should == DateTime.new(date_time.year, date_time.month, date_time.day, 23, 59, 59)
76
+ end
77
+
78
+ describe 'for grouping :week' do
79
+
80
+ it 'should return the date of the sunday of the week date_time is in for any day in that week' do
81
+ date_time = DateTime.new(2008, 11, 27)
82
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:week), date_time)
83
+
84
+ reporting_period.last_date_time.should == Date.new(date_time.year, date_time.month, 30)
85
+ end
86
+
87
+ it 'should return the date of the sunday of the week date_time is in when the sunday is in a different month than the specified date' do
88
+ date_time = DateTime.new(2008, 10, 30)
89
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:week), date_time)
90
+
91
+ reporting_period.last_date_time.should == Date.new(2008, 11, 2)
92
+ end
93
+
94
+ it 'should return the date of the sunday of the week date_time is in when the sunday is in a different year than the specified date' do
95
+ date_time = DateTime.new(2008, 12, 29)
96
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:week), date_time)
97
+
98
+ reporting_period.last_date_time.should == Date.new(2009, 1, 4)
99
+ end
100
+
101
+ end
102
+
103
+ it 'should return the date of the last day of the month for grouping :month' do
104
+ date_time = DateTime.new(2009, 4, 29)
105
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), date_time)
106
+
107
+ reporting_period.last_date_time.should == Date.new(date_time.year, date_time.month, 30)
108
+ end
109
+
110
+ end
111
+
112
+ describe '.from_db_string' do
113
+
114
+ it 'should return a reporting period with the correct date and time and with minutes = seconds = 0 for grouping :hour' do
115
+ grouping = Saulabs::Reportable::Grouping.new(:hour)
116
+ grouping.stub!(:date_parts_from_db_string).and_return([2008, 1, 1, 12])
117
+
118
+ Saulabs::Reportable::ReportingPeriod.from_db_string(grouping, '').date_time.should == DateTime.new(2008, 1, 1, 12, 0, 0)
119
+ end
120
+
121
+ it 'should return a reporting period with the date part only for grouping :day' do
122
+ grouping = Saulabs::Reportable::Grouping.new(:day)
123
+ grouping.stub!(:date_parts_from_db_string).and_return([2008, 1, 1])
124
+
125
+ Saulabs::Reportable::ReportingPeriod.from_db_string(grouping, '').date_time.should == Date.new(2008, 1, 1)
126
+ end
127
+
128
+ it 'should return a reporting period with the date part of the monday of the week the date is in for grouping :week' do
129
+ grouping = Saulabs::Reportable::Grouping.new(:week)
130
+ grouping.stub!(:date_parts_from_db_string).and_return([2008, 1])
131
+
132
+ Saulabs::Reportable::ReportingPeriod.from_db_string(grouping, '').date_time.should == Date.new(2007, 12, 31)
133
+ end
134
+
135
+ it 'should return a reporting period with the correct date and with day = 1 for grouping :month' do
136
+ grouping = Saulabs::Reportable::Grouping.new(:month)
137
+ grouping.stub!(:date_parts_from_db_string).and_return([2008, 1])
138
+
139
+ Saulabs::Reportable::ReportingPeriod.from_db_string(grouping, '').date_time.should == Date.new(2008, 1, 1)
140
+ end
141
+
142
+ end
143
+
144
+ describe '#next' do
145
+
146
+ it 'should return a reporting period with date and time one hour after the current period for grouping :hour' do
147
+ now = Time.now
148
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:hour), now)
149
+ expected = now + 1.hour
150
+
151
+ reporting_period.next.date_time.should == DateTime.new(expected.year, expected.month, expected.day, expected.hour)
152
+ end
153
+
154
+ it 'should return a reporting period with date one day after the current period for grouping :day' do
155
+ now = Time.now
156
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:day), now)
157
+ expected = now + 1.day
158
+
159
+ reporting_period.next.date_time.should == Date.new(expected.year, expected.month, expected.day)
160
+ end
161
+
162
+ it 'should return a reporting period with date one week after the current period for grouping :week' do
163
+ now = DateTime.now
164
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:week), now)
165
+ expected = reporting_period.date_time + 1.week
166
+
167
+ reporting_period.next.date_time.should == Date.new(expected.year, expected.month, expected.day)
168
+ end
169
+
170
+ it 'should return a reporting period with date of the first day in the month one month after the current period' do
171
+ now = Time.now
172
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), now)
173
+ expected = reporting_period.date_time + 1.month
174
+
175
+ reporting_period.next.date_time.should == Date.new(expected.year, expected.month, 1)
176
+ end
177
+
178
+ end
179
+
180
+ describe '#previous' do
181
+
182
+ it 'should return a reporting period with date and time one hour before the current period for grouping :hour' do
183
+ now = Time.now
184
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:hour), now)
185
+ expected = now - 1.hour
186
+
187
+ reporting_period.previous.date_time.should == DateTime.new(expected.year, expected.month, expected.day, expected.hour)
188
+ end
189
+
190
+ it 'should return a reporting period with date one day before the current period for grouping :day' do
191
+ now = Time.now
192
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:day), now)
193
+ expected = now - 1.day
194
+
195
+ reporting_period.previous.date_time.should == Date.new(expected.year, expected.month, expected.day)
196
+ end
197
+
198
+ it 'should return a reporting period with date one week before the current period for grouping :week' do
199
+ now = DateTime.now
200
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:week), now)
201
+ expected = reporting_period.date_time - 1.week
202
+
203
+ reporting_period.previous.date_time.should == Date.new(expected.year, expected.month, expected.day)
204
+ end
205
+
206
+ it 'should return a reporting period with date of the first day in the month one month before the current period' do
207
+ now = Time.now
208
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), now)
209
+ expected = reporting_period.date_time - 1.month
210
+
211
+ reporting_period.previous.date_time.should == Date.new(expected.year, expected.month, 1)
212
+ end
213
+
214
+ end
215
+
216
+ describe '#==' do
217
+
218
+ it 'should return true for 2 reporting periods with the same date_time and grouping' do
219
+ now = DateTime.now
220
+ reporting_period1 = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), now)
221
+ reporting_period2 = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), now)
222
+
223
+ (reporting_period1 == reporting_period2).should == true
224
+ end
225
+
226
+ it 'should return false for 2 reporting periods with the same date_time but different groupings' do
227
+ now = Time.now
228
+ reporting_period1 = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), now)
229
+ reporting_period2 = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:day), now)
230
+
231
+ (reporting_period1 == reporting_period2).should == false
232
+ end
233
+
234
+ it 'should return true for 2 reporting periods with the same grouping but different date_times if the date times evaluate to the same reporting period identifier' do
235
+ reporting_period1 = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), Time.now)
236
+ reporting_period2 = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), Time.now + 1.day)
237
+
238
+ (reporting_period1 == reporting_period2).should == true
239
+ end
240
+
241
+ it 'should return false for 2 reporting periods with the same grouping but different date_times if the date times evaluate to different reporting period identifiers' do
242
+ reporting_period1 = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), Time.now)
243
+ reporting_period2 = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), Time.now + 2.months)
244
+
245
+ (reporting_period1 == reporting_period2).should == false
246
+ end
247
+
248
+ describe 'when invoked with DateTimes or Times' do
249
+
250
+ describe 'for grouping :hour' do
251
+
252
+ it 'should return true when the date and hour are equal' do
253
+ date_time = DateTime.new(2008, 10, 30, 12)
254
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:hour), date_time)
255
+
256
+ reporting_period.should == date_time
257
+ end
258
+
259
+ end
260
+
261
+ describe 'for grouping :day' do
262
+
263
+ it 'should return true when the date is equal' do
264
+ date_time = DateTime.new(2008, 10, 30)
265
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:day), date_time)
266
+
267
+ reporting_period.should == date_time
268
+ end
269
+
270
+ end
271
+
272
+ describe 'for grouping :week' do
273
+
274
+ it 'should return true when the date of the first day in that week is equal' do
275
+ date_time = DateTime.new(2009, 5, 4) #monday (first day of the week for reports_asp_sparkline)
276
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:week), date_time)
277
+
278
+ reporting_period.should == DateTime.new(2009, 5, 7) #thursday of same week, should be equal
279
+ end
280
+
281
+ end
282
+
283
+ describe 'for grouping :month' do
284
+
285
+ it 'should return true when the date of the first day in that month is equal' do
286
+ date_time = DateTime.new(2009, 5, 1)
287
+ reporting_period = Saulabs::Reportable::ReportingPeriod.new(Saulabs::Reportable::Grouping.new(:month), date_time)
288
+
289
+ reporting_period.should == DateTime.new(2009, 5, 17)
290
+ end
291
+
292
+ end
293
+
294
+ end
295
+
296
+ end
297
+
298
+ describe '.first' do
299
+
300
+ before do
301
+ @now = DateTime.now
302
+ DateTime.stub!(:now).and_return(@now)
303
+ end
304
+
305
+ it 'should return a reporting period with the date part of (DateTime.now - limit.hours with minutes = seconds = 0 for grouping :hour' do
306
+ reporting_period = Saulabs::Reportable::ReportingPeriod.first(Saulabs::Reportable::Grouping.new(:hour), 3)
307
+ expected = @now - 3.hours
308
+
309
+ reporting_period.date_time.should == DateTime.new(expected.year, expected.month, expected.day, expected.hour, 0, 0)
310
+ end
311
+
312
+ it 'should return a reporting period with the date part of (DateTime.now - limit.days) for grouping :day' do
313
+ reporting_period = Saulabs::Reportable::ReportingPeriod.first(Saulabs::Reportable::Grouping.new(:day), 3)
314
+ expected = @now - 3.days
315
+
316
+ reporting_period.date_time.should == Date.new(expected.year, expected.month, expected.day)
317
+ end
318
+
319
+ it 'should return a reporting period with the date of the first day of the month at (DateTime.now - limit.months) for grouping :month' do
320
+ DateTime.stub!(:now).and_return(DateTime.new(2008, 12, 31, 0, 0, 0))
321
+ reporting_period = Saulabs::Reportable::ReportingPeriod.first(Saulabs::Reportable::Grouping.new(:month), 3)
322
+
323
+ reporting_period.date_time.should == DateTime.new(2008, 9, 1)
324
+ end
325
+
326
+ it 'should return a reporting period with the date of the monday of the week at (DateTime.now - limit.weeks) for grouping :week' do
327
+ DateTime.stub!(:now).and_return(DateTime.new(2008, 12, 31, 0, 0, 0)) #wednesday
328
+ reporting_period = Saulabs::Reportable::ReportingPeriod.first(Saulabs::Reportable::Grouping.new(:week), 3)
329
+
330
+ reporting_period.date_time.should == DateTime.new(2008, 12, 8) #the monday 3 weeks earlier
331
+ end
332
+
333
+ end
334
+
335
+ end
@@ -0,0 +1,15 @@
1
+ sqlite3:
2
+ adapter: sqlite3
3
+ database: spec/db/reportable.sqlite3.db
4
+ mysql:
5
+ adapter: mysql
6
+ database: reportable_test
7
+ username: reportable
8
+ password: reportable
9
+ host: localhost
10
+ postgresql:
11
+ adapter: postgresql
12
+ database: reportable_test
13
+ username: reportable
14
+ password: reportable
15
+ host: localhost
@@ -0,0 +1,38 @@
1
+ ActiveRecord::Schema.define(:version => 1) do
2
+
3
+ create_table :users, :force => true do |t|
4
+ t.string :login, :null => false
5
+ t.integer :profile_visits, :null => false, :default => 0
6
+ t.string :type, :null => false, :default => 'User'
7
+
8
+ t.timestamps
9
+ end
10
+
11
+ create_table :reportable_cache, :force => true do |t|
12
+ t.string :model_name, :null => false
13
+ t.string :report_name, :null => false
14
+ t.string :grouping, :null => false
15
+ t.string :aggregation, :null => false
16
+ t.string :condition, :null => false
17
+ t.float :value, :null => false, :default => 0
18
+ t.datetime :reporting_period, :null => false
19
+
20
+ t.timestamps
21
+ end
22
+ add_index :reportable_cache, [
23
+ :model_name,
24
+ :report_name,
25
+ :grouping,
26
+ :aggregation,
27
+ :condition
28
+ ], :name => :name_model_grouping_agregation
29
+ add_index :reportable_cache, [
30
+ :model_name,
31
+ :report_name,
32
+ :grouping,
33
+ :aggregation,
34
+ :condition,
35
+ :reporting_period
36
+ ], :unique => true, :name => :name_model_grouping_aggregation_period
37
+
38
+ end
@@ -0,0 +1,44 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ describe Saulabs::Reportable do
4
+
5
+ describe 'for inherited models' do
6
+
7
+ before(:all) do
8
+ User.create!(:login => 'test 1', :created_at => Time.now - 1.days, :profile_visits => 1)
9
+ User.create!(:login => 'test 2', :created_at => Time.now - 2.days, :profile_visits => 2)
10
+ SpecialUser.create!(:login => 'test 3', :created_at => Time.now - 2.days, :profile_visits => 3)
11
+ end
12
+
13
+ it 'should include all data when invoked on the base model class' do
14
+ result = User.registrations_report.to_a
15
+
16
+ result[9][1].should == 1.0
17
+ result[8][1].should == 2.0
18
+ end
19
+
20
+ it 'should include only data for instances of the inherited model when invoked on the inherited model class' do
21
+ result = SpecialUser.registrations_report.to_a
22
+
23
+ result[9][1].should == 0.0
24
+ result[8][1].should == 1.0
25
+ end
26
+
27
+ after(:all) do
28
+ User.destroy_all
29
+ SpecialUser.destroy_all
30
+ end
31
+
32
+ end
33
+
34
+ after do
35
+ Saulabs::Reportable::ReportCache.destroy_all
36
+ end
37
+
38
+ end
39
+
40
+ class User < ActiveRecord::Base
41
+ reportable :registrations, :limit => 10
42
+ end
43
+
44
+ class SpecialUser < User; end
@@ -0,0 +1,64 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ describe Saulabs::Reportable::SparklineTagHelper do
4
+
5
+ before do
6
+ @helper = TestHelper.new
7
+ end
8
+
9
+ describe '#sparkline_tag' do
10
+
11
+ it 'should render an image with the correct source' do
12
+ @helper.should_receive(:image_tag).once.with(
13
+ 'http://chart.apis.google.com/chart?cht=ls&chs=300x34&chd=t:1.0,2.0,3.0&chco=0077cc&chm=B,e6f2fa,0,0,0&chls=1,0,0&chds=1.0,3.0',
14
+ { :title => '', :alt => '' }
15
+ )
16
+
17
+ @helper.sparkline_tag([[DateTime.now, 1.0], [DateTime.now, 2.0], [DateTime.now, 3.0]])
18
+ end
19
+
20
+ it 'should add parameters for labels to the source of the image if rendering of lables is specified' do
21
+ @helper.should_receive(:image_tag).once.with(
22
+ 'http://chart.apis.google.com/chart?cht=ls&chs=300x34&chd=t:1.0,2.0,3.0&chco=0077cc&chm=B,e6f2fa,0,0,0&chls=1,0,0&chds=1.0,3.0&chxt=x,y,r,t&chxr=0,0,3|1,0,3.0|2,0,3.0|3,0,3',
23
+ { :title => '', :alt => '' }
24
+ )
25
+
26
+ @helper.sparkline_tag([[DateTime.now, 1.0], [DateTime.now, 2.0], [DateTime.now, 3.0]], :labels => [:x, :y, :r, :t])
27
+ end
28
+
29
+ it 'should set the parameters for custom colors if custom colors are specified' do
30
+ @helper.should_receive(:image_tag).once.with(
31
+ 'http://chart.apis.google.com/chart?cht=ls&chs=300x34&chd=t:1.0,2.0,3.0&chco=000000&chm=B,ffffff,0,0,0&chls=1,0,0&chds=1.0,3.0',
32
+ { :title => '', :alt => '' }
33
+ )
34
+
35
+ @helper.sparkline_tag([[DateTime.now, 1.0], [DateTime.now, 2.0], [DateTime.now, 3.0]], :line_color => '000000', :fill_color => 'ffffff')
36
+ end
37
+
38
+ it 'should set the parameters for a custom title if a title specified' do
39
+ @helper.should_receive(:image_tag).once.with(
40
+ 'http://chart.apis.google.com/chart?cht=ls&chs=300x34&chd=t:1.0,2.0,3.0&chco=0077cc&chm=B,e6f2fa,0,0,0&chls=1,0,0&chds=1.0,3.0&chtt=title',
41
+ { :title => 'title', :alt => '' }
42
+ )
43
+
44
+ @helper.sparkline_tag([[DateTime.now, 1.0], [DateTime.now, 2.0], [DateTime.now, 3.0]], :title => 'title')
45
+ end
46
+
47
+ it 'should use a specified alt text as alt text for the image' do
48
+ @helper.should_receive(:image_tag).once.with(
49
+ 'http://chart.apis.google.com/chart?cht=ls&chs=300x34&chd=t:1.0,2.0,3.0&chco=0077cc&chm=B,e6f2fa,0,0,0&chls=1,0,0&chds=1.0,3.0',
50
+ { :title => '', :alt => 'alt' }
51
+ )
52
+
53
+ @helper.sparkline_tag([[DateTime.now, 1.0], [DateTime.now, 2.0], [DateTime.now, 3.0]], :alt => 'alt')
54
+ end
55
+
56
+ end
57
+
58
+ end
59
+
60
+ class TestHelper
61
+
62
+ include Saulabs::Reportable::SparklineTagHelper
63
+
64
+ end