test/groupdate_test.rb in groupdate-0.1.5 vs test/groupdate_test.rb in groupdate-0.1.6

- old
+ new

@@ -120,10 +120,15 @@ it "group_by_day_of_week with time zone" do assert_group_number_tz :day_of_week, "2013-03-03 00:00:00 UTC", 6 end + it "works with previous scopes" do + create_user "2013-05-01 00:00:00 UTC" + assert_equal({}, User.where("id = 0").group_by_day(:created_at).count) + end + describe "returns zeros" do it "group_by_second" do 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 @@ -170,63 +175,71 @@ it "group_by_day_of_week" do create_user "2013-05-01 00:00:00 UTC" expected = {} 7.times do |n| - expected[number_key(n, true)] = n == 3 ? 1 : 0 + expected[n] = n == 3 ? 1 : 0 end assert_equal(expected, User.group_by_day_of_week(:created_at, Time.zone, true).count(:created_at)) end it "group_by_hour_of_day" do create_user "2013-05-01 20:00:00 UTC" expected = {} 24.times do |n| - expected[number_key(n, true)] = n == 20 ? 1 : 0 + expected[n] = n == 20 ? 1 : 0 end assert_equal(expected, User.group_by_hour_of_day(:created_at, Time.zone, true).count(:created_at)) end it "excludes end" do create_user "2013-05-02 00:00:00 UTC" expected = { - time_key("2013-05-01 00:00:00 UTC") => 0 + 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(:created_at)) + 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) end + it "works with previous scopes" do + 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) + end + end end end # helper methods def assert_group(method, created_at, key, time_zone = nil) create_user created_at - assert_equal(ordered_hash({time_key(key) => 1}), User.send(:"group_by_#{method}", :created_at, time_zone).order(method).count) + assert_equal(ordered_hash({time_key(key) => 1}), User.send(:"group_by_#{method}", :created_at, time_zone).order(method.to_s).count) end def assert_group_tz(method, created_at, key) assert_group method, created_at, key, "Pacific Time (US & Canada)" end def assert_group_number(method, created_at, key, time_zone = nil) create_user created_at - assert_equal(ordered_hash({number_key(key) => 1}), User.send(:"group_by_#{method}", :created_at, time_zone).order(method).count) + assert_equal(ordered_hash({number_key(key) => 1}), User.send(:"group_by_#{method}", :created_at, time_zone).order(method.to_s).count) end def assert_group_number_tz(method, created_at, key) assert_group_number method, created_at, key, "Pacific Time (US & Canada)" end def assert_zeros(method, created_at, keys, range_start, range_end, time_zone = nil, java_hack = false) create_user created_at expected = {} keys.each_with_index do |key, i| - expected[time_key(key, java_hack)] = i == 1 ? 1 : 0 + expected[Time.parse(key)] = i == 1 ? 1 : 0 end - assert_equal(expected, User.send(:"group_by_#{method}", :created_at, time_zone, Time.parse(range_start)..Time.parse(range_end)).order(method).count(:created_at)) + assert_equal(expected, User.send(:"group_by_#{method}", :created_at, time_zone, Time.parse(range_start)..Time.parse(range_end)).count) end def assert_zeros_tz(method, created_at, keys, range_start, range_end) assert_zeros method, created_at, keys, range_start, range_end, "Pacific Time (US & Canada)", true end