From 6fab323ea8694918396f4e83bc4f566a351e4be4 Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Fri, 28 Jun 2024 15:58:16 +0100 Subject: [PATCH] further fixes --- db/seeds.rb | 6 +- .../fix_nil_letting_allocation_values.rake | 25 ------ spec/helpers/locations_helper_spec.rb | 14 ++- .../fix_nil_letting_allocation_values_spec.rb | 73 ---------------- .../sales/questions/deposit_amount_spec.rb | 86 +++++++------------ spec/requests/start_controller_spec.rb | 15 ++-- .../csv/sales_log_csv_service_spec.rb | 6 +- 7 files changed, 61 insertions(+), 164 deletions(-) delete mode 100644 lib/tasks/fix_nil_letting_allocation_values.rake delete mode 100644 spec/lib/tasks/fix_nil_letting_allocation_values_spec.rb diff --git a/db/seeds.rb b/db/seeds.rb index e478055d9..38125d8d1 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -255,7 +255,7 @@ unless Rails.env.test? assigned_to: support_user, owning_organisation: org, managing_organisation: org, - saledate: Date.new(2023, 4, 1), + saledate: Time.zone.today, purchid: "1", ownershipsch: 1, type: 2, @@ -267,7 +267,7 @@ unless Rails.env.test? assigned_to: support_user, owning_organisation: org, managing_organisation: org, - saledate: Date.new(2023, 4, 1), + saledate: Time.zone.today, purchid: "1", ownershipsch: 2, type: 9, @@ -279,7 +279,7 @@ unless Rails.env.test? assigned_to: support_user, owning_organisation: org, managing_organisation: org, - saledate: Date.new(2023, 4, 1), + saledate: Time.zone.today, purchid: "1", ownershipsch: 3, type: 10, diff --git a/lib/tasks/fix_nil_letting_allocation_values.rake b/lib/tasks/fix_nil_letting_allocation_values.rake deleted file mode 100644 index 1136ed684..000000000 --- a/lib/tasks/fix_nil_letting_allocation_values.rake +++ /dev/null @@ -1,25 +0,0 @@ -desc "Infer nil letting allocation values as no" -task fix_nil_letting_allocation_values: :environment do - LettingsLog.where(cbl: nil) - .or(LettingsLog.where(chr: nil)) - .or(LettingsLog.where(cap: nil)) - .or(LettingsLog.filter_by_year(2024).where(accessible_register: nil)) - .find_each do |log| - next unless log.cbl.present? || log.chr.present? || log.cap.present? || log.accessible_register.present? || log.letting_allocation_unknown.present? - - log.cbl = 0 if log.cbl.blank? - log.chr = 0 if log.chr.blank? - log.cap = 0 if log.cap.blank? - log.accessible_register = 0 if log.form.start_year_after_2024? && log.accessible_register.blank? - - log.letting_allocation_unknown = if log.cbl == 1 || log.chr == 1 || log.cap == 1 || log.accessible_register == 1 - 0 - else - 1 - end - - next if log.save - - Rails.logger.info("NilLettingsAllocationValues: Unable to save changes to log #{log.id}") - end -end diff --git a/spec/helpers/locations_helper_spec.rb b/spec/helpers/locations_helper_spec.rb index aba7d7c98..c453b0840 100644 --- a/spec/helpers/locations_helper_spec.rb +++ b/spec/helpers/locations_helper_spec.rb @@ -1,6 +1,8 @@ require "rails_helper" RSpec.describe LocationsHelper do + include CollectionTimeHelper + describe "mobility type selection" do expected_selection = [OpenStruct.new(id: "Wheelchair-user standard", name: "Wheelchair-user standard", description: "Suitable for someone who uses a wheelchair and offers the full use of all rooms and facilities."), OpenStruct.new(id: "Fitted with equipment and adaptations", name: "Fitted with equipment and adaptations", description: "Fitted with stairlifts, ramps, level access showers or grab rails."), @@ -201,18 +203,22 @@ RSpec.describe LocationsHelper do context "when viewing availability" do context "with no deactivations" do + before do + allow(Time).to receive(:now).and_call_original + end + it "displays current collection start date as availability date if created_at is later than collection start date" do - location.update!(startdate: nil, created_at: Time.zone.local(2024, 1, 16)) + location.update!(startdate: nil, created_at: current_collection_start_date + 6.months) availability_attribute = display_location_attributes(location).find { |x| x[:name] == "Availability" }[:value] - expect(availability_attribute).to eq("Active from 1 April 2023") + expect(availability_attribute).to eq("Active from 1 April #{current_collection_start_date.year}") end it "displays previous collection start date as availability date if created_at is later than collection start date and in crossover" do - location.update!(startdate: nil, created_at: Time.zone.local(2023, 4, 16)) + location.update!(startdate: nil, created_at: previous_collection_start_date + 1.week) availability_attribute = display_location_attributes(location).find { |x| x[:name] == "Availability" }[:value] - expect(availability_attribute).to eq("Active from 1 April 2022") + expect(availability_attribute).to eq("Active from 1 April #{previous_collection_start_date.year}") end context "when location was merged" do diff --git a/spec/lib/tasks/fix_nil_letting_allocation_values_spec.rb b/spec/lib/tasks/fix_nil_letting_allocation_values_spec.rb deleted file mode 100644 index 8db478eb8..000000000 --- a/spec/lib/tasks/fix_nil_letting_allocation_values_spec.rb +++ /dev/null @@ -1,73 +0,0 @@ -require "rails_helper" -require "rake" - -RSpec.describe "fix_nil_letting_allocation_values" do - describe ":fix_nil_letting_allocation_values", type: :task do - subject(:task) { Rake::Task["fix_nil_letting_allocation_values"] } - - before do - Rake.application.rake_require("tasks/fix_nil_letting_allocation_values") - Rake::Task.define_task(:environment) - task.reenable - end - - it "sets nil values to 0 when one allocation type value is non-nil" do - log = create(:lettings_log, :setup_completed, :startdate_today, cbl: nil, chr: nil, cap: 1, accessible_register: nil, letting_allocation_unknown: nil) - - task.invoke - - log.reload - expect(log.cbl).to be 0 - expect(log.chr).to be 0 - expect(log.cap).to be 1 - expect(log.accessible_register).to be 0 - expect(log.letting_allocation_unknown).to be 0 - end - - it "sets nil values to 0 and letting_allocation_unknown to 1 when non-nil allocation type values are 0" do - log = create(:lettings_log, :setup_completed, :startdate_today, cbl: 0, chr: 0, cap: nil, accessible_register: nil, letting_allocation_unknown: nil) - - task.invoke - - log.reload - expect(log.cbl).to be 0 - expect(log.chr).to be 0 - expect(log.cap).to be 0 - expect(log.accessible_register).to be 0 - expect(log.letting_allocation_unknown).to be 1 - end - - it "does not set anything when question has not been answered at all" do - log = create(:lettings_log, :setup_completed, :startdate_today, cbl: nil, chr: nil, cap: nil, accessible_register: nil, letting_allocation_unknown: nil) - - task.invoke - - log.reload - expect(log.cbl).to be_nil - expect(log.chr).to be_nil - expect(log.cap).to be_nil - expect(log.accessible_register).to be_nil - expect(log.letting_allocation_unknown).to be_nil - end - - it "does not set accessible_register for logs before 2024" do - log = create(:lettings_log, :setup_completed, startdate: Time.zone.local(2023, 5, 1), cbl: 1, chr: nil, cap: nil, accessible_register: nil, letting_allocation_unknown: nil) - - task.invoke - - log.reload - expect(log.cbl).to be 1 - expect(log.chr).to be 0 - expect(log.cap).to be 0 - expect(log.accessible_register).to be_nil - expect(log.letting_allocation_unknown).to be 0 - end - - it "logs the log id if the change cannot be saved" do - log = create(:lettings_log, :ignore_validation_errors, :setup_completed, startdate: Time.zone.local(2022, 4, 1), cbl: 1, chr: nil, cap: nil, letting_allocation_unknown: nil) - - expect(Rails.logger).to receive(:info).with(match(/log #{log.id}/)) - task.invoke - end - end -end diff --git a/spec/models/form/sales/questions/deposit_amount_spec.rb b/spec/models/form/sales/questions/deposit_amount_spec.rb index cb32a7ee1..6f8fefa7d 100644 --- a/spec/models/form/sales/questions/deposit_amount_spec.rb +++ b/spec/models/form/sales/questions/deposit_amount_spec.rb @@ -1,32 +1,13 @@ require "rails_helper" RSpec.describe Form::Sales::Questions::DepositAmount, type: :model do - subject(:question) { described_class.new(question_id, question_definition, page, ownershipsch: 1, optional: false) } + subject(:question) { described_class.new(question_id, question_definition, page, ownershipsch: 1, optional:) } + let(:optional) { false } let(:question_id) { nil } let(:question_definition) { nil } let(:page) { instance_double(Form::Page, subsection: instance_double(Form::Subsection, form: instance_double(Form, start_date: Time.zone.local(2023, 4, 1)))) } - it "has correct page" do - expect(question.page).to eq(page) - end - - it "has the correct id" do - expect(question.id).to eq("deposit") - end - - it "has the correct header" do - expect(question.header).to eq("How much cash deposit was paid on the property?") - end - - it "has the correct check_answer_label" do - expect(question.check_answer_label).to eq("Cash deposit") - end - - it "has the correct type" do - expect(question.type).to eq("numeric") - end - context "when the ownership type is shared" do let(:log) { create(:sales_log, :completed, ownershipsch: 1, mortgageused: 2) } @@ -36,7 +17,7 @@ RSpec.describe Form::Sales::Questions::DepositAmount, type: :model do end context "when the ownership type is discounted for 2023" do - let(:log) { create(:sales_log, :completed, ownershipsch: 2, mortgageused: 2, saledate: Time.zone.local(2024, 3, 1)) } + let(:log) { build(:sales_log, :completed, ownershipsch: 2, mortgageused: 2, saledate: Time.zone.local(2024, 3, 1)) } it "is not marked as derived" do expect(question.derived?(log)).to be false @@ -44,49 +25,48 @@ RSpec.describe Form::Sales::Questions::DepositAmount, type: :model do end context "when the ownership type is outright" do - let(:log) { create(:sales_log, :completed, ownershipsch: 3, mortgageused: 2) } + let(:log) { build(:sales_log, :outright_sale_setup_complete, mortgageused:) } - it "is not marked as derived when a mortgage is used" do - log.mortgageused = 1 - expect(question.derived?(log)).to be false - end + context "when a mortgage is used" do + let(:mortgageused) { 1 } - it "is marked as derived when a mortgage is not used" do - log.mortgageused = 2 - expect(question.derived?(log)).to be true + it "is not marked as derived " do + expect(question.derived?(log)).to be false + end end - it "is marked as derived when the mortgage use is unknown" do - log.mortgageused = 3 - expect(question.derived?(log)).to be true - end - end + context "when a mortgage is not used" do + let(:mortgageused) { 2 } - it "has the correct hint" do - expect(question.hint_text).to eq("Enter the total cash sum paid by the buyer towards the property that was not funded by the mortgage. This excludes any grant or loan") - end + it "is marked as derived " do + expect(question.derived?(log)).to be true + end + end - it "has correct width" do - expect(question.width).to eq(5) - end + context "when the mortgage use is unknown" do + let(:mortgageused) { 3 } - it "has correct prefix" do - expect(question.prefix).to eq("£") + it "is marked as derived " do + expect(question.derived?(log)).to be true + end + end end - it "has correct min" do - expect(question.min).to eq(0) - end + describe "hint text" do + context "when optional is false" do + let(:optional) { false } - it "has correct max" do - expect(question.max).to eq(999_999) - end + it "has the correct hint" do + expect(question.hint_text).to eq("Enter the total cash sum paid by the buyer towards the property that was not funded by the mortgage. This excludes any grant or loan") + end + end - context "when optional iis true" do - subject(:question) { described_class.new(question_id, question_definition, page, ownershipsch: 1, optional: true) } + context "when optional is true" do + let(:optional) { true } - it "has a correct hint_text" do - expect(question.hint_text).to eq("Enter the total cash sum paid by the buyer towards the property that was not funded by the mortgage. This excludes any grant or loan. As this is a fully staircased sale this question is optional. If you do not have the information available click save and continue") + it "has the correct hint" do + expect(question.hint_text).to eq("Enter the total cash sum paid by the buyer towards the property that was not funded by the mortgage. This excludes any grant or loan. As this is a fully staircased sale this question is optional. If you do not have the information available click save and continue") + end end end end diff --git a/spec/requests/start_controller_spec.rb b/spec/requests/start_controller_spec.rb index 017ab32e7..ee0fd101e 100644 --- a/spec/requests/start_controller_spec.rb +++ b/spec/requests/start_controller_spec.rb @@ -203,7 +203,8 @@ RSpec.describe StartController, type: :request do it "shows the correct counts of logs created by them" do last_year_in_progress_count = 2 this_year_in_progress_count = 3 - create_list(:lettings_log, last_year_in_progress_count, :in_progress, assigned_to: provider_1, startdate: Time.zone.today - 1.year) + old_logs = build_list(:lettings_log, last_year_in_progress_count, :in_progress, assigned_to: provider_1, startdate: Time.zone.today - 1.year) + old_logs.each { |log| log.save!(validate: false) } create_list(:lettings_log, this_year_in_progress_count, :in_progress, assigned_to: provider_1, startdate: Time.zone.today) get root_path @@ -217,7 +218,8 @@ RSpec.describe StartController, type: :request do end it "does not include logs created by other users in the count, whether in their organisation or not" do - create(:lettings_log, :in_progress, assigned_to: coordinator, startdate: Time.zone.today - 1.year) + old_log = build(:lettings_log, :in_progress, assigned_to: coordinator, startdate: Time.zone.today - 1.year) + old_log.save!(validate: false) create(:lettings_log, :in_progress, assigned_to: provider_2, startdate: Time.zone.today) get root_path @@ -237,7 +239,8 @@ RSpec.describe StartController, type: :request do it "shows the correct counts of logs created by all users in their organisation" do last_year_in_progress_count = 2 this_year_in_progress_count = 3 - create_list(:lettings_log, last_year_in_progress_count, :in_progress, assigned_to: provider_1, startdate: Time.zone.today - 1.year) + old_logs = build_list(:lettings_log, last_year_in_progress_count, :in_progress, assigned_to: provider_1, startdate: Time.zone.today - 1.year) + old_logs.each { |log| log.save!(validate: false) } create_list(:lettings_log, this_year_in_progress_count, :in_progress, assigned_to: coordinator, startdate: Time.zone.today) get root_path @@ -288,9 +291,11 @@ RSpec.describe StartController, type: :request do coordinator_lettings_this_year_in_progress_count = 3 provider_2_lettings_last_year_in_progress_count = 2 provider_2_sales_this_year_in_progress_count = 3 - create_list(:lettings_log, provider_1_lettings_last_year_in_progress_count, :in_progress, assigned_to: provider_1, startdate: Time.zone.today - 1.year) + old_logs = build_list(:lettings_log, provider_1_lettings_last_year_in_progress_count, :in_progress, assigned_to: provider_1, startdate: Time.zone.today - 1.year) + old_logs.each { |log| log.save!(validate: false) } create_list(:lettings_log, coordinator_lettings_this_year_in_progress_count, :in_progress, assigned_to: coordinator, startdate: Time.zone.today) - create_list(:lettings_log, provider_2_lettings_last_year_in_progress_count, :in_progress, assigned_to: provider_2, startdate: Time.zone.today - 1.year) + old_logs = build_list(:lettings_log, provider_2_lettings_last_year_in_progress_count, :in_progress, assigned_to: provider_2, startdate: Time.zone.today - 1.year) + old_logs.each { |log| log.save!(validate: false) } create_list(:sales_log, provider_2_sales_this_year_in_progress_count, :in_progress, assigned_to: provider_2, saledate: Time.zone.today) get root_path diff --git a/spec/services/csv/sales_log_csv_service_spec.rb b/spec/services/csv/sales_log_csv_service_spec.rb index 7f6ad8c2a..aee659ae5 100644 --- a/spec/services/csv/sales_log_csv_service_spec.rb +++ b/spec/services/csv/sales_log_csv_service_spec.rb @@ -3,7 +3,7 @@ require "rails_helper" RSpec.describe Csv::SalesLogCsvService do let(:form_handler_mock) { instance_double(FormHandler) } let(:organisation) { create(:organisation) } - let(:fixed_time) { Time.zone.local(2023, 12, 8) } + let(:fixed_time) { now } let(:now) { Time.zone.now } let(:user) { create(:user, :support, email: "billyboy@eyeKLAUD.com") } let(:log) do @@ -134,6 +134,8 @@ RSpec.describe Csv::SalesLogCsvService do context "when exporting with human readable labels" do let(:year) { 2023 } + let(:fixed_time) { Time.zone.local(2023, 12, 8) } + let(:now) { fixed_time } it "gives answers to radio questions as their labels" do national_column_index = csv.first.index("NATIONAL") @@ -220,6 +222,8 @@ RSpec.describe Csv::SalesLogCsvService do context "when exporting values as codes" do let(:service) { described_class.new(user:, export_type: "codes", year:) } let(:year) { 2023 } + let(:fixed_time) { Time.zone.local(2023, 12, 8) } + let(:now) { fixed_time } it "gives answers to radio questions as their codes" do national_column_index = csv.first.index("NATIONAL")