Commit c5df2119f88b9f73028e1f2e0f36a4d483773f9f

Authored by Andrew Kane
1 parent ef5976c4

Added test folder

test/mysql_test.rb 0 โ†’ 100644
... ... @@ -0,0 +1,22 @@
  1 +require "test_helper"
  2 +
  3 +class TestMysql < Minitest::Test
  4 + include TestGroupdate
  5 +
  6 + def setup
  7 + User.establish_connection :adapter => "mysql2", :database => "groupdate_test", :username => "root"
  8 + end
  9 +
  10 + def time_key(key)
  11 + if RUBY_PLATFORM == "java"
  12 + key.strftime("%Y-%m-%d %H:%M:%S").gsub(/ 00\:00\:00\z/, "")
  13 + else
  14 + key
  15 + end
  16 + end
  17 +
  18 + def number_key(key)
  19 + key
  20 + end
  21 +
  22 +end
... ...
test/postgresql_test.rb 0 โ†’ 100644
... ... @@ -0,0 +1,30 @@
  1 +require "test_helper"
  2 +
  3 +class TestPostgresql < Minitest::Test
  4 + include TestGroupdate
  5 +
  6 + def setup
  7 + User.establish_connection :adapter => "postgresql", :database => "groupdate_test"
  8 + end
  9 +
  10 + def time_key(key)
  11 + if RUBY_PLATFORM == "java"
  12 + key.utc.strftime("%Y-%m-%d %H:%M:%S%z")[0..-3]
  13 + else
  14 + if ActiveRecord::VERSION::MAJOR == 3
  15 + key.utc.strftime("%Y-%m-%d %H:%M:%S+00")
  16 + else
  17 + key
  18 + end
  19 + end
  20 + end
  21 +
  22 + def number_key(key)
  23 + if RUBY_PLATFORM != "java" and ActiveRecord::VERSION::MAJOR == 3
  24 + key.to_s
  25 + else
  26 + key
  27 + end
  28 + end
  29 +
  30 +end
