2022-11-19 06:03:59 +01:00
|
|
|
describe BatchOperationProcessOneJob, type: :job do
|
2022-11-18 16:59:46 +01:00
|
|
|
describe 'perform' do
|
|
|
|
let(:batch_operation) do
|
|
|
|
create(:batch_operation, :archiver,
|
|
|
|
options.merge(instructeur: create(:instructeur)))
|
|
|
|
end
|
|
|
|
let(:dossier_job) { batch_operation.dossiers.first }
|
2022-11-19 06:03:59 +01:00
|
|
|
subject { BatchOperationProcessOneJob.new(batch_operation, dossier_job) }
|
2022-11-18 16:59:46 +01:00
|
|
|
let(:options) { {} }
|
|
|
|
|
2022-11-25 15:43:00 +01:00
|
|
|
it 'when it works' do
|
|
|
|
allow_any_instance_of(BatchOperation).to receive(:process_one).with(dossier_job).and_return(true)
|
2022-11-18 16:59:46 +01:00
|
|
|
expect { subject.perform_now }
|
2022-11-25 15:43:00 +01:00
|
|
|
.to change { batch_operation.reload.success_dossier_ids }
|
|
|
|
.from([])
|
|
|
|
.to([dossier_job.id])
|
2022-11-18 16:59:46 +01:00
|
|
|
end
|
|
|
|
|
2022-11-25 15:43:00 +01:00
|
|
|
it 'when it fails for an "unknown" reason' do
|
|
|
|
allow_any_instance_of(BatchOperation).to receive(:process_one).with(dossier_job).and_raise("boom")
|
|
|
|
expect { subject.perform_now }.to raise_error('boom')
|
2022-11-18 16:59:46 +01:00
|
|
|
|
2022-11-25 15:43:00 +01:00
|
|
|
expect(batch_operation.reload.failed_dossier_ids).to eq([dossier_job.id])
|
2022-11-18 16:59:46 +01:00
|
|
|
end
|
|
|
|
|
2022-11-25 15:43:00 +01:00
|
|
|
context 'when the dossier is out of sync (ie: someone applied a transition somewhere we do not know)' do
|
|
|
|
let(:instructeur) { create(:instructeur) }
|
|
|
|
let(:procedure) { create(:simple_procedure, instructeurs: [instructeur]) }
|
|
|
|
let(:dossier) { create(:dossier, :accepte, :with_individual, archived: true, procedure: procedure) }
|
|
|
|
let(:batch_operation) { create(:batch_operation, operation: :archiver, instructeur: instructeur, dossiers: [dossier]) }
|
2022-11-18 16:59:46 +01:00
|
|
|
|
2022-11-25 15:43:00 +01:00
|
|
|
it 'does run process_one' do
|
|
|
|
allow(batch_operation).to receive(:process_one).and_raise("should have been prevented")
|
|
|
|
subject.perform_now
|
2022-11-18 16:59:46 +01:00
|
|
|
end
|
|
|
|
|
2022-11-25 15:43:00 +01:00
|
|
|
it 'when it fails from dossiers_safe_scope.find' do
|
|
|
|
scope = double
|
|
|
|
expect(scope).to receive(:find).with(dossier_job.id).and_raise(ActiveRecord::RecordNotFound)
|
|
|
|
expect_any_instance_of(BatchOperation).to receive(:dossiers_safe_scope).and_return(scope)
|
2022-11-18 16:59:46 +01:00
|
|
|
|
2022-11-25 15:43:00 +01:00
|
|
|
subject.perform_now
|
|
|
|
|
|
|
|
expect(batch_operation.reload.failed_dossier_ids).to eq([])
|
|
|
|
expect(batch_operation.dossiers).not_to include(dossier_job)
|
2022-11-18 16:59:46 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|