Browse Source

CLDC-3483: Add task to fix existing nil letting allocation values

pull/2453/head
Rachael Booth 2 years ago
parent
commit
64d4e80ea0
  1. 26
      lib/tasks/fix_nil_letting_allocation_values.rake
  2. 73
      spec/lib/tasks/fix_nil_letting_allocation_values_spec.rb

26
lib/tasks/fix_nil_letting_allocation_values.rake

@ -0,0 +1,26 @@
desc "Infer nil letting allocation values as no"
task fix_nil_letting_allocation_values: :environment do
logs = LettingsLog.where(cbl: nil)
.or(LettingsLog.where(chr: nil))
.or(LettingsLog.where(cap: nil))
.or(LettingsLog.filter_by_year(2024).where(accessible_register: nil))
logs.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.log("NilLettingsAllocationValues: Unable to save changes to log #{log.id}")
end
end

73
spec/lib/tasks/fix_nil_letting_allocation_values_spec.rb

@ -0,0 +1,73 @@
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(:log).with(match(/log #{log.id}/))
task.invoke
end
end
end
Loading…
Cancel
Save