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

- old
+ new

@@ -22,14 +22,12 @@ 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) } # migrations %w(postgresql mysql2).each do |adapter| ActiveRecord::Base.establish_connection :adapter => adapter, :database => "groupdate_test", :username => adapter == "mysql2" ? "root" : nil @@ -45,19 +43,18 @@ t.timestamp :created_at end end module TestGroupdate - def setup Groupdate.week_start = :sun end # second def test_second_end_of_second - if ActiveRecord::Base.connection.adapter_name == "Mysql2" and ActiveRecord::VERSION::STRING.starts_with?("4.2.") + if ActiveRecord::Base.connection.adapter_name == "Mysql2" && ActiveRecord::VERSION::STRING.starts_with?("4.2.") skip # no millisecond precision else assert_result_time :second, "2013-05-03 00:00:00 UTC", "2013-05-03 00:00:00.999" end end @@ -637,10 +634,33 @@ [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_group_day_of_week + 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, 0] => 0, + [1, 1] => 0, + [1, 2] => 0, + [1, 3] => 1, + [1, 4] => 0, + [1, 5] => 0, + [1, 6] => 0, + [2, 0] => 0, + [2, 1] => 0, + [2, 2] => 0, + [2, 3] => 0, + [2, 4] => 1, + [2, 5] => 1, + [2, 6] => 0 + } + assert_equal expected, User.group(:score).group_by_day_of_week(:created_at).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 } @@ -666,20 +686,30 @@ def test_respond_to_bad_method assert !User.group_by_day(:created_at).respond_to?(:no_such_method) end def test_last - create_user "2011-05-01 00:00:00 UTC" - create_user "2013-05-01 00:00:00 UTC" + create_user "2012-05-01 00:00:00 UTC" + create_user "2014-05-01 00:00:00 UTC" expected = { - utc.parse("2012-01-01 00:00:00 UTC") => 0, - utc.parse("2013-01-01 00:00:00 UTC") => 1, - utc.parse("2014-01-01 00:00:00 UTC") => 0 + 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 } 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" + expected = { + utc.parse("2013-01-01 00:00:00 UTC") => 0, + utc.parse("2014-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 create_user "2014-03-01 00:00:00 UTC" assert_format :day, "March 1, 2014", "%B %-e, %Y" end @@ -740,26 +770,50 @@ create_user "2014-03-01 00:00:00 UTC" assert_equal ({["Sun", 1] => 1}), User.group_by_week(:created_at, format: "%a").group(:score).count assert_equal ({[1, "Sun"] => 1}), User.group(:score).group_by_week(:created_at, format: "%a").count end + # permit + + def test_permit + 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 + + def test_permit_symbol_symbols + assert_equal ({}), User.group_by_period(:day, :created_at, permit: [:day]).count + end + + 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 + end + + def test_permit_string_strings + assert_equal ({}), User.group_by_period("day", :created_at, permit: %w[day]).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 end # activerecord default_timezone option def test_default_timezone_local - begin - User.default_timezone = :local - assert_raises(RuntimeError){ User.group_by_day(:created_at).count } - ensure - User.default_timezone = :utc - end + User.default_timezone = :local + assert_raises(RuntimeError) { User.group_by_day(:created_at).count } + ensure + User.default_timezone = :utc end # Brasilia Summer Time def test_brasilia_summer_time @@ -810,11 +864,11 @@ end assert_equal expected, call_method(method, :created_at, options.merge(time_zone: time_zone ? "Pacific Time (US & Canada)" : nil, range: Time.parse(range_start)..Time.parse(range_end))) end def call_method(method, field, options) - User.send(:"group_by_#{method}", field, options).count + 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) end @@ -828,7 +882,6 @@ end def teardown User.delete_all end - end