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

- old
+ new

@@ -1,17 +1,15 @@ require "bundler/setup" Bundler.require(:default) require "minitest/autorun" require "minitest/pride" require "logger" -require "active_record" -# TODO determine why this is necessary -if RUBY_PLATFORM == "java" - ENV["TZ"] = "UTC" -end +Minitest::Test = Minitest::Unit::TestCase unless defined?(Minitest::Test) +ENV["TZ"] = "UTC" + # for debugging # ActiveRecord::Base.logger = Logger.new(STDOUT) # rails does this in activerecord/lib/active_record/railtie.rb ActiveRecord::Base.default_timezone = :utc @@ -22,10 +20,18 @@ end 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) + } +} + # migrations %w(postgresql mysql2).each do |adapter| ActiveRecord::Base.establish_connection :adapter => adapter, :database => "groupdate_test", :username => adapter == "mysql2" ? "root" : nil ActiveRecord::Migration.create_table :users, :force => true do |t| @@ -47,11 +53,15 @@ end # second def test_second_end_of_second - assert_result_time :second, "2013-05-03 00:00:00 UTC", "2013-05-03 00:00:00.999" + if ActiveRecord::Base.connection.adapter_name == "Mysql2" and 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 def test_second_start_of_second assert_result_time :second, "2013-05-03 00:00:01 UTC", "2013-05-03 00:00:01.000" end @@ -711,10 +721,23 @@ def test_format_month_of_year create_user "2014-01-01 00:00:00 UTC" assert_format :month_of_year, "Jan", "%b" end + 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_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 + I18n.locale = :en + end + def test_format_multiple_groups 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 @@ -735,10 +758,32 @@ ensure User.default_timezone = :utc end end + # Brasilia Summer Time + + def test_brasilia_summer_time + # must parse and convert to UTC for ActiveRecord 3.1 + create_user(brasilia.parse("2014-10-19 02:00:00").utc.to_s) + create_user(brasilia.parse("2014-10-20 02:00:00").utc.to_s) + expected = { + brasilia.parse("2014-10-19 01:00:00") => 1, + brasilia.parse("2014-10-20 00:00:00") => 1 + } + assert_equal expected, User.group_by_day(:created_at, time_zone: "Brasilia").count + end + + # carry_forward option + + def test_carry_forward + create_user "2014-05-01 00:00:00 UTC" + create_user "2014-05-01 00:00:00 UTC" + create_user "2014-05-03 00:00:00 UTC" + assert_equal 2, User.group_by_day(:created_at, carry_forward: true).count[utc.parse("2014-05-02 00:00:00 UTC")] + end + # helpers def assert_format(method, expected, format, options = {}) assert_equal expected, call_method(method, :created_at, options.merge(format: format)).keys.first end @@ -774,9 +819,13 @@ User.create! :name => "Andrew", :score => score, :created_at => utc.parse(created_at) end def utc ActiveSupport::TimeZone["UTC"] + end + + def brasilia + ActiveSupport::TimeZone["Brasilia"] end def teardown User.delete_all end