test/groupdate_test.rb in groupdate-0.1.2 vs test/groupdate_test.rb in groupdate-0.1.3

- old
+ new

@@ -12,22 +12,29 @@ ActiveRecord::Base.time_zone_aware_attributes = true class User < ActiveRecord::Base end +# migrations +%w(postgresql mysql2).each do |adapter| + ActiveRecord::Base.establish_connection adapter: adapter, database: "groupdate_test", username: adapter == "mysql2" ? "root" : nil + + unless ActiveRecord::Base.connection.table_exists? "users" + ActiveRecord::Migration.create_table :users do |t| + t.string :name + t.integer :score + t.timestamps + end + end +end + describe Groupdate do %w(postgresql mysql2).each do |adapter| describe adapter do - before do - ActiveRecord::Base.establish_connection adapter: adapter, database: "groupdate" - # ActiveRecord::Migration.create_table :users do |t| - # t.string :name - # t.integer :score - # t.timestamps - # end - + before do + User.establish_connection adapter: adapter, database: "groupdate_test", username: adapter == "mysql2" ? "root" : nil User.delete_all end it "works!" do [ @@ -44,11 +51,13 @@ User.where("score > 1").group_by_day(:created_at).count ) end it "doesn't throw exception with order" do - User.group_by_day(:created_at).order("group_field desc").limit(20).count == {} + assert_equal({}, User.group_by_day(:created_at).order("day desc").limit(20).count) + assert_equal({}, User.group_by_week(:created_at).order("week asc").count) + assert_equal({}, User.group_by_hour_of_day(:created_at).order("hour_of_day desc").count) end it "group_by_second" do assert_group :second, "2013-04-01 00:00:01 UTC", "2013-04-01 00:00:01 UTC" end @@ -130,14 +139,22 @@ def assert_group_number_tz(method, created_at, key) assert_group_number method, created_at, key, "Pacific Time (US & Canada)" end def time_key(key) - User.connection.adapter_name == "PostgreSQL" && ActiveRecord::VERSION::MAJOR == 3 ? Time.parse(key).strftime("%Y-%m-%d %H:%M:%S+00") : Time.parse(key) + if RUBY_PLATFORM == "java" + User.connection.adapter_name == "PostgreSQL" ? Time.parse(key).strftime("%Y-%m-%d %H:%M:%S%z")[0..-3] : Time.parse(key).strftime("%Y-%m-%d %H:%M:%S").gsub(/ 00\:00\:00\z/, "") + else + User.connection.adapter_name == "PostgreSQL" && ActiveRecord::VERSION::MAJOR == 3 ? Time.parse(key).strftime("%Y-%m-%d %H:%M:%S%z")[0..-3] : Time.parse(key) + end end def number_key(key) - User.connection.adapter_name == "PostgreSQL" ? (ActiveRecord::VERSION::MAJOR == 3 ? key.to_s : key.to_f) : key + if RUBY_PLATFORM == "java" + User.connection.adapter_name == "PostgreSQL" ? key.to_f : key + else + User.connection.adapter_name == "PostgreSQL" ? (ActiveRecord::VERSION::MAJOR == 3 ? key.to_s : key.to_f) : key + end end def create_user(created_at) User.create!(name: "Andrew", score: 1, created_at: Time.parse(created_at)) end