Module: OMU::Support::Minitest::ValidationHelper
- Defined in:
- lib/omu_support/minitest/validation_helper.rb
Constant Summary collapse
- SUFFIX =
'Test'
Class Method Summary collapse
Instance Method Summary collapse
-
#validates_length_of(attribute, **option) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength.
-
#validates_numerical_range(attribute, **option) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength.
- #validates_numericality_of(attribute) ⇒ Object
- #validates_presence_of(*attributes) ⇒ Object
- #validates_presence_of_nested_model(attribute, ids: nil) ⇒ Object
- #validates_uniqueness_of(*attributes) ⇒ Object
Class Method Details
Instance Method Details
#validates_length_of(attribute, **option) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/omu_support/minitest/validation_helper.rb', line 38 def validates_length_of(attribute, **option) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength option[:maximum] = 255 if option.blank? controls = { is: { value: 1, error_key: :wrong_length }, minimum: { value: -1, error_key: :too_short }, maximum: { value: 1, error_key: :too_long } } key = option.keys.first value = option[key].to_i + controls.dig(key, :value).to_i error_key = controls.dig(key, :error_key) test "#{attribute} length must be #{option}" do object.public_send("#{attribute}=", (0..value).map { ('a'..'z').to_a[rand(26)] }.join) assert_not object.valid? assert object.errors.details[attribute].pluck(:error).include?(error_key) end end |
#validates_numerical_range(attribute, **option) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/omu_support/minitest/validation_helper.rb', line 66 def validates_numerical_range(attribute, **option) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength controls = { greater_than: 0, less_than: 0, greater_than_or_equal_to: -1, less_than_or_equal_to: 1 } key = option.keys.first value = option[key].to_i + controls[key].to_i test "#{attribute} must be #{key} #{value}" do object.public_send("#{attribute}=", value) assert_not object.valid? assert object.errors.details[attribute].pluck(:error).include?(key) end end |
#validates_numericality_of(attribute) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/omu_support/minitest/validation_helper.rb', line 58 def validates_numericality_of(attribute) test "#{attribute} must be a number" do object.public_send("#{attribute}=", 'some string') assert_not object.valid? assert object.errors.details[attribute].pluck(:error).include?(:not_a_number) end end |
#validates_presence_of(*attributes) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/omu_support/minitest/validation_helper.rb', line 9 def validates_presence_of(*attributes) attributes.each do |attribute| test "#{attribute} must be present (presence: true)" do object.public_send("#{attribute}=", nil) assert_not object.valid? assert_not_empty object.errors[attribute] end end end |
#validates_presence_of_nested_model(attribute, ids: nil) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/omu_support/minitest/validation_helper.rb', line 19 def validates_presence_of_nested_model(attribute, ids: nil) test "nested model (#{attribute}) must be present" do ids ||= "#{attribute.to_s.singularize}_ids" object.public_send("#{ids}=", nil) assert_not object.valid? assert_not_empty object.errors[attribute] end end |
#validates_uniqueness_of(*attributes) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/omu_support/minitest/validation_helper.rb', line 28 def validates_uniqueness_of(*attributes) attributes.each do |attribute| test "#{attribute} must be unique (uniqueness: true)" do duplicate_object = object.dup assert_not duplicate_object.valid? assert_not_empty duplicate_object.errors[attribute] end end end |