From 5a0eca5ccc0b3af6524b090c664ef2eb3073ac7b Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 26 Nov 2024 14:40:20 +0100 Subject: [PATCH] Do not raise exception when Avis is not valid, just fix the data --- ...ix_avis_experts_procedure_mismatch_task.rb | 2 +- ...is_experts_procedure_mismatch_task_spec.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/tasks/maintenance/fix_avis_experts_procedure_mismatch_task.rb b/app/tasks/maintenance/fix_avis_experts_procedure_mismatch_task.rb index a86f5d47d..72a4f5f93 100644 --- a/app/tasks/maintenance/fix_avis_experts_procedure_mismatch_task.rb +++ b/app/tasks/maintenance/fix_avis_experts_procedure_mismatch_task.rb @@ -20,7 +20,7 @@ module Maintenance expert: expert ) - avis.update!(experts_procedure: correct_experts_procedure) + avis.update_column(:experts_procedure_id, correct_experts_procedure.id) end end end diff --git a/spec/tasks/maintenance/fix_avis_experts_procedure_mismatch_task_spec.rb b/spec/tasks/maintenance/fix_avis_experts_procedure_mismatch_task_spec.rb index 247c2e4c4..52c8f3848 100644 --- a/spec/tasks/maintenance/fix_avis_experts_procedure_mismatch_task_spec.rb +++ b/spec/tasks/maintenance/fix_avis_experts_procedure_mismatch_task_spec.rb @@ -78,6 +78,25 @@ module Maintenance expect(avis.experts_procedure).to eq(correct_experts_procedure) end end + + context "when the avis has an invalid question_answer" do + let(:avis) do + create(:avis, + dossier: dossier, + expert: expert, + experts_procedure: wrong_experts_procedure, + question_label: "Some question", + question_answer: nil) + end + + it "fixes the experts_procedure association without validation errors" do + expect(avis).not_to be_valid + expect { process }.not_to raise_error + + avis.reload + expect(avis.experts_procedure.procedure_id).to eq(procedure2.id) + end + end end end end