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