test/groupdate_test.rb in groupdate-0.1.3 vs test/groupdate_test.rb in groupdate-0.1.4
- old
+ new
@@ -14,11 +14,11 @@
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
+ 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
@@ -30,36 +30,40 @@
describe Groupdate do
%w(postgresql mysql2).each do |adapter|
describe adapter do
before do
- User.establish_connection adapter: adapter, database: "groupdate_test", username: adapter == "mysql2" ? "root" : nil
+ User.establish_connection :adapter => adapter, :database => "groupdate_test", :username => adapter == "mysql2" ? "root" : nil
User.delete_all
end
it "works!" do
[
- {name: "Andrew", score: 1, created_at: Time.parse("2013-04-01 00:00:00 UTC")},
- {name: "Jordan", score: 2, created_at: Time.parse("2013-04-01 00:00:00 UTC")},
- {name: "Nick", score: 3, created_at: Time.parse("2013-04-02 00:00:00 UTC")}
+ {:name => "Andrew", :score => 1, :created_at => Time.parse("2013-04-01 00:00:00 UTC")},
+ {:name => "Jordan", :score => 2, :created_at => Time.parse("2013-04-01 00:00:00 UTC")},
+ {:name => "Nick", :score => 3, :created_at => Time.parse("2013-04-02 00:00:00 UTC")}
].each{|u| User.create!(u) }
assert_equal(
- {
+ ordered_hash({
time_key("2013-04-01 00:00:00 UTC") => 1,
time_key("2013-04-02 00:00:00 UTC") => 1
- },
+ }),
User.where("score > 1").group_by_day(:created_at).count
)
end
it "doesn't throw exception with order" do
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 "allows for table name" do
+ assert_equal({}, User.group_by_day("users.created_at").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
it "group_by_minute" do
@@ -122,31 +126,31 @@
# helper methods
def assert_group(method, created_at, key, time_zone = nil)
create_user created_at
- assert_equal({time_key(key) => 1}, User.send(:"group_by_#{method}", :created_at, time_zone).count)
+ assert_equal(ordered_hash({time_key(key) => 1}), User.send(:"group_by_#{method}", :created_at, time_zone).count)
end
def assert_group_tz(method, created_at, key)
assert_group method, created_at, key, "Pacific Time (US & Canada)"
end
def assert_group_number(method, created_at, key, time_zone = nil)
create_user created_at
- assert_equal({number_key(key) => 1}, User.send(:"group_by_#{method}", :created_at, time_zone).count)
+ assert_equal(ordered_hash({number_key(key) => 1}), User.send(:"group_by_#{method}", :created_at, time_zone).count)
end
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)
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)
+ User.connection.adapter_name == "PostgreSQL" && ActiveRecord::VERSION::MAJOR == 3 ? Time.parse(key).strftime("%Y-%m-%d %H:%M:%S+00") : Time.parse(key)
end
end
def number_key(key)
if RUBY_PLATFORM == "java"
@@ -154,10 +158,14 @@
else
User.connection.adapter_name == "PostgreSQL" ? (ActiveRecord::VERSION::MAJOR == 3 ? key.to_s : key.to_f) : key
end
end
+ def ordered_hash(hash)
+ RUBY_VERSION =~ /1\.8/ ? hash.inject(ActiveSupport::OrderedHash.new){|h, (k, v)| h[k] = v; h } : hash
+ end
+
def create_user(created_at)
- User.create!(name: "Andrew", score: 1, created_at: Time.parse(created_at))
+ User.create!(:name => "Andrew", :score => 1, :created_at => Time.parse(created_at))
end
end