2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2019-01-28 06:04:05 +00:00
|
|
|
RSpec.shared_examples 'ApplicationModel::ChecksImport' do
|
2021-05-25 12:30:12 +00:00
|
|
|
describe '#id (for referential integrity during (e.g. OTRS/Zendesk/Freshdesk) import)' do
|
2019-01-30 11:50:37 +00:00
|
|
|
subject { build(described_class.name.underscore, id: next_id + 1) }
|
|
|
|
|
|
|
|
let(:next_id) do
|
|
|
|
case ActiveRecord::Base.connection_config[:adapter]
|
|
|
|
when 'mysql2'
|
|
|
|
ActiveRecord::Base.connection.execute(<<~QUERY).first.first
|
|
|
|
SELECT max(auto_increment) FROM information_schema.tables WHERE table_name='#{described_class.table_name}'
|
|
|
|
QUERY
|
|
|
|
when 'postgresql'
|
|
|
|
ActiveRecord::Base.connection.execute(<<~QUERY).first['last_value'].next
|
|
|
|
SELECT last_value FROM #{described_class.table_name}_id_seq
|
|
|
|
QUERY
|
|
|
|
end
|
|
|
|
end
|
2019-01-22 16:35:01 +00:00
|
|
|
|
2019-01-28 06:08:33 +00:00
|
|
|
context 'when Setting.get("system_init_done") is false (regardless of import_mode)' do
|
|
|
|
before { Setting.set('system_init_done', false) }
|
2019-01-22 16:35:01 +00:00
|
|
|
|
2019-01-28 06:08:33 +00:00
|
|
|
it 'allows explicit setting of #id attribute' do
|
2021-06-23 11:35:27 +00:00
|
|
|
expect { subject.save! }.not_to change(subject, :id)
|
2019-01-28 06:08:33 +00:00
|
|
|
end
|
2019-01-22 16:35:01 +00:00
|
|
|
end
|
|
|
|
|
2019-01-28 06:08:33 +00:00
|
|
|
context 'when Setting.get("system_init_done") is true' do
|
|
|
|
before { Setting.set('system_init_done', true) }
|
2019-01-22 16:35:01 +00:00
|
|
|
|
2019-01-28 06:08:33 +00:00
|
|
|
context 'and Setting.get("import_mode") is false' do
|
|
|
|
before { Setting.set('import_mode', false) }
|
2019-01-22 16:35:01 +00:00
|
|
|
|
2019-01-28 06:08:33 +00:00
|
|
|
it 'prevents explicit setting of #id attribute' do
|
2021-06-23 11:35:27 +00:00
|
|
|
expect { subject.save! }.to change(subject, :id)
|
2019-01-28 06:08:33 +00:00
|
|
|
end
|
2019-01-22 16:35:01 +00:00
|
|
|
end
|
|
|
|
|
2019-01-28 06:08:33 +00:00
|
|
|
context 'and Setting.get("import_mode") is true' do
|
|
|
|
before { Setting.set('import_mode', true) }
|
2019-01-23 06:42:54 +00:00
|
|
|
|
2019-01-28 06:08:33 +00:00
|
|
|
shared_examples 'importable classes' do
|
|
|
|
it 'allows explicit setting of #id attribute' do
|
2021-06-23 11:35:27 +00:00
|
|
|
expect { subject.save! }.not_to change(subject, :id)
|
2019-01-28 06:08:33 +00:00
|
|
|
end
|
2019-01-23 06:42:54 +00:00
|
|
|
end
|
|
|
|
|
2019-01-28 06:08:33 +00:00
|
|
|
shared_examples 'non-importable classes' do
|
|
|
|
it 'prevents explicit setting of #id attribute' do
|
2021-06-23 11:35:27 +00:00
|
|
|
expect { subject.save! }.to change(subject, :id)
|
2019-01-28 06:08:33 +00:00
|
|
|
end
|
2019-01-23 06:42:54 +00:00
|
|
|
end
|
|
|
|
|
2019-01-28 06:08:33 +00:00
|
|
|
include_examples described_class.importable? ? 'importable classes' : 'non-importable classes'
|
|
|
|
end
|
2019-01-23 06:42:54 +00:00
|
|
|
end
|
2019-01-22 16:35:01 +00:00
|
|
|
end
|
|
|
|
end
|