test/test_helper.rb in groupdate-2.5.0 vs test/test_helper.rb in groupdate-2.5.1

- old
+ new

@@ -22,25 +22,28 @@ class Post < ActiveRecord::Base end # i18n I18n.enforce_available_locales = true -I18n.backend.store_translations :de, :date => { - :abbr_month_names => %w(Jan Feb Mar Apr Mai Jun Jul Aug Sep Okt Nov Dez).unshift(nil) +I18n.backend.store_translations :de, date: { + abbr_month_names: %w(Jan Feb Mar Apr Mai Jun Jul Aug Sep Okt Nov Dez).unshift(nil) +}, +time: { + formats: {special: "%b %e, %Y"} } # migrations %w(postgresql mysql2).each do |adapter| - ActiveRecord::Base.establish_connection :adapter => adapter, :database => "groupdate_test", :username => adapter == "mysql2" ? "root" : nil + ActiveRecord::Base.establish_connection adapter: adapter, database: "groupdate_test", username: adapter == "mysql2" ? "root" : nil - ActiveRecord::Migration.create_table :users, :force => true do |t| + ActiveRecord::Migration.create_table :users, force: true do |t| t.string :name t.integer :score t.timestamp :created_at end - ActiveRecord::Migration.create_table :posts, :force => true do |t| + ActiveRecord::Migration.create_table :posts, force: true do |t| t.references :user t.timestamp :created_at end end @@ -102,23 +105,23 @@ 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 + 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 + 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 + 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 + 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 @@ -174,23 +177,23 @@ 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 + 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 + 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 + 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 + 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 @@ -210,25 +213,61 @@ 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 + 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 + 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 + 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 + assert_result_time :month, "2013-03-01 02:00:00 PST", "2013-03-01 10:00:00", true, day_start: 2 end + # quarter + + def test_quarter_end_of_quarter + assert_result_time :quarter, "2013-04-01 00:00:00 UTC", "2013-06-30 23:59:59" + end + + def test_quarter_start_of_quarter + assert_result_time :quarter, "2013-04-01 00:00:00 UTC", "2013-04-01 00:00:00" + end + + def test_quarter_end_of_quarter_with_time_zone + assert_result_time :quarter, "2013-04-01 00:00:00 PDT", "2013-07-01 06:59:59", true + end + + def test_quarter_start_of_quarter_with_time_zone + assert_result_time :quarter, "2013-04-01 00:00:00 PDT", "2013-04-01 07:00:00", true + end + + # quarter starts at 2am + + def test_quarter_end_of_quarter_day_start_2am + assert_result_time :quarter, "2013-04-01 02:00:00 UTC", "2013-07-01 01:59:59", false, day_start: 2 + end + + def test_quarter_start_of_quarter_day_start_2am + assert_result_time :quarter, "2013-04-01 02:00:00 UTC", "2013-04-01 02:00:00", false, day_start: 2 + end + + def test_quarter_end_of_quarter_with_time_zone_day_start_2am + assert_result_time :quarter, "2013-01-01 02:00:00 PST", "2013-04-01 08:59:59", true, day_start: 2 + end + + def test_quarter_start_of_quarter_with_time_zone_day_start_2am + assert_result_time :quarter, "2013-01-01 02:00:00 PST", "2013-01-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 @@ -246,23 +285,23 @@ 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 + 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 + 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 + 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 + 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 @@ -282,23 +321,23 @@ 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 + 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 + 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 + 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 + 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 @@ -318,23 +357,23 @@ 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 + 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 + 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 + 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 + assert_result :day_of_week, 3, "2013-01-02 10:00:00", true, day_start: 2 end # day of month def test_day_of_month_end_of_day @@ -358,23 +397,23 @@ end # day of month starts at 2am def test_day_of_month_end_of_day_day_start_2am - assert_result :day_of_month, 31, "2013-01-01 01:59:59", false, :day_start => 2 + assert_result :day_of_month, 31, "2013-01-01 01:59:59", false, day_start: 2 end def test_day_of_month_start_of_day_day_start_2am - assert_result :day_of_month, 1, "2013-01-01 02:00:00", false, :day_start => 2 + assert_result :day_of_month, 1, "2013-01-01 02:00:00", false, day_start: 2 end def test_day_of_month_end_of_day_with_time_zone_day_start_2am - assert_result :day_of_month, 31, "2013-01-01 09:59:59", true, :day_start => 2 + assert_result :day_of_month, 31, "2013-01-01 09:59:59", true, day_start: 2 end def test_day_of_month_start_of_day_with_time_zone_day_start_2am - assert_result :day_of_month, 1, "2013-01-01 10:00:00", true, :day_start => 2 + assert_result :day_of_month, 1, "2013-01-01 10:00:00", true, day_start: 2 end # month of year def test_month_of_year_end_of_month @@ -394,23 +433,23 @@ end # month of year starts at 2am def test_month_of_year_end_of_month_day_start_2am - assert_result :month_of_year, 12, "2013-01-01 01:59:59", false, :day_start => 2 + assert_result :month_of_year, 12, "2013-01-01 01:59:59", false, day_start: 2 end def test_month_of_year_start_of_month_day_start_2am - assert_result :month_of_year, 1, "2013-01-01 02:00:00", false, :day_start => 2 + assert_result :month_of_year, 1, "2013-01-01 02:00:00", false, day_start: 2 end def test_month_of_year_end_of_month_with_time_zone_day_start_2am - assert_result :month_of_year, 12, "2013-01-01 09:59:59", true, :day_start => 2 + assert_result :month_of_year, 12, "2013-01-01 09:59:59", true, day_start: 2 end def test_month_of_year_start_of_month_with_time_zone_day_start_2am - assert_result :month_of_year, 1, "2013-01-01 10:00:00", true, :day_start => 2 + assert_result :month_of_year, 1, "2013-01-01 10:00:00", true, day_start: 2 end # zeros def test_zeros_second @@ -463,10 +502,18 @@ def test_zeros_month_time_zone assert_zeros :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", true end + def test_zeros_quarter + assert_zeros :quarter, "2013-04-16 20:00:00 UTC", ["2013-01-01 00:00:00 UTC", "2013-04-01 00:00:00 UTC", "2013-07-01 00:00:00 UTC"], "2013-01-01 00:00:00 UTC", "2013-09-30 23:59:59 UTC" + end + + def test_zeros_quarter_time_zone + assert_zeros :quarter, "2013-04-16 20:00:00 PDT", ["2013-01-01 00:00:00 PST", "2013-04-01 00:00:00 PDT", "2013-07-01 00:00:00 PDT"], "2013-01-01 00:00:00 PST", "2013-09-30 23:59:59 PDT", true + end + def test_zeros_year 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 def test_zeros_year_time_zone @@ -571,14 +618,16 @@ 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_reverse + skip if ActiveRecord::VERSION::MAJOR == 5 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 + skip if ActiveRecord::VERSION::MAJOR == 5 assert_equal 0, User.group_by_hour_of_day(:created_at).order("hour_of_day desc").reverse_order.count.keys.first end def test_order_hour_of_day_reverse_option assert_equal 23, call_method(:hour_of_day, :created_at, reverse: true).keys.first @@ -686,26 +735,26 @@ def test_respond_to_bad_method assert !User.group_by_day(:created_at).respond_to?(:no_such_method) end def test_last - create_user "2012-05-01 00:00:00 UTC" - create_user "2014-05-01 00:00:00 UTC" + create_user "#{this_year - 3}-01-01 00:00:00 UTC" + create_user "#{this_year - 1}-01-01 00:00:00 UTC" expected = { - utc.parse("2013-01-01 00:00:00 UTC") => 0, - utc.parse("2014-01-01 00:00:00 UTC") => 1, - utc.parse("2015-01-01 00:00:00 UTC") => 0 + utc.parse("#{this_year - 2}-01-01 00:00:00 UTC") => 0, + utc.parse("#{this_year - 1}-01-01 00:00:00 UTC") => 1, + utc.parse("#{this_year}-01-01 00:00:00 UTC") => 0 } assert_equal expected, User.group_by_year(:created_at, last: 3).count end def test_current - create_user "2012-05-01 00:00:00 UTC" - create_user "2014-05-01 00:00:00 UTC" + create_user "#{this_year - 3}-01-01 00:00:00 UTC" + create_user "#{this_year - 1}-01-01 00:00:00 UTC" expected = { - utc.parse("2013-01-01 00:00:00 UTC") => 0, - utc.parse("2014-01-01 00:00:00 UTC") => 1 + utc.parse("#{this_year - 2}-01-01 00:00:00 UTC") => 0, + utc.parse("#{this_year - 1}-01-01 00:00:00 UTC") => 1 } assert_equal expected, User.group_by_year(:created_at, last: 2, current: false).count end def test_format_day @@ -716,10 +765,15 @@ def test_format_month create_user "2014-03-01 00:00:00 UTC" assert_format :month, "March 2014", "%B %Y" end + def test_format_quarter + create_user "2014-03-05 00:00:00 UTC" + assert_format :quarter, "January 1, 2014", "%B %-e, %Y" + end + def test_format_year create_user "2014-03-01 00:00:00 UTC" assert_format :year, "2014", "%Y" end @@ -756,10 +810,15 @@ def test_format_locale create_user "2014-10-01 00:00:00 UTC" assert_equal ({"Okt" => 1}), User.group_by_day(:created_at, format: "%b", locale: :de).count end + def test_format_locale_by_symbol + create_user "2014-10-01 00:00:00 UTC" + assert_equal ({"Okt 1, 2014" => 1}), User.group_by_day(:created_at, format: :special, locale: :de).count + end + def test_format_locale_global create_user "2014-10-01 00:00:00 UTC" I18n.locale = :de assert_equal ({"Okt" => 1}), User.group_by_day(:created_at, format: "%b").count ensure @@ -773,11 +832,11 @@ end # permit def test_permit - assert_raises(ArgumentError, "Unpermitted period") { User.group_by_period(:day, :created_at, permit: %w[week]).count } + assert_raises(ArgumentError, "Unpermitted period") { User.group_by_period(:day, :created_at, permit: %w(week)).count } end def test_permit_bad_period assert_raises(ArgumentError, "Unpermitted period") { User.group_by_period(:bad_period, :created_at).count } end @@ -789,17 +848,28 @@ def test_permit_string_symbols assert_equal ({}), User.group_by_period("day", :created_at, permit: [:day]).count end def test_permit_symbol_strings - assert_equal ({}), User.group_by_period(:day, :created_at, permit: %w[day]).count + assert_equal ({}), User.group_by_period(:day, :created_at, permit: %w(day)).count end def test_permit_string_strings - assert_equal ({}), User.group_by_period("day", :created_at, permit: %w[day]).count + assert_equal ({}), User.group_by_period("day", :created_at, permit: %w(day)).count end + # default value + + def test_default_value + create_user "#{this_year}-01-01 00:00:00 UTC" + expected = { + utc.parse("#{this_year - 1}-01-01 00:00:00 UTC") => nil, + utc.parse("#{this_year}-01-01 00:00:00 UTC") => 1 + } + assert_equal expected, User.group_by_year(:created_at, last: 2, default_value: nil).count + end + # associations def test_associations user = create_user("2014-03-01 00:00:00 UTC") assert_empty user.posts.group_by_day(:created_at).count @@ -868,10 +938,14 @@ def call_method(method, field, options) User.group_by_period(method, field, options).count end def create_user(created_at, score = 1) - User.create! :name => "Andrew", :score => score, :created_at => utc.parse(created_at) + User.create! name: "Andrew", score: score, created_at: utc.parse(created_at) + end + + def this_year + Time.now.utc.year end def utc ActiveSupport::TimeZone["UTC"] end