... ...
test/test_helper.rb 0 โ†’ 100644
... ... @@ -0,0 +1,306 @@
  1 +require "bundler/setup"
  2 +Bundler.require(:default)
  3 +require "minitest/autorun"
  4 +require "minitest/pride"
  5 +require "logger"
  6 +
  7 +# for debugging
  8 +# ActiveRecord::Base.logger = Logger.new(STDOUT)
  9 +
  10 +# rails does this in activerecord/lib/active_record/railtie.rb
  11 +ActiveRecord::Base.default_timezone = :utc
  12 +ActiveRecord::Base.time_zone_aware_attributes = true
  13 +
  14 +class User < ActiveRecord::Base
  15 +end
  16 +
  17 +# migrations
  18 +%w(postgresql mysql2).each do |adapter|
  19 + ActiveRecord::Base.establish_connection :adapter => adapter, :database => "groupdate_test", :username => adapter == "mysql2" ? "root" : nil
  20 +
  21 + unless ActiveRecord::Base.connection.table_exists? "users"
  22 + ActiveRecord::Migration.create_table :users do |t|
  23 + t.string :name
  24 + t.integer :score
  25 + t.timestamps
  26 + end
  27 + end
  28 +end
  29 +
  30 +module TestGroupdate
  31 +
  32 + # second
  33 +
  34 + def test_second_end_of_second
  35 + assert_result_time :second, "2013-05-03 00:00:00 UTC", "2013-05-03 00:00:00.999"
  36 + end
  37 +
  38 + def test_second_start_of_second
  39 + assert_result_time :second, "2013-05-03 00:00:01 UTC", "2013-05-03 00:00:01.000"
  40 + end
  41 +
  42 + # minute
  43 +
  44 + def test_minute_end_of_minute
  45 + assert_result_time :minute, "2013-05-03 00:00:00 UTC", "2013-05-03 00:00:59"
  46 + end
  47 +
  48 + def test_minute_start_of_minute
  49 + assert_result_time :minute, "2013-05-03 00:01:00 UTC", "2013-05-03 00:01:00"
  50 + end
  51 +
  52 + # hour
  53 +
  54 + def test_hour_end_of_hour
  55 + assert_result_time :hour, "2013-05-03 00:00:00 UTC", "2013-05-03 00:59:59"
  56 + end
  57 +
  58 + def test_hour_start_of_hour
  59 + assert_result_time :hour, "2013-05-03 01:00:00 UTC", "2013-05-03 01:00:00"
  60 + end
  61 +
  62 + # day
  63 +
  64 + def test_day_end_of_day
  65 + assert_result_time :day, "2013-05-03 00:00:00 UTC", "2013-05-03 23:59:59"
  66 + end
  67 +
  68 + def test_day_start_of_day
  69 + assert_result_time :day, "2013-05-04 00:00:00 UTC", "2013-05-04 00:00:00"
  70 + end
  71 +
  72 + def test_day_end_of_day_with_time_zone
  73 + assert_result_time :day, "2013-05-02 00:00:00 PDT", "2013-05-03 06:59:59", true
  74 + end
  75 +
  76 + def test_day_start_of_day_with_time_zone
  77 + assert_result_time :day, "2013-05-03 00:00:00 PDT", "2013-05-03 07:00:00", true
  78 + end
  79 +
  80 + # week
  81 +
  82 + def test_week_end_of_week
  83 + assert_result_time :week, "2013-03-17 00:00:00 UTC", "2013-03-23 23:59:59"
  84 + end
  85 +
  86 + def test_week_start_of_week
  87 + assert_result_time :week, "2013-03-24 00:00:00 UTC", "2013-03-24 00:00:00"
  88 + end
  89 +
  90 + def test_week_end_of_week_with_time_zone
  91 + assert_result_time :week, "2013-03-10 00:00:00 PST", "2013-03-17 06:59:59", true
  92 + end
  93 +
  94 + def test_week_start_of_week_with_time_zone
  95 + assert_result_time :week, "2013-03-17 00:00:00 PDT", "2013-03-17 07:00:00", true
  96 + end
  97 +
  98 + # month
  99 +
  100 + def test_month_end_of_month
  101 + assert_result_time :month, "2013-05-01 00:00:00 UTC", "2013-05-31 23:59:59"
  102 + end
  103 +
  104 + def test_month_start_of_month
  105 + assert_result_time :month, "2013-06-01 00:00:00 UTC", "2013-06-01 00:00:00"
  106 + end
  107 +
  108 + def test_month_end_of_month_with_time_zone
  109 + assert_result_time :month, "2013-05-01 00:00:00 PDT", "2013-06-01 06:59:59", true
  110 + end
  111 +
  112 + def test_month_start_of_month_with_time_zone
  113 + assert_result_time :month, "2013-06-01 00:00:00 PDT", "2013-06-01 07:00:00", true
  114 + end
  115 +
  116 + # year
  117 +
  118 + def test_year_end_of_year
  119 + assert_result_time :year, "2013-01-01 00:00:00 UTC", "2013-12-31 23:59:59"
  120 + end
  121 +
  122 + def test_year_start_of_year
  123 + assert_result_time :year, "2014-01-01 00:00:00 UTC", "2014-01-01 00:00:00"
  124 + end
  125 +
  126 + def test_year_end_of_year_with_time_zone
  127 + assert_result_time :year, "2013-01-01 00:00:00 PST", "2014-01-01 07:59:59", true
  128 + end
  129 +
  130 + def test_year_start_of_year_with_time_zone
  131 + assert_result_time :year, "2014-01-01 00:00:00 PST", "2014-01-01 08:00:00", true
  132 + end
  133 +
  134 + # hour of day
  135 +
  136 + def test_hour_of_day_end_of_hour
  137 + assert_result :hour_of_day, 0, "2013-01-01 00:59:59"
  138 + end
  139 +
  140 + def test_hour_of_day_start_of_hour
  141 + assert_result :hour_of_day, 1, "2013-01-01 01:00:00"
  142 + end
  143 +
  144 + def test_hour_of_day_end_of_hour_with_time_zone
  145 + assert_result :hour_of_day, 0, "2013-01-01 08:59:59", true
  146 + end
  147 +
  148 + def test_hour_of_day_start_of_hour_with_time_zone
  149 + assert_result :hour_of_day, 1, "2013-01-01 09:00:00", true
  150 + end
  151 +
  152 + # day of week
  153 +
  154 + def test_day_of_week_end_of_day
  155 + assert_result :day_of_week, 2, "2013-01-01 23:59:59"
  156 + end
  157 +
  158 + def test_day_of_week_start_of_day
  159 + assert_result :day_of_week, 3, "2013-01-02 00:00:00"
  160 + end
  161 +
  162 + def test_day_of_week_end_of_week_with_time_zone
  163 + assert_result :day_of_week, 2, "2013-01-02 07:59:59", true
  164 + end
  165 +
  166 + def test_day_of_week_start_of_week_with_time_zone
  167 + assert_result :day_of_week, 3, "2013-01-02 08:00:00", true
  168 + end
  169 +
  170 + # zeros
  171 +
  172 + def test_zeros_second
  173 + 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"
  174 + end
  175 +
  176 + def test_zeros_minute
  177 + 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"
  178 + end
  179 +
  180 + def test_zeros_hour
  181 + 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"
  182 + end
  183 +
  184 + def test_zeros_day
  185 + 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"
  186 + end
  187 +
  188 + def test_zeros_day_time_zone
  189 + assert_zeros :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", true
  190 + end
  191 +
  192 + def test_zeros_week
  193 + 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"
  194 + end
  195 +
  196 + def test_zeros_week_time_zone
  197 + assert_zeros :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", true
  198 + end
  199 +
  200 + def test_zeros_month
  201 + 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"
  202 + end
  203 +
  204 + def test_zeros_month_time_zone
  205 + 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
  206 + end
  207 +
  208 + def test_zeros_year
  209 + 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"
  210 + end
  211 +
  212 + def test_zeros_year_time_zone
  213 + assert_zeros :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", true
  214 + end
  215 +
  216 + def test_zeros_day_of_week
  217 + create_user "2013-05-01 00:00:00 UTC"
  218 + expected = {}
  219 + 7.times do |n|
  220 + expected[n] = n == 3 ? 1 : 0
  221 + end
  222 + assert_equal(expected, User.group_by_day_of_week(:created_at, Time.zone, true).count(:created_at))
  223 + end
  224 +
  225 + def test_zeros_hour_of_day
  226 + create_user "2013-05-01 20:00:00 UTC"
  227 + expected = {}
  228 + 24.times do |n|
  229 + expected[n] = n == 20 ? 1 : 0
  230 + end
  231 + assert_equal(expected, User.group_by_hour_of_day(:created_at, Time.zone, true).count(:created_at))
  232 + end
  233 +
  234 + def test_zeros_excludes_end
  235 + create_user "2013-05-02 00:00:00 UTC"
  236 + expected = {
  237 + Time.parse("2013-05-01 00:00:00 UTC") => 0
  238 + }
  239 + 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)
  240 + end
  241 +
  242 + def test_zeros_previous_scope
  243 + create_user "2013-05-01 00:00:00 UTC"
  244 + expected = {
  245 + Time.parse("2013-05-01 00:00:00 UTC") => 0
  246 + }
  247 + assert_equal(expected, User.where("id = 0").group_by_day(:created_at, Time.zone, Time.parse("2013-05-01 00:00:00 UTC")..Time.parse("2013-05-01 23:59:59 UTC")).count)
  248 + end
  249 +
  250 + # misc
  251 +
  252 + def test_order_day
  253 + assert_equal({}, User.group_by_day(:created_at).order("day desc").limit(20).count)
  254 + end
  255 +
  256 + def test_order_week
  257 + assert_equal({}, User.group_by_week(:created_at).order("week asc").count)
  258 + end
  259 +
  260 + def test_order_hour_of_day
  261 + assert_equal({}, User.group_by_hour_of_day(:created_at).order("hour_of_day desc").count)
  262 + end
  263 +
  264 + def test_table_name
  265 + assert_equal({}, User.group_by_day("users.created_at").count)
  266 + end
  267 +
  268 + def test_previous_scopes
  269 + create_user "2013-05-01 00:00:00 UTC"
  270 + assert_equal({}, User.where("id = 0").group_by_day(:created_at).count)
  271 + end
  272 +
  273 + # helpers
  274 +
  275 + def assert_result_time(method, expected, time_str, time_zone = false)
  276 + assert_result method, Time.parse(expected), time_str, time_zone
  277 + end
  278 +
  279 + def assert_result(method, expected, time_str, time_zone = false)
  280 + create_user time_str
  281 + expected = expected.is_a?(Time) ? time_key(expected) : number_key(expected)
  282 + assert_equal(ordered_hash({expected => 1}), User.send(:"group_by_#{method}", :created_at, time_zone ? "Pacific Time (US & Canada)" : nil).order(method.to_s).count)
  283 + end
  284 +
  285 + def assert_zeros(method, created_at, keys, range_start, range_end, time_zone = nil)
  286 + create_user created_at
  287 + expected = {}
  288 + keys.each_with_index do |key, i|
  289 + expected[Time.parse(key)] = i == 1 ? 1 : 0
  290 + end
  291 + assert_equal(expected, User.send(:"group_by_#{method}", :created_at, time_zone ? "Pacific Time (US & Canada)" : nil, Time.parse(range_start)..Time.parse(range_end)).count)
  292 + end
  293 +
  294 + def ordered_hash(hash)
  295 + RUBY_VERSION =~ /1\.8/ ? hash.inject(ActiveSupport::OrderedHash.new){|h, (k, v)| h[k] = v; h } : hash
  296 + end
  297 +
  298 + def create_user(created_at)
  299 + User.create!(:name => "Andrew", :score => 1, :created_at => ActiveSupport::TimeZone["UTC"].parse(created_at))
  300 + end
  301 +
  302 + def teardown
  303 + User.delete_all
  304 + end
  305 +
  306 +end
... ...