aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/type/integer_test.rb
blob: 79bd7ee95dc22b004992e8f56751721136c85fc6 (plain) (blame)
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
# frozen_string_literal: true

require "cases/helper"
require "active_support/core_ext/numeric/time"

module ActiveModel
  module Type
    class IntegerTest < ActiveSupport::TestCase
      test "simple values" do
        type = Type::Integer.new
        assert_nil type.cast("")
        assert_equal 1, type.cast(1)
        assert_equal 1, type.cast("1")
        assert_equal 1, type.cast("1ignore")
        assert_equal 0, type.cast("bad1")
        assert_equal 0, type.cast("bad")
        assert_equal 1, type.cast(1.7)
        assert_equal 0, type.cast(false)
        assert_equal 1, type.cast(true)
        assert_nil type.cast(nil)
      end

      test "random objects cast to nil" do
        type = Type::Integer.new
        assert_nil type.cast([1, 2])
        assert_nil type.cast(1 => 2)
        assert_nil type.cast(1..2)
      end

      test "casting objects without to_i" do
        type = Type::Integer.new
        assert_nil type.cast(::Object.new)
      end

      test "casting nan and infinity" do
        type = Type::Integer.new
        assert_nil type.cast(::Float::NAN)
        assert_nil type.cast(1.0 / 0.0)
      end

      test "casting booleans for database" do
        type = Type::Integer.new
        assert_equal 1, type.serialize(true)
        assert_equal 0, type.serialize(false)
      end

      test "casting duration" do
        type = Type::Integer.new
        assert_equal 1800, type.cast(30.minutes)
        assert_equal 7200, type.cast(2.hours)
      end

      test "casting string for database" do
        type = Type::Integer.new
        assert_nil type.serialize("wibble")
        assert_equal 5, type.serialize("5wibble")
        assert_equal 5, type.serialize(" +5")
        assert_equal(-5, type.serialize(" -5"))
      end

      test "casting empty string" do
        type = Type::Integer.new
        assert_nil type.cast("")
        assert_nil type.serialize("")
        assert_nil type.deserialize("")
      end

      test "changed?" do
        type = Type::Integer.new

        assert type.changed?(0, 0, "wibble")
        assert type.changed?(5, 0, "wibble")
        assert_not type.changed?(5, 5, "5wibble")
        assert_not type.changed?(5, 5, "5")
        assert_not type.changed?(5, 5, "5.0")
        assert_not type.changed?(5, 5, "+5")
        assert_not type.changed?(5, 5, "+5.0")
        assert_not type.changed?(-5, -5, "-5")
        assert_not type.changed?(-5, -5, "-5.0")
        assert_not type.changed?(nil, nil, nil)
      end

      test "values below int min value are out of range" do
        assert_raises(ActiveModel::RangeError) do
          Integer.new.serialize(-2147483649)
        end
      end

      test "values above int max value are out of range" do
        assert_raises(ActiveModel::RangeError) do
          Integer.new.serialize(2147483648)
        end
      end

      test "very small numbers are out of range" do
        assert_raises(ActiveModel::RangeError) do
          Integer.new.serialize(-9999999999999999999999999999999)
        end
      end

      test "very large numbers are out of range" do
        assert_raises(ActiveModel::RangeError) do
          Integer.new.serialize(9999999999999999999999999999999)
        end
      end

      test "normal numbers are in range" do
        type = Integer.new
        assert_equal(0, type.serialize(0))
        assert_equal(-1, type.serialize(-1))
        assert_equal(1, type.serialize(1))
      end

      test "int max value is in range" do
        assert_equal(2147483647, Integer.new.serialize(2147483647))
      end

      test "int min value is in range" do
        assert_equal(-2147483648, Integer.new.serialize(-2147483648))
      end

      test "columns with a larger limit have larger ranges" do
        type = Integer.new(limit: 8)

        assert_equal(9223372036854775807, type.serialize(9223372036854775807))
        assert_equal(-9223372036854775808, type.serialize(-9223372036854775808))
        assert_raises(ActiveModel::RangeError) do
          type.serialize(-9999999999999999999999999999999)
        end
        assert_raises(ActiveModel::RangeError) do
          type.serialize(9999999999999999999999999999999)
        end
      end
    end
  end
end