2014-10-27 00:51:42 +00:00
|
|
|
|
require 'unit_spec_helper'
|
2010-12-13 22:28:59 +00:00
|
|
|
|
|
2014-12-24 21:37:04 +00:00
|
|
|
|
describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher, type: :model do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
class << self
|
|
|
|
|
def all_qualifiers
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
category: :comparison,
|
|
|
|
|
name: :is_greater_than,
|
|
|
|
|
argument: 1,
|
|
|
|
|
validation_name: :greater_than,
|
|
|
|
|
validation_value: 1,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
category: :comparison,
|
|
|
|
|
name: :is_greater_than_or_equal_to,
|
|
|
|
|
argument: 1,
|
|
|
|
|
validation_name: :greater_than_or_equal_to,
|
|
|
|
|
validation_value: 1,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
category: :comparison,
|
|
|
|
|
name: :is_less_than,
|
|
|
|
|
argument: 1,
|
|
|
|
|
validation_name: :less_than,
|
|
|
|
|
validation_value: 1,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
category: :comparison,
|
|
|
|
|
name: :is_less_than_or_equal_to,
|
|
|
|
|
argument: 1,
|
|
|
|
|
validation_name: :less_than_or_equal_to,
|
|
|
|
|
validation_value: 1,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
category: :comparison,
|
|
|
|
|
name: :is_equal_to,
|
|
|
|
|
argument: 1,
|
|
|
|
|
validation_name: :equal_to,
|
|
|
|
|
validation_value: 1,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
category: :cardinality,
|
|
|
|
|
name: :odd,
|
|
|
|
|
validation_name: :odd,
|
|
|
|
|
validation_value: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
category: :cardinality,
|
|
|
|
|
name: :even,
|
|
|
|
|
validation_name: :even,
|
|
|
|
|
validation_value: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: :only_integer,
|
|
|
|
|
validation_name: :only_integer,
|
|
|
|
|
validation_value: true,
|
2015-04-01 06:00:14 +00:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: :on,
|
|
|
|
|
argument: :customizable,
|
|
|
|
|
validation_name: :on,
|
|
|
|
|
validation_value: :customizable
|
2015-03-29 00:12:24 +00:00
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def qualifiers_under(category)
|
|
|
|
|
all_qualifiers.select do |qualifier|
|
|
|
|
|
qualifier[:category] == category
|
|
|
|
|
end
|
2012-10-16 17:45:06 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
def mutually_exclusive_qualifiers
|
|
|
|
|
qualifiers_under(:cardinality) + qualifiers_under(:comparison)
|
2010-12-13 22:28:59 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
def non_mutually_exclusive_qualifiers
|
|
|
|
|
all_qualifiers - mutually_exclusive_qualifiers
|
2011-10-27 02:21:06 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
def validations_by_qualifier
|
|
|
|
|
all_qualifiers.each_with_object({}) do |qualifier, hash|
|
|
|
|
|
hash[qualifier[:name]] = qualifier[:validation_name]
|
|
|
|
|
end
|
2013-04-12 17:11:07 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
def all_qualifier_combinations
|
|
|
|
|
combinations = []
|
|
|
|
|
|
|
|
|
|
([nil] + mutually_exclusive_qualifiers).each do |mutually_exclusive_qualifier|
|
|
|
|
|
(0..non_mutually_exclusive_qualifiers.length).each do |n|
|
|
|
|
|
non_mutually_exclusive_qualifiers.combination(n) do |combination|
|
|
|
|
|
super_combination = (
|
|
|
|
|
[mutually_exclusive_qualifier] +
|
|
|
|
|
combination
|
|
|
|
|
)
|
|
|
|
|
super_combination.select!(&:present?)
|
|
|
|
|
|
|
|
|
|
if super_combination.any?
|
|
|
|
|
combinations << super_combination
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
combinations
|
2011-10-27 02:21:06 +00:00
|
|
|
|
end
|
2013-03-19 04:50:39 +00:00
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
def default_qualifier_arguments
|
|
|
|
|
all_qualifiers.each_with_object({}) do |qualifier, hash|
|
|
|
|
|
hash[qualifier[:name]] = qualifier[:argument]
|
|
|
|
|
end
|
2013-03-19 04:50:39 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
def default_validation_values
|
|
|
|
|
all_qualifiers.each_with_object({}) do |qualifier, hash|
|
|
|
|
|
hash[qualifier[:validation_name]] = qualifier[:validation_value]
|
|
|
|
|
end
|
2013-03-19 04:50:39 +00:00
|
|
|
|
end
|
2011-10-27 02:21:06 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'qualified with nothing' do
|
|
|
|
|
context 'and validating numericality' do
|
|
|
|
|
it 'accepts' do
|
|
|
|
|
record = build_record_validating_numericality
|
|
|
|
|
expect(record).to validate_numericality
|
|
|
|
|
end
|
2015-10-07 05:12:30 +00:00
|
|
|
|
|
|
|
|
|
context 'when the column is an integer column' do
|
|
|
|
|
it 'raises an IneffectiveTestError' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :integer
|
|
|
|
|
)
|
|
|
|
|
assertion = -> { expect(record).to validate_numericality }
|
|
|
|
|
|
|
|
|
|
expect(&assertion).
|
|
|
|
|
to raise_error(described_class::IneffectiveTestError)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the column is a float column' do
|
|
|
|
|
it 'raises an IneffectiveTestError' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :float
|
|
|
|
|
)
|
|
|
|
|
assertion = -> { expect(record).to validate_numericality }
|
|
|
|
|
|
|
|
|
|
expect(&assertion).
|
|
|
|
|
to raise_error(described_class::IneffectiveTestError)
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-10-21 20:39:39 +00:00
|
|
|
|
|
|
|
|
|
context 'when the column is a decimal column' do
|
|
|
|
|
it 'raises an IneffectiveTestError' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :decimal,
|
|
|
|
|
)
|
|
|
|
|
assertion = -> { expect(record).to validate_numericality }
|
|
|
|
|
|
|
|
|
|
expect(&assertion).
|
|
|
|
|
to raise_error(described_class::IneffectiveTestError)
|
|
|
|
|
end
|
|
|
|
|
end
|
2014-02-21 22:31:48 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'and not validating anything' do
|
|
|
|
|
it 'rejects since it does not disallow non-numbers' do
|
|
|
|
|
record = build_record_validating_nothing
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
assertion = -> { expect(record).to validate_numericality }
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like a number.
|
2015-12-22 07:00:51 +00:00
|
|
|
|
After setting :attr to ‹"abcd"›, the matcher expected the Example to
|
|
|
|
|
be invalid, but it was valid instead.
|
2015-12-13 23:45:24 +00:00
|
|
|
|
MESSAGE
|
|
|
|
|
|
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
2014-02-21 22:31:48 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'qualified with allow_nil' do
|
|
|
|
|
context 'and validating with allow_nil' do
|
|
|
|
|
it 'accepts' do
|
|
|
|
|
record = build_record_validating_numericality(allow_nil: true)
|
|
|
|
|
expect(record).to validate_numericality.allow_nil
|
|
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'and not validating with allow_nil' do
|
|
|
|
|
it 'rejects since it tries to treat nil as a number' do
|
|
|
|
|
record = build_record_validating_numericality
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).to validate_numericality.allow_nil
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like a number, but
|
|
|
|
|
only if it is not nil.
|
2015-12-22 07:00:51 +00:00
|
|
|
|
In checking that Example allows :attr to be ‹nil›, after setting :attr
|
|
|
|
|
to ‹nil›, the matcher expected the Example to be valid, but it was
|
2015-12-13 23:45:24 +00:00
|
|
|
|
invalid instead, producing these validation errors:
|
|
|
|
|
|
|
|
|
|
* attr: ["is not a number"]
|
|
|
|
|
MESSAGE
|
|
|
|
|
|
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
|
end
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'qualified with only_integer' do
|
|
|
|
|
context 'and validating with only_integer' do
|
|
|
|
|
it 'accepts' do
|
|
|
|
|
record = build_record_validating_numericality(only_integer: true)
|
|
|
|
|
expect(record).to validate_numericality.only_integer
|
|
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'and not validating with only_integer' do
|
|
|
|
|
it 'rejects since it does not disallow non-integers' do
|
|
|
|
|
record = build_record_validating_numericality
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).to validate_numericality.only_integer
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like an integer.
|
2015-12-22 07:00:51 +00:00
|
|
|
|
After setting :attr to ‹"0.1"›, the matcher expected the Example to be
|
2015-12-13 23:45:24 +00:00
|
|
|
|
invalid, but it was valid instead.
|
|
|
|
|
MESSAGE
|
|
|
|
|
|
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
2013-03-19 04:50:39 +00:00
|
|
|
|
end
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
2013-03-19 04:50:39 +00:00
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'qualified with odd' do
|
|
|
|
|
context 'and validating with odd' do
|
|
|
|
|
it 'accepts' do
|
|
|
|
|
record = build_record_validating_numericality(odd: true)
|
|
|
|
|
expect(record).to validate_numericality.odd
|
|
|
|
|
end
|
2015-10-07 05:12:30 +00:00
|
|
|
|
|
|
|
|
|
context 'when the column is an integer column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :integer,
|
|
|
|
|
odd: true
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).to validate_numericality.odd
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the column is a float column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :float,
|
|
|
|
|
odd: true
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).to validate_numericality.odd
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-10-21 20:39:39 +00:00
|
|
|
|
|
|
|
|
|
context 'when the column is a decimal column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :decimal,
|
|
|
|
|
odd: true,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).to validate_numericality.odd
|
|
|
|
|
end
|
|
|
|
|
end
|
2013-03-19 04:50:39 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'and not validating with odd' do
|
|
|
|
|
it 'rejects since it does not disallow even numbers' do
|
|
|
|
|
record = build_record_validating_numericality
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).to validate_numericality.odd
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like an odd number.
|
2015-12-22 07:00:51 +00:00
|
|
|
|
After setting :attr to ‹"2"›, the matcher expected the Example to be
|
2015-12-13 23:45:24 +00:00
|
|
|
|
invalid, but it was valid instead.
|
|
|
|
|
MESSAGE
|
|
|
|
|
|
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
2013-03-19 04:50:39 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'qualified with even' do
|
|
|
|
|
context 'and validating with even' do
|
2015-10-07 05:12:30 +00:00
|
|
|
|
it 'accepts' do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
record = build_record_validating_numericality(even: true)
|
|
|
|
|
expect(record).to validate_numericality.even
|
|
|
|
|
end
|
2015-10-07 05:12:30 +00:00
|
|
|
|
|
|
|
|
|
context 'when the column is an integer column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :integer,
|
|
|
|
|
even: true
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).to validate_numericality.even
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the column is a float column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :float,
|
|
|
|
|
even: true
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).to validate_numericality.even
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-10-21 20:39:39 +00:00
|
|
|
|
|
|
|
|
|
context 'when the column is a decimal column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :decimal,
|
|
|
|
|
even: true,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).to validate_numericality.even
|
|
|
|
|
end
|
|
|
|
|
end
|
2013-03-19 04:50:39 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'and not validating with even' do
|
|
|
|
|
it 'rejects since it does not disallow odd numbers' do
|
|
|
|
|
record = build_record_validating_numericality
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).to validate_numericality.even
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like an even number.
|
2015-12-22 07:00:51 +00:00
|
|
|
|
After setting :attr to ‹"1"›, the matcher expected the Example to be
|
2015-12-13 23:45:24 +00:00
|
|
|
|
invalid, but it was valid instead.
|
|
|
|
|
MESSAGE
|
|
|
|
|
|
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
2013-03-19 04:50:39 +00:00
|
|
|
|
end
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
2013-03-19 04:50:39 +00:00
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'qualified with is_less_than_or_equal_to' do
|
|
|
|
|
context 'and validating with less_than_or_equal_to' do
|
|
|
|
|
it 'accepts' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
less_than_or_equal_to: 18
|
|
|
|
|
)
|
|
|
|
|
expect(record).to validate_numericality.is_less_than_or_equal_to(18)
|
|
|
|
|
end
|
2015-10-07 05:12:30 +00:00
|
|
|
|
|
|
|
|
|
context 'when the column is an integer column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :integer,
|
|
|
|
|
less_than_or_equal_to: 18
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).to validate_numericality.is_less_than_or_equal_to(18)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the column is a float column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :float,
|
|
|
|
|
less_than_or_equal_to: 18
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).to validate_numericality.is_less_than_or_equal_to(18)
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-10-21 20:39:39 +00:00
|
|
|
|
|
|
|
|
|
context 'when the column is a decimal column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :decimal,
|
|
|
|
|
less_than_or_equal_to: 18,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).to validate_numericality.is_less_than_or_equal_to(18)
|
|
|
|
|
end
|
|
|
|
|
end
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'and not validating with less_than_or_equal_to' do
|
|
|
|
|
it 'rejects since it does not disallow numbers greater than the value' do
|
|
|
|
|
record = build_record_validating_numericality
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
assertion = lambda do
|
2015-12-13 23:45:24 +00:00
|
|
|
|
expect(record).to validate_numericality.is_less_than_or_equal_to(18)
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like a number less
|
|
|
|
|
than or equal to 18.
|
2015-12-22 07:00:51 +00:00
|
|
|
|
After setting :attr to ‹"19"›, the matcher expected the Example to be
|
2015-12-13 23:45:24 +00:00
|
|
|
|
invalid, but it was valid instead.
|
|
|
|
|
MESSAGE
|
|
|
|
|
|
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
2015-01-21 22:46:26 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'qualified with is_less_than' do
|
|
|
|
|
context 'and validating with less_than' do
|
|
|
|
|
it 'accepts' do
|
|
|
|
|
record = build_record_validating_numericality(less_than: 18)
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
is_less_than(18)
|
|
|
|
|
end
|
2015-10-07 05:12:30 +00:00
|
|
|
|
|
|
|
|
|
context 'when the column is an integer column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :integer,
|
|
|
|
|
less_than: 18
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).to validate_numericality.is_less_than(18)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the column is a float column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :float,
|
|
|
|
|
less_than: 18
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).to validate_numericality.is_less_than(18)
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-10-21 20:39:39 +00:00
|
|
|
|
|
|
|
|
|
context 'when the column is a decimal column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :decimal,
|
|
|
|
|
less_than: 18,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).to validate_numericality.is_less_than(18)
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'and not validating with less_than' do
|
|
|
|
|
it 'rejects since it does not disallow numbers greater than or equal to the value' do
|
|
|
|
|
record = build_record_validating_numericality
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
assertion = lambda do
|
2015-12-13 23:45:24 +00:00
|
|
|
|
expect(record).to validate_numericality.is_less_than(18)
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like a number less
|
|
|
|
|
than 18.
|
2015-12-22 07:00:51 +00:00
|
|
|
|
After setting :attr to ‹"19"›, the matcher expected the Example to be
|
2015-12-13 23:45:24 +00:00
|
|
|
|
invalid, but it was valid instead.
|
|
|
|
|
MESSAGE
|
|
|
|
|
|
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
2015-01-21 22:46:26 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'qualified with is_equal_to' do
|
|
|
|
|
context 'and validating with equal_to' do
|
|
|
|
|
it 'accepts' do
|
|
|
|
|
record = build_record_validating_numericality(equal_to: 18)
|
|
|
|
|
expect(record).to validate_numericality.is_equal_to(18)
|
|
|
|
|
end
|
2015-10-07 05:12:30 +00:00
|
|
|
|
|
|
|
|
|
context 'when the column is an integer column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :integer,
|
|
|
|
|
equal_to: 18
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).to validate_numericality.is_equal_to(18)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the column is a float column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :float,
|
|
|
|
|
equal_to: 18
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).to validate_numericality.is_equal_to(18)
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-10-21 20:39:39 +00:00
|
|
|
|
|
|
|
|
|
context 'when the column is a decimal column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :decimal,
|
|
|
|
|
equal_to: 18,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).to validate_numericality.is_equal_to(18)
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-01-21 22:46:26 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'and not validating with equal_to' do
|
|
|
|
|
it 'rejects since it does not disallow numbers that are not the value' do
|
|
|
|
|
record = build_record_validating_numericality
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).to validate_numericality.is_equal_to(18)
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like a number equal
|
|
|
|
|
to 18.
|
2015-12-22 07:00:51 +00:00
|
|
|
|
After setting :attr to ‹"19"›, the matcher expected the Example to be
|
2015-12-13 23:45:24 +00:00
|
|
|
|
invalid, but it was valid instead.
|
|
|
|
|
MESSAGE
|
|
|
|
|
|
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
2015-01-21 22:46:26 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'qualified with is_greater_than_or_equal to' do
|
|
|
|
|
context 'validating with greater_than_or_equal_to' do
|
|
|
|
|
it 'accepts' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
greater_than_or_equal_to: 18
|
|
|
|
|
)
|
2015-01-21 22:46:26 +00:00
|
|
|
|
expect(record).
|
2015-03-29 00:12:24 +00:00
|
|
|
|
to validate_numericality.
|
|
|
|
|
is_greater_than_or_equal_to(18)
|
|
|
|
|
end
|
2015-10-07 05:12:30 +00:00
|
|
|
|
|
|
|
|
|
context 'when the column is an integer column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :integer,
|
|
|
|
|
greater_than_or_equal_to: 18
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
is_greater_than_or_equal_to(18)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the column is a float column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :float,
|
|
|
|
|
greater_than_or_equal_to: 18
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
is_greater_than_or_equal_to(18)
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-10-21 20:39:39 +00:00
|
|
|
|
|
|
|
|
|
context 'when the column is a decimal column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :decimal,
|
|
|
|
|
greater_than_or_equal_to: 18,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
is_greater_than_or_equal_to(18)
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'not validating with greater_than_or_equal_to' do
|
|
|
|
|
it 'rejects since it does not disallow numbers that are less than the value' do
|
|
|
|
|
record = build_record_validating_numericality
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
assertion = lambda do
|
2015-12-13 23:45:24 +00:00
|
|
|
|
expect(record).to validate_numericality.
|
2015-03-29 00:12:24 +00:00
|
|
|
|
is_greater_than_or_equal_to(18)
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like a number greater
|
|
|
|
|
than or equal to 18.
|
2015-12-22 07:00:51 +00:00
|
|
|
|
After setting :attr to ‹"17"›, the matcher expected the Example to be
|
2015-12-13 23:45:24 +00:00
|
|
|
|
invalid, but it was valid instead.
|
|
|
|
|
MESSAGE
|
|
|
|
|
|
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
2015-01-21 22:46:26 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'qualified with is_greater_than' do
|
|
|
|
|
context 'and validating with greater_than' do
|
|
|
|
|
it 'accepts' do
|
|
|
|
|
record = build_record_validating_numericality(greater_than: 18)
|
2015-01-21 22:46:26 +00:00
|
|
|
|
expect(record).
|
2015-03-29 00:12:24 +00:00
|
|
|
|
to validate_numericality.
|
|
|
|
|
is_greater_than(18)
|
|
|
|
|
end
|
2015-10-07 05:12:30 +00:00
|
|
|
|
|
|
|
|
|
context 'when the column is an integer column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :integer,
|
|
|
|
|
greater_than: 18
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
is_greater_than(18)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'when the column is a float column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :float,
|
|
|
|
|
greater_than: 18
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
is_greater_than(18)
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-10-21 20:39:39 +00:00
|
|
|
|
|
|
|
|
|
context 'when the column is a decimal column' do
|
|
|
|
|
it 'accepts (and does not raise an error)' do
|
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
column_type: :decimal,
|
|
|
|
|
greater_than: 18,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
is_greater_than(18)
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'and not validating with greater_than' do
|
|
|
|
|
it 'rejects since it does not disallow numbers that are less than or equal to the value' do
|
|
|
|
|
record = build_record_validating_numericality
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
assertion = lambda do
|
2015-12-13 23:45:24 +00:00
|
|
|
|
expect(record).to validate_numericality.is_greater_than(18)
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like a number greater
|
|
|
|
|
than 18.
|
2015-12-22 07:00:51 +00:00
|
|
|
|
After setting :attr to ‹"18"›, the matcher expected the Example to be
|
2015-12-13 23:45:24 +00:00
|
|
|
|
invalid, but it was valid instead.
|
|
|
|
|
MESSAGE
|
|
|
|
|
|
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
2015-01-21 22:46:26 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'qualified with with_message' do
|
|
|
|
|
context 'and validating with the same message' do
|
|
|
|
|
it 'accepts' do
|
|
|
|
|
record = build_record_validating_numericality(message: 'custom')
|
|
|
|
|
expect(record).to validate_numericality.with_message(/custom/)
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
2014-02-27 03:26:37 +00:00
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'and validating with a different message' do
|
2015-12-13 23:45:24 +00:00
|
|
|
|
it 'rejects with the correct failure message' do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
record = build_record_validating_numericality(message: 'custom')
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).to validate_numericality.with_message(/wrong/)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like a number,
|
|
|
|
|
producing a custom validation error on failure.
|
2015-12-22 07:00:51 +00:00
|
|
|
|
After setting :attr to ‹"abcd"›, the matcher expected the Example to
|
|
|
|
|
be invalid and to produce a validation error matching ‹/wrong/› on
|
|
|
|
|
:attr. The record was indeed invalid, but it produced these validation
|
|
|
|
|
errors instead:
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
* attr: ["custom"]
|
|
|
|
|
MESSAGE
|
|
|
|
|
|
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'and no message is provided' do
|
|
|
|
|
it 'ignores the qualifier' do
|
|
|
|
|
record = build_record_validating_numericality
|
|
|
|
|
expect(record).to validate_numericality.with_message(nil)
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
context 'and the validation is missing from the model' do
|
|
|
|
|
it 'rejects with the correct failure message' do
|
|
|
|
|
model = define_model_validating_nothing
|
|
|
|
|
|
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(model.new).to validate_numericality.with_message(/wrong/)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like a number,
|
|
|
|
|
producing a custom validation error on failure.
|
2015-12-22 07:00:51 +00:00
|
|
|
|
After setting :attr to ‹"abcd"›, the matcher expected the Example to
|
|
|
|
|
be invalid, but it was valid instead.
|
2015-12-13 23:45:24 +00:00
|
|
|
|
MESSAGE
|
|
|
|
|
|
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
2014-02-27 03:26:37 +00:00
|
|
|
|
|
2014-12-03 22:06:23 +00:00
|
|
|
|
context 'qualified with strict' do
|
|
|
|
|
context 'and validating strictly' do
|
|
|
|
|
it 'accepts' do
|
|
|
|
|
record = build_record_validating_numericality(strict: true)
|
|
|
|
|
expect(record).to validate_numericality.strict
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'and not validating strictly' do
|
|
|
|
|
it 'rejects since ActiveModel::StrictValidationFailed is never raised' do
|
|
|
|
|
record = build_record_validating_numericality(attribute_name: :attr)
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2014-12-03 22:06:23 +00:00
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).to validate_numericality_of(:attr).strict
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like a number,
|
|
|
|
|
raising a validation exception on failure.
|
2015-12-22 07:00:51 +00:00
|
|
|
|
After setting :attr to ‹"abcd"›, the matcher expected the Example to
|
|
|
|
|
be invalid and to raise a validation exception, but the record
|
|
|
|
|
produced validation errors instead.
|
2015-12-13 23:45:24 +00:00
|
|
|
|
MESSAGE
|
|
|
|
|
|
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2014-12-03 22:06:23 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-04-01 06:00:14 +00:00
|
|
|
|
context 'qualified with on and validating with on' do
|
|
|
|
|
it 'accepts' do
|
|
|
|
|
record = build_record_validating_numericality(on: :customizable)
|
|
|
|
|
expect(record).to validate_numericality.on(:customizable)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'qualified with on but not validating with on' do
|
|
|
|
|
it 'accepts since the validation never considers a context' do
|
|
|
|
|
record = build_record_validating_numericality
|
|
|
|
|
expect(record).to validate_numericality.on(:customizable)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'not qualified with on but validating with on' do
|
|
|
|
|
it 'rejects since the validation never runs' do
|
|
|
|
|
record = build_record_validating_numericality(on: :customizable)
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-01 06:00:14 +00:00
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).to validate_numericality
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like a number.
|
2015-12-22 07:00:51 +00:00
|
|
|
|
After setting :attr to ‹"abcd"›, the matcher expected the Example to
|
|
|
|
|
be invalid, but it was valid instead.
|
2015-12-13 23:45:24 +00:00
|
|
|
|
MESSAGE
|
|
|
|
|
|
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2015-04-01 06:00:14 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'with combinations of qualifiers together' do
|
|
|
|
|
all_qualifier_combinations.each do |combination|
|
|
|
|
|
if combination.size > 1
|
|
|
|
|
it do
|
|
|
|
|
validation_options = build_validation_options(for: combination)
|
|
|
|
|
record = build_record_validating_numericality(validation_options)
|
|
|
|
|
validate_numericality = self.validate_numericality
|
|
|
|
|
apply_qualifiers!(for: combination, to: validate_numericality)
|
|
|
|
|
expect(record).to validate_numericality
|
|
|
|
|
end
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'when the qualifiers do not match the validation options' do
|
2015-04-09 14:27:26 +00:00
|
|
|
|
specify 'such as validating even but testing that only_integer is validated' do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
even: true,
|
|
|
|
|
greater_than: 18
|
|
|
|
|
)
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
only_integer.
|
|
|
|
|
is_greater_than(18)
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like an integer
|
|
|
|
|
greater than 18.
|
|
|
|
|
In checking that Example disallows :attr from being a decimal number,
|
2015-12-22 07:00:51 +00:00
|
|
|
|
after setting :attr to ‹"0.1"›, the matcher expected the Example to be
|
2015-12-13 23:45:24 +00:00
|
|
|
|
invalid and to produce the validation error "must be an integer" on
|
|
|
|
|
:attr. The record was indeed invalid, but it produced these validation
|
|
|
|
|
errors instead:
|
|
|
|
|
|
|
|
|
|
* attr: ["must be greater than 18"]
|
Tighten CouldNotSetAttributeError restriction
Why:
* Previously, `allow_value` would raise a CouldNotSetAttributeError
if the value being set didn't match the value the attribute had after
being set, but only if the attribute was being changed from nil to
non-nil or non-nil to nil.
* It turns out it doesn't matter which value you're trying to set the
attribute to -- if the attribute rejects that change it's confusing
either way. (In fact, I was recently bit by a case in which I was
trying to validate numericality of an attribute, where the writer
method for that attribute was overridden to ensure that the attribute
always stored a number and never contained non-number characters.
This ended up making the numericality validation useless, of
course -- but it caused confusion because the test acted in a way
I didn't expect.)
To satisfy the above:
* `allow_value` now raises a CouldNotSetAttributeError if the attribute
rejects the value being set in *any* way.
* However, add a `ignoring_interference_by_writer` qualifier so that
it is possible to manually override this behavior.
* Fix tests that are failing now because of this new change:
* Fix tests for allow_value matcher
* Fix tests for numericality matcher
* Remove tests for numericality matcher + integer column
* An integer column will typecast any non-integer value to an
integer.
* Because of the typecasting, our tests for the numericality matcher
against an integer column don't quite work, because we can't
really test what happens when the attribute is set to a
non-integer value. Now that `allow_value` is more strict, we're
getting a CouldNotSetAttributeError when attempting to do so.
* The tests mentioned were originally added to ensure that we are
handling RangeErrors that ActiveRecord used to emit. This doesn't
happen anymore, so the tests aren't necessary anymore either.
* Fix tests for acceptance matcher
* Fix tests for absence matcher
2015-09-26 05:10:00 +00:00
|
|
|
|
MESSAGE
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
specify 'such as not validating only_integer but testing that only_integer is validated' do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
record = build_record_validating_numericality(greater_than: 18)
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
only_integer.
|
|
|
|
|
is_greater_than(18)
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
Tighten CouldNotSetAttributeError restriction
Why:
* Previously, `allow_value` would raise a CouldNotSetAttributeError
if the value being set didn't match the value the attribute had after
being set, but only if the attribute was being changed from nil to
non-nil or non-nil to nil.
* It turns out it doesn't matter which value you're trying to set the
attribute to -- if the attribute rejects that change it's confusing
either way. (In fact, I was recently bit by a case in which I was
trying to validate numericality of an attribute, where the writer
method for that attribute was overridden to ensure that the attribute
always stored a number and never contained non-number characters.
This ended up making the numericality validation useless, of
course -- but it caused confusion because the test acted in a way
I didn't expect.)
To satisfy the above:
* `allow_value` now raises a CouldNotSetAttributeError if the attribute
rejects the value being set in *any* way.
* However, add a `ignoring_interference_by_writer` qualifier so that
it is possible to manually override this behavior.
* Fix tests that are failing now because of this new change:
* Fix tests for allow_value matcher
* Fix tests for numericality matcher
* Remove tests for numericality matcher + integer column
* An integer column will typecast any non-integer value to an
integer.
* Because of the typecasting, our tests for the numericality matcher
against an integer column don't quite work, because we can't
really test what happens when the attribute is set to a
non-integer value. Now that `allow_value` is more strict, we're
getting a CouldNotSetAttributeError when attempting to do so.
* The tests mentioned were originally added to ensure that we are
handling RangeErrors that ActiveRecord used to emit. This doesn't
happen anymore, so the tests aren't necessary anymore either.
* Fix tests for acceptance matcher
* Fix tests for absence matcher
2015-09-26 05:10:00 +00:00
|
|
|
|
message = <<-MESSAGE.strip_heredoc
|
2015-12-13 23:45:24 +00:00
|
|
|
|
Example did not properly validate that :attr looks like an integer
|
|
|
|
|
greater than 18.
|
|
|
|
|
In checking that Example disallows :attr from being a decimal number,
|
2015-12-22 07:00:51 +00:00
|
|
|
|
after setting :attr to ‹"0.1"›, the matcher expected the Example to be
|
2015-12-13 23:45:24 +00:00
|
|
|
|
invalid and to produce the validation error "must be an integer" on
|
|
|
|
|
:attr. The record was indeed invalid, but it produced these validation
|
|
|
|
|
errors instead:
|
|
|
|
|
|
|
|
|
|
* attr: ["must be greater than 18"]
|
Tighten CouldNotSetAttributeError restriction
Why:
* Previously, `allow_value` would raise a CouldNotSetAttributeError
if the value being set didn't match the value the attribute had after
being set, but only if the attribute was being changed from nil to
non-nil or non-nil to nil.
* It turns out it doesn't matter which value you're trying to set the
attribute to -- if the attribute rejects that change it's confusing
either way. (In fact, I was recently bit by a case in which I was
trying to validate numericality of an attribute, where the writer
method for that attribute was overridden to ensure that the attribute
always stored a number and never contained non-number characters.
This ended up making the numericality validation useless, of
course -- but it caused confusion because the test acted in a way
I didn't expect.)
To satisfy the above:
* `allow_value` now raises a CouldNotSetAttributeError if the attribute
rejects the value being set in *any* way.
* However, add a `ignoring_interference_by_writer` qualifier so that
it is possible to manually override this behavior.
* Fix tests that are failing now because of this new change:
* Fix tests for allow_value matcher
* Fix tests for numericality matcher
* Remove tests for numericality matcher + integer column
* An integer column will typecast any non-integer value to an
integer.
* Because of the typecasting, our tests for the numericality matcher
against an integer column don't quite work, because we can't
really test what happens when the attribute is set to a
non-integer value. Now that `allow_value` is more strict, we're
getting a CouldNotSetAttributeError when attempting to do so.
* The tests mentioned were originally added to ensure that we are
handling RangeErrors that ActiveRecord used to emit. This doesn't
happen anymore, so the tests aren't necessary anymore either.
* Fix tests for acceptance matcher
* Fix tests for absence matcher
2015-09-26 05:10:00 +00:00
|
|
|
|
MESSAGE
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
specify 'such as validating greater_than_or_equal_to (+ even) but testing that greater_than is validated' do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
even: true,
|
|
|
|
|
greater_than_or_equal_to: 18
|
|
|
|
|
)
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
even.
|
|
|
|
|
is_greater_than(18)
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like an even number
|
|
|
|
|
greater than 18.
|
|
|
|
|
In checking that Example disallows :attr from being a number that is
|
2015-12-22 07:00:51 +00:00
|
|
|
|
not greater than 18, after setting :attr to ‹"18"›, the matcher
|
|
|
|
|
expected the Example to be invalid, but it was valid instead.
|
Tighten CouldNotSetAttributeError restriction
Why:
* Previously, `allow_value` would raise a CouldNotSetAttributeError
if the value being set didn't match the value the attribute had after
being set, but only if the attribute was being changed from nil to
non-nil or non-nil to nil.
* It turns out it doesn't matter which value you're trying to set the
attribute to -- if the attribute rejects that change it's confusing
either way. (In fact, I was recently bit by a case in which I was
trying to validate numericality of an attribute, where the writer
method for that attribute was overridden to ensure that the attribute
always stored a number and never contained non-number characters.
This ended up making the numericality validation useless, of
course -- but it caused confusion because the test acted in a way
I didn't expect.)
To satisfy the above:
* `allow_value` now raises a CouldNotSetAttributeError if the attribute
rejects the value being set in *any* way.
* However, add a `ignoring_interference_by_writer` qualifier so that
it is possible to manually override this behavior.
* Fix tests that are failing now because of this new change:
* Fix tests for allow_value matcher
* Fix tests for numericality matcher
* Remove tests for numericality matcher + integer column
* An integer column will typecast any non-integer value to an
integer.
* Because of the typecasting, our tests for the numericality matcher
against an integer column don't quite work, because we can't
really test what happens when the attribute is set to a
non-integer value. Now that `allow_value` is more strict, we're
getting a CouldNotSetAttributeError when attempting to do so.
* The tests mentioned were originally added to ensure that we are
handling RangeErrors that ActiveRecord used to emit. This doesn't
happen anymore, so the tests aren't necessary anymore either.
* Fix tests for acceptance matcher
* Fix tests for absence matcher
2015-09-26 05:10:00 +00:00
|
|
|
|
MESSAGE
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
specify 'such as validating odd (+ greater_than) but testing that even is validated' do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
odd: true,
|
|
|
|
|
greater_than: 18
|
|
|
|
|
)
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
even.
|
|
|
|
|
is_greater_than(18)
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like an even number
|
|
|
|
|
greater than 18.
|
|
|
|
|
In checking that Example disallows :attr from being an odd number,
|
2015-12-22 07:00:51 +00:00
|
|
|
|
after setting :attr to ‹"1"›, the matcher expected the Example to be
|
2015-12-13 23:45:24 +00:00
|
|
|
|
invalid and to produce the validation error "must be even" on :attr.
|
|
|
|
|
The record was indeed invalid, but it produced these validation errors
|
|
|
|
|
instead:
|
|
|
|
|
|
|
|
|
|
* attr: ["must be greater than 18"]
|
Tighten CouldNotSetAttributeError restriction
Why:
* Previously, `allow_value` would raise a CouldNotSetAttributeError
if the value being set didn't match the value the attribute had after
being set, but only if the attribute was being changed from nil to
non-nil or non-nil to nil.
* It turns out it doesn't matter which value you're trying to set the
attribute to -- if the attribute rejects that change it's confusing
either way. (In fact, I was recently bit by a case in which I was
trying to validate numericality of an attribute, where the writer
method for that attribute was overridden to ensure that the attribute
always stored a number and never contained non-number characters.
This ended up making the numericality validation useless, of
course -- but it caused confusion because the test acted in a way
I didn't expect.)
To satisfy the above:
* `allow_value` now raises a CouldNotSetAttributeError if the attribute
rejects the value being set in *any* way.
* However, add a `ignoring_interference_by_writer` qualifier so that
it is possible to manually override this behavior.
* Fix tests that are failing now because of this new change:
* Fix tests for allow_value matcher
* Fix tests for numericality matcher
* Remove tests for numericality matcher + integer column
* An integer column will typecast any non-integer value to an
integer.
* Because of the typecasting, our tests for the numericality matcher
against an integer column don't quite work, because we can't
really test what happens when the attribute is set to a
non-integer value. Now that `allow_value` is more strict, we're
getting a CouldNotSetAttributeError when attempting to do so.
* The tests mentioned were originally added to ensure that we are
handling RangeErrors that ActiveRecord used to emit. This doesn't
happen anymore, so the tests aren't necessary anymore either.
* Fix tests for acceptance matcher
* Fix tests for absence matcher
2015-09-26 05:10:00 +00:00
|
|
|
|
MESSAGE
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
specify 'such as validating greater_than_or_equal_to (+ odd) but testing that is_less_than_or_equal_to is validated' do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
odd: true,
|
|
|
|
|
greater_than_or_equal_to: 99
|
|
|
|
|
)
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
odd.
|
|
|
|
|
is_less_than_or_equal_to(99)
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like an odd number
|
|
|
|
|
less than or equal to 99.
|
|
|
|
|
In checking that Example disallows :attr from being a number that is
|
2015-12-22 07:00:51 +00:00
|
|
|
|
not less than or equal to 99, after setting :attr to ‹"101"›, the
|
2015-12-13 23:45:24 +00:00
|
|
|
|
matcher expected the Example to be invalid, but it was valid instead.
|
Tighten CouldNotSetAttributeError restriction
Why:
* Previously, `allow_value` would raise a CouldNotSetAttributeError
if the value being set didn't match the value the attribute had after
being set, but only if the attribute was being changed from nil to
non-nil or non-nil to nil.
* It turns out it doesn't matter which value you're trying to set the
attribute to -- if the attribute rejects that change it's confusing
either way. (In fact, I was recently bit by a case in which I was
trying to validate numericality of an attribute, where the writer
method for that attribute was overridden to ensure that the attribute
always stored a number and never contained non-number characters.
This ended up making the numericality validation useless, of
course -- but it caused confusion because the test acted in a way
I didn't expect.)
To satisfy the above:
* `allow_value` now raises a CouldNotSetAttributeError if the attribute
rejects the value being set in *any* way.
* However, add a `ignoring_interference_by_writer` qualifier so that
it is possible to manually override this behavior.
* Fix tests that are failing now because of this new change:
* Fix tests for allow_value matcher
* Fix tests for numericality matcher
* Remove tests for numericality matcher + integer column
* An integer column will typecast any non-integer value to an
integer.
* Because of the typecasting, our tests for the numericality matcher
against an integer column don't quite work, because we can't
really test what happens when the attribute is set to a
non-integer value. Now that `allow_value` is more strict, we're
getting a CouldNotSetAttributeError when attempting to do so.
* The tests mentioned were originally added to ensure that we are
handling RangeErrors that ActiveRecord used to emit. This doesn't
happen anymore, so the tests aren't necessary anymore either.
* Fix tests for acceptance matcher
* Fix tests for absence matcher
2015-09-26 05:10:00 +00:00
|
|
|
|
MESSAGE
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
specify 'such as validating greater_than_or_equal_to (+ only_integer + less_than) but testing that greater_than is validated' do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
only_integer: true,
|
|
|
|
|
greater_than_or_equal_to: 18,
|
|
|
|
|
less_than: 99
|
|
|
|
|
)
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
only_integer.
|
|
|
|
|
is_greater_than(18).
|
|
|
|
|
is_less_than(99)
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like an integer
|
|
|
|
|
greater than 18 and less than 99.
|
|
|
|
|
In checking that Example disallows :attr from being a number that is
|
2015-12-22 07:00:51 +00:00
|
|
|
|
not greater than 18, after setting :attr to ‹"18"›, the matcher
|
|
|
|
|
expected the Example to be invalid, but it was valid instead.
|
Tighten CouldNotSetAttributeError restriction
Why:
* Previously, `allow_value` would raise a CouldNotSetAttributeError
if the value being set didn't match the value the attribute had after
being set, but only if the attribute was being changed from nil to
non-nil or non-nil to nil.
* It turns out it doesn't matter which value you're trying to set the
attribute to -- if the attribute rejects that change it's confusing
either way. (In fact, I was recently bit by a case in which I was
trying to validate numericality of an attribute, where the writer
method for that attribute was overridden to ensure that the attribute
always stored a number and never contained non-number characters.
This ended up making the numericality validation useless, of
course -- but it caused confusion because the test acted in a way
I didn't expect.)
To satisfy the above:
* `allow_value` now raises a CouldNotSetAttributeError if the attribute
rejects the value being set in *any* way.
* However, add a `ignoring_interference_by_writer` qualifier so that
it is possible to manually override this behavior.
* Fix tests that are failing now because of this new change:
* Fix tests for allow_value matcher
* Fix tests for numericality matcher
* Remove tests for numericality matcher + integer column
* An integer column will typecast any non-integer value to an
integer.
* Because of the typecasting, our tests for the numericality matcher
against an integer column don't quite work, because we can't
really test what happens when the attribute is set to a
non-integer value. Now that `allow_value` is more strict, we're
getting a CouldNotSetAttributeError when attempting to do so.
* The tests mentioned were originally added to ensure that we are
handling RangeErrors that ActiveRecord used to emit. This doesn't
happen anymore, so the tests aren't necessary anymore either.
* Fix tests for acceptance matcher
* Fix tests for absence matcher
2015-09-26 05:10:00 +00:00
|
|
|
|
MESSAGE
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'when qualifiers match the validation options but the values are different' do
|
2015-04-09 14:27:26 +00:00
|
|
|
|
specify 'such as testing greater_than (+ only_integer) with lower value' do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
only_integer: true,
|
|
|
|
|
greater_than: 19
|
|
|
|
|
)
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
only_integer.
|
|
|
|
|
is_greater_than(18)
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
# why is value "19" here?
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like an integer
|
|
|
|
|
greater than 18.
|
|
|
|
|
In checking that Example disallows :attr from being a number that is
|
2015-12-22 07:00:51 +00:00
|
|
|
|
not greater than 18, after setting :attr to ‹"18"›, the matcher
|
|
|
|
|
expected the Example to be invalid and to produce the validation error
|
|
|
|
|
"must be greater than 18" on :attr. The record was indeed invalid, but
|
|
|
|
|
it produced these validation errors instead:
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
* attr: ["must be greater than 19"]
|
Tighten CouldNotSetAttributeError restriction
Why:
* Previously, `allow_value` would raise a CouldNotSetAttributeError
if the value being set didn't match the value the attribute had after
being set, but only if the attribute was being changed from nil to
non-nil or non-nil to nil.
* It turns out it doesn't matter which value you're trying to set the
attribute to -- if the attribute rejects that change it's confusing
either way. (In fact, I was recently bit by a case in which I was
trying to validate numericality of an attribute, where the writer
method for that attribute was overridden to ensure that the attribute
always stored a number and never contained non-number characters.
This ended up making the numericality validation useless, of
course -- but it caused confusion because the test acted in a way
I didn't expect.)
To satisfy the above:
* `allow_value` now raises a CouldNotSetAttributeError if the attribute
rejects the value being set in *any* way.
* However, add a `ignoring_interference_by_writer` qualifier so that
it is possible to manually override this behavior.
* Fix tests that are failing now because of this new change:
* Fix tests for allow_value matcher
* Fix tests for numericality matcher
* Remove tests for numericality matcher + integer column
* An integer column will typecast any non-integer value to an
integer.
* Because of the typecasting, our tests for the numericality matcher
against an integer column don't quite work, because we can't
really test what happens when the attribute is set to a
non-integer value. Now that `allow_value` is more strict, we're
getting a CouldNotSetAttributeError when attempting to do so.
* The tests mentioned were originally added to ensure that we are
handling RangeErrors that ActiveRecord used to emit. This doesn't
happen anymore, so the tests aren't necessary anymore either.
* Fix tests for acceptance matcher
* Fix tests for absence matcher
2015-09-26 05:10:00 +00:00
|
|
|
|
MESSAGE
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
specify 'such as testing greater_than (+ only_integer) with higher value' do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
only_integer: true,
|
|
|
|
|
greater_than: 17
|
|
|
|
|
)
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
only_integer.
|
|
|
|
|
is_greater_than(18)
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like an integer
|
|
|
|
|
greater than 18.
|
|
|
|
|
In checking that Example disallows :attr from being a number that is
|
2015-12-22 07:00:51 +00:00
|
|
|
|
not greater than 18, after setting :attr to ‹"18"›, the matcher
|
|
|
|
|
expected the Example to be invalid, but it was valid instead.
|
Tighten CouldNotSetAttributeError restriction
Why:
* Previously, `allow_value` would raise a CouldNotSetAttributeError
if the value being set didn't match the value the attribute had after
being set, but only if the attribute was being changed from nil to
non-nil or non-nil to nil.
* It turns out it doesn't matter which value you're trying to set the
attribute to -- if the attribute rejects that change it's confusing
either way. (In fact, I was recently bit by a case in which I was
trying to validate numericality of an attribute, where the writer
method for that attribute was overridden to ensure that the attribute
always stored a number and never contained non-number characters.
This ended up making the numericality validation useless, of
course -- but it caused confusion because the test acted in a way
I didn't expect.)
To satisfy the above:
* `allow_value` now raises a CouldNotSetAttributeError if the attribute
rejects the value being set in *any* way.
* However, add a `ignoring_interference_by_writer` qualifier so that
it is possible to manually override this behavior.
* Fix tests that are failing now because of this new change:
* Fix tests for allow_value matcher
* Fix tests for numericality matcher
* Remove tests for numericality matcher + integer column
* An integer column will typecast any non-integer value to an
integer.
* Because of the typecasting, our tests for the numericality matcher
against an integer column don't quite work, because we can't
really test what happens when the attribute is set to a
non-integer value. Now that `allow_value` is more strict, we're
getting a CouldNotSetAttributeError when attempting to do so.
* The tests mentioned were originally added to ensure that we are
handling RangeErrors that ActiveRecord used to emit. This doesn't
happen anymore, so the tests aren't necessary anymore either.
* Fix tests for acceptance matcher
* Fix tests for absence matcher
2015-09-26 05:10:00 +00:00
|
|
|
|
MESSAGE
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
specify 'such as testing greater_than (+ even) with lower value' do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
even: true,
|
|
|
|
|
greater_than: 20
|
|
|
|
|
)
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
even.
|
|
|
|
|
is_greater_than(18)
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
# why is value "20" here?
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like an even number
|
|
|
|
|
greater than 18.
|
|
|
|
|
In checking that Example disallows :attr from being a number that is
|
2015-12-22 07:00:51 +00:00
|
|
|
|
not greater than 18, after setting :attr to ‹"18"›, the matcher
|
|
|
|
|
expected the Example to be invalid and to produce the validation error
|
|
|
|
|
"must be greater than 18" on :attr. The record was indeed invalid, but
|
|
|
|
|
it produced these validation errors instead:
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
* attr: ["must be greater than 20"]
|
Tighten CouldNotSetAttributeError restriction
Why:
* Previously, `allow_value` would raise a CouldNotSetAttributeError
if the value being set didn't match the value the attribute had after
being set, but only if the attribute was being changed from nil to
non-nil or non-nil to nil.
* It turns out it doesn't matter which value you're trying to set the
attribute to -- if the attribute rejects that change it's confusing
either way. (In fact, I was recently bit by a case in which I was
trying to validate numericality of an attribute, where the writer
method for that attribute was overridden to ensure that the attribute
always stored a number and never contained non-number characters.
This ended up making the numericality validation useless, of
course -- but it caused confusion because the test acted in a way
I didn't expect.)
To satisfy the above:
* `allow_value` now raises a CouldNotSetAttributeError if the attribute
rejects the value being set in *any* way.
* However, add a `ignoring_interference_by_writer` qualifier so that
it is possible to manually override this behavior.
* Fix tests that are failing now because of this new change:
* Fix tests for allow_value matcher
* Fix tests for numericality matcher
* Remove tests for numericality matcher + integer column
* An integer column will typecast any non-integer value to an
integer.
* Because of the typecasting, our tests for the numericality matcher
against an integer column don't quite work, because we can't
really test what happens when the attribute is set to a
non-integer value. Now that `allow_value` is more strict, we're
getting a CouldNotSetAttributeError when attempting to do so.
* The tests mentioned were originally added to ensure that we are
handling RangeErrors that ActiveRecord used to emit. This doesn't
happen anymore, so the tests aren't necessary anymore either.
* Fix tests for acceptance matcher
* Fix tests for absence matcher
2015-09-26 05:10:00 +00:00
|
|
|
|
MESSAGE
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
specify 'such as testing greater than (+ even) with higher value' do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
even: true,
|
|
|
|
|
greater_than: 16
|
|
|
|
|
)
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
even.
|
|
|
|
|
is_greater_than(18)
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like an even number
|
|
|
|
|
greater than 18.
|
|
|
|
|
In checking that Example disallows :attr from being a number that is
|
2015-12-22 07:00:51 +00:00
|
|
|
|
not greater than 18, after setting :attr to ‹"18"›, the matcher
|
|
|
|
|
expected the Example to be invalid, but it was valid instead.
|
Tighten CouldNotSetAttributeError restriction
Why:
* Previously, `allow_value` would raise a CouldNotSetAttributeError
if the value being set didn't match the value the attribute had after
being set, but only if the attribute was being changed from nil to
non-nil or non-nil to nil.
* It turns out it doesn't matter which value you're trying to set the
attribute to -- if the attribute rejects that change it's confusing
either way. (In fact, I was recently bit by a case in which I was
trying to validate numericality of an attribute, where the writer
method for that attribute was overridden to ensure that the attribute
always stored a number and never contained non-number characters.
This ended up making the numericality validation useless, of
course -- but it caused confusion because the test acted in a way
I didn't expect.)
To satisfy the above:
* `allow_value` now raises a CouldNotSetAttributeError if the attribute
rejects the value being set in *any* way.
* However, add a `ignoring_interference_by_writer` qualifier so that
it is possible to manually override this behavior.
* Fix tests that are failing now because of this new change:
* Fix tests for allow_value matcher
* Fix tests for numericality matcher
* Remove tests for numericality matcher + integer column
* An integer column will typecast any non-integer value to an
integer.
* Because of the typecasting, our tests for the numericality matcher
against an integer column don't quite work, because we can't
really test what happens when the attribute is set to a
non-integer value. Now that `allow_value` is more strict, we're
getting a CouldNotSetAttributeError when attempting to do so.
* The tests mentioned were originally added to ensure that we are
handling RangeErrors that ActiveRecord used to emit. This doesn't
happen anymore, so the tests aren't necessary anymore either.
* Fix tests for acceptance matcher
* Fix tests for absence matcher
2015-09-26 05:10:00 +00:00
|
|
|
|
MESSAGE
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
specify 'such as testing less_than_or_equal_to (+ odd) with lower value' do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
odd: true,
|
|
|
|
|
less_than_or_equal_to: 101
|
|
|
|
|
)
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
odd.
|
|
|
|
|
is_less_than_or_equal_to(99)
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like an odd number
|
|
|
|
|
less than or equal to 99.
|
|
|
|
|
In checking that Example disallows :attr from being a number that is
|
2015-12-22 07:00:51 +00:00
|
|
|
|
not less than or equal to 99, after setting :attr to ‹"101"›, the
|
2015-12-13 23:45:24 +00:00
|
|
|
|
matcher expected the Example to be invalid, but it was valid instead.
|
Tighten CouldNotSetAttributeError restriction
Why:
* Previously, `allow_value` would raise a CouldNotSetAttributeError
if the value being set didn't match the value the attribute had after
being set, but only if the attribute was being changed from nil to
non-nil or non-nil to nil.
* It turns out it doesn't matter which value you're trying to set the
attribute to -- if the attribute rejects that change it's confusing
either way. (In fact, I was recently bit by a case in which I was
trying to validate numericality of an attribute, where the writer
method for that attribute was overridden to ensure that the attribute
always stored a number and never contained non-number characters.
This ended up making the numericality validation useless, of
course -- but it caused confusion because the test acted in a way
I didn't expect.)
To satisfy the above:
* `allow_value` now raises a CouldNotSetAttributeError if the attribute
rejects the value being set in *any* way.
* However, add a `ignoring_interference_by_writer` qualifier so that
it is possible to manually override this behavior.
* Fix tests that are failing now because of this new change:
* Fix tests for allow_value matcher
* Fix tests for numericality matcher
* Remove tests for numericality matcher + integer column
* An integer column will typecast any non-integer value to an
integer.
* Because of the typecasting, our tests for the numericality matcher
against an integer column don't quite work, because we can't
really test what happens when the attribute is set to a
non-integer value. Now that `allow_value` is more strict, we're
getting a CouldNotSetAttributeError when attempting to do so.
* The tests mentioned were originally added to ensure that we are
handling RangeErrors that ActiveRecord used to emit. This doesn't
happen anymore, so the tests aren't necessary anymore either.
* Fix tests for acceptance matcher
* Fix tests for absence matcher
2015-09-26 05:10:00 +00:00
|
|
|
|
MESSAGE
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
specify 'such as testing less_than_or_equal_to (+ odd) with higher value' do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
odd: true,
|
|
|
|
|
less_than_or_equal_to: 97
|
|
|
|
|
)
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
odd.
|
|
|
|
|
is_less_than_or_equal_to(99)
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like an odd number
|
|
|
|
|
less than or equal to 99.
|
|
|
|
|
In checking that Example disallows :attr from being a number that is
|
2015-12-22 07:00:51 +00:00
|
|
|
|
not less than or equal to 99, after setting :attr to ‹"101"›, the
|
2015-12-13 23:45:24 +00:00
|
|
|
|
matcher expected the Example to be invalid and to produce the
|
|
|
|
|
validation error "must be less than or equal to 99" on :attr. The
|
|
|
|
|
record was indeed invalid, but it produced these validation errors
|
|
|
|
|
instead:
|
|
|
|
|
|
|
|
|
|
* attr: ["must be less than or equal to 97"]
|
Tighten CouldNotSetAttributeError restriction
Why:
* Previously, `allow_value` would raise a CouldNotSetAttributeError
if the value being set didn't match the value the attribute had after
being set, but only if the attribute was being changed from nil to
non-nil or non-nil to nil.
* It turns out it doesn't matter which value you're trying to set the
attribute to -- if the attribute rejects that change it's confusing
either way. (In fact, I was recently bit by a case in which I was
trying to validate numericality of an attribute, where the writer
method for that attribute was overridden to ensure that the attribute
always stored a number and never contained non-number characters.
This ended up making the numericality validation useless, of
course -- but it caused confusion because the test acted in a way
I didn't expect.)
To satisfy the above:
* `allow_value` now raises a CouldNotSetAttributeError if the attribute
rejects the value being set in *any* way.
* However, add a `ignoring_interference_by_writer` qualifier so that
it is possible to manually override this behavior.
* Fix tests that are failing now because of this new change:
* Fix tests for allow_value matcher
* Fix tests for numericality matcher
* Remove tests for numericality matcher + integer column
* An integer column will typecast any non-integer value to an
integer.
* Because of the typecasting, our tests for the numericality matcher
against an integer column don't quite work, because we can't
really test what happens when the attribute is set to a
non-integer value. Now that `allow_value` is more strict, we're
getting a CouldNotSetAttributeError when attempting to do so.
* The tests mentioned were originally added to ensure that we are
handling RangeErrors that ActiveRecord used to emit. This doesn't
happen anymore, so the tests aren't necessary anymore either.
* Fix tests for acceptance matcher
* Fix tests for absence matcher
2015-09-26 05:10:00 +00:00
|
|
|
|
MESSAGE
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
specify 'such as testing greater_than (+ only_integer + less_than) with lower value' do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
only_integer: true,
|
|
|
|
|
greater_than: 19,
|
|
|
|
|
less_than: 99
|
|
|
|
|
)
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
only_integer.
|
|
|
|
|
is_greater_than(18).
|
|
|
|
|
is_less_than(99)
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
# why is value "19" here?
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like an integer
|
|
|
|
|
greater than 18 and less than 99.
|
|
|
|
|
In checking that Example disallows :attr from being a number that is
|
2015-12-22 07:00:51 +00:00
|
|
|
|
not greater than 18, after setting :attr to ‹"18"›, the matcher
|
|
|
|
|
expected the Example to be invalid and to produce the validation error
|
|
|
|
|
"must be greater than 18" on :attr. The record was indeed invalid, but
|
|
|
|
|
it produced these validation errors instead:
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
* attr: ["must be greater than 19"]
|
Tighten CouldNotSetAttributeError restriction
Why:
* Previously, `allow_value` would raise a CouldNotSetAttributeError
if the value being set didn't match the value the attribute had after
being set, but only if the attribute was being changed from nil to
non-nil or non-nil to nil.
* It turns out it doesn't matter which value you're trying to set the
attribute to -- if the attribute rejects that change it's confusing
either way. (In fact, I was recently bit by a case in which I was
trying to validate numericality of an attribute, where the writer
method for that attribute was overridden to ensure that the attribute
always stored a number and never contained non-number characters.
This ended up making the numericality validation useless, of
course -- but it caused confusion because the test acted in a way
I didn't expect.)
To satisfy the above:
* `allow_value` now raises a CouldNotSetAttributeError if the attribute
rejects the value being set in *any* way.
* However, add a `ignoring_interference_by_writer` qualifier so that
it is possible to manually override this behavior.
* Fix tests that are failing now because of this new change:
* Fix tests for allow_value matcher
* Fix tests for numericality matcher
* Remove tests for numericality matcher + integer column
* An integer column will typecast any non-integer value to an
integer.
* Because of the typecasting, our tests for the numericality matcher
against an integer column don't quite work, because we can't
really test what happens when the attribute is set to a
non-integer value. Now that `allow_value` is more strict, we're
getting a CouldNotSetAttributeError when attempting to do so.
* The tests mentioned were originally added to ensure that we are
handling RangeErrors that ActiveRecord used to emit. This doesn't
happen anymore, so the tests aren't necessary anymore either.
* Fix tests for acceptance matcher
* Fix tests for absence matcher
2015-09-26 05:10:00 +00:00
|
|
|
|
MESSAGE
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
specify 'such as testing less_than (+ only_integer + greater_than) with higher value' do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
only_integer: true,
|
|
|
|
|
greater_than: 18,
|
|
|
|
|
less_than: 100
|
|
|
|
|
)
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
assertion = lambda do
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
only_integer.
|
|
|
|
|
is_greater_than(18).
|
|
|
|
|
is_less_than(99)
|
|
|
|
|
end
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
|
|
|
|
message = <<-MESSAGE
|
|
|
|
|
Example did not properly validate that :attr looks like an integer
|
|
|
|
|
greater than 18 and less than 99.
|
|
|
|
|
In checking that Example disallows :attr from being a number that is
|
2015-12-22 07:00:51 +00:00
|
|
|
|
not less than 99, after setting :attr to ‹"100"›, the matcher expected
|
2015-12-13 23:45:24 +00:00
|
|
|
|
the Example to be invalid and to produce the validation error "must be
|
|
|
|
|
less than 99" on :attr. The record was indeed invalid, but it produced
|
|
|
|
|
these validation errors instead:
|
|
|
|
|
|
|
|
|
|
* attr: ["must be less than 100"]
|
Tighten CouldNotSetAttributeError restriction
Why:
* Previously, `allow_value` would raise a CouldNotSetAttributeError
if the value being set didn't match the value the attribute had after
being set, but only if the attribute was being changed from nil to
non-nil or non-nil to nil.
* It turns out it doesn't matter which value you're trying to set the
attribute to -- if the attribute rejects that change it's confusing
either way. (In fact, I was recently bit by a case in which I was
trying to validate numericality of an attribute, where the writer
method for that attribute was overridden to ensure that the attribute
always stored a number and never contained non-number characters.
This ended up making the numericality validation useless, of
course -- but it caused confusion because the test acted in a way
I didn't expect.)
To satisfy the above:
* `allow_value` now raises a CouldNotSetAttributeError if the attribute
rejects the value being set in *any* way.
* However, add a `ignoring_interference_by_writer` qualifier so that
it is possible to manually override this behavior.
* Fix tests that are failing now because of this new change:
* Fix tests for allow_value matcher
* Fix tests for numericality matcher
* Remove tests for numericality matcher + integer column
* An integer column will typecast any non-integer value to an
integer.
* Because of the typecasting, our tests for the numericality matcher
against an integer column don't quite work, because we can't
really test what happens when the attribute is set to a
non-integer value. Now that `allow_value` is more strict, we're
getting a CouldNotSetAttributeError when attempting to do so.
* The tests mentioned were originally added to ensure that we are
handling RangeErrors that ActiveRecord used to emit. This doesn't
happen anymore, so the tests aren't necessary anymore either.
* Fix tests for acceptance matcher
* Fix tests for absence matcher
2015-09-26 05:10:00 +00:00
|
|
|
|
MESSAGE
|
2015-12-13 23:45:24 +00:00
|
|
|
|
|
2015-04-09 14:27:26 +00:00
|
|
|
|
expect(&assertion).to fail_with_message(message)
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
2013-03-19 04:50:39 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-04-15 21:16:01 +00:00
|
|
|
|
context 'with large numbers' do
|
|
|
|
|
it do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
record = build_record_validating_numericality(greater_than: 100_000)
|
|
|
|
|
expect(record).to validate_numericality.is_greater_than(100_000)
|
2014-04-15 21:16:01 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
record = build_record_validating_numericality(less_than: 100_000)
|
|
|
|
|
expect(record).to validate_numericality.is_less_than(100_000)
|
2014-04-15 21:16:01 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
greater_than_or_equal_to: 100_000
|
|
|
|
|
)
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
is_greater_than_or_equal_to(100_000)
|
2014-04-15 21:16:01 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
record = build_record_validating_numericality(
|
|
|
|
|
less_than_or_equal_to: 100_000
|
|
|
|
|
)
|
|
|
|
|
expect(record).
|
|
|
|
|
to validate_numericality.
|
|
|
|
|
is_less_than_or_equal_to(100_000)
|
2012-10-16 17:45:06 +00:00
|
|
|
|
end
|
2012-12-20 05:04:27 +00:00
|
|
|
|
end
|
2012-10-16 17:45:06 +00:00
|
|
|
|
|
2013-07-10 17:05:41 +00:00
|
|
|
|
context 'when the subject is stubbed' do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
it 'retains that stub while the validate_numericality is matching' do
|
|
|
|
|
model = define_model :example, attr: :string do
|
2014-01-17 20:20:44 +00:00
|
|
|
|
validates_numericality_of :attr, odd: true
|
2013-07-10 17:05:41 +00:00
|
|
|
|
before_validation :set_attr!
|
|
|
|
|
def set_attr!; self.attr = 5 end
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
record = model.new
|
|
|
|
|
allow(record).to receive(:set_attr!)
|
2013-07-10 17:05:41 +00:00
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
expect(record).to validate_numericality_of(:attr).odd
|
2013-07-10 17:05:41 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-10-07 05:12:30 +00:00
|
|
|
|
context 'against an ActiveModel model' do
|
|
|
|
|
it 'accepts' do
|
|
|
|
|
model = define_active_model_class :example, accessors: [:attr] do
|
|
|
|
|
validates_numericality_of :attr
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
expect(model.new).to validate_numericality_of(:attr)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-01-13 01:43:36 +00:00
|
|
|
|
describe '#description' do
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'qualified with nothing' do
|
|
|
|
|
it 'describes that it allows numbers' do
|
|
|
|
|
matcher = validate_numericality_of(:attr)
|
2015-12-13 23:45:24 +00:00
|
|
|
|
expect(matcher.description).to eq(
|
|
|
|
|
'validate that :attr looks like a number'
|
|
|
|
|
)
|
2015-03-29 00:12:24 +00:00
|
|
|
|
end
|
2014-01-13 01:43:36 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'qualified with only_integer' do
|
|
|
|
|
it 'describes that it allows integers' do
|
|
|
|
|
matcher = validate_numericality_of(:attr).only_integer
|
2015-12-13 23:45:24 +00:00
|
|
|
|
expect(matcher.description).to eq(
|
|
|
|
|
'validate that :attr looks like an integer'
|
|
|
|
|
)
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
2014-01-13 01:43:36 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
qualifiers_under(:cardinality).each do |qualifier|
|
|
|
|
|
context "qualified with #{qualifier[:name]}" do
|
|
|
|
|
it "describes that it allows #{qualifier[:name]} numbers" do
|
|
|
|
|
matcher = validate_numericality_of(:attr).__send__(qualifier[:name])
|
2015-12-13 23:45:24 +00:00
|
|
|
|
expect(matcher.description).to eq(
|
|
|
|
|
"validate that :attr looks like an #{qualifier[:name]} number"
|
|
|
|
|
)
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
2014-01-13 01:43:36 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
qualifiers_under(:comparison).each do |qualifier|
|
|
|
|
|
comparison_phrase = qualifier[:validation_name].to_s.gsub('_', ' ')
|
|
|
|
|
|
|
|
|
|
context "qualified with #{qualifier[:name]}" do
|
|
|
|
|
it "describes that it allows numbers #{comparison_phrase} a certain value" do
|
|
|
|
|
matcher = validate_numericality_of(:attr).
|
|
|
|
|
__send__(qualifier[:name], 18)
|
|
|
|
|
|
|
|
|
|
expect(matcher.description).to eq(
|
2015-12-13 23:45:24 +00:00
|
|
|
|
"validate that :attr looks like a number #{comparison_phrase} 18"
|
2014-02-27 03:26:37 +00:00
|
|
|
|
)
|
|
|
|
|
end
|
2014-01-13 01:43:36 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'qualified with odd + is_greater_than_or_equal_to' do
|
|
|
|
|
it "describes that it allows odd numbers greater than or equal to a certain value" do
|
|
|
|
|
matcher = validate_numericality_of(:attr).
|
|
|
|
|
odd.
|
|
|
|
|
is_greater_than_or_equal_to(18)
|
|
|
|
|
|
|
|
|
|
expect(matcher.description).to eq(
|
2015-12-13 23:45:24 +00:00
|
|
|
|
'validate that :attr looks like an odd number greater than or equal to 18'
|
2015-03-29 00:12:24 +00:00
|
|
|
|
)
|
2014-02-27 03:26:37 +00:00
|
|
|
|
end
|
2014-01-13 01:43:36 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
context 'qualified with only integer + is_greater_than + less_than_or_equal_to' do
|
|
|
|
|
it 'describes that it allows integer greater than one value and less than or equal to another' do
|
|
|
|
|
matcher = validate_numericality_of(:attr).
|
|
|
|
|
only_integer.
|
|
|
|
|
is_greater_than(18).
|
|
|
|
|
is_less_than_or_equal_to(100)
|
|
|
|
|
|
|
|
|
|
expect(matcher.description).to eq(
|
2015-12-13 23:45:24 +00:00
|
|
|
|
'validate that :attr looks like an integer greater than 18 and less than or equal to 100'
|
2015-03-29 00:12:24 +00:00
|
|
|
|
)
|
|
|
|
|
end
|
2014-01-13 01:43:36 +00:00
|
|
|
|
end
|
2014-12-03 22:06:23 +00:00
|
|
|
|
|
|
|
|
|
context 'qualified with strict' do
|
|
|
|
|
it 'describes that it relies upon a strict validation' do
|
|
|
|
|
matcher = validate_numericality_of(:attr).strict
|
|
|
|
|
expect(matcher.description).to eq(
|
2015-12-13 23:45:24 +00:00
|
|
|
|
'validate that :attr looks like a number, raising a validation exception on failure'
|
2014-12-03 22:06:23 +00:00
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'and qualified with a comparison qualifier' do
|
|
|
|
|
it 'places the comparison description after "strictly"' do
|
|
|
|
|
matcher = validate_numericality_of(:attr).is_less_than(18).strict
|
|
|
|
|
expect(matcher.description).to eq(
|
2015-12-13 23:45:24 +00:00
|
|
|
|
'validate that :attr looks like a number less than 18, raising a validation exception on failure'
|
2014-12-03 22:06:23 +00:00
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2014-01-13 01:43:36 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
def build_validation_options(args)
|
|
|
|
|
combination = args.fetch(:for)
|
2014-01-13 01:43:36 +00:00
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
combination.each_with_object({}) do |qualifier, hash|
|
|
|
|
|
value = self.class.default_validation_values.fetch(qualifier[:validation_name])
|
|
|
|
|
hash[qualifier[:validation_name]] = value
|
|
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
def apply_qualifiers!(args)
|
|
|
|
|
combination = args.fetch(:for)
|
|
|
|
|
matcher = args.fetch(:to)
|
|
|
|
|
|
|
|
|
|
combination.each do |qualifier|
|
|
|
|
|
args = self.class.default_qualifier_arguments.fetch(qualifier[:name])
|
|
|
|
|
matcher.__send__(qualifier[:name], *args)
|
|
|
|
|
end
|
2014-01-13 01:43:36 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
def define_model_validating_numericality(options = {})
|
2015-04-01 06:00:14 +00:00
|
|
|
|
attribute_name = options.delete(:attribute_name) { self.attribute_name }
|
2015-10-07 05:12:30 +00:00
|
|
|
|
column_type = options.delete(:column_type) { :string }
|
2015-04-01 06:00:14 +00:00
|
|
|
|
|
2015-10-07 05:12:30 +00:00
|
|
|
|
define_model 'Example', attribute_name => { type: column_type } do |model|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
model.validates_numericality_of(attribute_name, options)
|
|
|
|
|
end
|
2012-10-16 17:45:06 +00:00
|
|
|
|
end
|
2015-01-21 22:46:26 +00:00
|
|
|
|
|
2015-03-29 00:12:24 +00:00
|
|
|
|
def build_record_validating_numericality(options = {})
|
|
|
|
|
define_model_validating_numericality(options).new
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def define_model_validating_nothing
|
|
|
|
|
define_model('Example', attribute_name => :string)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def build_record_validating_nothing
|
|
|
|
|
define_model_validating_nothing.new
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def validate_numericality
|
|
|
|
|
validate_numericality_of(attribute_name)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def attribute_name
|
|
|
|
|
:attr
|
|
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
|
end
|