diff --git a/lib/tasks/correct_has_other_client_group_values.rake b/lib/tasks/correct_has_other_client_group_values.rake new file mode 100644 index 000000000..1ee51a5e4 --- /dev/null +++ b/lib/tasks/correct_has_other_client_group_values.rake @@ -0,0 +1,5 @@ +desc "Update has_other_client_group values for schemes" +task correct_has_other_client_group_values: :environment do + Scheme.where(confirmed: true, secondary_client_group: nil).update_all(has_other_client_group: 0) + Scheme.where(confirmed: true).where.not(secondary_client_group: nil).update_all(has_other_client_group: 1) +end diff --git a/spec/lib/tasks/correct_has_other_client_group_values_spec.rb b/spec/lib/tasks/correct_has_other_client_group_values_spec.rb new file mode 100644 index 000000000..9e021b05e --- /dev/null +++ b/spec/lib/tasks/correct_has_other_client_group_values_spec.rb @@ -0,0 +1,56 @@ +require "rails_helper" +require "rake" + +RSpec.describe "correct_has_other_client_group_values" do + describe ":correct_has_other_client_group_values", type: :task do + subject(:task) { Rake::Task["correct_has_other_client_group_values"] } + + before do + Rake.application.rake_require("tasks/correct_has_other_client_group_values") + Rake::Task.define_task(:environment) + task.reenable + end + + context "when the rake task is run" do + let!(:scheme) { create(:scheme, secondary_client_group: nil) } + + before do + scheme.has_other_client_group = nil + scheme.save!(validate: false) + end + + context "and the scheme is marked confirmed" do + it "updates schemes with secondary_client_group to have has_other_client_group 1 (yes)" do + scheme.secondary_client_group = "G" + scheme.save!(validate: false) + task.invoke + expect(scheme.reload.has_other_client_group).to eq("Yes") + end + + it "updates schemes without secondary_client_group to have has_other_client_group 0 (no)" do + task.invoke + expect(scheme.reload.has_other_client_group).to eq("No") + end + end + + context "and the scheme is not marked confirmed" do + before do + scheme.confirmed = false + scheme.save!(validate: false) + end + + it "does not update schemes with secondary_client_group" do + scheme.secondary_client_group = "G" + scheme.save!(validate: false) + task.invoke + expect(scheme.reload.has_other_client_group).to eq(nil) + end + + it "does not update schemes without secondary_client_group" do + task.invoke + expect(scheme.reload.has_other_client_group).to eq(nil) + end + end + end + end +end