Commit 271220c3179a7b2578695247db37c24cc2329249
1 parent
d5e8b92d
Exists in
master
and in
17 other branches
Much better tests
Showing
3 changed files
with
24 additions
and
15 deletions
Show diff stats
Gemfile
lib/groupdate.rb
test/groupdate_test.rb
1 | +require "bundler/setup" | ||
2 | +Bundler.require(:default) | ||
1 | require "minitest/autorun" | 3 | require "minitest/autorun" |
2 | require "minitest/pride" | 4 | require "minitest/pride" |
3 | -require "active_record" | ||
4 | -require "groupdate" | ||
5 | require "logger" | 5 | require "logger" |
6 | 6 | ||
7 | # for debugging | 7 | # for debugging |
@@ -37,7 +37,10 @@ describe Groupdate do | @@ -37,7 +37,10 @@ describe Groupdate do | ||
37 | ].each{|u| User.create!(u) } | 37 | ].each{|u| User.create!(u) } |
38 | 38 | ||
39 | assert_equal( | 39 | assert_equal( |
40 | - {Time.parse("2013-04-01 00:00:00 UTC") => 1, Time.parse("2013-04-02 00:00:00 UTC") => 1}, | 40 | + { |
41 | + time_key("2013-04-01 00:00:00 UTC") => 1, | ||
42 | + time_key("2013-04-02 00:00:00 UTC") => 1 | ||
43 | + }, | ||
41 | User.where("score > 1").group_by_day(:created_at).count | 44 | User.where("score > 1").group_by_day(:created_at).count |
42 | ) | 45 | ) |
43 | end | 46 | end |
@@ -87,19 +90,19 @@ describe Groupdate do | @@ -87,19 +90,19 @@ describe Groupdate do | ||
87 | end | 90 | end |
88 | 91 | ||
89 | it "group_by_hour_of_day" do | 92 | it "group_by_hour_of_day" do |
90 | - assert_group_number :hour_of_day, "2013-01-01 11:00:00 UTC", 11, adapter | 93 | + assert_group_number :hour_of_day, "2013-01-01 11:00:00 UTC", 11 |
91 | end | 94 | end |
92 | 95 | ||
93 | it "group_by_hour_of_day with time zone" do | 96 | it "group_by_hour_of_day with time zone" do |
94 | - assert_group_number_tz :hour_of_day, "2013-01-01 11:00:00 UTC", 3, adapter | 97 | + assert_group_number_tz :hour_of_day, "2013-01-01 11:00:00 UTC", 3 |
95 | end | 98 | end |
96 | 99 | ||
97 | it "group_by_day_of_week" do | 100 | it "group_by_day_of_week" do |
98 | - assert_group_number :day_of_week, "2013-03-03 00:00:00 UTC", 0, adapter | 101 | + assert_group_number :day_of_week, "2013-03-03 00:00:00 UTC", 0 |
99 | end | 102 | end |
100 | 103 | ||
101 | it "group_by_day_of_week with time zone" do | 104 | it "group_by_day_of_week with time zone" do |
102 | - assert_group_number_tz :day_of_week, "2013-03-03 00:00:00 UTC", 6, adapter | 105 | + assert_group_number_tz :day_of_week, "2013-03-03 00:00:00 UTC", 6 |
103 | end | 106 | end |
104 | end | 107 | end |
105 | end | 108 | end |
@@ -108,21 +111,28 @@ describe Groupdate do | @@ -108,21 +111,28 @@ describe Groupdate do | ||
108 | 111 | ||
109 | def assert_group(method, created_at, key, time_zone = nil) | 112 | def assert_group(method, created_at, key, time_zone = nil) |
110 | create_user created_at | 113 | create_user created_at |
111 | - assert_equal({Time.parse(key) => 1}, User.send(:"group_by_#{method}", :created_at, time_zone).count) | 114 | + assert_equal({time_key(key) => 1}, User.send(:"group_by_#{method}", :created_at, time_zone).count) |
112 | end | 115 | end |
113 | 116 | ||
114 | def assert_group_tz(method, created_at, key) | 117 | def assert_group_tz(method, created_at, key) |
115 | assert_group method, created_at, key, "Pacific Time (US & Canada)" | 118 | assert_group method, created_at, key, "Pacific Time (US & Canada)" |
116 | end | 119 | end |
117 | 120 | ||
118 | - def assert_group_number(method, created_at, key, adapter, time_zone = nil) | 121 | + def assert_group_number(method, created_at, key, time_zone = nil) |
119 | create_user created_at | 122 | create_user created_at |
120 | - key = adapter == "postgresql" ? key.to_f : key | ||
121 | - assert_equal({key => 1}, User.send(:"group_by_#{method}", :created_at, time_zone).count) | 123 | + assert_equal({number_key(key) => 1}, User.send(:"group_by_#{method}", :created_at, time_zone).count) |
122 | end | 124 | end |
123 | 125 | ||
124 | - def assert_group_number_tz(method, created_at, key, adapter) | ||
125 | - assert_group_number method, created_at, key, adapter, "Pacific Time (US & Canada)" | 126 | + def assert_group_number_tz(method, created_at, key) |
127 | + assert_group_number method, created_at, key, "Pacific Time (US & Canada)" | ||
128 | + end | ||
129 | + | ||
130 | + def time_key(key) | ||
131 | + User.connection.adapter_name == "PostgreSQL" && ActiveRecord::VERSION::MAJOR == 3 ? Time.parse(key).strftime("%Y-%m-%d %H:%M:%S+00") : Time.parse(key) | ||
132 | + end | ||
133 | + | ||
134 | + def number_key(key) | ||
135 | + User.connection.adapter_name == "PostgreSQL" ? (ActiveRecord::VERSION::MAJOR == 3 ? key.to_s : key.to_f) : key | ||
126 | end | 136 | end |
127 | 137 | ||
128 | def create_user(created_at) | 138 | def create_user(created_at) |