checks_import_examples.rb 2.1 KB

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