groupdate 3.2.0 → 6.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_helper.rb DELETED
@@ -1,1207 +0,0 @@
1
- require "bundler/setup"
2
- Bundler.require(:default)
3
- require "minitest/autorun"
4
- require "minitest/pride"
5
- require "logger"
6
- require "active_record"
7
-
8
- Minitest::Test = Minitest::Unit::TestCase unless defined?(Minitest::Test)
9
-
10
- ENV["TZ"] = "UTC"
11
-
12
- # for debugging
13
- # ActiveRecord::Base.logger = Logger.new(STDOUT)
14
-
15
- # rails does this in activerecord/lib/active_record/railtie.rb
16
- ActiveRecord::Base.default_timezone = :utc
17
- ActiveRecord::Base.time_zone_aware_attributes = true
18
-
19
- class User < ActiveRecord::Base
20
- has_many :posts
21
-
22
- def self.groupdate_calculation_methods
23
- [:custom_count, :undefined_calculation]
24
- end
25
-
26
- def self.custom_count
27
- count
28
- end
29
-
30
- def self.unlisted_calculation
31
- count
32
- end
33
- end
34
-
35
- class Post < ActiveRecord::Base
36
- end
37
-
38
- # i18n
39
- I18n.enforce_available_locales = true
40
- I18n.backend.store_translations :de, date: {
41
- abbr_month_names: %w(Jan Feb Mar Apr Mai Jun Jul Aug Sep Okt Nov Dez).unshift(nil)
42
- },
43
- time: {
44
- formats: {special: "%b %e, %Y"}
45
- }
46
-
47
- # migrations
48
- def create_tables
49
- ActiveRecord::Migration.verbose = false
50
-
51
- ActiveRecord::Migration.create_table :users, force: true do |t|
52
- t.string :name
53
- t.integer :score
54
- t.timestamp :created_at
55
- t.date :created_on
56
- end
57
-
58
- ActiveRecord::Migration.create_table :posts, force: true do |t|
59
- t.references :user
60
- t.timestamp :created_at
61
- end
62
- end
63
-
64
- def create_redshift_tables
65
- ActiveRecord::Migration.verbose = false
66
-
67
- if ActiveRecord::Migration.table_exists?(:users)
68
- ActiveRecord::Migration.drop_table(:users, force: :cascade)
69
- end
70
-
71
- if ActiveRecord::Migration.table_exists?(:posts)
72
- ActiveRecord::Migration.drop_table(:posts, force: :cascade)
73
- end
74
-
75
- ActiveRecord::Migration.execute "CREATE TABLE users (id INT IDENTITY(1,1) PRIMARY KEY, name VARCHAR(255), score INT, created_at DATETIME, created_on DATE);"
76
-
77
- ActiveRecord::Migration.execute "CREATE TABLE posts (id INT IDENTITY(1,1) PRIMARY KEY, user_id INT REFERENCES users, created_at DATETIME);"
78
- end
79
-
80
- module TestDatabase
81
- def test_zeros_previous_scope
82
- create_user "2013-05-01"
83
- expected = {
84
- Date.parse("2013-05-01") => 0
85
- }
86
- assert_equal expected, User.where("id = 0").group_by_day(:created_at, range: Date.parse("2013-05-01")..Date.parse("2013-05-01 23:59:59 UTC")).count
87
- end
88
-
89
- def test_order_hour_of_day
90
- assert_equal 23, User.group_by_hour_of_day(:created_at).order("hour_of_day desc").count.keys.first
91
- end
92
-
93
- def test_order_hour_of_day_case
94
- assert_equal 23, User.group_by_hour_of_day(:created_at).order("hour_of_day DESC").count.keys.first
95
- end
96
-
97
- def test_order_hour_of_day_reverse
98
- skip if ActiveRecord::VERSION::MAJOR == 5
99
- assert_equal 23, User.group_by_hour_of_day(:created_at).reverse_order.count.keys.first
100
- end
101
-
102
- def test_order_hour_of_day_order_reverse
103
- skip if ActiveRecord::VERSION::MAJOR == 5
104
- assert_equal 0, User.group_by_hour_of_day(:created_at).order("hour_of_day desc").reverse_order.count.keys.first
105
- end
106
-
107
- def test_table_name
108
- # This test is to ensure there's not an error when using the table
109
- # name as part of the column name.
110
- assert_empty User.group_by_day("users.created_at").count
111
- end
112
-
113
- def test_previous_scopes
114
- create_user "2013-05-01"
115
- assert_empty User.where("id = 0").group_by_day(:created_at).count
116
- end
117
-
118
- def test_where_after
119
- create_user "2013-05-01"
120
- create_user "2013-05-02"
121
- expected = {Date.parse("2013-05-02") => 1}
122
- assert_equal expected, User.group_by_day(:created_at).where("created_at > ?", "2013-05-01").count
123
- end
124
-
125
- def test_group_before
126
- create_user "2013-05-01", 1
127
- create_user "2013-05-02", 2
128
- create_user "2013-05-03", 2
129
- expected = {
130
- [1, Date.parse("2013-05-01")] => 1,
131
- [1, Date.parse("2013-05-02")] => 0,
132
- [1, Date.parse("2013-05-03")] => 0,
133
- [2, Date.parse("2013-05-01")] => 0,
134
- [2, Date.parse("2013-05-02")] => 1,
135
- [2, Date.parse("2013-05-03")] => 1
136
- }
137
- assert_equal expected, User.group(:score).group_by_day(:created_at).order(:score).count
138
- end
139
-
140
- def test_group_after
141
- create_user "2013-05-01", 1
142
- create_user "2013-05-02", 2
143
- create_user "2013-05-03", 2
144
- expected = {
145
- [Date.parse("2013-05-01"), 1] => 1,
146
- [Date.parse("2013-05-02"), 1] => 0,
147
- [Date.parse("2013-05-03"), 1] => 0,
148
- [Date.parse("2013-05-01"), 2] => 0,
149
- [Date.parse("2013-05-02"), 2] => 1,
150
- [Date.parse("2013-05-03"), 2] => 1
151
- }
152
- assert_equal expected, User.group_by_day(:created_at).group(:score).order(:score).count
153
- end
154
-
155
- def test_group_day_of_week
156
- create_user "2013-05-01", 1
157
- create_user "2013-05-02", 2
158
- create_user "2013-05-03", 2
159
- expected = {
160
- [1, 0] => 0,
161
- [1, 1] => 0,
162
- [1, 2] => 0,
163
- [1, 3] => 1,
164
- [1, 4] => 0,
165
- [1, 5] => 0,
166
- [1, 6] => 0,
167
- [2, 0] => 0,
168
- [2, 1] => 0,
169
- [2, 2] => 0,
170
- [2, 3] => 0,
171
- [2, 4] => 1,
172
- [2, 5] => 1,
173
- [2, 6] => 0
174
- }
175
- assert_equal expected, User.group(:score).group_by_day_of_week(:created_at).count
176
- end
177
-
178
- def test_groupdate_multiple
179
- create_user "2013-05-01", 1
180
- expected = {
181
- [Date.parse("2013-05-01"), Date.parse("2013-01-01")] => 1
182
- }
183
- assert_equal expected, User.group_by_day(:created_at).group_by_year(:created_at).count
184
- end
185
-
186
- def test_groupdate_multiple_hour_of_day_day_of_week
187
- create_user "2013-05-01 00:00:00 UTC", 1
188
- expected = {}
189
- 24.times do |i|
190
- 7.times do |j|
191
- expected[[i, j]] = i == 0 && j == 3 ? 1 : 0
192
- end
193
- end
194
- assert_equal expected, User.group_by_hour_of_day(:created_at).group_by_day_of_week(:created_at).count
195
- end
196
-
197
- def test_not_modified
198
- create_user "2013-05-01"
199
- expected = {Date.parse("2013-05-01") => 1}
200
- relation = User.group_by_day(:created_at)
201
- relation.where("created_at > ?", "2013-05-01")
202
- assert_equal expected, relation.count
203
- end
204
-
205
- def test_bad_method
206
- assert_raises(NoMethodError) { User.group_by_day(:created_at).no_such_method }
207
- end
208
-
209
- def test_respond_to_order
210
- assert User.group_by_day(:created_at).respond_to?(:order)
211
- end
212
-
213
- def test_respond_to_bad_method
214
- assert !User.group_by_day(:created_at).respond_to?(:no_such_method)
215
- end
216
-
217
- def test_last
218
- create_user "#{this_year - 3}-01-01"
219
- create_user "#{this_year - 1}-01-01"
220
- expected = {
221
- Date.parse("#{this_year - 2}-01-01") => 0,
222
- Date.parse("#{this_year - 1}-01-01") => 1,
223
- Date.parse("#{this_year}-01-01") => 0
224
- }
225
- assert_equal expected, User.group_by_year(:created_at, last: 3).count
226
- end
227
-
228
- def test_last_date
229
- Time.zone = pt
230
- today = Date.today
231
- create_user today.to_s
232
- this_month = pt.parse(today.to_s).beginning_of_month
233
- last_month = this_month - 1.month
234
- expected = {
235
- last_month.to_date => 0,
236
- this_month.to_date => 1
237
- }
238
- assert_equal expected, call_method(:month, :created_on, last: 2)
239
- ensure
240
- Time.zone = nil
241
- end
242
-
243
- def test_last_hour_of_day
244
- error = assert_raises(ArgumentError) { User.group_by_hour_of_day(:created_at, last: 3).count }
245
- assert_equal "Cannot use last option with hour_of_day", error.message
246
- end
247
-
248
- def test_current
249
- create_user "#{this_year - 3}-01-01"
250
- create_user "#{this_year - 1}-01-01"
251
- expected = {
252
- Date.parse("#{this_year - 2}-01-01") => 0,
253
- Date.parse("#{this_year - 1}-01-01") => 1
254
- }
255
- assert_equal expected, User.group_by_year(:created_at, last: 2, current: false).count
256
- end
257
-
258
- def test_quarter_and_last
259
- today = Date.today
260
- create_user today.to_s
261
- this_quarter = today.to_time.beginning_of_quarter
262
- last_quarter = this_quarter - 3.months
263
- expected = {
264
- last_quarter.to_date => 0,
265
- this_quarter.to_date => 1
266
- }
267
- assert_equal expected, call_method(:quarter, :created_at, last: 2)
268
- end
269
-
270
- def test_format_locale
271
- create_user "2014-10-01"
272
- assert_equal ({"Okt" => 1}), User.group_by_day(:created_at, format: "%b", locale: :de).count
273
- end
274
-
275
- def test_format_locale_by_symbol
276
- create_user "2014-10-01"
277
- assert_equal ({"Okt 1, 2014" => 1}), User.group_by_day(:created_at, format: :special, locale: :de).count
278
- end
279
-
280
- def test_format_locale_global
281
- create_user "2014-10-01"
282
- I18n.locale = :de
283
- assert_equal ({"Okt" => 1}), User.group_by_day(:created_at, format: "%b").count
284
- ensure
285
- I18n.locale = :en
286
- end
287
-
288
- def test_format_multiple_groups
289
- create_user "2014-03-01"
290
- assert_equal ({["Sun", 1] => 1}), User.group_by_week(:created_at, format: "%a").group(:score).count
291
- assert_equal ({[1, "Sun"] => 1}), User.group(:score).group_by_week(:created_at, format: "%a").count
292
- end
293
-
294
- # permit
295
-
296
- def test_permit
297
- error = assert_raises(ArgumentError) { User.group_by_period(:day, :created_at, permit: %w(week)).count }
298
- assert_equal "Unpermitted period", error.message
299
- end
300
-
301
- def test_permit_bad_period
302
- error = assert_raises(ArgumentError) { User.group_by_period(:bad_period, :created_at).count }
303
- assert_equal "Unpermitted period", error.message
304
- end
305
-
306
- def test_permit_symbol_symbols
307
- assert_equal ({}), User.group_by_period(:day, :created_at, permit: [:day]).count
308
- end
309
-
310
- def test_permit_string_symbols
311
- assert_equal ({}), User.group_by_period("day", :created_at, permit: [:day]).count
312
- end
313
-
314
- def test_permit_symbol_strings
315
- assert_equal ({}), User.group_by_period(:day, :created_at, permit: %w(day)).count
316
- end
317
-
318
- def test_permit_string_strings
319
- assert_equal ({}), User.group_by_period("day", :created_at, permit: %w(day)).count
320
- end
321
-
322
- # default value
323
-
324
- def test_default_value
325
- create_user "#{this_year}-01-01"
326
- expected = {
327
- Date.parse("#{this_year - 1}-01-01") => nil,
328
- Date.parse("#{this_year}-01-01") => 1
329
- }
330
- assert_equal expected, User.group_by_year(:created_at, last: 2, default_value: nil).count
331
- end
332
-
333
- # associations
334
-
335
- def test_associations
336
- user = create_user("2014-03-01")
337
- user.posts.create!(created_at: "2014-04-01 00:00:00 UTC")
338
- expected = {
339
- Date.parse("2014-04-01") => 1
340
- }
341
- assert_equal expected, user.posts.group_by_day(:created_at).count
342
- end
343
-
344
- def test_associations_period
345
- user = create_user("2014-03-01")
346
- user.posts.create!(created_at: "2014-04-01 00:00:00 UTC")
347
- expected = {
348
- Date.parse("2014-04-01") => 1
349
- }
350
- assert_equal expected, user.posts.group_by_period(:day, :created_at).count
351
- end
352
-
353
- # activerecord default_timezone option
354
-
355
- def test_default_timezone_local
356
- User.default_timezone = :local
357
- assert_raises(Groupdate::Error) { User.group_by_day(:created_at).count }
358
- ensure
359
- User.default_timezone = :utc
360
- end
361
-
362
- # Brasilia Summer Time
363
-
364
- def test_brasilia_summer_time
365
- # must parse and convert to UTC for ActiveRecord 3.1
366
- create_user(brasilia.parse("2014-10-19 02:00:00").utc.to_s)
367
- create_user(brasilia.parse("2014-10-20 02:00:00").utc.to_s)
368
- expected = {
369
- Date.parse("2014-10-19") => 1,
370
- Date.parse("2014-10-20") => 1
371
- }
372
- assert_equal expected, call_method(:day, :created_at, time_zone: "Brasilia")
373
- end
374
-
375
- # carry_forward option
376
-
377
- def test_carry_forward
378
- create_user "2014-05-01"
379
- create_user "2014-05-01"
380
- create_user "2014-05-03"
381
- assert_equal 2, User.group_by_day(:created_at, carry_forward: true).count[Date.parse("2014-05-02")]
382
- end
383
-
384
- # no column
385
-
386
- def test_no_column
387
- assert_raises(ArgumentError) { User.group_by_day.first }
388
- end
389
-
390
- # custom model calculation methods
391
-
392
- def test_custom_model_calculation_method
393
- create_user "2014-05-01"
394
- create_user "2014-05-01"
395
- create_user "2014-05-03"
396
-
397
- expected = {
398
- Date.parse("2014-05-01") => 2,
399
- Date.parse("2014-05-02") => 0,
400
- Date.parse("2014-05-03") => 1
401
- }
402
-
403
- assert_equal expected, User.group_by_day(:created_at).custom_count
404
- end
405
-
406
- def test_using_unlisted_calculation_method_returns_new_series_instance
407
- assert_instance_of Groupdate::Series, User.group_by_day(:created_at).unlisted_calculation
408
- end
409
-
410
- def test_using_listed_but_undefined_custom_calculation_method_raises_error
411
- assert_raises(NoMethodError) do
412
- User.group_by_day(:created_at).undefined_calculation
413
- end
414
- end
415
-
416
- private
417
-
418
- def call_method(method, field, options)
419
- User.group_by_period(method, field, options).count
420
- end
421
-
422
- def create_user(created_at, score = 1)
423
- user =
424
- User.create!(
425
- name: "Andrew",
426
- score: score,
427
- created_at: created_at ? utc.parse(created_at) : nil,
428
- created_on: created_at ? Date.parse(created_at) : nil
429
- )
430
-
431
- # hack for Redshift adapter, which doesn't return id on creation...
432
- user = User.last if user.id.nil?
433
-
434
- # hack for MySQL & Redshift adapters
435
- user.update_attributes(created_at: nil, created_on: nil) if created_at.nil? && is_redshift?
436
-
437
- user
438
- end
439
-
440
- def is_redshift?
441
- ActiveRecord::Base.connection.adapter_name == "Redshift"
442
- end
443
-
444
- def teardown
445
- User.delete_all
446
- end
447
-
448
- def enumerable_test?
449
- false
450
- end
451
- end
452
-
453
- module TestGroupdate
454
- def setup
455
- Groupdate.week_start = :sun
456
- end
457
-
458
- # second
459
-
460
- def test_second_end_of_second
461
- if enumerable_test? || ActiveRecord::Base.connection.adapter_name == "Mysql2"
462
- skip # no millisecond precision
463
- else
464
- assert_result_time :second, "2013-05-03 00:00:00 UTC", "2013-05-03 00:00:00.999"
465
- end
466
- end
467
-
468
- def test_second_start_of_second
469
- assert_result_time :second, "2013-05-03 00:00:01 UTC", "2013-05-03 00:00:01.000"
470
- end
471
-
472
- # minute
473
-
474
- def test_minute_end_of_minute
475
- assert_result_time :minute, "2013-05-03 00:00:00 UTC", "2013-05-03 00:00:59"
476
- end
477
-
478
- def test_minute_start_of_minute
479
- assert_result_time :minute, "2013-05-03 00:01:00 UTC", "2013-05-03 00:01:00"
480
- end
481
-
482
- # hour
483
-
484
- def test_hour_end_of_hour
485
- assert_result_time :hour, "2013-05-03 00:00:00 UTC", "2013-05-03 00:59:59"
486
- end
487
-
488
- def test_hour_start_of_hour
489
- assert_result_time :hour, "2013-05-03 01:00:00 UTC", "2013-05-03 01:00:00"
490
- end
491
-
492
- # day
493
-
494
- def test_day_end_of_day
495
- assert_result_date :day, "2013-05-03", "2013-05-03 23:59:59"
496
- end
497
-
498
- def test_day_start_of_day
499
- assert_result_date :day, "2013-05-04", "2013-05-04 00:00:00"
500
- end
501
-
502
- def test_day_end_of_day_with_time_zone
503
- assert_result_date :day, "2013-05-02", "2013-05-03 06:59:59", true
504
- end
505
-
506
- def test_day_start_of_day_with_time_zone
507
- assert_result_date :day, "2013-05-03", "2013-05-03 07:00:00", true
508
- end
509
-
510
- # day hour starts at 2 am
511
-
512
- def test_test_day_end_of_day_day_start_2am
513
- assert_result_date :day, "2013-05-03", "2013-05-04 01:59:59", false, day_start: 2
514
- end
515
-
516
- def test_test_day_start_of_day_day_start_2am
517
- assert_result_date :day, "2013-05-03", "2013-05-03 02:00:00", false, day_start: 2
518
- end
519
-
520
- def test_test_day_end_of_day_with_time_zone_day_start_2am
521
- assert_result_date :day, "2013-05-03", "2013-05-04 07:59:59", true, day_start: 2
522
- end
523
-
524
- def test_test_day_start_of_day_with_time_zone_day_start_2am
525
- assert_result_date :day, "2013-05-03", "2013-05-03 09:00:00", true, day_start: 2
526
- end
527
-
528
- # week
529
-
530
- def test_week_end_of_week
531
- assert_result_date :week, "2013-03-17", "2013-03-23 23:59:59"
532
- end
533
-
534
- def test_week_start_of_week
535
- assert_result_date :week, "2013-03-24", "2013-03-24 00:00:00"
536
- end
537
-
538
- def test_week_end_of_week_with_time_zone
539
- assert_result_date :week, "2013-03-10", "2013-03-17 06:59:59", true
540
- end
541
-
542
- def test_week_start_of_week_with_time_zone
543
- assert_result_date :week, "2013-03-17", "2013-03-17 07:00:00", true
544
- end
545
-
546
- # week starting on monday
547
-
548
- def test_week_end_of_week_mon
549
- assert_result_date :week, "2013-03-18", "2013-03-24 23:59:59", false, week_start: :mon
550
- end
551
-
552
- def test_week_start_of_week_mon
553
- assert_result_date :week, "2013-03-25", "2013-03-25 00:00:00", false, week_start: :mon
554
- end
555
-
556
- def test_week_end_of_week_with_time_zone_mon
557
- assert_result_date :week, "2013-03-11", "2013-03-18 06:59:59", true, week_start: :mon
558
- end
559
-
560
- def test_week_start_of_week_with_time_zone_mon
561
- assert_result_date :week, "2013-03-18", "2013-03-18 07:00:00", true, week_start: :mon
562
- end
563
-
564
- # week starting on saturday
565
-
566
- def test_week_end_of_week_sat
567
- assert_result_date :week, "2013-03-16", "2013-03-22 23:59:59", false, week_start: :sat
568
- end
569
-
570
- def test_week_start_of_week_sat
571
- assert_result_date :week, "2013-03-23", "2013-03-23 00:00:00", false, week_start: :sat
572
- end
573
-
574
- def test_week_end_of_week_with_time_zone_sat
575
- assert_result_date :week, "2013-03-09", "2013-03-16 06:59:59", true, week_start: :sat
576
- end
577
-
578
- def test_week_start_of_week_with_time_zone_sat
579
- assert_result_date :week, "2013-03-16", "2013-03-16 07:00:00", true, week_start: :sat
580
- end
581
-
582
- # week starting at 2am
583
-
584
- def test_week_end_of_week_day_start_2am
585
- assert_result_date :week, "2013-03-17", "2013-03-24 01:59:59", false, day_start: 2
586
- end
587
-
588
- def test_week_start_of_week_day_start_2am
589
- assert_result_date :week, "2013-03-17", "2013-03-17 02:00:00", false, day_start: 2
590
- end
591
-
592
- def test_week_end_of_week_day_with_time_zone_start_2am
593
- assert_result_date :week, "2013-03-17", "2013-03-24 08:59:59", true, day_start: 2
594
- end
595
-
596
- def test_week_start_of_week_day_with_time_zone_start_2am
597
- assert_result_date :week, "2013-03-17", "2013-03-17 09:00:00", true, day_start: 2
598
- end
599
-
600
- # month
601
-
602
- def test_month_end_of_month
603
- assert_result_date :month, "2013-05-01", "2013-05-31 23:59:59"
604
- end
605
-
606
- def test_month_start_of_month
607
- assert_result_date :month, "2013-06-01", "2013-06-01 00:00:00"
608
- end
609
-
610
- def test_month_end_of_month_with_time_zone
611
- assert_result_date :month, "2013-05-01", "2013-06-01 06:59:59", true
612
- end
613
-
614
- def test_month_start_of_month_with_time_zone
615
- assert_result_date :month, "2013-06-01", "2013-06-01 07:00:00", true
616
- end
617
-
618
- # month starts at 2am
619
-
620
- def test_month_end_of_month_day_start_2am
621
- assert_result_date :month, "2013-03-01", "2013-04-01 01:59:59", false, day_start: 2
622
- end
623
-
624
- def test_month_start_of_month_day_start_2am
625
- assert_result_date :month, "2013-03-01", "2013-03-01 02:00:00", false, day_start: 2
626
- end
627
-
628
- def test_month_end_of_month_with_time_zone_day_start_2am
629
- assert_result_date :month, "2013-03-01", "2013-04-01 08:59:59", true, day_start: 2
630
- end
631
-
632
- def test_month_start_of_month_with_time_zone_day_start_2am
633
- assert_result_date :month, "2013-03-01", "2013-03-01 10:00:00", true, day_start: 2
634
- end
635
-
636
- # quarter
637
-
638
- def test_quarter_end_of_quarter
639
- assert_result_date :quarter, "2013-04-01", "2013-06-30 23:59:59"
640
- end
641
-
642
- def test_quarter_start_of_quarter
643
- assert_result_date :quarter, "2013-04-01", "2013-04-01 00:00:00"
644
- end
645
-
646
- def test_quarter_end_of_quarter_with_time_zone
647
- assert_result_date :quarter, "2013-04-01", "2013-07-01 06:59:59", true
648
- end
649
-
650
- def test_quarter_start_of_quarter_with_time_zone
651
- assert_result_date :quarter, "2013-04-01", "2013-04-01 07:00:00", true
652
- end
653
-
654
- # quarter starts at 2am
655
-
656
- def test_quarter_end_of_quarter_day_start_2am
657
- assert_result_date :quarter, "2013-04-01", "2013-07-01 01:59:59", false, day_start: 2
658
- end
659
-
660
- def test_quarter_start_of_quarter_day_start_2am
661
- assert_result_date :quarter, "2013-04-01", "2013-04-01 02:00:00", false, day_start: 2
662
- end
663
-
664
- def test_quarter_end_of_quarter_with_time_zone_day_start_2am
665
- assert_result_date :quarter, "2013-01-01", "2013-04-01 08:59:59", true, day_start: 2
666
- end
667
-
668
- def test_quarter_start_of_quarter_with_time_zone_day_start_2am
669
- assert_result_date :quarter, "2013-01-01", "2013-01-01 10:00:00", true, day_start: 2
670
- end
671
-
672
- # year
673
-
674
- def test_year_end_of_year
675
- assert_result_date :year, "2013-01-01", "2013-12-31 23:59:59"
676
- end
677
-
678
- def test_year_start_of_year
679
- assert_result_date :year, "2014-01-01", "2014-01-01 00:00:00"
680
- end
681
-
682
- def test_year_end_of_year_with_time_zone
683
- assert_result_date :year, "2013-01-01", "2014-01-01 07:59:59", true
684
- end
685
-
686
- def test_year_start_of_year_with_time_zone
687
- assert_result_date :year, "2014-01-01", "2014-01-01 08:00:00", true
688
- end
689
-
690
- # year starts at 2am
691
-
692
- def test_year_end_of_year_day_start_2am
693
- assert_result_date :year, "2013-01-01", "2014-01-01 01:59:59", false, day_start: 2
694
- end
695
-
696
- def test_year_start_of_year_day_start_2am
697
- assert_result_date :year, "2013-01-01", "2013-01-01 02:00:00", false, day_start: 2
698
- end
699
-
700
- def test_year_end_of_year_with_time_zone_day_start_2am
701
- assert_result_date :year, "2013-01-01", "2014-01-01 09:59:59", true, day_start: 2
702
- end
703
-
704
- def test_year_start_of_year_with_time_zone_day_start_2am
705
- assert_result_date :year, "2013-01-01", "2013-01-01 10:00:00", true, day_start: 2
706
- end
707
-
708
- # hour of day
709
-
710
- def test_hour_of_day_end_of_hour
711
- assert_result :hour_of_day, 0, "2013-01-01 00:59:59"
712
- end
713
-
714
- def test_hour_of_day_start_of_hour
715
- assert_result :hour_of_day, 1, "2013-01-01 01:00:00"
716
- end
717
-
718
- def test_hour_of_day_end_of_hour_with_time_zone
719
- assert_result :hour_of_day, 0, "2013-01-01 08:59:59", true
720
- end
721
-
722
- def test_hour_of_day_start_of_hour_with_time_zone
723
- assert_result :hour_of_day, 1, "2013-01-01 09:00:00", true
724
- end
725
-
726
- # hour of day starts at 2am
727
-
728
- def test_hour_of_day_end_of_day_day_start_2am
729
- assert_result :hour_of_day, 23, "2013-01-01 01:59:59", false, day_start: 2
730
- end
731
-
732
- def test_hour_of_day_start_of_day_day_start_2am
733
- assert_result :hour_of_day, 0, "2013-01-01 02:00:00", false, day_start: 2
734
- end
735
-
736
- def test_hour_of_day_end_of_day_with_time_zone_day_start_2am
737
- assert_result :hour_of_day, 23, "2013-01-01 09:59:59", true, day_start: 2
738
- end
739
-
740
- def test_hour_of_day_start_of_day_with_time_zone_day_start_2am
741
- assert_result :hour_of_day, 0, "2013-01-01 10:00:00", true, day_start: 2
742
- end
743
-
744
- # day of week
745
-
746
- def test_day_of_week_end_of_day
747
- assert_result :day_of_week, 2, "2013-01-01 23:59:59"
748
- end
749
-
750
- def test_day_of_week_start_of_day
751
- assert_result :day_of_week, 3, "2013-01-02 00:00:00"
752
- end
753
-
754
- def test_day_of_week_end_of_week_with_time_zone
755
- assert_result :day_of_week, 2, "2013-01-02 07:59:59", true
756
- end
757
-
758
- def test_day_of_week_start_of_week_with_time_zone
759
- assert_result :day_of_week, 3, "2013-01-02 08:00:00", true
760
- end
761
-
762
- # day of week starts at 2am
763
-
764
- def test_day_of_week_end_of_day_day_start_2am
765
- assert_result :day_of_week, 3, "2013-01-03 01:59:59", false, day_start: 2
766
- end
767
-
768
- def test_day_of_week_start_of_day_day_start_2am
769
- assert_result :day_of_week, 3, "2013-01-02 02:00:00", false, day_start: 2
770
- end
771
-
772
- def test_day_of_week_end_of_day_with_time_zone_day_start_2am
773
- assert_result :day_of_week, 3, "2013-01-03 09:59:59", true, day_start: 2
774
- end
775
-
776
- def test_day_of_week_start_of_day_with_time_zone_day_start_2am
777
- assert_result :day_of_week, 3, "2013-01-02 10:00:00", true, day_start: 2
778
- end
779
-
780
- # day of month
781
-
782
- def test_day_of_month_end_of_day
783
- assert_result :day_of_month, 31, "2013-01-31 23:59:59"
784
- end
785
-
786
- def test_day_of_month_end_of_day_feb_leap_year
787
- assert_result :day_of_month, 29, "2012-02-29 23:59:59"
788
- end
789
-
790
- def test_day_of_month_start_of_day
791
- assert_result :day_of_month, 3, "2013-01-03 00:00:00"
792
- end
793
-
794
- def test_day_of_month_end_of_day_with_time_zone
795
- assert_result :day_of_month, 31, "2013-02-01 07:59:59", true
796
- end
797
-
798
- def test_day_of_month_start_of_day_with_time_zone
799
- assert_result :day_of_month, 1, "2013-01-01 08:00:00", true
800
- end
801
-
802
- # day of month starts at 2am
803
-
804
- def test_day_of_month_end_of_day_day_start_2am
805
- assert_result :day_of_month, 31, "2013-01-01 01:59:59", false, day_start: 2
806
- end
807
-
808
- def test_day_of_month_start_of_day_day_start_2am
809
- assert_result :day_of_month, 1, "2013-01-01 02:00:00", false, day_start: 2
810
- end
811
-
812
- def test_day_of_month_end_of_day_with_time_zone_day_start_2am
813
- assert_result :day_of_month, 31, "2013-01-01 09:59:59", true, day_start: 2
814
- end
815
-
816
- def test_day_of_month_start_of_day_with_time_zone_day_start_2am
817
- assert_result :day_of_month, 1, "2013-01-01 10:00:00", true, day_start: 2
818
- end
819
-
820
- # month of year
821
-
822
- def test_month_of_year_end_of_month
823
- assert_result :month_of_year, 1, "2013-01-31 23:59:59"
824
- end
825
-
826
- def test_month_of_year_start_of_month
827
- assert_result :month_of_year, 1, "2013-01-01 00:00:00"
828
- end
829
-
830
- def test_month_of_year_end_of_month_with_time_zone
831
- assert_result :month_of_year, 1, "2013-02-01 07:59:59", true
832
- end
833
-
834
- def test_month_of_year_start_of_month_with_time_zone
835
- assert_result :month_of_year, 1, "2013-01-01 08:00:00", true
836
- end
837
-
838
- # month of year starts at 2am
839
-
840
- def test_month_of_year_end_of_month_day_start_2am
841
- assert_result :month_of_year, 12, "2013-01-01 01:59:59", false, day_start: 2
842
- end
843
-
844
- def test_month_of_year_start_of_month_day_start_2am
845
- assert_result :month_of_year, 1, "2013-01-01 02:00:00", false, day_start: 2
846
- end
847
-
848
- def test_month_of_year_end_of_month_with_time_zone_day_start_2am
849
- assert_result :month_of_year, 12, "2013-01-01 09:59:59", true, day_start: 2
850
- end
851
-
852
- def test_month_of_year_start_of_month_with_time_zone_day_start_2am
853
- assert_result :month_of_year, 1, "2013-01-01 10:00:00", true, day_start: 2
854
- end
855
-
856
- # zeros
857
-
858
- def test_zeros_second
859
- assert_zeros :second, "2013-05-01 00:00:01 UTC", ["2013-05-01 00:00:00 UTC", "2013-05-01 00:00:01 UTC", "2013-05-01 00:00:02 UTC"], "2013-05-01 00:00:00.999 UTC", "2013-05-01 00:00:02 UTC"
860
- end
861
-
862
- def test_zeros_minute
863
- assert_zeros :minute, "2013-05-01 00:01:00 UTC", ["2013-05-01 00:00:00 UTC", "2013-05-01 00:01:00 UTC", "2013-05-01 00:02:00 UTC"], "2013-05-01 00:00:59 UTC", "2013-05-01 00:02:00 UTC"
864
- end
865
-
866
- def test_zeros_hour
867
- assert_zeros :hour, "2013-05-01 04:01:01 UTC", ["2013-05-01 03:00:00 UTC", "2013-05-01 04:00:00 UTC", "2013-05-01 05:00:00 UTC"], "2013-05-01 03:59:59 UTC", "2013-05-01 05:00:00 UTC"
868
- end
869
-
870
- def test_zeros_day
871
- assert_zeros_date :day, "2013-05-01 20:00:00 UTC", ["2013-04-30", "2013-05-01", "2013-05-02"], "2013-04-30 00:00:00 UTC", "2013-05-02 23:59:59 UTC"
872
- end
873
-
874
- def test_zeros_day_time_zone
875
- assert_zeros_date :day, "2013-05-01 20:00:00 PDT", ["2013-04-30", "2013-05-01", "2013-05-02"], "2013-04-30 00:00:00 PDT", "2013-05-02 23:59:59 PDT", true
876
- end
877
-
878
- def test_zeros_week
879
- assert_zeros_date :week, "2013-05-01 20:00:00 UTC", ["2013-04-21", "2013-04-28", "2013-05-05"], "2013-04-27 23:59:59 UTC", "2013-05-11 23:59:59 UTC"
880
- end
881
-
882
- def test_zeros_week_time_zone
883
- assert_zeros_date :week, "2013-05-01 20:00:00 PDT", ["2013-04-21", "2013-04-28", "2013-05-05"], "2013-04-27 23:59:59 PDT", "2013-05-11 23:59:59 PDT", true
884
- end
885
-
886
- def test_zeros_week_mon
887
- assert_zeros_date :week, "2013-05-01 20:00:00 UTC", ["2013-04-22", "2013-04-29", "2013-05-06"], "2013-04-27 23:59:59 UTC", "2013-05-11 23:59:59 UTC", false, week_start: :mon
888
- end
889
-
890
- def test_zeros_week_time_zone_mon
891
- assert_zeros_date :week, "2013-05-01 20:00:00 PDT", ["2013-04-22", "2013-04-29", "2013-05-06"], "2013-04-27 23:59:59 PDT", "2013-05-11 23:59:59 PDT", true, week_start: :mon
892
- end
893
-
894
- def test_zeros_week_sat
895
- assert_zeros_date :week, "2013-05-01 20:00:00 UTC", ["2013-04-20", "2013-04-27", "2013-05-04"], "2013-04-26 23:59:59 UTC", "2013-05-10 23:59:59 UTC", false, week_start: :sat
896
- end
897
-
898
- def test_zeros_week_time_zone_sat
899
- assert_zeros_date :week, "2013-05-01 20:00:00 PDT", ["2013-04-20", "2013-04-27", "2013-05-04"], "2013-04-26 23:59:59 PDT", "2013-05-10 23:59:59 PDT", true, week_start: :sat
900
- end
901
-
902
- def test_zeros_month
903
- assert_zeros_date :month, "2013-04-16 20:00:00 UTC", ["2013-03-01", "2013-04-01", "2013-05-01"], "2013-03-01", "2013-05-31 23:59:59 UTC"
904
- end
905
-
906
- def test_zeros_month_time_zone
907
- assert_zeros_date :month, "2013-04-16 20:00:00 PDT", ["2013-03-01", "2013-04-01", "2013-05-01"], "2013-03-01 00:00:00 PST", "2013-05-31 23:59:59 PDT", true
908
- end
909
-
910
- def test_zeros_quarter
911
- assert_zeros_date :quarter, "2013-04-16 20:00:00 UTC", ["2013-01-01", "2013-04-01", "2013-07-01"], "2013-01-01", "2013-09-30 23:59:59 UTC"
912
- end
913
-
914
- def test_zeros_quarter_time_zone
915
- assert_zeros_date :quarter, "2013-04-16 20:00:00 PDT", ["2013-01-01", "2013-04-01", "2013-07-01"], "2013-01-01 00:00:00 PST", "2013-09-30 23:59:59 PDT", true
916
- end
917
-
918
- def test_zeros_year
919
- assert_zeros_date :year, "2013-04-16 20:00:00 UTC", ["2012-01-01", "2013-01-01", "2014-01-01"], "2012-01-01", "2014-12-31 23:59:59 UTC"
920
- end
921
-
922
- def test_zeros_year_time_zone
923
- assert_zeros_date :year, "2013-04-16 20:00:00 PDT", ["2012-01-01 00:00:00 PST", "2013-01-01 00:00:00 PST", "2014-01-01 00:00:00 PST"], "2012-01-01 00:00:00 PST", "2014-12-31 23:59:59 PST", true
924
- end
925
-
926
- def test_zeros_day_of_week
927
- create_user "2013-05-01"
928
- expected = {}
929
- 7.times do |n|
930
- expected[n] = n == 3 ? 1 : 0
931
- end
932
- assert_equal expected, call_method(:day_of_week, :created_at, {series: true})
933
- end
934
-
935
- def test_zeros_hour_of_day
936
- create_user "2013-05-01 20:00:00 UTC"
937
- expected = {}
938
- 24.times do |n|
939
- expected[n] = n == 20 ? 1 : 0
940
- end
941
- assert_equal expected, call_method(:hour_of_day, :created_at, {series: true})
942
- end
943
-
944
- def test_zeros_day_of_month
945
- create_user "1978-12-18"
946
- expected = {}
947
- (1..31).each do |n|
948
- expected[n] = n == 18 ? 1 : 0
949
- end
950
- assert_equal expected, call_method(:day_of_month, :created_at, {series: true})
951
- end
952
-
953
- def test_zeros_month_of_year
954
- create_user "2013-05-01"
955
- expected = {}
956
- (1..12).each do |n|
957
- expected[n] = n == 5 ? 1 : 0
958
- end
959
- assert_equal expected, call_method(:month_of_year, :created_at, {series: true})
960
- end
961
-
962
- def test_zeros_excludes_end
963
- create_user "2013-05-02"
964
- expected = {
965
- Date.parse("2013-05-01") => 0
966
- }
967
- assert_equal expected, call_method(:day, :created_at, range: Date.parse("2013-05-01")...Date.parse("2013-05-02"), series: true)
968
- end
969
-
970
- def test_zeros_datetime
971
- create_user "2013-05-01"
972
- expected = {
973
- Date.parse("2013-05-01") => 1
974
- }
975
- assert_equal expected, call_method(:day, :created_at, range: DateTime.parse("2013-05-01")..DateTime.parse("2013-05-01"), series: true)
976
- end
977
-
978
- def test_zeros_null_value
979
- create_user nil
980
- assert_equal 0, call_method(:hour_of_day, :created_at, range: true, series: true)[0]
981
- end
982
-
983
- def test_zeroes_range_true
984
- create_user "2013-05-01"
985
- create_user "2013-05-03"
986
- expected = {
987
- Date.parse("2013-05-01") => 1,
988
- Date.parse("2013-05-02") => 0,
989
- Date.parse("2013-05-03") => 1
990
- }
991
- assert_equal expected, call_method(:day, :created_at, range: true, series: true)
992
- end
993
-
994
- # week_start
995
-
996
- def test_week_start
997
- Groupdate.week_start = :mon
998
- assert_result_date :week, "2013-03-18", "2013-03-24 23:59:59"
999
- end
1000
-
1001
- def test_week_start_and_start_option
1002
- Groupdate.week_start = :mon
1003
- assert_result_date :week, "2013-03-16", "2013-03-22 23:59:59", false, week_start: :sat
1004
- end
1005
-
1006
- # misc
1007
-
1008
- def test_order_hour_of_day_reverse_option
1009
- assert_equal 23, call_method(:hour_of_day, :created_at, reverse: true, series: true).keys.first
1010
- end
1011
-
1012
- def test_time_zone
1013
- create_user "2013-05-01"
1014
- time_zone = "Pacific Time (US & Canada)"
1015
- assert_equal time_zone, call_method(:hour, :created_at, time_zone: time_zone).keys.first.time_zone.name
1016
- end
1017
-
1018
- def test_format_day
1019
- create_user "2014-03-01"
1020
- assert_format :day, "March 1, 2014", "%B %-e, %Y"
1021
- end
1022
-
1023
- def test_format_month
1024
- create_user "2014-03-01"
1025
- assert_format :month, "March 2014", "%B %Y"
1026
- end
1027
-
1028
- def test_format_quarter
1029
- create_user "2014-03-05"
1030
- assert_format :quarter, "January 1, 2014", "%B %-e, %Y"
1031
- end
1032
-
1033
- def test_format_year
1034
- create_user "2014-03-01"
1035
- assert_format :year, "2014", "%Y"
1036
- end
1037
-
1038
- def test_format_hour_of_day
1039
- create_user "2014-03-01"
1040
- assert_format :hour_of_day, "12 am", "%-l %P"
1041
- end
1042
-
1043
- def test_format_hour_of_day_day_start
1044
- create_user "2014-03-01"
1045
- assert_format :hour_of_day, "12 am", "%-l %P", day_start: 2
1046
- end
1047
-
1048
- def test_format_day_of_week
1049
- create_user "2014-03-01"
1050
- assert_format :day_of_week, "Sat", "%a"
1051
- end
1052
-
1053
- def test_format_day_of_week_day_start
1054
- create_user "2014-03-01"
1055
- assert_format :day_of_week, "Fri", "%a", day_start: 2
1056
- end
1057
-
1058
- def test_format_day_of_week_week_start
1059
- create_user "2014-03-01"
1060
- assert_format :day_of_week, "Sat", "%a", week_start: :mon
1061
- end
1062
-
1063
- def test_format_day_of_month
1064
- create_user "2014-03-01"
1065
- assert_format :day_of_month, " 1", "%e"
1066
- end
1067
-
1068
- def test_format_month_of_year
1069
- create_user "2014-01-01"
1070
- assert_format :month_of_year, "Jan", "%b"
1071
- end
1072
-
1073
- # date column
1074
-
1075
- def test_date_column
1076
- expected = {
1077
- Date.parse("2013-05-03") => 1
1078
- }
1079
- assert_equal expected, result(:day, "2013-05-03", false)
1080
- end
1081
-
1082
- def test_date_column_with_time_zone
1083
- expected = {
1084
- Date.parse("2013-05-02") => 1
1085
- }
1086
- assert_equal expected, result(:day, "2013-05-03", true)
1087
- end
1088
-
1089
- def test_date_column_with_time_zone_false
1090
- Time.zone = pt
1091
- create_user "2013-05-03"
1092
- expected = {
1093
- Date.parse("2013-05-03") => 1
1094
- }
1095
- assert_equal expected, call_method(:day, :created_at, time_zone: false)
1096
- ensure
1097
- Time.zone = nil
1098
- end
1099
-
1100
- # date range
1101
-
1102
- def test_date_range
1103
- ENV["TZ"] = "Europe/Oslo"
1104
- expected = {
1105
- Date.parse("2013-05-01") => 0,
1106
- Date.parse("2013-05-02") => 0,
1107
- Date.parse("2013-05-03") => 0
1108
- }
1109
- assert_equal expected, call_method(:day, :created_at, series: true, range: Date.parse("2013-05-01")..Date.parse("2013-05-03"))
1110
- ensure
1111
- ENV["TZ"] = "UTC"
1112
- end
1113
-
1114
- def test_date_range_exclude_end
1115
- ENV["TZ"] = "Europe/Oslo"
1116
- expected = {
1117
- Date.parse("2013-05-01") => 0,
1118
- Date.parse("2013-05-02") => 0
1119
- }
1120
- assert_equal expected, call_method(:day, :created_at, series: true, range: Date.parse("2013-05-01")...Date.parse("2013-05-03"))
1121
- ensure
1122
- ENV["TZ"] = "UTC"
1123
- end
1124
-
1125
- # day start
1126
-
1127
- def test_day_start_decimal_end_of_day
1128
- assert_result_date :day, "2013-05-03", "2013-05-04 02:29:59", false, day_start: 2.5
1129
- end
1130
-
1131
- def test_day_start_decimal_start_of_day
1132
- assert_result_date :day, "2013-05-03", "2013-05-03 02:30:00", false, day_start: 2.5
1133
- end
1134
-
1135
- private
1136
-
1137
- # helpers
1138
-
1139
- def assert_format(method, expected, format, options = {})
1140
- assert_equal({expected => 1}, call_method(method, :created_at, options.merge(format: format, series: false)))
1141
- end
1142
-
1143
- def assert_result_time(method, expected, time_str, time_zone = false, options = {})
1144
- expected = {utc.parse(expected).in_time_zone(time_zone ? "Pacific Time (US & Canada)" : utc) => 1}
1145
- assert_equal expected, result(method, time_str, time_zone, options)
1146
- end
1147
-
1148
- def assert_result_date(method, expected_str, time_str, time_zone = false, options = {})
1149
- create_user time_str
1150
- expected = {Date.parse(expected_str) => 1}
1151
- assert_equal expected, call_method(method, :created_at, options.merge(time_zone: time_zone ? "Pacific Time (US & Canada)" : nil))
1152
- expected = {(time_zone ? pt : utc).parse(expected_str) + options[:day_start].to_f.hours => 1}
1153
- assert_equal expected, call_method(method, :created_at, options.merge(dates: false, time_zone: time_zone ? "Pacific Time (US & Canada)" : nil))
1154
- # assert_equal expected, call_method(method, :created_on, options.merge(time_zone: time_zone ? "Pacific Time (US & Canada)" : nil))
1155
- end
1156
-
1157
- def assert_result(method, expected, time_str, time_zone = false, options = {})
1158
- assert_equal 1, result(method, time_str, time_zone, options)[expected]
1159
- end
1160
-
1161
- def result(method, time_str, time_zone = false, options = {})
1162
- create_user time_str
1163
- call_method(method, :created_at, options.merge(time_zone: time_zone ? "Pacific Time (US & Canada)" : nil))
1164
- end
1165
-
1166
- def assert_zeros(method, created_at, keys, range_start, range_end, time_zone = nil, options = {})
1167
- create_user created_at
1168
- expected = {}
1169
- keys.each_with_index do |key, i|
1170
- expected[utc.parse(key).in_time_zone(time_zone ? "Pacific Time (US & Canada)" : utc)] = i == 1 ? 1 : 0
1171
- end
1172
- assert_equal expected, call_method(method, :created_at, options.merge(series: true, time_zone: time_zone ? "Pacific Time (US & Canada)" : nil, range: Time.parse(range_start)..Time.parse(range_end)))
1173
- end
1174
-
1175
- def assert_zeros_date(method, created_at, keys, range_start, range_end, time_zone = nil, options = {})
1176
- create_user created_at
1177
- expected = {}
1178
- keys.each_with_index do |key, i|
1179
- expected[Date.parse(key)] = i == 1 ? 1 : 0
1180
- end
1181
- assert_equal expected, call_method(method, :created_at, options.merge(series: true, time_zone: time_zone ? "Pacific Time (US & Canada)" : nil, range: Time.parse(range_start)..Time.parse(range_end)))
1182
- end
1183
-
1184
- def this_quarters_month
1185
- Time.now.beginning_of_quarter.month
1186
- end
1187
-
1188
- def this_year
1189
- Time.now.year
1190
- end
1191
-
1192
- def this_month
1193
- Time.now.month
1194
- end
1195
-
1196
- def utc
1197
- ActiveSupport::TimeZone["UTC"]
1198
- end
1199
-
1200
- def pt
1201
- ActiveSupport::TimeZone["Pacific Time (US & Canada)"]
1202
- end
1203
-
1204
- def brasilia
1205
- ActiveSupport::TimeZone["Brasilia"]
1206
- end
1207
- end