validation_spec.rb 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. require 'rails_helper'
  2. require 'lib/mixin/has_backends_examples'
  3. RSpec.describe ObjectManager::Attribute::Validation, application_handle: 'application_server' do
  4. it_behaves_like 'Mixin::HasBackends'
  5. it 'is a ActiveModel::Validator' do
  6. expect(described_class).to be < ActiveModel::Validator
  7. end
  8. describe '#validate' do
  9. subject { described_class.new }
  10. let(:record) { build(:user) }
  11. let(:backend) { spy }
  12. around do |example|
  13. original_backends = described_class.backends.dup
  14. begin
  15. example.run
  16. ensure
  17. described_class.backends = original_backends
  18. end
  19. end
  20. before do
  21. described_class.backends = Set[backend]
  22. end
  23. it 'sends .validate to backends' do
  24. subject.validate(record)
  25. expect(backend).to have_received(:validate).with(record: record, attribute: instance_of(::ObjectManager::Attribute)).at_least(:once)
  26. end
  27. context 'for cached ObjectManager::Attribute records' do
  28. it 'fetches current records when in memory Cache is blank' do
  29. allow(::ObjectManager::Attribute).to receive(:where).and_call_original
  30. subject.validate(record)
  31. expect(::ObjectManager::Attribute).to have_received(:where).twice
  32. end
  33. it "doesn't fetch current records when in memory Cache is valid" do
  34. subject.validate(record)
  35. allow(::ObjectManager::Attribute).to receive(:where).and_call_original
  36. subject.validate(record)
  37. expect(::ObjectManager::Attribute).to have_received(:where).once
  38. end
  39. it 'fetches current records when in memory Cache is outdated' do
  40. subject.validate(record)
  41. ::ObjectManager::Attribute.last.touch
  42. allow(::ObjectManager::Attribute).to receive(:where).and_call_original
  43. subject.validate(record)
  44. expect(::ObjectManager::Attribute).to have_received(:where).twice
  45. end
  46. end
  47. context 'when no validation is performed' do
  48. it 'is skipped because of irrelevant ApplicationHandleInfo', application_handle: 'non_application_server' do
  49. subject.validate(record)
  50. expect(backend).not_to have_received(:validate)
  51. end
  52. it 'is skipped because of import_mode is active' do
  53. allow(Setting).to receive(:get).with('import_mode').and_return(true)
  54. subject.validate(record)
  55. expect(backend).not_to have_received(:validate)
  56. end
  57. it 'is skipped because of unchanged attributes' do
  58. record.save!
  59. RSpec::Mocks.space.proxy_for(backend).reset
  60. subject.validate(record)
  61. expect(backend).not_to have_received(:validate)
  62. end
  63. context 'when caused by ObjectManager::Attribute records' do
  64. it 'is skipped because no custom attributes are present' do
  65. ::ObjectManager::Attribute.update(editable: false)
  66. subject.validate(record)
  67. expect(backend).not_to have_received(:validate)
  68. end
  69. it 'is skipped because no active attributes are present' do
  70. ::ObjectManager::Attribute.update(active: false)
  71. subject.validate(record)
  72. expect(backend).not_to have_received(:validate)
  73. end
  74. end
  75. end
  76. context 'when custom attribute exists' do
  77. before do
  78. allow(subject).to receive(:attributes_unchanged?) # rubocop:disable RSpec/SubjectStub
  79. end
  80. it 'runs validation in default context' do
  81. ApplicationHandleInfo.in_context(nil) do
  82. subject.validate(record)
  83. end
  84. expect(subject).to have_received(:attributes_unchanged?) # rubocop:disable RSpec/SubjectStub
  85. end
  86. it 'does not run validations in contexts that do not use custom attributes' do
  87. ApplicationHandleInfo.in_context('merge') do
  88. subject.validate(record)
  89. end
  90. expect(subject).not_to have_received(:attributes_unchanged?) # rubocop:disable RSpec/SubjectStub
  91. end
  92. end
  93. end
  94. describe '#validation_needed' do
  95. it 'runs validation in default context' do
  96. ApplicationHandleInfo.in_context(nil) do
  97. expect(subject.send(:validation_needed?)).to be true
  98. end
  99. end
  100. it 'does not run validations in contexts that do not use custom attributes' do
  101. ApplicationHandleInfo.in_context('merge') do
  102. expect(subject.send(:validation_needed?)).to be false
  103. end
  104. end
  105. end
  106. end