checks_import_examples.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. RSpec.shared_examples 'ApplicationModel::ChecksImport' do
  2. describe '#id (for referential integrity during OTRS/Zendesk import)' do
  3. subject { build(described_class.name.underscore, id: next_id + 1) }
  4. let(:next_id) do
  5. case ActiveRecord::Base.connection_config[:adapter]
  6. when 'mysql2'
  7. ActiveRecord::Base.connection.execute(<<~QUERY).first.first
  8. SELECT max(auto_increment) FROM information_schema.tables WHERE table_name='#{described_class.table_name}'
  9. QUERY
  10. when 'postgresql'
  11. ActiveRecord::Base.connection.execute(<<~QUERY).first['last_value'].next
  12. SELECT last_value FROM #{described_class.table_name}_id_seq
  13. QUERY
  14. end
  15. end
  16. context 'when Setting.get("system_init_done") is false (regardless of import_mode)' do
  17. before { Setting.set('system_init_done', false) }
  18. it 'allows explicit setting of #id attribute' do
  19. expect { subject.save }.not_to change(subject, :id)
  20. end
  21. end
  22. context 'when Setting.get("system_init_done") is true' do
  23. before { Setting.set('system_init_done', true) }
  24. context 'and Setting.get("import_mode") is false' do
  25. before { Setting.set('import_mode', false) }
  26. it 'prevents explicit setting of #id attribute' do
  27. expect { subject.save }.to change(subject, :id)
  28. end
  29. end
  30. context 'and Setting.get("import_mode") is true' do
  31. before { Setting.set('import_mode', true) }
  32. shared_examples 'importable classes' do
  33. it 'allows explicit setting of #id attribute' do
  34. expect { subject.save }.not_to change(subject, :id)
  35. end
  36. end
  37. shared_examples 'non-importable classes' do
  38. it 'prevents explicit setting of #id attribute' do
  39. expect { subject.save }.to change(subject, :id)
  40. end
  41. end
  42. include_examples described_class.importable? ? 'importable classes' : 'non-importable classes'
  43. end
  44. end
  45. end
  46. end