test/test_helper.rb in groupdate-2.1.1 vs test/test_helper.rb in groupdate-2.2.0

- old
+ new

@@ -1,10 +1,11 @@ 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 @@ -24,11 +25,11 @@ ActiveRecord::Base.establish_connection :adapter => adapter, :database => "groupdate_test", :username => adapter == "mysql2" ? "root" : nil ActiveRecord::Migration.create_table :users, :force => true do |t| t.string :name t.integer :score - t.timestamps + t.timestamp :created_at end end module TestGroupdate @@ -384,28 +385,28 @@ create_user "2013-05-01 00:00:00 UTC" expected = {} 7.times do |n| expected[n] = n == 3 ? 1 : 0 end - assert_equal expected, User.group_by_day_of_week(:created_at, range: true).count + assert_equal expected, call_method(:day_of_week, :created_at, {}) end def test_zeros_hour_of_day create_user "2013-05-01 20:00:00 UTC" expected = {} 24.times do |n| expected[n] = n == 20 ? 1 : 0 end - assert_equal expected, User.group_by_hour_of_day(:created_at, range: true).count + assert_equal expected, call_method(:hour_of_day, :created_at, {}) end def test_zeros_excludes_end create_user "2013-05-02 00:00:00 UTC" expected = { utc.parse("2013-05-01 00:00:00 UTC") => 0 } - assert_equal expected, User.group_by_day(:created_at, range: Time.parse("2013-05-01 00:00:00 UTC")...Time.parse("2013-05-02 00:00:00 UTC")).count + assert_equal expected, call_method(:day, :created_at, range: Time.parse("2013-05-01 00:00:00 UTC")...Time.parse("2013-05-02 00:00:00 UTC")) end def test_zeros_previous_scope create_user "2013-05-01 00:00:00 UTC" expected = { @@ -417,28 +418,28 @@ def test_zeros_datetime create_user "2013-05-01 00:00:00 UTC" expected = { utc.parse("2013-05-01 00:00:00 UTC") => 1 } - assert_equal expected, User.group_by_day(:created_at, range: DateTime.parse("2013-05-01 00:00:00 UTC")..DateTime.parse("2013-05-01 00:00:00 UTC")).count + assert_equal expected, call_method(:day, :created_at, range: DateTime.parse("2013-05-01 00:00:00 UTC")..DateTime.parse("2013-05-01 00:00:00 UTC")) end def test_zeros_null_value user = User.create!(name: "Andrew") user.update_column :created_at, nil - assert_equal 0, User.group_by_hour_of_day(:created_at, range: true).count[0] + assert_equal 0, call_method(:hour_of_day, :created_at, range: true)[0] end def test_zeroes_range_true create_user "2013-05-01 00:00:00 UTC" create_user "2013-05-03 00:00:00 UTC" expected = { utc.parse("2013-05-01 00:00:00 UTC") => 1, utc.parse("2013-05-02 00:00:00 UTC") => 0, utc.parse("2013-05-03 00:00:00 UTC") => 1 } - assert_equal expected, User.group_by_day(:created_at, range: true).count + assert_equal expected, call_method(:day, :created_at, range: true) end # week_start def test_week_start @@ -467,10 +468,14 @@ def test_order_hour_of_day_order_reverse 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 + end + def test_table_name assert_empty User.group_by_day("users.created_at").count end def test_previous_scopes @@ -479,11 +484,11 @@ end def test_time_zone create_user "2013-05-01 00:00:00 UTC" time_zone = "Pacific Time (US & Canada)" - assert_equal time_zone, User.group_by_day(:created_at, time_zone: time_zone).count.keys.first.time_zone.name + assert_equal time_zone, call_method(:day, :created_at, time_zone: time_zone).keys.first.time_zone.name end def test_where_after create_user "2013-05-01 00:00:00 UTC" create_user "2013-05-02 00:00:00 UTC" @@ -602,11 +607,11 @@ end # helpers def assert_format(method, expected, format, options = {}) - assert_equal expected, User.send(:"group_by_#{method}", :created_at, options.merge(format: format)).count.keys.first + assert_equal expected, call_method(method, :created_at, options.merge(format: format)).keys.first end def assert_result_time(method, expected, time_str, time_zone = false, options = {}) expected = {utc.parse(expected).in_time_zone(time_zone ? "Pacific Time (US & Canada)" : utc) => 1} assert_equal expected, result(method, time_str, time_zone, options) @@ -616,19 +621,23 @@ assert_equal 1, result(method, time_str, time_zone, options)[expected] end def result(method, time_str, time_zone = false, options = {}) create_user time_str - User.send(:"group_by_#{method}", :created_at, options.merge(time_zone: time_zone ? "Pacific Time (US & Canada)" : nil)).count + call_method(method, :created_at, options.merge(time_zone: time_zone ? "Pacific Time (US & Canada)" : nil)) end def assert_zeros(method, created_at, keys, range_start, range_end, time_zone = nil, options = {}) create_user created_at expected = {} keys.each_with_index do |key, i| expected[utc.parse(key).in_time_zone(time_zone ? "Pacific Time (US & Canada)" : utc)] = i == 1 ? 1 : 0 end - assert_equal expected, User.send(:"group_by_#{method}", :created_at, options.merge(time_zone: time_zone ? "Pacific Time (US & Canada)" : nil, range: Time.parse(range_start)..Time.parse(range_end))).count + 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 end def create_user(created_at, score = 1) User.create! :name => "Andrew", :score => score, :created_at => utc.parse(created_at) end