validation_spec.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. end
  77. end