aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/validations/numericality_validation_test.rb
blob: ca3c3bc40d772c4188f1a92fa37eebdb999bd9f1 (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
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
# frozen_string_literal: true

require "cases/helper"

require "models/topic"
require "models/person"

require "bigdecimal"
require "active_support/core_ext/big_decimal"

class NumericalityValidationTest < ActiveModel::TestCase
  def teardown
    Topic.clear_validators!
  end

  NIL = [nil]
  BLANK = ["", " ", " \t \r \n"]
  BIGDECIMAL_STRINGS = %w(12345678901234567890.1234567890) # 30 significant digits
  FLOAT_STRINGS = %w(0.0 +0.0 -0.0 10.0 10.5 -10.5 -0.0001 -090.1 90.1e1 -90.1e5 -90.1e-5 90e-5)
  INTEGER_STRINGS = %w(0 +0 -0 10 +10 -10 0090 -090)
  FLOATS = [0.0, 10.0, 10.5, -10.5, -0.0001] + FLOAT_STRINGS
  INTEGERS = [0, 10, -10] + INTEGER_STRINGS
  BIGDECIMAL = BIGDECIMAL_STRINGS.collect! { |bd| BigDecimal(bd) }
  JUNK = ["not a number", "42 not a number", "0xdeadbeef", "0xinvalidhex", "0Xdeadbeef", "00-1", "--3", "+-3", "+3-1", "-+019.0", "12.12.13.12", "123\nnot a number"]
  INFINITY = [1.0 / 0.0]

  def test_default_validates_numericality_of
    Topic.validates_numericality_of :approved
    invalid!(NIL + BLANK + JUNK)
    valid!(FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
  end

  def test_validates_numericality_of_with_nil_allowed
    Topic.validates_numericality_of :approved, allow_nil: true

    invalid!(JUNK + BLANK)
    valid!(NIL + FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
  end

  def test_validates_numericality_of_with_blank_allowed
    Topic.validates_numericality_of :approved, allow_blank: true

    invalid!(JUNK)
    valid!(NIL + BLANK + FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
  end

  def test_validates_numericality_of_with_integer_only
    Topic.validates_numericality_of :approved, only_integer: true

    invalid!(NIL + BLANK + JUNK + FLOATS + BIGDECIMAL + INFINITY)
    valid!(INTEGERS)
  end

  def test_validates_numericality_of_with_integer_only_and_nil_allowed
    Topic.validates_numericality_of :approved, only_integer: true, allow_nil: true

    invalid!(JUNK + BLANK + FLOATS + BIGDECIMAL + INFINITY)
    valid!(NIL + INTEGERS)
  end

  def test_validates_numericality_of_with_integer_only_and_symbol_as_value
    Topic.validates_numericality_of :approved, only_integer: :condition_is_false

    invalid!(NIL + BLANK + JUNK)
    valid!(FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
  end

  def test_validates_numericality_of_with_integer_only_and_proc_as_value
    Topic.send(:define_method, :allow_only_integers?, lambda { false })
    Topic.validates_numericality_of :approved, only_integer: Proc.new(&:allow_only_integers?)

    invalid!(NIL + BLANK + JUNK)
    valid!(FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
  end

  def test_validates_numericality_with_greater_than
    Topic.validates_numericality_of :approved, greater_than: 10

    invalid!([-10, 10], "must be greater than 10")
    valid!([11])
  end

  def test_validates_numericality_with_greater_than_using_differing_numeric_types
    Topic.validates_numericality_of :approved, greater_than: BigDecimal("97.18")

    invalid!([-97.18, BigDecimal("97.18"), BigDecimal("-97.18")], "must be greater than 97.18")
    valid!([97.19, 98, BigDecimal("98"), BigDecimal("97.19")])
  end

  def test_validates_numericality_with_greater_than_using_string_value
    Topic.validates_numericality_of :approved, greater_than: 10

    invalid!(["-10", "9", "9.9", "10"], "must be greater than 10")
    valid!(["10.1", "11"])
  end

  def test_validates_numericality_with_greater_than_or_equal
    Topic.validates_numericality_of :approved, greater_than_or_equal_to: 10

    invalid!([-9, 9], "must be greater than or equal to 10")
    valid!([10])
  end

  def test_validates_numericality_with_greater_than_or_equal_using_differing_numeric_types
    Topic.validates_numericality_of :approved, greater_than_or_equal_to: BigDecimal("97.18")

    invalid!([-97.18, 97.17, 97, BigDecimal("97.17"), BigDecimal("-97.18")], "must be greater than or equal to 97.18")
    valid!([97.18, 98, BigDecimal("97.19")])
  end

  def test_validates_numericality_with_greater_than_or_equal_using_string_value
    Topic.validates_numericality_of :approved, greater_than_or_equal_to: 10

    invalid!(["-10", "9", "9.9"], "must be greater than or equal to 10")
    valid!(["10", "10.1", "11"])
  end

  def test_validates_numericality_with_equal_to
    Topic.validates_numericality_of :approved, equal_to: 10

    invalid!([-10, 11] + INFINITY, "must be equal to 10")
    valid!([10])
  end

  def test_validates_numericality_with_equal_to_using_differing_numeric_types
    Topic.validates_numericality_of :approved, equal_to: BigDecimal("97.18")

    invalid!([-97.18], "must be equal to 97.18")
    valid!([BigDecimal("97.18")])
  end

  def test_validates_numericality_with_equal_to_using_string_value
    Topic.validates_numericality_of :approved, equal_to: 10

    invalid!(["-10", "9", "9.9", "10.1", "11"], "must be equal to 10")
    valid!(["10"])
  end

  def test_validates_numericality_with_less_than
    Topic.validates_numericality_of :approved, less_than: 10

    invalid!([10], "must be less than 10")
    valid!([-9, 9])
  end

  def test_validates_numericality_with_less_than_using_differing_numeric_types
    Topic.validates_numericality_of :approved, less_than: BigDecimal("97.18")

    invalid!([97.18, BigDecimal("97.18")], "must be less than 97.18")
    valid!([-97.0, 97.0, -97, 97, BigDecimal("-97"), BigDecimal("97")])
  end

  def test_validates_numericality_with_less_than_using_string_value
    Topic.validates_numericality_of :approved, less_than: 10

    invalid!(["10", "10.1", "11"], "must be less than 10")
    valid!(["-10", "9", "9.9"])
  end

  def test_validates_numericality_with_less_than_or_equal_to
    Topic.validates_numericality_of :approved, less_than_or_equal_to: 10

    invalid!([11], "must be less than or equal to 10")
    valid!([-10, 10])
  end

  def test_validates_numericality_with_less_than_or_equal_to_using_differing_numeric_types
    Topic.validates_numericality_of :approved, less_than_or_equal_to: BigDecimal("97.18")

    invalid!([97.19, 98], "must be less than or equal to 97.18")
    valid!([-97.18, BigDecimal("-97.18"), BigDecimal("97.18")])
  end

  def test_validates_numericality_with_less_than_or_equal_using_string_value
    Topic.validates_numericality_of :approved, less_than_or_equal_to: 10

    invalid!(["10.1", "11"], "must be less than or equal to 10")
    valid!(["-10", "9", "9.9", "10"])
  end

  def test_validates_numericality_with_odd
    Topic.validates_numericality_of :approved, odd: true

    invalid!([-2, 2], "must be odd")
    valid!([-1, 1])
  end

  def test_validates_numericality_with_even
    Topic.validates_numericality_of :approved, even: true

    invalid!([-1, 1], "must be even")
    valid!([-2, 2])
  end

  def test_validates_numericality_with_greater_than_less_than_and_even
    Topic.validates_numericality_of :approved, greater_than: 1, less_than: 4, even: true

    invalid!([1, 3, 4])
    valid!([2])
  end

  def test_validates_numericality_with_other_than
    Topic.validates_numericality_of :approved, other_than: 0

    invalid!([0, 0.0])
    valid!([-1, 42])
  end

  def test_validates_numericality_with_other_than_using_string_value
    Topic.validates_numericality_of :approved, other_than: 0

    invalid!(["0", "0.0"])
    valid!(["-1", "1.1", "42"])
  end

  def test_validates_numericality_with_proc
    Topic.send(:define_method, :min_approved, lambda { 5 })
    Topic.validates_numericality_of :approved, greater_than_or_equal_to: Proc.new(&:min_approved)

    invalid!([3, 4])
    valid!([5, 6])
  ensure
    Topic.send(:remove_method, :min_approved)
  end

  def test_validates_numericality_with_symbol
    Topic.send(:define_method, :max_approved, lambda { 5 })
    Topic.validates_numericality_of :approved, less_than_or_equal_to: :max_approved

    invalid!([6])
    valid!([4, 5])
  ensure
    Topic.send(:remove_method, :max_approved)
  end

  def test_validates_numericality_with_numeric_message
    Topic.validates_numericality_of :approved, less_than: 4, message: "smaller than %{count}"
    topic = Topic.new("title" => "numeric test", "approved" => 10)

    assert_not_predicate topic, :valid?
    assert_equal ["smaller than 4"], topic.errors[:approved]

    Topic.validates_numericality_of :approved, greater_than: 4, message: "greater than %{count}"
    topic = Topic.new("title" => "numeric test", "approved" => 1)

    assert_not_predicate topic, :valid?
    assert_equal ["greater than 4"], topic.errors[:approved]
  end

  def test_validates_numericality_of_for_ruby_class
    Person.validates_numericality_of :karma, allow_nil: false

    p = Person.new
    p.karma = "Pix"
    assert_predicate p, :invalid?

    assert_equal ["is not a number"], p.errors[:karma]

    p.karma = "1234"
    assert_predicate p, :valid?
  ensure
    Person.clear_validators!
  end

  def test_validates_numericality_using_value_before_type_cast_if_possible
    Topic.validates_numericality_of :price

    topic = Topic.new(price: 50)

    assert_equal "$50.00", topic.price
    assert_equal 50, topic.price_before_type_cast
    assert_predicate topic, :valid?
  end

  def test_validates_numericality_with_exponent_number
    base = 10_000_000_000_000_000
    Topic.validates_numericality_of :approved, less_than_or_equal_to: base
    topic = Topic.new
    topic.approved = (base + 1).to_s

    assert_predicate topic, :invalid?
  end

  def test_validates_numericality_with_invalid_args
    assert_raise(ArgumentError) { Topic.validates_numericality_of :approved, greater_than_or_equal_to: "foo" }
    assert_raise(ArgumentError) { Topic.validates_numericality_of :approved, less_than_or_equal_to: "foo" }
    assert_raise(ArgumentError) { Topic.validates_numericality_of :approved, greater_than: "foo" }
    assert_raise(ArgumentError) { Topic.validates_numericality_of :approved, less_than: "foo" }
    assert_raise(ArgumentError) { Topic.validates_numericality_of :approved, equal_to: "foo" }
  end

  private

    def invalid!(values, error = nil)
      with_each_topic_approved_value(values) do |topic, value|
        assert topic.invalid?, "#{value.inspect} not rejected as a number"
        assert topic.errors[:approved].any?, "FAILED for #{value.inspect}"
        assert_equal error, topic.errors[:approved].first if error
      end
    end

    def valid!(values)
      with_each_topic_approved_value(values) do |topic, value|
        assert topic.valid?, "#{value.inspect} not accepted as a number with validation error: #{topic.errors[:approved].first}"
      end
    end

    def with_each_topic_approved_value(values)
      topic = Topic.new(title: "numeric test", content: "whatever")
      values.each do |value|
        topic.approved = value
        yield topic, value
      end
    end
end