test/test_helper.rb in groupdate-1.0.5 vs test/test_helper.rb in groupdate-2.0.0

- old
+ new

@@ -82,10 +82,28 @@ def test_day_start_of_day_with_time_zone assert_result_time :day, "2013-05-03 00:00:00 PDT", "2013-05-03 07:00:00", true end + # day hour starts at 2 am + + def test_test_day_end_of_day_day_start_2am + assert_result_time :day, "2013-05-03 02:00:00 UTC", "2013-05-04 01:59:59", false, :day_start => 2 + end + + def test_test_day_start_of_day_day_start_2am + assert_result_time :day, "2013-05-03 02:00:00 UTC", "2013-05-03 02:00:00", false, :day_start => 2 + end + + def test_test_day_end_of_day_with_time_zone_day_start_2am + assert_result_time :day, "2013-05-03 02:00:00 PDT", "2013-05-04 07:59:59", true, :day_start => 2 + end + + def test_test_day_start_of_day_with_time_zone_day_start_2am + assert_result_time :day, "2013-05-03 02:00:00 PDT", "2013-05-03 09:00:00", true, :day_start => 2 + end + # week def test_week_end_of_week assert_result_time :week, "2013-03-17 00:00:00 UTC", "2013-03-23 23:59:59" end @@ -103,43 +121,61 @@ end # week starting on monday def test_week_end_of_week_mon - assert_result_time :week, "2013-03-18 00:00:00 UTC", "2013-03-24 23:59:59", false, :start => :mon + assert_result_time :week, "2013-03-18 00:00:00 UTC", "2013-03-24 23:59:59", false, week_start: :mon end def test_week_start_of_week_mon - assert_result_time :week, "2013-03-25 00:00:00 UTC", "2013-03-25 00:00:00", false, :start => :mon + assert_result_time :week, "2013-03-25 00:00:00 UTC", "2013-03-25 00:00:00", false, week_start: :mon end def test_week_end_of_week_with_time_zone_mon - assert_result_time :week, "2013-03-11 00:00:00 PDT", "2013-03-18 06:59:59", true, :start => :mon + assert_result_time :week, "2013-03-11 00:00:00 PDT", "2013-03-18 06:59:59", true, week_start: :mon end def test_week_start_of_week_with_time_zone_mon - assert_result_time :week, "2013-03-18 00:00:00 PDT", "2013-03-18 07:00:00", true, :start => :mon + assert_result_time :week, "2013-03-18 00:00:00 PDT", "2013-03-18 07:00:00", true, week_start: :mon end # week starting on saturday def test_week_end_of_week_sat - assert_result_time :week, "2013-03-16 00:00:00 UTC", "2013-03-22 23:59:59", false, :start => :sat + assert_result_time :week, "2013-03-16 00:00:00 UTC", "2013-03-22 23:59:59", false, week_start: :sat end def test_week_start_of_week_sat - assert_result_time :week, "2013-03-23 00:00:00 UTC", "2013-03-23 00:00:00", false, :start => :sat + assert_result_time :week, "2013-03-23 00:00:00 UTC", "2013-03-23 00:00:00", false, week_start: :sat end def test_week_end_of_week_with_time_zone_sat - assert_result_time :week, "2013-03-09 00:00:00 PST", "2013-03-16 06:59:59", true, :start => :sat + assert_result_time :week, "2013-03-09 00:00:00 PST", "2013-03-16 06:59:59", true, week_start: :sat end def test_week_start_of_week_with_time_zone_sat - assert_result_time :week, "2013-03-16 00:00:00 PDT", "2013-03-16 07:00:00", true, :start => :sat + assert_result_time :week, "2013-03-16 00:00:00 PDT", "2013-03-16 07:00:00", true, week_start: :sat end + # week starting at 2am + + def test_week_end_of_week_day_start_2am + assert_result_time :week, "2013-03-17 02:00:00 UTC", "2013-03-24 01:59:59", false, :day_start => 2 + end + + def test_week_start_of_week_day_start_2am + assert_result_time :week, "2013-03-17 02:00:00 UTC", "2013-03-17 02:00:00", false, :day_start => 2 + end + + def test_week_end_of_week_day_with_time_zone_start_2am + assert_result_time :week, "2013-03-17 02:00:00 PDT", "2013-03-24 08:59:59", true, :day_start => 2 + end + + def test_week_start_of_week_day_with_time_zone_start_2am + assert_result_time :week, "2013-03-17 02:00:00 PDT", "2013-03-17 09:00:00", true, :day_start => 2 + end + # month def test_month_end_of_month assert_result_time :month, "2013-05-01 00:00:00 UTC", "2013-05-31 23:59:59" end @@ -154,10 +190,28 @@ def test_month_start_of_month_with_time_zone assert_result_time :month, "2013-06-01 00:00:00 PDT", "2013-06-01 07:00:00", true end + # month starts at 2am + + def test_month_end_of_month_day_start_2am + assert_result_time :month, "2013-03-01 02:00:00 UTC", "2013-04-01 01:59:59", false, :day_start => 2 + end + + def test_month_start_of_month_day_start_2am + assert_result_time :month, "2013-03-01 02:00:00 UTC", "2013-03-01 02:00:00", false, :day_start => 2 + end + + def test_month_end_of_month_with_time_zone_day_start_2am + assert_result_time :month, "2013-03-01 02:00:00 PST", "2013-04-01 08:59:59", true, :day_start => 2 + end + + def test_month_start_of_month_with_time_zone_day_start_2am + assert_result_time :month, "2013-03-01 02:00:00 PST", "2013-03-01 10:00:00", true, :day_start => 2 + end + # year def test_year_end_of_year assert_result_time :year, "2013-01-01 00:00:00 UTC", "2013-12-31 23:59:59" end @@ -172,10 +226,28 @@ def test_year_start_of_year_with_time_zone assert_result_time :year, "2014-01-01 00:00:00 PST", "2014-01-01 08:00:00", true end + # year starts at 2am + + def test_year_end_of_year_day_start_2am + assert_result_time :year, "2013-01-01 02:00:00 UTC", "2014-01-01 01:59:59", false, :day_start => 2 + end + + def test_year_start_of_year_day_start_2am + assert_result_time :year, "2013-01-01 02:00:00 UTC", "2013-01-01 02:00:00", false, :day_start => 2 + end + + def test_year_end_of_year_with_time_zone_day_start_2am + assert_result_time :year, "2013-01-01 02:00:00 PST", "2014-01-01 09:59:59", true, :day_start => 2 + end + + def test_year_start_of_year_with_time_zone_day_start_2am + assert_result_time :year, "2013-01-01 02:00:00 PST", "2013-01-01 10:00:00", true, :day_start => 2 + end + # hour of day def test_hour_of_day_end_of_hour assert_result :hour_of_day, 0, "2013-01-01 00:59:59" end @@ -190,10 +262,28 @@ def test_hour_of_day_start_of_hour_with_time_zone assert_result :hour_of_day, 1, "2013-01-01 09:00:00", true end + # hour of day starts at 2am + + def test_hour_of_day_end_of_day_day_start_2am + assert_result :hour_of_day, 23, "2013-01-01 01:59:59", false, :day_start => 2 + end + + def test_hour_of_day_start_of_day_day_start_2am + assert_result :hour_of_day, 0, "2013-01-01 02:00:00", false, :day_start => 2 + end + + def test_hour_of_day_end_of_day_with_time_zone_day_start_2am + assert_result :hour_of_day, 23, "2013-01-01 09:59:59", true, :day_start => 2 + end + + def test_hour_of_day_start_of_day_with_time_zone_day_start_2am + assert_result :hour_of_day, 0, "2013-01-01 10:00:00", true, :day_start => 2 + end + # day of week def test_day_of_week_end_of_day assert_result :day_of_week, 2, "2013-01-01 23:59:59" end @@ -208,10 +298,28 @@ def test_day_of_week_start_of_week_with_time_zone assert_result :day_of_week, 3, "2013-01-02 08:00:00", true end + # day of week starts at 2am + + def test_day_of_week_end_of_day_day_start_2am + assert_result :day_of_week, 3, "2013-01-03 01:59:59", false, :day_start => 2 + end + + def test_day_of_week_start_of_day_day_start_2am + assert_result :day_of_week, 3, "2013-01-02 02:00:00", false, :day_start => 2 + end + + def test_day_of_week_end_of_day_with_time_zone_day_start_2am + assert_result :day_of_week, 3, "2013-01-03 09:59:59", true, :day_start => 2 + end + + def test_day_of_week_start_of_day_with_time_zone_day_start_2am + assert_result :day_of_week, 3, "2013-01-02 10:00:00", true, :day_start => 2 + end + # zeros def test_zeros_second 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" end @@ -239,23 +347,23 @@ def test_zeros_week_time_zone assert_zeros :week, "2013-05-01 20:00:00 PDT", ["2013-04-21 00:00:00 PDT", "2013-04-28 00:00:00 PDT", "2013-05-05 00:00:00 PDT"], "2013-04-27 23:59:59 PDT", "2013-05-11 23:59:59 PDT", true end def test_zeros_week_mon - assert_zeros :week, "2013-05-01 20:00:00 UTC", ["2013-04-22 00:00:00 UTC", "2013-04-29 00:00:00 UTC", "2013-05-06 00:00:00 UTC"], "2013-04-27 23:59:59 UTC", "2013-05-11 23:59:59 UTC", false, :start => :mon + assert_zeros :week, "2013-05-01 20:00:00 UTC", ["2013-04-22 00:00:00 UTC", "2013-04-29 00:00:00 UTC", "2013-05-06 00:00:00 UTC"], "2013-04-27 23:59:59 UTC", "2013-05-11 23:59:59 UTC", false, week_start: :mon end def test_zeros_week_time_zone_mon - assert_zeros :week, "2013-05-01 20:00:00 PDT", ["2013-04-22 00:00:00 PDT", "2013-04-29 00:00:00 PDT", "2013-05-06 00:00:00 PDT"], "2013-04-27 23:59:59 PDT", "2013-05-11 23:59:59 PDT", true, :start => :mon + assert_zeros :week, "2013-05-01 20:00:00 PDT", ["2013-04-22 00:00:00 PDT", "2013-04-29 00:00:00 PDT", "2013-05-06 00:00:00 PDT"], "2013-04-27 23:59:59 PDT", "2013-05-11 23:59:59 PDT", true, week_start: :mon end def test_zeros_week_sat - assert_zeros :week, "2013-05-01 20:00:00 UTC", ["2013-04-20 00:00:00 UTC", "2013-04-27 00:00:00 UTC", "2013-05-04 00:00:00 UTC"], "2013-04-26 23:59:59 UTC", "2013-05-10 23:59:59 UTC", false, :start => :sat + assert_zeros :week, "2013-05-01 20:00:00 UTC", ["2013-04-20 00:00:00 UTC", "2013-04-27 00:00:00 UTC", "2013-05-04 00:00:00 UTC"], "2013-04-26 23:59:59 UTC", "2013-05-10 23:59:59 UTC", false, week_start: :sat end def test_zeros_week_time_zone_sat - assert_zeros :week, "2013-05-01 20:00:00 PDT", ["2013-04-20 00:00:00 PDT", "2013-04-27 00:00:00 PDT", "2013-05-04 00:00:00 PDT"], "2013-04-26 23:59:59 PDT", "2013-05-10 23:59:59 PDT", true, :start => :sat + assert_zeros :week, "2013-05-01 20:00:00 PDT", ["2013-04-20 00:00:00 PDT", "2013-04-27 00:00:00 PDT", "2013-05-04 00:00:00 PDT"], "2013-04-26 23:59:59 PDT", "2013-05-10 23:59:59 PDT", true, week_start: :sat end def test_zeros_month assert_zeros :month, "2013-04-16 20:00:00 UTC", ["2013-03-01 00:00:00 UTC", "2013-04-01 00:00:00 UTC", "2013-05-01 00:00:00 UTC"], "2013-03-01 00:00:00 UTC", "2013-05-31 23:59:59 UTC" end @@ -276,61 +384,61 @@ create_user "2013-05-01 00:00:00 UTC" expected = {} 7.times do |n| expected[n] = n == 3 ? 1 : 0 end - assert_equal expected, User.group_by_day_of_week(:created_at, Time.zone, true).count(:created_at) + assert_equal expected, User.group_by_day_of_week(:created_at, range: true).count(:created_at) end def test_zeros_hour_of_day create_user "2013-05-01 20:00:00 UTC" expected = {} 24.times do |n| expected[n] = n == 20 ? 1 : 0 end - assert_equal expected, User.group_by_hour_of_day(:created_at, Time.zone, true).count(:created_at) + assert_equal expected, User.group_by_hour_of_day(:created_at, range: true).count(:created_at) end def test_zeros_excludes_end create_user "2013-05-02 00:00:00 UTC" expected = { Time.parse("2013-05-01 00:00:00 UTC") => 0 } - assert_equal expected, User.group_by_day(:created_at, Time.zone, Time.parse("2013-05-01 00:00:00 UTC")...Time.parse("2013-05-02 00:00:00 UTC")).count + assert_equal expected, User.group_by_day(:created_at, range: Time.parse("2013-05-01 00:00:00 UTC")...Time.parse("2013-05-02 00:00:00 UTC")).count end def test_zeros_previous_scope create_user "2013-05-01 00:00:00 UTC" expected = { Time.parse("2013-05-01 00:00:00 UTC") => 0 } - assert_equal expected, User.where("id = 0").group_by_day(:created_at, Time.zone, Time.parse("2013-05-01 00:00:00 UTC")..Time.parse("2013-05-01 23:59:59 UTC")).count + assert_equal expected, User.where("id = 0").group_by_day(:created_at, range: Time.parse("2013-05-01 00:00:00 UTC")..Time.parse("2013-05-01 23:59:59 UTC")).count end def test_zeros_datetime create_user "2013-05-01 00:00:00 UTC" expected = { Time.parse("2013-05-01 00:00:00 UTC") => 1 } - assert_equal expected, User.group_by_day(:created_at, Time.zone, DateTime.parse("2013-05-01 00:00:00 UTC")..DateTime.parse("2013-05-01 00:00:00 UTC")).count + assert_equal expected, User.group_by_day(:created_at, range: DateTime.parse("2013-05-01 00:00:00 UTC")..DateTime.parse("2013-05-01 00:00:00 UTC")).count end def test_zeros_null_value user = User.create!(name: "Andrew") user.update_column :created_at, nil - assert_equal 0, User.group_by_hour_of_day(:created_at, Time.zone, true).count[0] + assert_equal 0, User.group_by_hour_of_day(:created_at, range: true).count[0] end def test_zeroes_range_true create_user "2013-05-01 00:00:00 UTC" create_user "2013-05-03 00:00:00 UTC" expected = { Time.parse("2013-05-01 00:00:00 UTC") => 1, Time.parse("2013-05-02 00:00:00 UTC") => 0, Time.parse("2013-05-03 00:00:00 UTC") => 1 } - assert_equal expected, User.group_by_day(:created_at, Time.zone, true).count + assert_equal expected, User.group_by_day(:created_at, range: true).count end # week_start def test_week_start @@ -338,25 +446,25 @@ assert_result_time :week, "2013-03-18 00:00:00 UTC", "2013-03-24 23:59:59" end def test_week_start_and_start_option Groupdate.week_start = :mon - assert_result_time :week, "2013-03-16 00:00:00 UTC", "2013-03-22 23:59:59", false, :start => :sat + assert_result_time :week, "2013-03-16 00:00:00 UTC", "2013-03-22 23:59:59", false, week_start: :sat end # misc def test_order_day - assert_empty User.group_by_day(:created_at).order("day desc").limit(20).count + assert_empty User.group_by_day(:created_at, series: false).order("day desc").limit(20).count end def test_order_week - assert_empty User.group_by_week(:created_at).order("week asc").count + assert_empty User.group_by_week(:created_at, series: false).order("week asc").count end def test_order_hour_of_day - assert_empty User.group_by_hour_of_day(:created_at).order("hour_of_day desc").count + assert_empty User.group_by_hour_of_day(:created_at, series: false).order("hour_of_day desc").count end def test_table_name assert_empty User.group_by_day("users.created_at").count end @@ -373,20 +481,20 @@ end def assert_result(method, expected, time_str, time_zone = false, options = {}) create_user time_str expected = expected.is_a?(Time) ? time_key(expected) : number_key(expected) - assert_equal ordered_hash({expected => 1}), User.send(:"group_by_#{method}", :created_at, time_zone ? "Pacific Time (US & Canada)" : nil, options).order(method.to_s).count - assert_equal 1, User.send(:"group_by_#{method}", :created_at, time_zone ? "Pacific Time (US & Canada)" : nil, true, options).count[expected] + assert_equal ordered_hash({expected => 1}), User.send(:"group_by_#{method}", :created_at, options.merge(series: false, time_zone: time_zone ? "Pacific Time (US & Canada)" : nil)).order(method.to_s).count + assert_equal 1, User.send(:"group_by_#{method}", :created_at, options.merge(time_zone: time_zone ? "Pacific Time (US & Canada)" : nil)).count[expected] end def assert_zeros(method, created_at, keys, range_start, range_end, time_zone = nil, options = {}) create_user created_at expected = {} keys.each_with_index do |key, i| expected[Time.parse(key)] = i == 1 ? 1 : 0 end - assert_equal expected, User.send(:"group_by_#{method}", :created_at, time_zone ? "Pacific Time (US & Canada)" : nil, Time.parse(range_start)..Time.parse(range_end), options).count + assert_equal expected, User.send(:"group_by_#{method}", :created_at, options.merge(time_zone: time_zone ? "Pacific Time (US & Canada)" : nil, range: Time.parse(range_start)..Time.parse(range_end))).count end def ordered_hash(hash) RUBY_VERSION =~ /1\.8/ ? hash.inject(ActiveSupport::OrderedHash.new){|h, (k, v)| h[k] = v; h } : hash end