From decc011d6bb3f560cb7370c756df84a7c0969cab Mon Sep 17 00:00:00 2001 From: Manny Dinssa <44172848+Dinssa@users.noreply.github.com> Date: Mon, 17 Mar 2025 17:01:12 +0000 Subject: [PATCH] Rake task and tests --- ...ddress_entry_selected_prexisting_logs.rake | 24 ++++ ...ess_entry_selected_prexisting_logs_spec.rb | 130 ++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 lib/tasks/update_manual_address_entry_selected_prexisting_logs.rake create mode 100644 spec/lib/tasks/update_manual_address_entry_selected_prexisting_logs_spec.rb diff --git a/lib/tasks/update_manual_address_entry_selected_prexisting_logs.rake b/lib/tasks/update_manual_address_entry_selected_prexisting_logs.rake new file mode 100644 index 000000000..1aa4f1ad0 --- /dev/null +++ b/lib/tasks/update_manual_address_entry_selected_prexisting_logs.rake @@ -0,0 +1,24 @@ +namespace :bulk_update do + desc "Update logs with specific criteria and set manual_address_entry_selected to true" + task update_manual_address_entry_selected: :environment do + lettings_logs = LettingsLog.filter_by_year(2024) + .where(status: %w[in_progress completed]) + .where(needstype: 1, manual_address_entry_selected: false, uprn: nil) + + lettings_logs.find_each do |log| + log.update(manual_address_entry_selected: true) + end + + puts "#{lettings_logs.count} lettings logs updated." + + sales_logs = SalesLog.filter_by_year(2024) + .where(status: %w[in_progress completed]) + .where(manual_address_entry_selected: false, uprn: nil) + + sales_logs.find_each do |log| + log.update(manual_address_entry_selected: true) + end + + puts "#{sales_logs.count} sales logs updated." + end +end diff --git a/spec/lib/tasks/update_manual_address_entry_selected_prexisting_logs_spec.rb b/spec/lib/tasks/update_manual_address_entry_selected_prexisting_logs_spec.rb new file mode 100644 index 000000000..8efaa6e71 --- /dev/null +++ b/spec/lib/tasks/update_manual_address_entry_selected_prexisting_logs_spec.rb @@ -0,0 +1,130 @@ +# spec/tasks/update_manual_address_entry_selected_prexisting_logs_spec.rb +require "rails_helper" +require "rake" + +RSpec.describe "bulk_update:update_manual_address_entry_selected", type: :task do + let(:task) { Rake::Task["bulk_update:update_manual_address_entry_selected"] } + + let(:lettings_log_uprn_entered) do + create(:lettings_log, :completed, + needstype: 1, + manual_address_entry_selected: false, + uprn: "123", + address_line1: nil, + address_line2: nil, + town_or_city: nil, + postcode_full: nil, + address_line1_input: nil, + postcode_full_input: nil) + end + + let(:lettings_log_uprn_found) do + create(:lettings_log, :completed, + needstype: 1, + manual_address_entry_selected: false, + uprn: "123", + address_line1: nil, + address_line2: nil, + town_or_city: nil, + postcode_full: nil, + address_line1_input: "1 Test Street", + postcode_full_input: "SW1 1AA") + end + + let(:lettings_log_address_manually_entered) do + create(:lettings_log, :completed, + needstype: 1, + manual_address_entry_selected: false, + uprn: nil, + address_line1: "1 Test Street", + address_line2: "Testville", + town_or_city: "Testford", + postcode_full: "SW1 1AA", + address_line1_input: nil, + postcode_full_input: nil) + end + + let(:sales_log_uprn_entered) do + create(:sales_log, :completed, + manual_address_entry_selected: false, + uprn: "123", + address_line1: nil, + address_line2: nil, + town_or_city: nil, + postcode_full: nil, + address_line1_input: nil, + postcode_full_input: nil) + end + + let(:sales_log_uprn_found) do + create(:sales_log, :completed, + manual_address_entry_selected: false, + uprn: "123", + address_line1: nil, + address_line2: nil, + town_or_city: nil, + postcode_full: nil, + address_line1_input: "1 Test Street", + postcode_full_input: "SW1 1AA") + end + + let(:sales_log_address_manually_entered) do + create(:sales_log, :completed, + manual_address_entry_selected: false, + uprn: nil, + address_line1: "1 Test Street", + address_line2: "Testville", + town_or_city: "Testford", + postcode_full: "SW1 1AA", + address_line1_input: nil, + postcode_full_input: nil) + end + + before do + Rake.application.rake_require("tasks/update_manual_address_entry_selected_prexisting_logs") + Rake::Task.define_task(:environment) + end + + context "when running the task" do + + context "when logs do not meet the criteria" do + it "does not update logs with a UPRN entered" do + task.invoke + lettings_log_uprn_entered.reload + sales_log_uprn_entered.reload + expect(lettings_log_uprn_entered.manual_address_entry_selected).to be false + expect(lettings_log_uprn_entered.uprn).to eq("123") + expect(sales_log_uprn_entered.manual_address_entry_selected).to be false + expect(sales_log_uprn_entered.uprn).to eq("123") + end + + it "does not update logs with a UPRN found" do + task.invoke + lettings_log_uprn_found.reload + sales_log_uprn_found.reload + expect(lettings_log_uprn_found.manual_address_entry_selected).to be false + expect(lettings_log_uprn_found.uprn).to eq("123") + expect(sales_log_uprn_found.manual_address_entry_selected).to be false + expect(sales_log_uprn_found.uprn).to eq("123") + end + end + + context "when logs do meet the criteria" do + it "updates logs with an address manually entered" do + task.invoke + lettings_log_address_manually_entered.reload + sales_log_address_manually_entered.reload + expect(lettings_log_address_manually_entered.manual_address_entry_selected).to be true + expect(lettings_log_address_manually_entered.address_line1).to eq("1 Test Street") + expect(lettings_log_address_manually_entered.address_line2).to eq("Testville") + expect(lettings_log_address_manually_entered.town_or_city).to eq("Testford") + expect(lettings_log_address_manually_entered.postcode_full).to eq("SW1 1AA") + expect(sales_log_address_manually_entered.manual_address_entry_selected).to be true + expect(sales_log_address_manually_entered.address_line1).to eq("1 Test Street") + expect(sales_log_address_manually_entered.address_line2).to eq("Testville") + expect(sales_log_address_manually_entered.town_or_city).to eq("Testford") + expect(sales_log_address_manually_entered.postcode_full).to eq("SW1 1AA") + end + end + end +end