test/test_helper.rb in groupdate-2.0.3 vs test/test_helper.rb in groupdate-2.0.4

- old
+ new

@@ -384,42 +384,42 @@ 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, range: true).count(:created_at) + assert_equal expected, User.group_by_day_of_week(:created_at, range: true).count 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, range: true).count(:created_at) + assert_equal expected, User.group_by_hour_of_day(:created_at, range: true).count 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 + utc.parse("2013-05-01 00:00:00 UTC") => 0 } 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 + utc.parse("2013-05-01 00:00:00 UTC") => 0 } 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 + utc.parse("2013-05-01 00:00:00 UTC") => 1 } 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 @@ -430,13 +430,13 @@ 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 + utc.parse("2013-05-01 00:00:00 UTC") => 1, + utc.parse("2013-05-02 00:00:00 UTC") => 0, + utc.parse("2013-05-03 00:00:00 UTC") => 1 } assert_equal expected, User.group_by_day(:created_at, range: true).count end # week_start @@ -451,22 +451,26 @@ 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, series: false).order("day desc").limit(20).count + def test_order_hour_of_day + assert_equal 23, User.group_by_hour_of_day(:created_at).order("hour_of_day desc").count.keys.first end - def test_order_week - assert_empty User.group_by_week(:created_at, series: false).order("week asc").count + def test_order_hour_of_day_case + assert_equal 23, User.group_by_hour_of_day(:created_at).order("hour_of_day DESC").count.keys.first end - def test_order_hour_of_day - assert_empty User.group_by_hour_of_day(:created_at, series: false).order("hour_of_day desc").count + def test_order_hour_of_day_reverse + assert_equal 23, User.group_by_hour_of_day(:created_at).reverse_order.count.keys.first end + def test_order_hour_of_day_order_reverse + assert_equal 0, User.group_by_hour_of_day(:created_at).order("hour_of_day desc").reverse_order.count.keys.first + end + def test_table_name assert_empty User.group_by_day("users.created_at").count end def test_previous_scopes @@ -485,10 +489,56 @@ create_user "2013-05-02 00:00:00 UTC" expected = {utc.parse("2013-05-02 00:00:00 UTC") => 1} assert_equal expected, User.group_by_day(:created_at).where("created_at > ?", "2013-05-01 00:00:00 UTC").count end + def test_group_before + create_user "2013-05-01 00:00:00 UTC", 1 + create_user "2013-05-02 00:00:00 UTC", 2 + create_user "2013-05-03 00:00:00 UTC", 2 + expected = { + [1, utc.parse("2013-05-01 00:00:00 UTC")] => 1, + [1, utc.parse("2013-05-02 00:00:00 UTC")] => 0, + [1, utc.parse("2013-05-03 00:00:00 UTC")] => 0, + [2, utc.parse("2013-05-01 00:00:00 UTC")] => 0, + [2, utc.parse("2013-05-02 00:00:00 UTC")] => 1, + [2, utc.parse("2013-05-03 00:00:00 UTC")] => 1 + } + assert_equal expected, User.group(:score).group_by_day(:created_at).order(:score).count + end + + def test_group_after + create_user "2013-05-01 00:00:00 UTC", 1 + create_user "2013-05-02 00:00:00 UTC", 2 + create_user "2013-05-03 00:00:00 UTC", 2 + expected = { + [utc.parse("2013-05-01 00:00:00 UTC"), 1] => 1, + [utc.parse("2013-05-02 00:00:00 UTC"), 1] => 0, + [utc.parse("2013-05-03 00:00:00 UTC"), 1] => 0, + [utc.parse("2013-05-01 00:00:00 UTC"), 2] => 0, + [utc.parse("2013-05-02 00:00:00 UTC"), 2] => 1, + [utc.parse("2013-05-03 00:00:00 UTC"), 2] => 1 + } + assert_equal expected, User.group_by_day(:created_at).group(:score).order(:score).count + end + + def test_groupdate_multiple + create_user "2013-05-01 00:00:00 UTC", 1 + expected = { + [utc.parse("2013-05-01 00:00:00 UTC"), utc.parse("2013-01-01 00:00:00 UTC")] => 1 + } + assert_equal expected, User.group_by_day(:created_at).group_by_year(:created_at).count + end + + def test_not_modified + create_user "2013-05-01 00:00:00 UTC" + expected = {utc.parse("2013-05-01 00:00:00 UTC") => 1} + relation = User.group_by_day(:created_at) + relation.where("created_at > ?", "2013-05-01 00:00:00 UTC") + assert_equal expected, relation.count + end + def test_bad_method assert_raises(NoMethodError) { User.group_by_day(:created_at).no_such_method } end def test_respond_to_where @@ -500,34 +550,33 @@ end # helpers def assert_result_time(method, expected, time_str, time_zone = false, options = {}) - assert_result method, Time.parse(expected), time_str, time_zone, options + expected = {utc.parse(expected).in_time_zone(time_zone ? "Pacific Time (US & Canada)" : utc) => 1} + assert_equal expected, result(method, time_str, time_zone, options) end def assert_result(method, expected, time_str, time_zone = false, options = {}) + assert_equal 1, result(method, time_str, time_zone, options)[expected] + end + + def result(method, 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, 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] + User.send(:"group_by_#{method}", :created_at, options.merge(time_zone: time_zone ? "Pacific Time (US & Canada)" : nil)).count 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 + expected[utc.parse(key).in_time_zone(time_zone ? "Pacific Time (US & Canada)" : utc)] = i == 1 ? 1 : 0 end 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 - - def create_user(created_at) - User.create! :name => "Andrew", :score => 1, :created_at => utc.parse(created_at) + def create_user(created_at, score = 1) + User.create! :name => "Andrew", :score => score, :created_at => utc.parse(created_at) end def utc ActiveSupport::TimeZone["UTC"] end