From 9a0c3f001a3f264b05b3d40cfb4571883a72d837 Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 25 Aug 2023 12:30:52 +0100 Subject: [PATCH] Add a rake task to update has_other_client_group --- ...correct_has_other_client_group_values.rake | 5 ++ ...rect_has_other_client_group_values_spec.rb | 56 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 lib/tasks/correct_has_other_client_group_values.rake create mode 100644 spec/lib/tasks/correct_has_other_client_group_values_spec.rb 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