Commit 18eb7af70afbaa9dc77ccc65e41090c064ea58c6
1 parent
9d92cbf3
Exists in
cumulative_sum
Fixed tests
Showing
3 changed files
with
42 additions
and
26 deletions
Show diff stats
lib/groupdate/magic.rb
... | ... | @@ -22,10 +22,10 @@ module Groupdate |
22 | 22 | def relation(column, relation) |
23 | 23 | column = relation.connection.quote_table_name(column) |
24 | 24 | time_zone = self.time_zone.tzinfo.name |
25 | + @relation = relation | |
25 | 26 | |
26 | - adapter_name = relation.connection.adapter_name | |
27 | 27 | query = |
28 | - if mysql?(adapter_name) | |
28 | + if mysql? | |
29 | 29 | case field |
30 | 30 | when :day_of_week # Sunday = 0, Monday = 1, etc |
31 | 31 | # use CONCAT for consistent return type (String) |
... | ... | @@ -53,7 +53,7 @@ module Groupdate |
53 | 53 | |
54 | 54 | ["DATE_ADD(CONVERT_TZ(DATE_FORMAT(CONVERT_TZ(DATE_SUB(#{column}, INTERVAL #{day_start} HOUR), '+00:00', ?), '#{format}'), ?, '+00:00'), INTERVAL #{day_start} HOUR)", time_zone, time_zone] |
55 | 55 | end |
56 | - elsif postgresql?(adapter_name) | |
56 | + elsif postgresql? | |
57 | 57 | case field |
58 | 58 | when :day_of_week |
59 | 59 | ["EXTRACT(DOW from (#{column}::timestamptz AT TIME ZONE ? - INTERVAL '#{day_start} hour'))::integer", time_zone] |
... | ... | @@ -239,12 +239,12 @@ module Groupdate |
239 | 239 | end] |
240 | 240 | end |
241 | 241 | |
242 | - def mysql?(adapter_name) | |
243 | - ["MySQL", "Mysql2"].include?(adapter_name) | |
242 | + def mysql? | |
243 | + ["MySQL", "Mysql2"].include?(@relation.connection.adapter_name) | |
244 | 244 | end |
245 | 245 | |
246 | - def postgresql?(adapter_name) | |
247 | - ["PostgreSQL", "PostGIS"].include?(adapter_name) | |
246 | + def postgresql? | |
247 | + ["PostgreSQL", "PostGIS"].include?(@relation.connection.adapter_name) | |
248 | 248 | end |
249 | 249 | |
250 | 250 | def round_time(time) | ... | ... |
lib/groupdate/series.rb
... | ... | @@ -10,10 +10,10 @@ module Groupdate |
10 | 10 | def cumulative_sum |
11 | 11 | magic.options[:carry_forward] = true |
12 | 12 | sql = |
13 | - if magic.send(:postgresql?, relation.connection.adapter_name) | |
13 | + if magic.send(:postgresql?) | |
14 | 14 | "SUM(COUNT(id)) OVER (ORDER BY #{relation.group_values[0]})::integer" |
15 | 15 | else |
16 | - raise "`cumulative_sum` not supported with MySQL" | |
16 | + raise NoMethodError, "method `cumulative_sum' not supported for MySQL" | |
17 | 17 | end |
18 | 18 | |
19 | 19 | custom(sql) | ... | ... |
test/test_helper.rb
... | ... | @@ -625,26 +625,34 @@ module TestGroupdate |
625 | 625 | # cumulative sum |
626 | 626 | |
627 | 627 | def test_cumulative_sum |
628 | - create_user "2011-05-01 00:00:00 UTC" | |
629 | - create_user "2013-05-01 00:00:00 UTC" | |
630 | - create_user "2013-05-01 00:00:00 UTC" | |
631 | - expected = { | |
632 | - utc.parse("2011-01-01 00:00:00 UTC") => 1, | |
633 | - utc.parse("2012-01-01 00:00:00 UTC") => 1, | |
634 | - utc.parse("2013-01-01 00:00:00 UTC") => 3 | |
635 | - } | |
636 | - assert_equal expected, User.group_by_year(:created_at).cumulative_sum | |
628 | + if postgresql? | |
629 | + create_user "2011-05-01 00:00:00 UTC" | |
630 | + create_user "2013-05-01 00:00:00 UTC" | |
631 | + create_user "2013-05-01 00:00:00 UTC" | |
632 | + expected = { | |
633 | + utc.parse("2011-01-01 00:00:00 UTC") => 1, | |
634 | + utc.parse("2012-01-01 00:00:00 UTC") => 1, | |
635 | + utc.parse("2013-01-01 00:00:00 UTC") => 3 | |
636 | + } | |
637 | + assert_equal expected, User.group_by_year(:created_at).cumulative_sum | |
638 | + elsif mysql? | |
639 | + assert_raises(NoMethodError) { User.group_by_year(:created_at).cumulative_sum } | |
640 | + end | |
637 | 641 | end |
638 | 642 | |
639 | 643 | def test_cumulative_sum_last |
640 | - create_user "2011-05-01 00:00:00 UTC" | |
641 | - create_user "2013-05-01 00:00:00 UTC" | |
642 | - create_user "2013-05-01 00:00:00 UTC" | |
643 | - expected = { | |
644 | - utc.parse("2013-01-01 00:00:00 UTC") => 3, | |
645 | - utc.parse("2014-01-01 00:00:00 UTC") => 3 | |
646 | - } | |
647 | - assert_equal expected, User.group_by_year(:created_at, last: 2).cumulative_sum | |
644 | + if postgresql? | |
645 | + create_user "2011-05-01 00:00:00 UTC" | |
646 | + create_user "2013-05-01 00:00:00 UTC" | |
647 | + create_user "2013-05-01 00:00:00 UTC" | |
648 | + expected = { | |
649 | + utc.parse("2013-01-01 00:00:00 UTC") => 3, | |
650 | + utc.parse("2014-01-01 00:00:00 UTC") => 3 | |
651 | + } | |
652 | + assert_equal expected, User.group_by_year(:created_at, last: 2).cumulative_sum | |
653 | + elsif mysql? | |
654 | + assert_raises(NoMethodError) { User.group_by_year(:created_at, last: 2).cumulative_sum } | |
655 | + end | |
648 | 656 | end |
649 | 657 | |
650 | 658 | # helpers |
... | ... | @@ -688,6 +696,14 @@ module TestGroupdate |
688 | 696 | ActiveSupport::TimeZone["UTC"] |
689 | 697 | end |
690 | 698 | |
699 | + def postgresql? | |
700 | + self.class == TestPostgresql | |
701 | + end | |
702 | + | |
703 | + def mysql? | |
704 | + self.class == TestMysql | |
705 | + end | |
706 | + | |
691 | 707 | def teardown |
692 | 708 | User.delete_all |
693 | 709 | end | ... | ... |