min_max_spec.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. before do
  13. allow(subject).to receive(:value).and_return(value) # rubocop:disable RSpec/SubjectStub, RSpec/NamedSubject
  14. end
  15. context 'when validation should not be performed' do
  16. context 'for blank value' do
  17. let(:value) { nil }
  18. it_behaves_like 'a validation without errors'
  19. end
  20. context 'for irrelevant attribute data_type' do
  21. let(:value) { 'some_value' }
  22. before { attribute.data_type = 'select' }
  23. it_behaves_like 'a validation without errors'
  24. end
  25. end
  26. context 'when validation should be performed' do
  27. shared_examples 'data_option validator' do |data_option:, data_option_value:, valid:, invalid:|
  28. context "for data_option '#{data_option}'" do
  29. before { attribute.data_option[data_option] = data_option_value }
  30. context 'when value is the same as data_option' do
  31. let(:value) { data_option_value }
  32. it_behaves_like 'a validation without errors'
  33. end
  34. context 'when value is valid' do
  35. let(:value) { valid }
  36. it_behaves_like 'a validation without errors'
  37. end
  38. context 'when value is invalid' do
  39. let(:value) { invalid }
  40. it_behaves_like 'a validation with errors'
  41. end
  42. end
  43. end
  44. it_behaves_like 'data_option validator', data_option: :min, data_option_value: 1, valid: 2, invalid: 0
  45. it_behaves_like 'data_option validator', data_option: :max, data_option_value: 1, valid: 0, invalid: 2
  46. end
  47. end