min_max_spec.rb 1.8 KB

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