test_helper.rb 35.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
require "bundler/setup"
Bundler.require(:default)
require "minitest/autorun"
require "minitest/pride"
require "logger"
require "active_record"

Minitest::Test = Minitest::Unit::TestCase unless defined?(Minitest::Test)

ENV["TZ"] = "UTC"

# for debugging
# ActiveRecord::Base.logger = Logger.new(STDOUT)

# rails does this in activerecord/lib/active_record/railtie.rb
ActiveRecord::Base.default_timezone = :utc
ActiveRecord::Base.time_zone_aware_attributes = true

class User < ActiveRecord::Base
  has_many :posts

  def self.custom_count
    count
  end
end

class Post < ActiveRecord::Base
end

# i18n
I18n.enforce_available_locales = true
I18n.backend.store_translations :de, date: {
  abbr_month_names: %w(Jan Feb Mar Apr Mai Jun Jul Aug Sep Okt Nov Dez).unshift(nil)
},
time: {
  formats: {special: "%b %e, %Y"}
}

# migrations
def create_tables
  ActiveRecord::Migration.verbose = false

  ActiveRecord::Migration.create_table :users, force: true do |t|
    t.string :name
    t.integer :score
    t.timestamp :created_at
    t.date :created_on
  end

  ActiveRecord::Migration.create_table :posts, force: true do |t|
    t.references :user
    t.timestamp :created_at
  end
end

def create_redshift_tables
  ActiveRecord::Migration.verbose = false

  if ActiveRecord::Migration.table_exists?(:users)
    ActiveRecord::Migration.drop_table(:users, force: :cascade)
  end

  if ActiveRecord::Migration.table_exists?(:posts)
    ActiveRecord::Migration.drop_table(:posts, force: :cascade)
  end

  ActiveRecord::Migration.execute "CREATE TABLE users (id INT IDENTITY(1,1) PRIMARY KEY, name VARCHAR(255), score INT, created_at DATETIME, created_on DATE);"

  ActiveRecord::Migration.execute "CREATE TABLE posts (id INT IDENTITY(1,1) PRIMARY KEY, user_id INT REFERENCES users, created_at DATETIME);"
end

