2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
require './lib/param_validation.rb'
|
|
|
|
require 'minitest/autorun'
|
|
|
|
|
|
|
|
class ParamValidationTest < Minitest::Test
|
2019-07-30 21:29:24 +00:00
|
|
|
def setup; end
|
2019-01-10 00:22:42 +00:00
|
|
|
|
|
|
|
def test_required
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({}, x: { required: true })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
|
|
|
assert_equal :x, e.data[:key]
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
# If a key is not required, then don't run the tests on it
|
|
|
|
def test_not_required_and_absent_then_tests_do_not_run
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.new({}, x: { max: 100 })
|
2019-01-10 00:22:42 +00:00
|
|
|
assert true
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_not_blank_fail
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: '' }, x: { not_blank: true })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
|
|
|
assert_equal :x, e.data[:key]
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_not_blank_fail_nil
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: nil }, x: { not_blank: true, required: true })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
2019-07-30 21:29:24 +00:00
|
|
|
assert(e.data.one? { |i| i[:name] == :not_blank && i[:key] == :x })
|
|
|
|
assert(e.data.one? { |i| i[:name] == :required && i[:key] == :x })
|
2019-01-10 00:22:42 +00:00
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_not_blank_succeed
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.new({ x: 'x' }, x: { not_blank: true })
|
2019-01-10 00:22:42 +00:00
|
|
|
assert true
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_require_no_err
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: 1 }, x: { required: true })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; end
|
|
|
|
assert e.nil?
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_absent
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: 1 }, x: { absent: true })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
|
|
|
assert_equal :x, e.data[:key]
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_not_included_in
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: 1 }, x: { not_included_in: [1] })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
|
|
|
assert_equal :x, e.data[:key]
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_included_in
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: 1 }, x: { included_in: [2] })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
|
|
|
assert_equal :x, e.data[:key]
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_format
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: 'x' }, x: { format: /y/ })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
|
|
|
assert_equal :x, e.data[:key]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_is_reference_string
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: '-0' }, x: { is_reference: true })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
|
|
|
assert_equal :x, e.data[:key]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_is_reference_negative_integer
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: -1 }, x: { is_reference: true })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
|
|
|
assert_equal :x, e.data[:key]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_is_reference_passes
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.new({ x: '0' }, x: { is_reference: true })
|
|
|
|
ParamValidation.new({ x: 1 }, x: { is_reference: true })
|
|
|
|
ParamValidation.new({ x: '' }, x: { is_reference: true })
|
|
|
|
pass
|
2019-01-10 00:22:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_is_integer
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: 'x' }, x: { is_integer: true })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
|
|
|
assert_equal :x, e.data[:key]
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_is_float
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: 'x' }, x: { is_float: true })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
|
|
|
assert_equal :x, e.data[:key]
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_min_length
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: [] }, x: { min_length: 2 })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
|
|
|
assert_equal :x, e.data[:key]
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_max_length
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: [1, 2, 3] }, x: { max_length: 2 })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
|
|
|
assert_equal e.data[:key], :x
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_length_range
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: [1, 2, 3, 4] }, x: { length_range: 1..3 })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
|
|
|
assert_equal e.data[:key], :x
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_length_equals
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: [1, 2] }, x: { length_equals: 1 })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
|
|
|
assert_equal e.data[:key], :x
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_min
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: 1 }, x: { min: 2 })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
|
|
|
assert_equal e.data[:key], :x
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_max
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: 4 }, x: { max: 2 })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
|
|
|
assert_equal e.data[:name], :max
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_in_range
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: 1 }, x: { in_range: 2..4 })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
|
|
|
assert_equal e.data[:val], 1
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_equals
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: 1 }, x: { equals: 2 })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
2019-07-30 21:29:24 +00:00
|
|
|
assert_equal 'x should equal 2', e.to_s
|
2019-01-10 00:22:42 +00:00
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_root_array_of_hashes
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: 1 }, root: { array_of_hashes: { x: { required: true } } })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
2019-07-30 21:29:24 +00:00
|
|
|
assert_equal 'Please pass in an array of hashes', e.to_s
|
2019-01-10 00:22:42 +00:00
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_root_array_of_hashes_with_nesting_ok
|
2019-07-30 21:29:24 +00:00
|
|
|
v = ParamValidation.new([{ 'x' => 1 }, { x: 1 }], root: { array_of_hashes: { x: { is_integer: true } } })
|
2019-01-10 00:22:42 +00:00
|
|
|
assert_equal v, v # test that it does not raise
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_root_array_of_hashes_with_nesting
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new([{ x: 1 }, { x: 'hi' }], root: { array_of_hashes: { x: { is_integer: true } } })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
2019-07-30 21:29:24 +00:00
|
|
|
assert_equal 'x should be an integer', e.to_s
|
2019-01-10 00:22:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_is_json_with_string
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: '[[[[[[[' }, x: { is_json: true })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
2019-07-30 21:29:24 +00:00
|
|
|
assert_equal 'x should be valid JSON', e.to_s
|
2019-01-10 00:22:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_is_json_without_string
|
2019-07-30 21:29:24 +00:00
|
|
|
begin; ParamValidation.new({ x: {} }, x: { is_json: true })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e; e; end
|
2019-07-30 21:29:24 +00:00
|
|
|
assert_equal 'x should be valid JSON', e.to_s
|
2019-01-10 00:22:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_is_a_single
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.new({ x: 5.6 }, x: { is_a: Float })
|
2019-01-10 00:22:42 +00:00
|
|
|
begin
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.new({ x: 5.6 }, x: { is_a: Integer })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e
|
|
|
|
e
|
|
|
|
end
|
|
|
|
assert_equal 'x should be of the type(s): Integer', e.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_is_a_multiple
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.new({ x: 5.6 }, x: { is_a: [Integer, Float] })
|
2019-01-10 00:22:42 +00:00
|
|
|
begin
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.new({ x: 5.6 }, x: { is_a: [Integer, Array] })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e
|
|
|
|
e
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 'x should be of the type(s): Integer, Array', e.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_can_be_date
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.new({ x: Date.new }, x: { can_be_date: true })
|
|
|
|
ParamValidation.new({ x: DateTime.new }, x: { can_be_date: true })
|
|
|
|
ParamValidation.new({ x: '2017-05-15T12:00:00.000Z' }, x: { can_be_date: true })
|
|
|
|
ParamValidation.new({ x: '2017-05-15' }, x: { can_be_date: true })
|
2019-01-10 00:22:42 +00:00
|
|
|
|
|
|
|
begin
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.new({ x: 'not_a _date' }, x: { can_be_date: true })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e
|
|
|
|
e
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 'x should be a datetime or be parsable as one', e.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_validator
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.add_validator(:dollars) { |val, _arg, _data| val =~ /^\d+(\.\d\d)?$/ }
|
2019-01-10 00:22:42 +00:00
|
|
|
begin
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.new({ x: 'hi' }, x: { dollars: true })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e
|
|
|
|
e
|
|
|
|
end
|
|
|
|
assert_equal :dollars, e.data[:name]
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
|
2019-01-10 00:22:42 +00:00
|
|
|
def test_set_message
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.add_validator(:dollars) { |val, _arg, _data| val =~ /^\d+(\.\d\d)?$/ }
|
|
|
|
ParamValidation.set_message(:dollars) { |h| "#{h[:key]} must be a dollar amount" }
|
2019-01-10 00:22:42 +00:00
|
|
|
begin
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.new({ x: 'hi' }, x: { dollars: true })
|
2019-01-10 00:22:42 +00:00
|
|
|
rescue ParamValidation::ValidationError => e
|
|
|
|
e
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
assert_equal 'x must be a dollar amount', e.to_s
|
2019-01-10 00:22:42 +00:00
|
|
|
end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def test_custom_validator; end
|
2019-01-10 00:22:42 +00:00
|
|
|
end
|