min_max_spec.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. require 'rails_helper'
  2. require 'models/object_manager/attribute/validation/backend_examples'
  3. RSpec.describe ::ObjectManager::Attribute::Validation::MinMax do
  4. subject do
  5. described_class.new(
  6. record: record,
  7. attribute: attribute
  8. )
  9. end
  10. let(:record) { build(:user) }
  11. let(:attribute) { build(:object_manager_attribute_integer) }
  12. it_behaves_like 'validate backend'
  13. context 'when validation should not be performed' do
  14. context 'for blank value' do
  15. let(:value) { nil }
  16. it_behaves_like 'a validation without errors'
  17. end
  18. context 'for irrelevant attribute data_type' do
  19. let(:value) { 'some_value' }
  20. before { attribute.data_type = 'select' }
  21. it_behaves_like 'a validation without errors'
  22. end
  23. end
  24. context 'when validation should be performed' do
  25. shared_examples 'data_option validator' do |data_option:, data_option_value:, valid:, invalid:|
  26. context "for data_option '#{data_option}'" do
  27. before { attribute.data_option[data_option] = data_option_value }
  28. context 'when value is the same as data_option' do
  29. let(:value) { data_option_value }
  30. it_behaves_like 'a validation without errors'
  31. end
  32. context 'when value is valid' do
  33. let(:value) { valid }
  34. it_behaves_like 'a validation without errors'
  35. end
  36. context 'when value is invalid' do
  37. let(:value) { invalid }
  38. it_behaves_like 'a validation with errors'
  39. end
  40. end
  41. end
  42. it_behaves_like 'data_option validator', data_option: :min, data_option_value: 1, valid: 2, invalid: 0
  43. it_behaves_like 'data_option validator', data_option: :max, data_option_value: 1, valid: 0, invalid: 2
  44. end
  45. end