module TestDatabase
  def test_zeros_previous_scope
    create_user "2013-05-01"
    expected = {
      Date.parse("2013-05-01") => 0
    }
    assert_equal expected, User.where("id = 0").group_by_day(:created_at, range: Date.parse("2013-05-01")..Date.parse("2013-05-01 23:59:59 UTC")).count
  end

  def test_order_hour_of_day
    assert_equal 23, User.group_by_hour_of_day(:created_at).order("hour_of_day desc").count.keys.first
  end

  def test_order_hour_of_day_case
    assert_equal 23, User.group_by_hour_of_day(:created_at).order("hour_of_day DESC").count.keys.first
  end

  def test_order_hour_of_day_reverse
    skip if ActiveRecord::VERSION::MAJOR == 5
    assert_equal 23, User.group_by_hour_of_day(:created_at).reverse_order.count.keys.first
  end

  def test_order_hour_of_day_order_reverse
    skip if ActiveRecord::VERSION::MAJOR == 5
    assert_equal 0, User.group_by_hour_of_day(:created_at).order("hour_of_day desc").reverse_order.count.keys.first
  end

  def test_table_name
    # This test is to ensure there's not an error when using the table
    # name as part of the column name.
    assert_empty User.group_by_day("users.created_at").count
  end

  def test_previous_scopes
    create_user "2013-05-01"
    assert_empty User.where("id = 0").group_by_day(:created_at).count
  end

  def test_where_after
    create_user "2013-05-01"
    create_user "2013-05-02"
    expected = {Date.parse("2013-05-02") => 1}
    assert_equal expected, User.group_by_day(:created_at).where("created_at > ?", "2013-05-01").count
  end

  def test_group_before
    create_user "2013-05-01", 1
    create_user "2013-05-02", 2
    create_user "2013-05-03", 2
    expected = {
      [1, Date.parse("2013-05-01")] => 1,
      [1, Date.parse("2013-05-02")] => 0,
      [1, Date.parse("2013-05-03")] => 0,
      [2, Date.parse("2013-05-01")] => 0,
      [2, Date.parse("2013-05-02")] => 1,
      [2, Date.parse("2013-05-03")] => 1
    }
    assert_equal expected, User.group(:score).group_by_day(:created_at).order(:score).count
  end

  def test_group_after
    create_user "2013-05-01", 1
    create_user "2013-05-02", 2
    create_user "2013-05-03", 2
    expected = {
      [Date.parse("2013-05-01"), 1] => 1,
      [Date.parse("2013-05-02"), 1] => 0,
      [Date.parse("2013-05-03"), 1] => 0,
      [Date.parse("2013-05-01"), 2] => 0,
      [Date.parse("2013-05-02"), 2] => 1,
      [Date.parse("2013-05-03"), 2] => 1
    }
    assert_equal expected, User.group_by_day(:created_at).group(:score).order(:score).count
  end

  def test_group_day_of_week
    create_user "2013-05-01", 1
    create_user "2013-05-02", 2
    create_user "2013-05-03", 2
    expected = {
      [1, 0] => 0,
      [1, 1] => 0,
      [1, 2] => 0,
      [1, 3] => 1,
      [1, 4] => 0,
      [1, 5] => 0,
      [1, 6] => 0,
      [2, 0] => 0,
      [2, 1] => 0,
      [2, 2] => 0,
      [2, 3] => 0,
      [2, 4] => 1,
      [2, 5] => 1,
      [2, 6] => 0
    }
    assert_equal expected, User.group(:score).group_by_day_of_week(:created_at).count
  end

  def test_groupdate_multiple
    create_user "2013-05-01", 1
    expected = {
      [Date.parse("2013-05-01"), Date.parse("2013-01-01")] => 1
    }
    assert_equal expected, User.group_by_day(:created_at).group_by_year(:created_at).count
  end

  def test_groupdate_multiple_hour_of_day_day_of_week
    create_user "2013-05-01 00:00:00 UTC", 1
    expected = {}
    24.times do |i|
      7.times do |j|
        expected[[i, j]] = i == 0 && j == 3 ? 1 : 0
      end
    end
    assert_equal expected, User.group_by_hour_of_day(:created_at).group_by_day_of_week(:created_at).count
  end

  def test_not_modified
    create_user "2013-05-01"
    expected = {Date.parse("2013-05-01") => 1}
    relation = User.group_by_day(:created_at)
    relation.where("created_at > ?", "2013-05-01")
    assert_equal expected, relation.count
  end

  def test_bad_method
    assert_raises(NoMethodError) { User.group_by_day(:created_at).no_such_method }
  end

  def test_respond_to_order
    assert User.group_by_day(:created_at).respond_to?(:order)
  end

  def test_respond_to_bad_method
    assert !User.group_by_day(:created_at).respond_to?(:no_such_method)
  end

  def test_last
    create_user "#{this_year - 3}-01-01"
    create_user "#{this_year - 1}-01-01"
    expected = {
      Date.parse("#{this_year - 2}-01-01") => 0,
      Date.parse("#{this_year - 1}-01-01") => 1,
      Date.parse("#{this_year}-01-01") => 0
    }
    assert_equal expected, User.group_by_year(:created_at, last: 3).count
  end

  def test_last_date
    Time.zone = pt
    today = Date.today
    create_user today.to_s
    this_month = pt.parse(today.to_s).beginning_of_month
    last_month = this_month - 1.month
    expected = {
      last_month.to_date => 0,
      this_month.to_date => 1
    }
    assert_equal expected, call_method(:month, :created_on, last: 2)
  ensure
    Time.zone = nil
  end

  def test_last_hour_of_day
    error = assert_raises(ArgumentError) { User.group_by_hour_of_day(:created_at, last: 3).count }
    assert_equal "Cannot use last option with hour_of_day", error.message
  end

  def test_current
    create_user "#{this_year - 3}-01-01"
    create_user "#{this_year - 1}-01-01"
    expected = {
      Date.parse("#{this_year - 2}-01-01") => 0,
      Date.parse("#{this_year - 1}-01-01") => 1
    }
    assert_equal expected, User.group_by_year(:created_at, last: 2, current: false).count
  end

  def test_quarter_and_last
    today = Date.today
    create_user today.to_s
    this_quarter = today.to_time.beginning_of_quarter
    last_quarter = this_quarter - 3.months
    expected = {
      last_quarter.to_date => 0,
      this_quarter.to_date => 1
    }
    assert_equal expected, call_method(:quarter, :created_at, last: 2)
  end

  def test_format_locale
    create_user "2014-10-01"
    assert_equal ({"Okt" => 1}), User.group_by_day(:created_at, format: "%b", locale: :de).count
  end

  def test_format_locale_by_symbol
    create_user "2014-10-01"
    assert_equal ({"Okt  1, 2014" => 1}), User.group_by_day(:created_at, format: :special, locale: :de).count
  end

  def test_format_locale_global
    create_user "2014-10-01"
    I18n.locale = :de
    assert_equal ({"Okt" => 1}), User.group_by_day(:created_at, format: "%b").count
  ensure
    I18n.locale = :en
  end

  def test_format_multiple_groups
    create_user "2014-03-01"
    assert_equal ({["Sun", 1] => 1}), User.group_by_week(:created_at, format: "%a").group(:score).count
    assert_equal ({[1, "Sun"] => 1}), User.group(:score).group_by_week(:created_at, format: "%a").count
  end

  # permit

  def test_permit
    error = assert_raises(ArgumentError) { User.group_by_period(:day, :created_at, permit: %w(week)).count }
    assert_equal "Unpermitted period", error.message
  end

  def test_permit_bad_period
    error = assert_raises(ArgumentError) { User.group_by_period(:bad_period, :created_at).count }
    assert_equal "Unpermitted period", error.message
  end

  def test_permit_symbol_symbols
    assert_equal ({}), User.group_by_period(:day, :created_at, permit: [:day]).count
  end

  def test_permit_string_symbols
    assert_equal ({}), User.group_by_period("day", :created_at, permit: [:day]).count
  end

  def test_permit_symbol_strings
    assert_equal ({}), User.group_by_period(:day, :created_at, permit: %w(day)).count
  end

  def test_permit_string_strings
    assert_equal ({}), User.group_by_period("day", :created_at, permit: %w(day)).count
  end

  # default value

  def test_default_value
    create_user "#{this_year}-01-01"
    expected = {
      Date.parse("#{this_year - 1}-01-01") => nil,
      Date.parse("#{this_year}-01-01") => 1
    }
    assert_equal expected, User.group_by_year(:created_at, last: 2, default_value: nil).count
  end

  # associations

  def test_associations
    user = create_user("2014-03-01")
    user.posts.create!(created_at: "2014-04-01 00:00:00 UTC")
    expected = {
      Date.parse("2014-04-01") => 1
    }
    assert_equal expected, user.posts.group_by_day(:created_at).count
  end

  def test_associations_period
    user = create_user("2014-03-01")
    user.posts.create!(created_at: "2014-04-01 00:00:00 UTC")
    expected = {
      Date.parse("2014-04-01") => 1
    }
    assert_equal expected, user.posts.group_by_period(:day, :created_at).count
  end

  # activerecord default_timezone option

  def test_default_timezone_local
    User.default_timezone = :local
    assert_raises(Groupdate::Error) { User.group_by_day(:created_at).count }
  ensure
    User.default_timezone = :utc
  end

  # Brasilia Summer Time

  def test_brasilia_summer_time
    # must parse and convert to UTC for ActiveRecord 3.1
    create_user(brasilia.parse("2014-10-19 02:00:00").utc.to_s)
    create_user(brasilia.parse("2014-10-20 02:00:00").utc.to_s)
    expected = {
      Date.parse("2014-10-19") => 1,
      Date.parse("2014-10-20") => 1
    }
    assert_equal expected, call_method(:day, :created_at, time_zone: "Brasilia")
  end

  # carry_forward option

  def test_carry_forward
    create_user "2014-05-01"
    create_user "2014-05-01"
    create_user "2014-05-03"
    assert_equal 2, User.group_by_day(:created_at, carry_forward: true).count[Date.parse("2014-05-02")]
  end

  # no column

  def test_no_column
    assert_raises(ArgumentError) { User.group_by_day.first }
  end

  # custom calculation methods

  def test_custom_calculation_method
    create_user "2014-05-01"
    create_user "2014-05-01"
    create_user "2014-05-03"

    expected = {
      Date.parse("2014-05-01") => 2,
      Date.parse("2014-05-02") => 0,
      Date.parse("2014-05-03") => 1
    }

    assert_equal expected, User.group_by_day(:created_at).custom_count
  end

  private

  def call_method(method, field, options)
    User.group_by_period(method, field, options).count
  end

  def create_user(created_at, score = 1)
    user =
      User.create!(
        name: "Andrew",
        score: score,
        created_at: created_at ? utc.parse(created_at) : nil,
        created_on: created_at ? Date.parse(created_at) : nil
      )

    # hack for Redshift adapter, which doesn't return id on creation...
    user = User.last if user.id.nil?

    # hack for MySQL & Redshift adapters
    user.update_attributes(created_at: nil, created_on: nil) if created_at.nil? && is_redshift?

    user
  end

  def is_redshift?
    ActiveRecord::Base.connection.adapter_name == "Redshift"
  end

  def teardown
    User.delete_all
  end

  def enumerable_test?
    false
  end
