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

- old
+ new

@@ -119,45 +119,151 @@ end 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 + + 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 + + it "group_by_minute" do + 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" + end + + it "group_by_hour" do + 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" + end + + it "group_by_day" do + assert_zeros :day, "2013-05-01 20:00:00 UTC", ["2013-04-30 00:00:00 UTC", "2013-05-01 00:00:00 UTC", "2013-05-02 00:00:00 UTC"], "2013-04-30 00:00:00 UTC", "2013-05-02 23:59:59 UTC" + end + + it "group_by_day with time zone" do + assert_zeros_tz :day, "2013-05-01 20:00:00 PDT", ["2013-04-30 00:00:00 PDT", "2013-05-01 00:00:00 PDT", "2013-05-02 00:00:00 PDT"], "2013-04-30 00:00:00 PDT", "2013-05-02 23:59:59 PDT" + end + + it "group_by_week" do + assert_zeros :week, "2013-05-01 20:00:00 UTC", ["2013-04-21 00:00:00 UTC", "2013-04-28 00:00:00 UTC", "2013-05-05 00:00:00 UTC"], "2013-04-27 23:59:59 UTC", "2013-05-11 23:59:59 UTC" + end + + it "group_by_week with time zone" do + assert_zeros_tz :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" + end + + it "group_by_month" do + 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 + + it "group_by_month with time zone" do + assert_zeros_tz :month, "2013-04-16 20:00:00 PDT", ["2013-03-01 00:00:00 PST", "2013-04-01 00:00:00 PDT", "2013-05-01 00:00:00 PDT"], "2013-03-01 00:00:00 PST", "2013-05-31 23:59:59 PDT" + end + + it "group_by_year" do + assert_zeros :year, "2013-04-16 20:00:00 UTC", ["2012-01-01 00:00:00 UTC", "2013-01-01 00:00:00 UTC", "2014-01-01 00:00:00 UTC"], "2012-01-01 00:00:00 UTC", "2014-12-31 23:59:59 UTC" + end + + it "group_by_year with time zone" do + assert_zeros_tz :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" + end + + 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 + 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 + 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 + } + 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)) + 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).count) + assert_equal(ordered_hash({time_key(key) => 1}), User.send(:"group_by_#{method}", :created_at, time_zone).order(method).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).count) + assert_equal(ordered_hash({number_key(key) => 1}), User.send(:"group_by_#{method}", :created_at, time_zone).order(method).count) end def assert_group_number_tz(method, created_at, key) assert_group_number method, created_at, key, "Pacific Time (US & Canada)" end - def time_key(key) + 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 + 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)) + 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 + + def time_key(key, java_hack = false) if RUBY_PLATFORM == "java" - User.connection.adapter_name == "PostgreSQL" ? Time.parse(key).strftime("%Y-%m-%d %H:%M:%S%z")[0..-3] : Time.parse(key).strftime("%Y-%m-%d %H:%M:%S").gsub(/ 00\:00\:00\z/, "") + if User.connection.adapter_name == "PostgreSQL" + Time.parse(key).utc.strftime("%Y-%m-%d %H:%M:%S%z")[0..-3] + elsif java_hack + Time.parse(key).utc.strftime("%Y-%m-%d %H:%M:%S") + else + Time.parse(key).strftime("%Y-%m-%d %H:%M:%S").gsub(/ 00\:00\:00\z/, "") + end else - User.connection.adapter_name == "PostgreSQL" && ActiveRecord::VERSION::MAJOR == 3 ? Time.parse(key).strftime("%Y-%m-%d %H:%M:%S+00") : Time.parse(key) + if User.connection.adapter_name == "PostgreSQL" and ActiveRecord::VERSION::MAJOR == 3 + Time.parse(key).utc.strftime("%Y-%m-%d %H:%M:%S+00") + else + Time.parse(key) + end end end - def number_key(key) + def number_key(key, java_hack = false) if RUBY_PLATFORM == "java" - User.connection.adapter_name == "PostgreSQL" ? key.to_f : key + if User.connection.adapter_name == "PostgreSQL" and !java_hack + key.to_f + else + key + end else - User.connection.adapter_name == "PostgreSQL" ? (ActiveRecord::VERSION::MAJOR == 3 ? key.to_s : key.to_f) : key + if User.connection.adapter_name == "PostgreSQL" + ActiveRecord::VERSION::MAJOR == 3 ? key.to_s : key.to_f + else + key + end end end def ordered_hash(hash) RUBY_VERSION =~ /1\.8/ ? hash.inject(ActiveSupport::OrderedHash.new){|h, (k, v)| h[k] = v; h } : hash