houdini/gems/ruby-param-validation/test/param_validation_test.rb

236 lines
7.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2019-01-10 00:22:42 +00:00
require './lib/param_validation.rb'
require 'minitest/autorun'
class ParamValidationTest < Minitest::Test
def setup; end
2019-01-10 00:22:42 +00:00
def test_required
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-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
ParamValidation.new({}, x: { max: 100 })
2019-01-10 00:22:42 +00:00
assert true
end
2019-01-10 00:22:42 +00:00
def test_not_blank_fail
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-01-10 00:22:42 +00:00
def test_not_blank_fail_nil
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
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-01-10 00:22:42 +00:00
def test_not_blank_succeed
ParamValidation.new({ x: 'x' }, x: { not_blank: true })
2019-01-10 00:22:42 +00:00
assert true
end
2019-01-10 00:22:42 +00:00
def test_require_no_err
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-01-10 00:22:42 +00:00
def test_absent
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-01-10 00:22:42 +00:00
def test_not_included_in
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-01-10 00:22:42 +00:00
def test_included_in
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-01-10 00:22:42 +00:00
def test_format
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
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
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
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
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-01-10 00:22:42 +00:00
def test_is_float
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-01-10 00:22:42 +00:00
def test_min_length
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-01-10 00:22:42 +00:00
def test_max_length
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-01-10 00:22:42 +00:00
def test_length_range
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-01-10 00:22:42 +00:00
def test_length_equals
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-01-10 00:22:42 +00:00
def test_min
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-01-10 00:22:42 +00:00
def test_max
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-01-10 00:22:42 +00:00
def test_in_range
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-01-10 00:22:42 +00:00
def test_equals
begin; ParamValidation.new({ x: 1 }, x: { equals: 2 })
2019-01-10 00:22:42 +00:00
rescue ParamValidation::ValidationError => e; e; end
assert_equal 'x should equal 2', e.to_s
2019-01-10 00:22:42 +00:00
end
2019-01-10 00:22:42 +00:00
def test_root_array_of_hashes
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
assert_equal 'Please pass in an array of hashes', e.to_s
2019-01-10 00:22:42 +00:00
end
2019-01-10 00:22:42 +00:00
def test_root_array_of_hashes_with_nesting_ok
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-01-10 00:22:42 +00:00
def test_root_array_of_hashes_with_nesting
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
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
begin; ParamValidation.new({ x: '[[[[[[[' }, x: { is_json: true })
2019-01-10 00:22:42 +00:00
rescue ParamValidation::ValidationError => e; e; end
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
begin; ParamValidation.new({ x: {} }, x: { is_json: true })
2019-01-10 00:22:42 +00:00
rescue ParamValidation::ValidationError => e; e; end
assert_equal 'x should be valid JSON', e.to_s
2019-01-10 00:22:42 +00:00
end
def test_is_a_single
ParamValidation.new({ x: 5.6 }, x: { is_a: Float })
2019-01-10 00:22:42 +00:00
begin
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
ParamValidation.new({ x: 5.6 }, x: { is_a: [Integer, Float] })
2019-01-10 00:22:42 +00:00
begin
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
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
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
ParamValidation.add_validator(:dollars) { |val, _arg, _data| val =~ /^\d+(\.\d\d)?$/ }
2019-01-10 00:22:42 +00:00
begin
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-01-10 00:22:42 +00:00
def test_set_message
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
ParamValidation.new({ x: 'hi' }, x: { dollars: true })
2019-01-10 00:22:42 +00:00
rescue ParamValidation::ValidationError => e
e
end
assert_equal 'x must be a dollar amount', e.to_s
2019-01-10 00:22:42 +00:00
end
def test_custom_validator; end
2019-01-10 00:22:42 +00:00
end