end

module TestGroupdate
  def setup
    Groupdate.week_start = :sun
  end

  # second

  def test_second_end_of_second
    if enumerable_test? || ActiveRecord::Base.connection.adapter_name == "Mysql2"
      skip # no millisecond precision
    else
      assert_result_time :second, "2013-05-03 00:00:00 UTC", "2013-05-03 00:00:00.999"
    end
  end

  def test_second_start_of_second
    assert_result_time :second, "2013-05-03 00:00:01 UTC", "2013-05-03 00:00:01.000"
  end

  # minute

  def test_minute_end_of_minute
    assert_result_time :minute, "2013-05-03 00:00:00 UTC", "2013-05-03 00:00:59"
  end

  def test_minute_start_of_minute
    assert_result_time :minute, "2013-05-03 00:01:00 UTC", "2013-05-03 00:01:00"
  end

  # hour

  def test_hour_end_of_hour
    assert_result_time :hour, "2013-05-03 00:00:00 UTC", "2013-05-03 00:59:59"
  end

  def test_hour_start_of_hour
    assert_result_time :hour, "2013-05-03 01:00:00 UTC", "2013-05-03 01:00:00"
  end

  # day

  def test_day_end_of_day
    assert_result_date :day, "2013-05-03", "2013-05-03 23:59:59"
  end

  def test_day_start_of_day
    assert_result_date :day, "2013-05-04", "2013-05-04 00:00:00"
  end

  def test_day_end_of_day_with_time_zone
    assert_result_date :day, "2013-05-02", "2013-05-03 06:59:59", true
  end

  def test_day_start_of_day_with_time_zone
    assert_result_date :day, "2013-05-03", "2013-05-03 07:00:00", true
  end

  # day hour starts at 2 am

  def test_test_day_end_of_day_day_start_2am
    assert_result_date :day, "2013-05-03", "2013-05-04 01:59:59", false, day_start: 2
  end

  def test_test_day_start_of_day_day_start_2am
    assert_result_date :day, "2013-05-03", "2013-05-03 02:00:00", false, day_start: 2
  end

  def test_test_day_end_of_day_with_time_zone_day_start_2am
    assert_result_date :day, "2013-05-03", "2013-05-04 07:59:59", true, day_start: 2
  end

  def test_test_day_start_of_day_with_time_zone_day_start_2am
    assert_result_date :day, "2013-05-03", "2013-05-03 09:00:00", true, day_start: 2
  end

  # week

  def test_week_end_of_week
    assert_result_date :week, "2013-03-17", "2013-03-23 23:59:59"
  end

  def test_week_start_of_week
    assert_result_date :week, "2013-03-24", "2013-03-24 00:00:00"
  end

  def test_week_end_of_week_with_time_zone
    assert_result_date :week, "2013-03-10", "2013-03-17 06:59:59", true
  end

  def test_week_start_of_week_with_time_zone
    assert_result_date :week, "2013-03-17", "2013-03-17 07:00:00", true
  end

  # week starting on monday

  def test_week_end_of_week_mon
    assert_result_date :week, "2013-03-18", "2013-03-24 23:59:59", false, week_start: :mon
  end

  def test_week_start_of_week_mon
    assert_result_date :week, "2013-03-25", "2013-03-25 00:00:00", false, week_start: :mon
  end

  def test_week_end_of_week_with_time_zone_mon
    assert_result_date :week, "2013-03-11", "2013-03-18 06:59:59", true, week_start: :mon
  end

  def test_week_start_of_week_with_time_zone_mon
    assert_result_date :week, "2013-03-18", "2013-03-18 07:00:00", true, week_start: :mon
  end

  # week starting on saturday

  def test_week_end_of_week_sat
    assert_result_date :week, "2013-03-16", "2013-03-22 23:59:59", false, week_start: :sat
  end

  def test_week_start_of_week_sat
    assert_result_date :week, "2013-03-23", "2013-03-23 00:00:00", false, week_start: :sat
  end

  def test_week_end_of_week_with_time_zone_sat
    assert_result_date :week, "2013-03-09", "2013-03-16 06:59:59", true, week_start: :sat
  end

  def test_week_start_of_week_with_time_zone_sat
    assert_result_date :week, "2013-03-16", "2013-03-16 07:00:00", true, week_start: :sat
  end

  # week starting at 2am

  def test_week_end_of_week_day_start_2am
    assert_result_date :week, "2013-03-17", "2013-03-24 01:59:59", false, day_start: 2
  end

  def test_week_start_of_week_day_start_2am
    assert_result_date :week, "2013-03-17", "2013-03-17 02:00:00", false, day_start: 2
  end

  def test_week_end_of_week_day_with_time_zone_start_2am
    assert_result_date :week, "2013-03-17", "2013-03-24 08:59:59", true, day_start: 2
  end

  def test_week_start_of_week_day_with_time_zone_start_2am
    assert_result_date :week, "2013-03-17", "2013-03-17 09:00:00", true, day_start: 2
  end

  # month

  def test_month_end_of_month
    assert_result_date :month, "2013-05-01", "2013-05-31 23:59:59"
  end

  def test_month_start_of_month
    assert_result_date :month, "2013-06-01", "2013-06-01 00:00:00"
  end

  def test_month_end_of_month_with_time_zone
    assert_result_date :month, "2013-05-01", "2013-06-01 06:59:59", true
  end

  def test_month_start_of_month_with_time_zone
    assert_result_date :month, "2013-06-01", "2013-06-01 07:00:00", true
  end

  # month starts at 2am

  def test_month_end_of_month_day_start_2am
    assert_result_date :month, "2013-03-01", "2013-04-01 01:59:59", false, day_start: 2
  end

  def test_month_start_of_month_day_start_2am
    assert_result_date :month, "2013-03-01", "2013-03-01 02:00:00", false, day_start: 2
  end

  def test_month_end_of_month_with_time_zone_day_start_2am
    assert_result_date :month, "2013-03-01", "2013-04-01 08:59:59", true, day_start: 2
  end

  def test_month_start_of_month_with_time_zone_day_start_2am
    assert_result_date :month, "2013-03-01", "2013-03-01 10:00:00", true, day_start: 2
  end

  # quarter

  def test_quarter_end_of_quarter
    assert_result_date :quarter, "2013-04-01", "2013-06-30 23:59:59"
  end

  def test_quarter_start_of_quarter
    assert_result_date :quarter, "2013-04-01", "2013-04-01 00:00:00"
  end

  def test_quarter_end_of_quarter_with_time_zone
    assert_result_date :quarter, "2013-04-01", "2013-07-01 06:59:59", true
  end

  def test_quarter_start_of_quarter_with_time_zone
    assert_result_date :quarter, "2013-04-01", "2013-04-01 07:00:00", true
  end

  # quarter starts at 2am

  def test_quarter_end_of_quarter_day_start_2am
    assert_result_date :quarter, "2013-04-01", "2013-07-01 01:59:59", false, day_start: 2
  end

  def test_quarter_start_of_quarter_day_start_2am
    assert_result_date :quarter, "2013-04-01", "2013-04-01 02:00:00", false, day_start: 2
  end

  def test_quarter_end_of_quarter_with_time_zone_day_start_2am
    assert_result_date :quarter, "2013-01-01", "2013-04-01 08:59:59", true, day_start: 2
  end

  def test_quarter_start_of_quarter_with_time_zone_day_start_2am
    assert_result_date :quarter, "2013-01-01", "2013-01-01 10:00:00", true, day_start: 2
  end

  # year

  def test_year_end_of_year
    assert_result_date :year, "2013-01-01", "2013-12-31 23:59:59"
  end

  def test_year_start_of_year
    assert_result_date :year, "2014-01-01", "2014-01-01 00:00:00"
  end

  def test_year_end_of_year_with_time_zone
    assert_result_date :year, "2013-01-01", "2014-01-01 07:59:59", true
  end

  def test_year_start_of_year_with_time_zone
    assert_result_date :year, "2014-01-01", "2014-01-01 08:00:00", true
  end

  # year starts at 2am

  def test_year_end_of_year_day_start_2am
    assert_result_date :year, "2013-01-01", "2014-01-01 01:59:59", false, day_start: 2
  end

  def test_year_start_of_year_day_start_2am
    assert_result_date :year, "2013-01-01", "2013-01-01 02:00:00", false, day_start: 2
  end

  def test_year_end_of_year_with_time_zone_day_start_2am
    assert_result_date :year, "2013-01-01", "2014-01-01 09:59:59", true, day_start: 2
  end

  def test_year_start_of_year_with_time_zone_day_start_2am
    assert_result_date :year, "2013-01-01", "2013-01-01 10:00:00", true, day_start: 2
  end

  # hour of day

  def test_hour_of_day_end_of_hour
    assert_result :hour_of_day, 0, "2013-01-01 00:59:59"
  end

  def test_hour_of_day_start_of_hour
    assert_result :hour_of_day, 1, "2013-01-01 01:00:00"
  end

  def test_hour_of_day_end_of_hour_with_time_zone
    assert_result :hour_of_day, 0, "2013-01-01 08:59:59", true
  end

  def test_hour_of_day_start_of_hour_with_time_zone
    assert_result :hour_of_day, 1, "2013-01-01 09:00:00", true
  end

  # hour of day starts at 2am

  def test_hour_of_day_end_of_day_day_start_2am
    assert_result :hour_of_day, 23, "2013-01-01 01:59:59", false, day_start: 2
  end

  def test_hour_of_day_start_of_day_day_start_2am
    assert_result :hour_of_day, 0, "2013-01-01 02:00:00", false, day_start: 2
  end

  def test_hour_of_day_end_of_day_with_time_zone_day_start_2am
    assert_result :hour_of_day, 23, "2013-01-01 09:59:59", true, day_start: 2
  end

  def test_hour_of_day_start_of_day_with_time_zone_day_start_2am
    assert_result :hour_of_day, 0, "2013-01-01 10:00:00", true, day_start: 2
  end

  # minute of hour

  def test_minute_of_hour_end_of_hour
    assert_result :minute_of_hour, 59, "2017-02-09 23:59:59"
  end

  def test_minute_of_hour_beginning_of_hour
    assert_result :minute_of_hour, 0, "2017-02-09 00:00:00"
  end

  # day of week

  def test_day_of_week_end_of_day
    assert_result :day_of_week, 2, "2013-01-01 23:59:59"
  end

  def test_day_of_week_start_of_day
    assert_result :day_of_week, 3, "2013-01-02 00:00:00"
  end

  def test_day_of_week_end_of_week_with_time_zone
    assert_result :day_of_week, 2, "2013-01-02 07:59:59", true
  end

  def test_day_of_week_start_of_week_with_time_zone
    assert_result :day_of_week, 3, "2013-01-02 08:00:00", true
  end

  # day of week starts at 2am

  def test_day_of_week_end_of_day_day_start_2am
    assert_result :day_of_week, 3, "2013-01-03 01:59:59", false, day_start: 2
  end

  def test_day_of_week_start_of_day_day_start_2am
    assert_result :day_of_week, 3, "2013-01-02 02:00:00", false, day_start: 2
  end

  def test_day_of_week_end_of_day_with_time_zone_day_start_2am
    assert_result :day_of_week, 3, "2013-01-03 09:59:59", true, day_start: 2
  end

  def test_day_of_week_start_of_day_with_time_zone_day_start_2am
    assert_result :day_of_week, 3, "2013-01-02 10:00:00", true, day_start: 2
  end

  # day of month

  def test_day_of_month_end_of_day
    assert_result :day_of_month, 31, "2013-01-31 23:59:59"
  end

  def test_day_of_month_end_of_day_feb_leap_year
    assert_result :day_of_month, 29, "2012-02-29 23:59:59"
  end

  def test_day_of_month_start_of_day
    assert_result :day_of_month, 3, "2013-01-03 00:00:00"
  end

  def test_day_of_month_end_of_day_with_time_zone
    assert_result :day_of_month, 31, "2013-02-01 07:59:59", true
  end

  def test_day_of_month_start_of_day_with_time_zone
    assert_result :day_of_month, 1, "2013-01-01 08:00:00", true
  end

  # day of month starts at 2am

  def test_day_of_month_end_of_day_day_start_2am
    assert_result :day_of_month, 31, "2013-01-01 01:59:59", false, day_start: 2
  end

  def test_day_of_month_start_of_day_day_start_2am
    assert_result :day_of_month, 1, "2013-01-01 02:00:00", false, day_start: 2
  end

  def test_day_of_month_end_of_day_with_time_zone_day_start_2am
    assert_result :day_of_month, 31, "2013-01-01 09:59:59", true, day_start: 2
  end

  def test_day_of_month_start_of_day_with_time_zone_day_start_2am
    assert_result :day_of_month, 1, "2013-01-01 10:00:00", true, day_start: 2
  end

  # month of year

  def test_month_of_year_end_of_month
    assert_result :month_of_year, 1, "2013-01-31 23:59:59"
  end

  def test_month_of_year_start_of_month
    assert_result :month_of_year, 1, "2013-01-01 00:00:00"
  end

  def test_month_of_year_end_of_month_with_time_zone
    assert_result :month_of_year, 1, "2013-02-01 07:59:59", true
  end

  def test_month_of_year_start_of_month_with_time_zone
    assert_result :month_of_year, 1, "2013-01-01 08:00:00", true
  end

  # month of year starts at 2am

  def test_month_of_year_end_of_month_day_start_2am
    assert_result :month_of_year, 12, "2013-01-01 01:59:59", false, day_start: 2
  end

  def test_month_of_year_start_of_month_day_start_2am
    assert_result :month_of_year, 1, "2013-01-01 02:00:00", false, day_start: 2
  end

  def test_month_of_year_end_of_month_with_time_zone_day_start_2am
    assert_result :month_of_year, 12, "2013-01-01 09:59:59", true, day_start: 2
  end

  def test_month_of_year_start_of_month_with_time_zone_day_start_2am
    assert_result :month_of_year, 1, "2013-01-01 10:00:00", true, day_start: 2
  end

  # zeros

  def test_zeros_second
    assert_zeros :second, "2013-05-01 00:00:01 UTC", ["2013-05-01 00:00:00 UTC", "2013-05-01 00:00:01 UTC", "2013-05-01 00:00:02 UTC"], "2013-05-01 00:00:00.999 UTC", "2013-05-01 00:00:02 UTC"
  end

  def test_zeros_minute
    assert_zeros :minute, "2013-05-01 00:01:00 UTC", ["2013-05-01 00:00:00 UTC", "2013-05-01 00:01:00 UTC", "2013-05-01 00:02:00 UTC"], "2013-05-01 00:00:59 UTC", "2013-05-01 00:02:00 UTC"
  end

  def test_zeros_hour
    assert_zeros :hour, "2013-05-01 04:01:01 UTC", ["2013-05-01 03:00:00 UTC", "2013-05-01 04:00:00 UTC", "2013-05-01 05:00:00 UTC"], "2013-05-01 03:59:59 UTC", "2013-05-01 05:00:00 UTC"
  end

  def test_zeros_day
    assert_zeros_date :day, "2013-05-01 20:00:00 UTC", ["2013-04-30", "2013-05-01", "2013-05-02"], "2013-04-30 00:00:00 UTC", "2013-05-02 23:59:59 UTC"
  end

  def test_zeros_day_time_zone
    assert_zeros_date :day, "2013-05-01 20:00:00 PDT", ["2013-04-30", "2013-05-01", "2013-05-02"], "2013-04-30 00:00:00 PDT", "2013-05-02 23:59:59 PDT", true
  end

  def test_zeros_week
    assert_zeros_date :week, "2013-05-01 20:00:00 UTC", ["2013-04-21", "2013-04-28", "2013-05-05"], "2013-04-27 23:59:59 UTC", "2013-05-11 23:59:59 UTC"
  end

  def test_zeros_week_time_zone
    assert_zeros_date :week, "2013-05-01 20:00:00 PDT", ["2013-04-21", "2013-04-28", "2013-05-05"], "2013-04-27 23:59:59 PDT", "2013-05-11 23:59:59 PDT", true
  end

  def test_zeros_week_mon
    assert_zeros_date :week, "2013-05-01 20:00:00 UTC", ["2013-04-22", "2013-04-29", "2013-05-06"], "2013-04-27 23:59:59 UTC", "2013-05-11 23:59:59 UTC", false, week_start: :mon
  end

  def test_zeros_week_time_zone_mon
    assert_zeros_date :week, "2013-05-01 20:00:00 PDT", ["2013-04-22", "2013-04-29", "2013-05-06"], "2013-04-27 23:59:59 PDT", "2013-05-11 23:59:59 PDT", true, week_start: :mon
  end

  def test_zeros_week_sat
    assert_zeros_date :week, "2013-05-01 20:00:00 UTC", ["2013-04-20", "2013-04-27", "2013-05-04"], "2013-04-26 23:59:59 UTC", "2013-05-10 23:59:59 UTC", false, week_start: :sat
  end

  def test_zeros_week_time_zone_sat
    assert_zeros_date :week, "2013-05-01 20:00:00 PDT", ["2013-04-20", "2013-04-27", "2013-05-04"], "2013-04-26 23:59:59 PDT", "2013-05-10 23:59:59 PDT", true, week_start: :sat
  end

  def test_zeros_month
    assert_zeros_date :month, "2013-04-16 20:00:00 UTC", ["2013-03-01", "2013-04-01", "2013-05-01"], "2013-03-01", "2013-05-31 23:59:59 UTC"
  end

  def test_zeros_month_time_zone
    assert_zeros_date :month, "2013-04-16 20:00:00 PDT", ["2013-03-01", "2013-04-01", "2013-05-01"], "2013-03-01 00:00:00 PST", "2013-05-31 23:59:59 PDT", true
  end

  def test_zeros_quarter
    assert_zeros_date :quarter, "2013-04-16 20:00:00 UTC", ["2013-01-01", "2013-04-01", "2013-07-01"], "2013-01-01", "2013-09-30 23:59:59 UTC"
  end

  def test_zeros_quarter_time_zone
    assert_zeros_date :quarter, "2013-04-16 20:00:00 PDT", ["2013-01-01", "2013-04-01", "2013-07-01"], "2013-01-01 00:00:00 PST", "2013-09-30 23:59:59 PDT", true
  end

  def test_zeros_year
    assert_zeros_date :year, "2013-04-16 20:00:00 UTC", ["2012-01-01", "2013-01-01", "2014-01-01"], "2012-01-01", "2014-12-31 23:59:59 UTC"
  end

  def test_zeros_year_time_zone
    assert_zeros_date :year, "2013-04-16 20:00:00 PDT", ["2012-01-01 00:00:00 PST", "2013-01-01 00:00:00 PST", "2014-01-01 00:00:00 PST"], "2012-01-01 00:00:00 PST", "2014-12-31 23:59:59 PST", true
  end

  def test_zeros_day_of_week
    create_user "2013-05-01"
    expected = {}
    7.times do |n|
      expected[n] = n == 3 ? 1 : 0
    end
    assert_equal expected, call_method(:day_of_week, :created_at, {series: true})
  end

  def test_zeros_hour_of_day
    create_user "2013-05-01 20:00:00 UTC"
    expected = {}
    24.times do |n|
      expected[n] = n == 20 ? 1 : 0
    end
    assert_equal expected, call_method(:hour_of_day, :created_at, {series: true})
  end

  def test_zeros_minute_of_hour
    create_user "2017-02-09 20:05:00 UTC"
    expected = {}
    60.times do |n|
      expected[n] = n == 5 ? 1 : 0
    end
    assert_equal expected, call_method(:minute_of_hour, :created_at, {series: true})
  end

  def test_zeros_day_of_month
    create_user "1978-12-18"
    expected = {}
    (1..31).each do |n|
      expected[n] = n == 18 ? 1 : 0
    end
    assert_equal expected, call_method(:day_of_month, :created_at, {series: true})
  end

  def test_zeros_month_of_year
    create_user "2013-05-01"
    expected = {}
    (1..12).each do |n|
      expected[n] = n == 5 ? 1 : 0
    end
    assert_equal expected, call_method(:month_of_year, :created_at, {series: true})
  end

  def test_zeros_excludes_end
    create_user "2013-05-02"
    expected = {
      Date.parse("2013-05-01") => 0
    }
    assert_equal expected, call_method(:day, :created_at, range: Date.parse("2013-05-01")...Date.parse("2013-05-02"), series: true)
  end

  def test_zeros_datetime
    create_user "2013-05-01"
    expected = {
      Date.parse("2013-05-01") => 1
    }
    assert_equal expected, call_method(:day, :created_at, range: DateTime.parse("2013-05-01")..DateTime.parse("2013-05-01"), series: true)
  end

  def test_zeros_null_value
    create_user nil
    assert_equal 0, call_method(:hour_of_day, :created_at, range: true, series: true)[0]
  end

  def test_zeroes_range_true
    create_user "2013-05-01"
    create_user "2013-05-03"
    expected = {
      Date.parse("2013-05-01") => 1,
      Date.parse("2013-05-02") => 0,
      Date.parse("2013-05-03") => 1
    }
    assert_equal expected, call_method(:day, :created_at, range: true, series: true)
  end

  # week_start

  def test_week_start
    Groupdate.week_start = :mon
    assert_result_date :week, "2013-03-18", "2013-03-24 23:59:59"
  end

  def test_week_start_and_start_option
    Groupdate.week_start = :mon
    assert_result_date :week, "2013-03-16", "2013-03-22 23:59:59", false, week_start: :sat
  end

  # misc

  def test_order_hour_of_day_reverse_option
    assert_equal 23, call_method(:hour_of_day, :created_at, reverse: true, series: true).keys.first
  end

  def test_time_zone
    create_user "2013-05-01"
    time_zone = "Pacific Time (US & Canada)"
    assert_equal time_zone, call_method(:hour, :created_at, time_zone: time_zone).keys.first.time_zone.name
  end

  def test_format_day
    create_user "2014-03-01"
    assert_format :day, "March 1, 2014", "%B %-e, %Y"
  end

  def test_format_month
    create_user "2014-03-01"
    assert_format :month, "March 2014", "%B %Y"
  end

  def test_format_quarter
    create_user "2014-03-05"
    assert_format :quarter, "January 1, 2014", "%B %-e, %Y"
  end

  def test_format_year
    create_user "2014-03-01"
    assert_format :year, "2014", "%Y"
  end

  def test_format_hour_of_day
    create_user "2014-03-01"
    assert_format :hour_of_day, "12 am", "%-l %P"
  end

  def test_format_hour_of_day_day_start
    create_user "2014-03-01"
    assert_format :hour_of_day, "12 am", "%-l %P", day_start: 2
  end

  def test_format_minute_of_hour
    create_user "2017-02-09"
    assert_format :minute_of_hour, "0", "%-M"
  end

  def test_format_minute_of_hour_day_start
    create_user "2017-02-09"
    assert_format :minute_of_hour, "0", "%-M", day_start: 2
  end

  def test_format_day_of_week
    create_user "2014-03-01"
    assert_format :day_of_week, "Sat", "%a"
  end

  def test_format_day_of_week_day_start
    create_user "2014-03-01"
    assert_format :day_of_week, "Fri", "%a", day_start: 2
  end

  def test_format_day_of_week_week_start
    create_user "2014-03-01"
    assert_format :day_of_week, "Sat", "%a", week_start: :mon
  end

  def test_format_day_of_month
    create_user "2014-03-01"
    assert_format :day_of_month, " 1", "%e"
  end

  def test_format_month_of_year
    create_user "2014-01-01"
    assert_format :month_of_year, "Jan", "%b"
  end

  # date column

  def test_date_column
    expected = {
      Date.parse("2013-05-03") => 1
    }
    assert_equal expected, result(:day, "2013-05-03", false)
  end

  def test_date_column_with_time_zone
    expected = {
      Date.parse("2013-05-02") => 1
    }
    assert_equal expected, result(:day, "2013-05-03", true)
  end

  def test_date_column_with_time_zone_false
    Time.zone = pt
    create_user "2013-05-03"
    expected = {
      Date.parse("2013-05-03") => 1
    }
    assert_equal expected, call_method(:day, :created_at, time_zone: false)
  ensure
    Time.zone = nil
  end

  # date range

  def test_date_range
    ENV["TZ"] = "Europe/Oslo"
    expected = {
      Date.parse("2013-05-01") => 0,
      Date.parse("2013-05-02") => 0,
      Date.parse("2013-05-03") => 0
    }
    assert_equal expected, call_method(:day, :created_at, series: true, range: Date.parse("2013-05-01")..Date.parse("2013-05-03"))
  ensure
    ENV["TZ"] = "UTC"
  end

  def test_date_range_exclude_end
    ENV["TZ"] = "Europe/Oslo"
    expected = {
      Date.parse("2013-05-01") => 0,
      Date.parse("2013-05-02") => 0
    }
    assert_equal expected, call_method(:day, :created_at, series: true, range: Date.parse("2013-05-01")...Date.parse("2013-05-03"))
  ensure
    ENV["TZ"] = "UTC"
  end

  # day start

  def test_day_start_decimal_end_of_day
    assert_result_date :day, "2013-05-03", "2013-05-04 02:29:59", false, day_start: 2.5
  end

  def test_day_start_decimal_start_of_day
    assert_result_date :day, "2013-05-03", "2013-05-03 02:30:00", false, day_start: 2.5
  end

  private

  # helpers

  def assert_format(method, expected, format, options = {})
    assert_equal({expected => 1}, call_method(method, :created_at, options.merge(format: format, series: false)))
  end

  def assert_result_time(method, expected, time_str, time_zone = false, options = {})
    expected = {utc.parse(expected).in_time_zone(time_zone ? "Pacific Time (US & Canada)" : utc) => 1}
    assert_equal expected, result(method, time_str, time_zone, options)
  end

  def assert_result_date(method, expected_str, time_str, time_zone = false, options = {})
    create_user time_str
    expected = {Date.parse(expected_str) => 1}
    assert_equal expected, call_method(method, :created_at, options.merge(time_zone: time_zone ? "Pacific Time (US & Canada)" : nil))
    expected = {(time_zone ? pt : utc).parse(expected_str) + options[:day_start].to_f.hours => 1}
    assert_equal expected, call_method(method, :created_at, options.merge(dates: false, time_zone: time_zone ? "Pacific Time (US & Canada)" : nil))
    # assert_equal expected, call_method(method, :created_on, options.merge(time_zone: time_zone ? "Pacific Time (US & Canada)" : nil))
  end

  def assert_result(method, expected, time_str, time_zone = false, options = {})
    assert_equal 1, result(method, time_str, time_zone, options)[expected]
  end

  def result(method, time_str, time_zone = false, options = {})
    create_user time_str
    call_method(method, :created_at, options.merge(time_zone: time_zone ? "Pacific Time (US & Canada)" : nil))
  end

  def assert_zeros(method, created_at, keys, range_start, range_end, time_zone = nil, options = {})
    create_user created_at
    expected = {}
    keys.each_with_index do |key, i|
      expected[utc.parse(key).in_time_zone(time_zone ? "Pacific Time (US & Canada)" : utc)] = i == 1 ? 1 : 0
    end
    assert_equal expected, call_method(method, :created_at, options.merge(series: true, time_zone: time_zone ? "Pacific Time (US & Canada)" : nil, range: Time.parse(range_start)..Time.parse(range_end)))
  end

  def assert_zeros_date(method, created_at, keys, range_start, range_end, time_zone = nil, options = {})
    create_user created_at
    expected = {}
    keys.each_with_index do |key, i|
      expected[Date.parse(key)] = i == 1 ? 1 : 0
    end
    assert_equal expected, call_method(method, :created_at, options.merge(series: true, time_zone: time_zone ? "Pacific Time (US & Canada)" : nil, range: Time.parse(range_start)..Time.parse(range_end)))
  end

  def this_quarters_month
    Time.now.beginning_of_quarter.month
  end

  def this_year
    Time.now.year
  end

  def this_month
    Time.now.month
  end

  def utc
    ActiveSupport::TimeZone["UTC"]
  end

  def pt
    ActiveSupport::TimeZone["Pacific Time (US & Canada)"]
  end

  def brasilia
    ActiveSupport::TimeZone["Brasilia"]
  end
end