Commit 14bf05e442373feaf61c4f343dfdf54286f0bba5

Authored by Andrew Kane
1 parent 791b0bfb

Slightly better tests

test/enumerable_test.rb
1 1 require_relative "test_helper"
  2 +require "ostruct"
2 3  
3 4 class TestEnumerable < Minitest::Test
4 5 include TestGroupdate
5 6  
6 7 def test_enumerable
7   - user_a = User.new(created_at: utc.parse("2014-01-21"))
8   - user_b = User.new(created_at: utc.parse("2014-03-14"))
  8 + user_a = create_user("2014-01-21")
  9 + user_b = create_user("2014-03-14")
9 10 expected = {
10 11 utc.parse("2014-01-01") => [user_a],
11 12 utc.parse("2014-02-01") => [],
... ... @@ -19,6 +20,27 @@ class TestEnumerable &lt; Minitest::Test
19 20 end
20 21  
21 22 def call_method(method, field, options)
22   - Hash[User.all.to_a.group_by_period(method, options) { |u| u.send(field) }.map { |k, v| [k, v.size] }]
  23 + # p @users.group_by_period(method, options) { |u| u.send(field) }
  24 + # p field
  25 + Hash[@users.group_by_period(method, options) { |u| u.send(field) }.map { |k, v| [k, v.size] }]
  26 + end
  27 +
  28 + def create_user(created_at, score = 1)
  29 + user = OpenStruct.new(name: "Andrew", score: score, created_at: created_at ? utc.parse(created_at) : nil)
  30 + @users << user
  31 + user
  32 + end
  33 +
  34 + def setup
  35 + super
  36 + @users = []
  37 + end
  38 +
  39 + def teardown
  40 + # do nothing
  41 + end
  42 +
  43 + def enumerable_test?
  44 + true
23 45 end
24 46 end
... ...
test/mysql_test.rb
... ... @@ -2,9 +2,14 @@ require_relative &quot;test_helper&quot;
2 2  
3 3 class TestMysql < Minitest::Test
4 4 include TestGroupdate
  5 + include TestDatabase
5 6  
6 7 def setup
7 8 super
8   - User.establish_connection adapter: "mysql2", database: "groupdate_test", username: "root"
  9 + @@setup ||= begin
  10 + ActiveRecord::Base.establish_connection adapter: "mysql2", database: "groupdate_test", username: "root"
  11 + create_tables
  12 + true
  13 + end
9 14 end
10 15 end
... ...
test/postgresql_test.rb
... ... @@ -2,13 +2,14 @@ require_relative &quot;test_helper&quot;
2 2  
3 3 class TestPostgresql < Minitest::Test
4 4 include TestGroupdate
  5 + include TestDatabase
5 6  
6 7 def setup
7 8 super
8   - User.establish_connection adapter: "postgresql", database: "groupdate_test"
9   - end
10   -
11   - def test_no_column
12   - assert_raises(ArgumentError) { User.group_by_day.first }
  9 + @@setup ||= begin
  10 + ActiveRecord::Base.establish_connection adapter: "postgresql", database: "groupdate_test"
  11 + create_tables
  12 + true
  13 + end
13 14 end
14 15 end
... ...
test/test_helper.rb
... ... @@ -32,8 +32,8 @@ time: {
32 32 }
33 33  
34 34 # migrations
35   -%w(postgresql mysql2).each do |adapter|
36   - ActiveRecord::Base.establish_connection adapter: adapter, database: "groupdate_test", username: adapter == "mysql2" ? "root" : nil
  35 +def create_tables
  36 + ActiveRecord::Migration.verbose = false
37 37  
38 38 ActiveRecord::Migration.create_table :users, force: true do |t|
39 39 t.string :name
... ... @@ -47,6 +47,278 @@ time: {
47 47 end
48 48 end
49 49  
  50 +module TestDatabase
  51 + def test_zeros_previous_scope
  52 + create_user "2013-05-01 00:00:00 UTC"
  53 + expected = {
  54 + utc.parse("2013-05-01 00:00:00 UTC") => 0
  55 + }
  56 + assert_equal expected, User.where("id = 0").group_by_day(:created_at, range: Time.parse("2013-05-01 00:00:00 UTC")..Time.parse("2013-05-01 23:59:59 UTC")).count
  57 + end
  58 +
  59 + def test_order_hour_of_day
  60 + assert_equal 23, User.group_by_hour_of_day(:created_at).order("hour_of_day desc").count.keys.first
  61 + end
  62 +
  63 + def test_order_hour_of_day_case
  64 + assert_equal 23, User.group_by_hour_of_day(:created_at).order("hour_of_day DESC").count.keys.first
  65 + end
  66 +
  67 + def test_order_hour_of_day_reverse
  68 + skip if ActiveRecord::VERSION::MAJOR == 5
  69 + assert_equal 23, User.group_by_hour_of_day(:created_at).reverse_order.count.keys.first
  70 + end
  71 +
  72 + def test_order_hour_of_day_order_reverse
  73 + skip if ActiveRecord::VERSION::MAJOR == 5
  74 + assert_equal 0, User.group_by_hour_of_day(:created_at).order("hour_of_day desc").reverse_order.count.keys.first
  75 + end
  76 +
  77 + def test_table_name
  78 + assert_empty User.group_by_day("users.created_at").count
  79 + end
  80 +
  81 + def test_previous_scopes
  82 + create_user "2013-05-01 00:00:00 UTC"
  83 + assert_empty User.where("id = 0").group_by_day(:created_at).count
  84 + end
  85 +
  86 + def test_where_after
  87 + create_user "2013-05-01 00:00:00 UTC"
  88 + create_user "2013-05-02 00:00:00 UTC"
  89 + expected = {utc.parse("2013-05-02 00:00:00 UTC") => 1}
  90 + assert_equal expected, User.group_by_day(:created_at).where("created_at > ?", "2013-05-01 00:00:00 UTC").count
  91 + end
  92 +
  93 + def test_group_before
  94 + create_user "2013-05-01 00:00:00 UTC", 1
  95 + create_user "2013-05-02 00:00:00 UTC", 2
  96 + create_user "2013-05-03 00:00:00 UTC", 2
  97 + expected = {
  98 + [1, utc.parse("2013-05-01 00:00:00 UTC")] => 1,
  99 + [1, utc.parse("2013-05-02 00:00:00 UTC")] => 0,
  100 + [1, utc.parse("2013-05-03 00:00:00 UTC")] => 0,
  101 + [2, utc.parse("2013-05-01 00:00:00 UTC")] => 0,
  102 + [2, utc.parse("2013-05-02 00:00:00 UTC")] => 1,
  103 + [2, utc.parse("2013-05-03 00:00:00 UTC")] => 1
  104 + }
  105 + assert_equal expected, User.group(:score).group_by_day(:created_at).order(:score).count
  106 + end
  107 +
  108 + def test_group_after
  109 + create_user "2013-05-01 00:00:00 UTC", 1
  110 + create_user "2013-05-02 00:00:00 UTC", 2
  111 + create_user "2013-05-03 00:00:00 UTC", 2
  112 + expected = {
  113 + [utc.parse("2013-05-01 00:00:00 UTC"), 1] => 1,
  114 + [utc.parse("2013-05-02 00:00:00 UTC"), 1] => 0,
  115 + [utc.parse("2013-05-03 00:00:00 UTC"), 1] => 0,
  116 + [utc.parse("2013-05-01 00:00:00 UTC"), 2] => 0,
  117 + [utc.parse("2013-05-02 00:00:00 UTC"), 2] => 1,
  118 + [utc.parse("2013-05-03 00:00:00 UTC"), 2] => 1
  119 + }
  120 + assert_equal expected, User.group_by_day(:created_at).group(:score).order(:score).count
  121 + end
  122 +
  123 + def test_group_day_of_week
  124 + create_user "2013-05-01 00:00:00 UTC", 1
  125 + create_user "2013-05-02 00:00:00 UTC", 2
  126 + create_user "2013-05-03 00:00:00 UTC", 2
  127 + expected = {
  128 + [1, 0] => 0,
  129 + [1, 1] => 0,
  130 + [1, 2] => 0,
  131 + [1, 3] => 1,
  132 + [1, 4] => 0,
  133 + [1, 5] => 0,
  134 + [1, 6] => 0,
  135 + [2, 0] => 0,
  136 + [2, 1] => 0,
  137 + [2, 2] => 0,
  138 + [2, 3] => 0,
  139 + [2, 4] => 1,
  140 + [2, 5] => 1,
  141 + [2, 6] => 0
  142 + }
  143 + assert_equal expected, User.group(:score).group_by_day_of_week(:created_at).count
  144 + end
  145 +
  146 + def test_groupdate_multiple
  147 + create_user "2013-05-01 00:00:00 UTC", 1
  148 + expected = {
  149 + [utc.parse("2013-05-01 00:00:00 UTC"), utc.parse("2013-01-01 00:00:00 UTC")] => 1
  150 + }
  151 + assert_equal expected, User.group_by_day(:created_at).group_by_year(:created_at).count
  152 + end
  153 +
  154 + def test_not_modified
  155 + create_user "2013-05-01 00:00:00 UTC"
  156 + expected = {utc.parse("2013-05-01 00:00:00 UTC") => 1}
  157 + relation = User.group_by_day(:created_at)
  158 + relation.where("created_at > ?", "2013-05-01 00:00:00 UTC")
  159 + assert_equal expected, relation.count
  160 + end
  161 +
  162 + def test_bad_method
  163 + assert_raises(NoMethodError) { User.group_by_day(:created_at).no_such_method }
  164 + end
  165 +
  166 + def test_respond_to_where
  167 + assert User.group_by_day(:created_at).respond_to?(:order)
  168 + end
  169 +
  170 + def test_respond_to_bad_method
  171 + assert !User.group_by_day(:created_at).respond_to?(:no_such_method)
  172 + end
  173 +
  174 + def test_last
  175 + create_user "#{this_year - 3}-01-01 00:00:00 UTC"
  176 + create_user "#{this_year - 1}-01-01 00:00:00 UTC"
  177 + expected = {
  178 + utc.parse("#{this_year - 2}-01-01 00:00:00 UTC") => 0,
  179 + utc.parse("#{this_year - 1}-01-01 00:00:00 UTC") => 1,
  180 + utc.parse("#{this_year}-01-01 00:00:00 UTC") => 0
  181 + }
  182 + assert_equal expected, User.group_by_year(:created_at, last: 3).count
  183 + end
  184 +
  185 + def test_current
  186 + create_user "#{this_year - 3}-01-01 00:00:00 UTC"
  187 + create_user "#{this_year - 1}-01-01 00:00:00 UTC"
  188 + expected = {
  189 + utc.parse("#{this_year - 2}-01-01 00:00:00 UTC") => 0,
  190 + utc.parse("#{this_year - 1}-01-01 00:00:00 UTC") => 1
  191 + }
  192 + assert_equal expected, User.group_by_year(:created_at, last: 2, current: false).count
  193 + end
  194 +
  195 + def test_format_locale
  196 + create_user "2014-10-01 00:00:00 UTC"
  197 + assert_equal ({"Okt" => 1}), User.group_by_day(:created_at, format: "%b", locale: :de).count
  198 + end
  199 +
  200 + def test_format_locale_by_symbol
  201 + create_user "2014-10-01 00:00:00 UTC"
  202 + assert_equal ({"Okt 1, 2014" => 1}), User.group_by_day(:created_at, format: :special, locale: :de).count
  203 + end
  204 +
  205 + def test_format_locale_global
  206 + create_user "2014-10-01 00:00:00 UTC"
  207 + I18n.locale = :de
  208 + assert_equal ({"Okt" => 1}), User.group_by_day(:created_at, format: "%b").count
  209 + ensure
  210 + I18n.locale = :en
  211 + end
  212 +
  213 + def test_format_multiple_groups
  214 + create_user "2014-03-01 00:00:00 UTC"
  215 + assert_equal ({["Sun", 1] => 1}), User.group_by_week(:created_at, format: "%a").group(:score).count
  216 + assert_equal ({[1, "Sun"] => 1}), User.group(:score).group_by_week(:created_at, format: "%a").count
  217 + end
  218 +
  219 + # permit
  220 +
  221 + def test_permit
  222 + assert_raises(ArgumentError, "Unpermitted period") { User.group_by_period(:day, :created_at, permit: %w(week)).count }
  223 + end
  224 +
  225 + def test_permit_bad_period
  226 + assert_raises(ArgumentError, "Unpermitted period") { User.group_by_period(:bad_period, :created_at).count }
  227 + end
  228 +
  229 + def test_permit_symbol_symbols
  230 + assert_equal ({}), User.group_by_period(:day, :created_at, permit: [:day]).count
  231 + end
  232 +
  233 + def test_permit_string_symbols
  234 + assert_equal ({}), User.group_by_period("day", :created_at, permit: [:day]).count
  235 + end
  236 +
  237 + def test_permit_symbol_strings
  238 + assert_equal ({}), User.group_by_period(:day, :created_at, permit: %w(day)).count
  239 + end
  240 +
  241 + def test_permit_string_strings
  242 + assert_equal ({}), User.group_by_period("day", :created_at, permit: %w(day)).count
  243 + end
  244 +
  245 + # default value
  246 +
  247 + def test_default_value
  248 + create_user "#{this_year}-01-01 00:00:00 UTC"
  249 + expected = {
  250 + utc.parse("#{this_year - 1}-01-01 00:00:00 UTC") => nil,
  251 + utc.parse("#{this_year}-01-01 00:00:00 UTC") => 1
  252 + }
  253 + assert_equal expected, User.group_by_year(:created_at, last: 2, default_value: nil).count
  254 + end
  255 +
  256 + # associations
  257 +
  258 + def test_associations
  259 + user = create_user("2014-03-01 00:00:00 UTC")
  260 + assert_empty user.posts.group_by_day(:created_at).count
  261 + end
  262 +
  263 + # activerecord default_timezone option
  264 +
  265 + def test_default_timezone_local
  266 + User.default_timezone = :local
  267 + assert_raises(RuntimeError) { User.group_by_day(:created_at).count }
  268 + ensure
  269 + User.default_timezone = :utc
  270 + end
  271 +
  272 + # Brasilia Summer Time
  273 +
  274 + def test_brasilia_summer_time
  275 + # must parse and convert to UTC for ActiveRecord 3.1
  276 + create_user(brasilia.parse("2014-10-19 02:00:00").utc.to_s)
  277 + create_user(brasilia.parse("2014-10-20 02:00:00").utc.to_s)
  278 + expected = {
  279 + brasilia.parse("2014-10-19 01:00:00") => 1,
  280 + brasilia.parse("2014-10-20 00:00:00") => 1
  281 + }
  282 + assert_equal expected, User.group_by_day(:created_at, time_zone: "Brasilia").count
  283 + end
  284 +
  285 + # carry_forward option
  286 +
  287 + def test_carry_forward
  288 + create_user "2014-05-01 00:00:00 UTC"
  289 + create_user "2014-05-01 00:00:00 UTC"
  290 + create_user "2014-05-03 00:00:00 UTC"
  291 + assert_equal 2, User.group_by_day(:created_at, carry_forward: true).count[utc.parse("2014-05-02 00:00:00 UTC")]
  292 + end
  293 +
  294 + # dates
  295 +
  296 + def test_dates
  297 + create_user "2014-03-01 12:00:00 UTC"
  298 + assert_equal ({Date.parse("2014-03-01") => 1}), User.group_by_day(:created_at, dates: true).count
  299 + end
  300 +
  301 + def test_no_column
  302 + assert_raises(ArgumentError) { User.group_by_day.first }
  303 + end
  304 +
  305 + def call_method(method, field, options)
  306 + User.group_by_period(method, field, options).count
  307 + end
  308 +
  309 + def create_user(created_at, score = 1)
  310 + User.create! name: "Andrew", score: score, created_at: created_at ? utc.parse(created_at) : nil
  311 + end
  312 +
  313 + def teardown
  314 + User.delete_all
  315 + end
  316 +
  317 + def enumerable_test?
  318 + false
  319 + end
  320 +end
  321 +
50 322 module TestGroupdate
51 323 def setup
52 324 Groupdate.week_start = :sun
... ... @@ -55,7 +327,7 @@ module TestGroupdate
55 327 # second
56 328  
57 329 def test_second_end_of_second
58   - if ActiveRecord::Base.connection.adapter_name == "Mysql2" && ActiveRecord::VERSION::STRING.starts_with?("4.2.")
  330 + if enumerable_test? || (ActiveRecord::Base.connection.adapter_name == "Mysql2" && ActiveRecord::VERSION::STRING.starts_with?("4.2."))
59 331 skip # no millisecond precision
60 332 else
61 333 assert_result_time :second, "2013-05-03 00:00:00 UTC", "2013-05-03 00:00:00.999"
... ... @@ -557,19 +829,11 @@ module TestGroupdate
557 829 end
558 830  
559 831 def test_zeros_excludes_end
560   - create_user "2013-05-02 00:00:00 UTC"
561   - expected = {
562   - utc.parse("2013-05-01 00:00:00 UTC") => 0
563   - }
564   - assert_equal expected, call_method(:day, :created_at, range: Time.parse("2013-05-01 00:00:00 UTC")...Time.parse("2013-05-02 00:00:00 UTC"))
565   - end
566   -
567   - def test_zeros_previous_scope
568   - create_user "2013-05-01 00:00:00 UTC"
  832 + create_user "2013-05-02 00:00:00 UTC"
569 833 expected = {
570 834 utc.parse("2013-05-01 00:00:00 UTC") => 0
571 835 }
572   - assert_equal expected, User.where("id = 0").group_by_day(:created_at, range: Time.parse("2013-05-01 00:00:00 UTC")..Time.parse("2013-05-01 23:59:59 UTC")).count
  836 + assert_equal expected, call_method(:day, :created_at, range: Time.parse("2013-05-01 00:00:00 UTC")...Time.parse("2013-05-02 00:00:00 UTC"))
573 837 end
574 838  
575 839 def test_zeros_datetime
... ... @@ -581,8 +845,7 @@ module TestGroupdate
581 845 end
582 846  
583 847 def test_zeros_null_value
584   - user = User.create!(name: "Andrew")
585   - user.update_column :created_at, nil
  848 + create_user nil
586 849 assert_equal 0, call_method(:hour_of_day, :created_at, range: true)[0]
587 850 end
588 851  
... ... @@ -611,152 +874,16 @@ module TestGroupdate
611 874  
612 875 # misc
613 876  
614   - def test_order_hour_of_day
615   - assert_equal 23, User.group_by_hour_of_day(:created_at).order("hour_of_day desc").count.keys.first
616   - end
617   -
618   - def test_order_hour_of_day_case
619   - assert_equal 23, User.group_by_hour_of_day(:created_at).order("hour_of_day DESC").count.keys.first
620   - end
621   -
622   - def test_order_hour_of_day_reverse
623   - skip if ActiveRecord::VERSION::MAJOR == 5
624   - assert_equal 23, User.group_by_hour_of_day(:created_at).reverse_order.count.keys.first
625   - end
626   -
627   - def test_order_hour_of_day_order_reverse
628   - skip if ActiveRecord::VERSION::MAJOR == 5
629   - assert_equal 0, User.group_by_hour_of_day(:created_at).order("hour_of_day desc").reverse_order.count.keys.first
630   - end
631   -
632 877 def test_order_hour_of_day_reverse_option
633 878 assert_equal 23, call_method(:hour_of_day, :created_at, reverse: true).keys.first
634 879 end
635 880  
636   - def test_table_name
637   - assert_empty User.group_by_day("users.created_at").count
638   - end
639   -
640   - def test_previous_scopes
641   - create_user "2013-05-01 00:00:00 UTC"
642   - assert_empty User.where("id = 0").group_by_day(:created_at).count
643   - end
644   -
645 881 def test_time_zone
646 882 create_user "2013-05-01 00:00:00 UTC"
647 883 time_zone = "Pacific Time (US & Canada)"
648 884 assert_equal time_zone, call_method(:day, :created_at, time_zone: time_zone).keys.first.time_zone.name
649 885 end
650 886  
651   - def test_where_after
652   - create_user "2013-05-01 00:00:00 UTC"
653   - create_user "2013-05-02 00:00:00 UTC"
654   - expected = {utc.parse("2013-05-02 00:00:00 UTC") => 1}
655   - assert_equal expected, User.group_by_day(:created_at).where("created_at > ?", "2013-05-01 00:00:00 UTC").count
656   - end
657   -
658   - def test_group_before
659   - create_user "2013-05-01 00:00:00 UTC", 1
660   - create_user "2013-05-02 00:00:00 UTC", 2
661   - create_user "2013-05-03 00:00:00 UTC", 2
662   - expected = {
663   - [1, utc.parse("2013-05-01 00:00:00 UTC")] => 1,
664   - [1, utc.parse("2013-05-02 00:00:00 UTC")] => 0,
665   - [1, utc.parse("2013-05-03 00:00:00 UTC")] => 0,
666   - [2, utc.parse("2013-05-01 00:00:00 UTC")] => 0,
667   - [2, utc.parse("2013-05-02 00:00:00 UTC")] => 1,
668   - [2, utc.parse("2013-05-03 00:00:00 UTC")] => 1
669   - }
670   - assert_equal expected, User.group(:score).group_by_day(:created_at).order(:score).count
671   - end
672   -
673   - def test_group_after
674   - create_user "2013-05-01 00:00:00 UTC", 1
675   - create_user "2013-05-02 00:00:00 UTC", 2
676   - create_user "2013-05-03 00:00:00 UTC", 2
677   - expected = {
678   - [utc.parse("2013-05-01 00:00:00 UTC"), 1] => 1,
679   - [utc.parse("2013-05-02 00:00:00 UTC"), 1] => 0,
680   - [utc.parse("2013-05-03 00:00:00 UTC"), 1] => 0,
681   - [utc.parse("2013-05-01 00:00:00 UTC"), 2] => 0,
682   - [utc.parse("2013-05-02 00:00:00 UTC"), 2] => 1,
683   - [utc.parse("2013-05-03 00:00:00 UTC"), 2] => 1
684   - }
685   - assert_equal expected, User.group_by_day(:created_at).group(:score).order(:score).count
686   - end
687   -
688   - def test_group_day_of_week
689   - create_user "2013-05-01 00:00:00 UTC", 1
690   - create_user "2013-05-02 00:00:00 UTC", 2
691   - create_user "2013-05-03 00:00:00 UTC", 2
692   - expected = {
693   - [1, 0] => 0,
694   - [1, 1] => 0,
695   - [1, 2] => 0,
696   - [1, 3] => 1,
697   - [1, 4] => 0,
698   - [1, 5] => 0,
699   - [1, 6] => 0,
700   - [2, 0] => 0,
701   - [2, 1] => 0,
702   - [2, 2] => 0,
703   - [2, 3] => 0,
704   - [2, 4] => 1,
705   - [2, 5] => 1,
706   - [2, 6] => 0
707   - }
708   - assert_equal expected, User.group(:score).group_by_day_of_week(:created_at).count
709   - end
710   -
711   - def test_groupdate_multiple
712   - create_user "2013-05-01 00:00:00 UTC", 1
713   - expected = {
714   - [utc.parse("2013-05-01 00:00:00 UTC"), utc.parse("2013-01-01 00:00:00 UTC")] => 1
715   - }
716   - assert_equal expected, User.group_by_day(:created_at).group_by_year(:created_at).count
717   - end
718   -
719   - def test_not_modified
720   - create_user "2013-05-01 00:00:00 UTC"
721   - expected = {utc.parse("2013-05-01 00:00:00 UTC") => 1}
722   - relation = User.group_by_day(:created_at)
723   - relation.where("created_at > ?", "2013-05-01 00:00:00 UTC")
724   - assert_equal expected, relation.count
725   - end
726   -
727   - def test_bad_method
728   - assert_raises(NoMethodError) { User.group_by_day(:created_at).no_such_method }
729   - end
730   -
731   - def test_respond_to_where
732   - assert User.group_by_day(:created_at).respond_to?(:order)
733   - end
734   -
735   - def test_respond_to_bad_method
736   - assert !User.group_by_day(:created_at).respond_to?(:no_such_method)
737   - end
738   -
739   - def test_last
740   - create_user "#{this_year - 3}-01-01 00:00:00 UTC"
741   - create_user "#{this_year - 1}-01-01 00:00:00 UTC"
742   - expected = {
743   - utc.parse("#{this_year - 2}-01-01 00:00:00 UTC") => 0,
744   - utc.parse("#{this_year - 1}-01-01 00:00:00 UTC") => 1,
745   - utc.parse("#{this_year}-01-01 00:00:00 UTC") => 0
746   - }
747   - assert_equal expected, User.group_by_year(:created_at, last: 3).count
748   - end
749   -
750   - def test_current
751   - create_user "#{this_year - 3}-01-01 00:00:00 UTC"
752   - create_user "#{this_year - 1}-01-01 00:00:00 UTC"
753   - expected = {
754   - utc.parse("#{this_year - 2}-01-01 00:00:00 UTC") => 0,
755   - utc.parse("#{this_year - 1}-01-01 00:00:00 UTC") => 1
756   - }
757   - assert_equal expected, User.group_by_year(:created_at, last: 2, current: false).count
758   - end
759   -
760 887 def test_format_day
761 888 create_user "2014-03-01 00:00:00 UTC"
762 889 assert_format :day, "March 1, 2014", "%B %-e, %Y"
... ... @@ -807,112 +934,6 @@ module TestGroupdate
807 934 assert_format :month_of_year, "Jan", "%b"
808 935 end
809 936  
810   - def test_format_locale
811   - create_user "2014-10-01 00:00:00 UTC"
812   - assert_equal ({"Okt" => 1}), User.group_by_day(:created_at, format: "%b", locale: :de).count
813   - end
814   -
815   - def test_format_locale_by_symbol
816   - create_user "2014-10-01 00:00:00 UTC"
817   - assert_equal ({"Okt 1, 2014" => 1}), User.group_by_day(:created_at, format: :special, locale: :de).count
818   - end
819   -
820   - def test_format_locale_global
821   - create_user "2014-10-01 00:00:00 UTC"
822   - I18n.locale = :de
823   - assert_equal ({"Okt" => 1}), User.group_by_day(:created_at, format: "%b").count
824   - ensure
825   - I18n.locale = :en
826   - end
827   -
828   - def test_format_multiple_groups
829   - create_user "2014-03-01 00:00:00 UTC"
830   - assert_equal ({["Sun", 1] => 1}), User.group_by_week(:created_at, format: "%a").group(:score).count
831   - assert_equal ({[1, "Sun"] => 1}), User.group(:score).group_by_week(:created_at, format: "%a").count
832   - end
833   -
834   - # permit
835   -
836   - def test_permit
837   - assert_raises(ArgumentError, "Unpermitted period") { User.group_by_period(:day, :created_at, permit: %w(week)).count }
838   - end
839   -
840   - def test_permit_bad_period
841   - assert_raises(ArgumentError, "Unpermitted period") { User.group_by_period(:bad_period, :created_at).count }
842   - end
843   -
844   - def test_permit_symbol_symbols
845   - assert_equal ({}), User.group_by_period(:day, :created_at, permit: [:day]).count
846   - end
847   -
848   - def test_permit_string_symbols
849   - assert_equal ({}), User.group_by_period("day", :created_at, permit: [:day]).count
850   - end
851   -
852   - def test_permit_symbol_strings
853   - assert_equal ({}), User.group_by_period(:day, :created_at, permit: %w(day)).count
854   - end
855   -
856   - def test_permit_string_strings
857   - assert_equal ({}), User.group_by_period("day", :created_at, permit: %w(day)).count
858   - end
859   -
860   - # default value
861   -
862   - def test_default_value
863   - create_user "#{this_year}-01-01 00:00:00 UTC"
864   - expected = {
865   - utc.parse("#{this_year - 1}-01-01 00:00:00 UTC") => nil,
866   - utc.parse("#{this_year}-01-01 00:00:00 UTC") => 1
867   - }
868   - assert_equal expected, User.group_by_year(:created_at, last: 2, default_value: nil).count
869   - end
870   -
871   - # associations
872   -
873   - def test_associations
874   - user = create_user("2014-03-01 00:00:00 UTC")
875   - assert_empty user.posts.group_by_day(:created_at).count
876   - end
877   -
878   - # activerecord default_timezone option
879   -
880   - def test_default_timezone_local
881   - User.default_timezone = :local
882   - assert_raises(RuntimeError) { User.group_by_day(:created_at).count }
883   - ensure
884   - User.default_timezone = :utc
885   - end
886   -
887   - # Brasilia Summer Time
888   -
889   - def test_brasilia_summer_time
890   - # must parse and convert to UTC for ActiveRecord 3.1
891   - create_user(brasilia.parse("2014-10-19 02:00:00").utc.to_s)
892   - create_user(brasilia.parse("2014-10-20 02:00:00").utc.to_s)
893   - expected = {
894   - brasilia.parse("2014-10-19 01:00:00") => 1,
895   - brasilia.parse("2014-10-20 00:00:00") => 1
896   - }
897   - assert_equal expected, User.group_by_day(:created_at, time_zone: "Brasilia").count
898   - end
899   -
900   - # carry_forward option
901   -
902   - def test_carry_forward
903   - create_user "2014-05-01 00:00:00 UTC"
904   - create_user "2014-05-01 00:00:00 UTC"
905   - create_user "2014-05-03 00:00:00 UTC"
906   - assert_equal 2, User.group_by_day(:created_at, carry_forward: true).count[utc.parse("2014-05-02 00:00:00 UTC")]
907   - end
908   -
909   - # dates
910   -
911   - def test_dates
912   - create_user "2014-03-01 12:00:00 UTC"
913   - assert_equal ({Date.parse("2014-03-01") => 1}), User.group_by_day(:created_at, dates: true).count
914   - end
915   -
916 937 # helpers
917 938  
918 939 def assert_format(method, expected, format, options = {})
... ... @@ -942,14 +963,6 @@ module TestGroupdate
942 963 assert_equal expected, call_method(method, :created_at, options.merge(time_zone: time_zone ? "Pacific Time (US & Canada)" : nil, range: Time.parse(range_start)..Time.parse(range_end)))
943 964 end
944 965  
945   - def call_method(method, field, options)
946   - User.group_by_period(method, field, options).count
947   - end
948   -
949   - def create_user(created_at, score = 1)
950   - User.create! name: "Andrew", score: score, created_at: utc.parse(created_at)
951   - end
952   -
953 966 def this_year
954 967 Time.now.utc.year
955 968 end
... ... @@ -961,8 +974,4 @@ module TestGroupdate
961 974 def brasilia
962 975 ActiveSupport::TimeZone["Brasilia"]
963 976 end
964   -
965   - def teardown
966   - User.delete_all
967   - end
968 977 end
... ...