Browse Source
* Infer lar and irproduct * Add recalculate lar values task * Create irproduct task * Tests * typopull/2009/head
kosiakkatrina
1 year ago
committed by
GitHub
13 changed files with 335 additions and 7 deletions
@ -0,0 +1,18 @@ |
|||||||
|
desc "Forces to recalculate irproduct values" |
||||||
|
task recalculate_irproduct_values: :environment do |
||||||
|
LettingsLog.exportable.where(rent_type: [3, 4, 5]).where(irproduct: nil).each do |log| # irproduct was never set |
||||||
|
Rails.logger.info("Could not update irproduct for LettingsLog #{log.id}") unless log.update(values_updated_at: Time.zone.now) |
||||||
|
end |
||||||
|
LettingsLog.exportable.where(rent_type: 3).where.not(irproduct: 1).each do |log| # irproduct was set wrong |
||||||
|
Rails.logger.info("Could not update irproduct for LettingsLog #{log.id}") unless log.update(values_updated_at: Time.zone.now) |
||||||
|
end |
||||||
|
LettingsLog.exportable.where(rent_type: 4).where.not(irproduct: 2).each do |log| # irproduct was set wrong |
||||||
|
Rails.logger.info("Could not update irproduct for LettingsLog #{log.id}") unless log.update(values_updated_at: Time.zone.now) |
||||||
|
end |
||||||
|
LettingsLog.exportable.where(rent_type: 5).where.not(irproduct: 3).each do |log| # irproduct was set wrong |
||||||
|
Rails.logger.info("Could not update irproduct for LettingsLog #{log.id}") unless log.update(values_updated_at: Time.zone.now) |
||||||
|
end |
||||||
|
LettingsLog.exportable.where.not(rent_type: [3, 4, 5]).where.not(irproduct: nil).each do |log| # irproduct was set when it should have been nil |
||||||
|
Rails.logger.info("Could not update irproduct for LettingsLog #{log.id}") unless log.update(values_updated_at: Time.zone.now) |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,13 @@ |
|||||||
|
desc "Forces to recalculate lar values for affordable rent types and clears irrelevant lar values" |
||||||
|
task recalculate_lar_values: :environment do |
||||||
|
LettingsLog.exportable.where(rent_type: [1, 2], lar: nil).each do |log| # lar was never set |
||||||
|
Rails.logger.info("Could not update lar for LettingsLog #{log.id}") unless log.update(values_updated_at: Time.zone.now) |
||||||
|
end |
||||||
|
LettingsLog.exportable.where(rent_type: 1).where.not(lar: 2).each do |log| # lar was set wrong |
||||||
|
Rails.logger.info("Could not update lar for LettingsLog #{log.id}") unless log.update(values_updated_at: Time.zone.now) |
||||||
|
end |
||||||
|
LettingsLog.exportable.where(rent_type: 2).where.not(lar: 1).each do |log| # lar was set wrong |
||||||
|
Rails.logger.info("Could not update lar for LettingsLog #{log.id}") unless log.update(values_updated_at: Time.zone.now) |
||||||
|
end |
||||||
|
LettingsLog.exportable.where.not(rent_type: [1, 2]).where.not(lar: nil).update_all(lar: nil) # lar was set to 2 but should never have been set |
||||||
|
end |
|
|
|
|
@ -0,0 +1,132 @@ |
|||||||
|
require "rails_helper" |
||||||
|
require "rake" |
||||||
|
|
||||||
|
RSpec.describe "recalculate_irproduct_values" do |
||||||
|
describe ":recalculate_irproduct_values", type: :task do |
||||||
|
subject(:task) { Rake::Task["recalculate_irproduct_values"] } |
||||||
|
|
||||||
|
before do |
||||||
|
Rake.application.rake_require("tasks/recalculate_irproduct_values") |
||||||
|
Rake::Task.define_task(:environment) |
||||||
|
task.reenable |
||||||
|
end |
||||||
|
|
||||||
|
context "when the rake task is run" do |
||||||
|
let!(:lettings_log) { create(:lettings_log, :completed, values_updated_at: nil) } |
||||||
|
|
||||||
|
it "updates irproduct to nil if it's set to 1 but rent type is not 3, 4 or 5" do |
||||||
|
lettings_log.irproduct = 1 |
||||||
|
lettings_log.rent_type = 2 |
||||||
|
lettings_log.save!(validate: false) |
||||||
|
task.invoke |
||||||
|
lettings_log.reload |
||||||
|
expect(lettings_log.irproduct).to eq(nil) |
||||||
|
expect(lettings_log.values_updated_at).not_to be_nil |
||||||
|
end |
||||||
|
|
||||||
|
it "updates irproduct to nil if it's set to 2 but rent type is not 3, 4 or 5" do |
||||||
|
lettings_log.irproduct = 2 |
||||||
|
lettings_log.rent_type = 2 |
||||||
|
lettings_log.save!(validate: false) |
||||||
|
task.invoke |
||||||
|
lettings_log.reload |
||||||
|
expect(lettings_log.irproduct).to eq(nil) |
||||||
|
expect(lettings_log.values_updated_at).not_to be_nil |
||||||
|
end |
||||||
|
|
||||||
|
it "updates irproduct to nil if it's set to 3 but rent type is not 3, 4 or 5" do |
||||||
|
lettings_log.irproduct = 3 |
||||||
|
lettings_log.rent_type = 2 |
||||||
|
lettings_log.save!(validate: false) |
||||||
|
task.invoke |
||||||
|
lettings_log.reload |
||||||
|
expect(lettings_log.irproduct).to eq(nil) |
||||||
|
expect(lettings_log.values_updated_at).not_to be_nil |
||||||
|
end |
||||||
|
|
||||||
|
it "updates irproduct to 1 if it's set to nil but rent type is 3" do |
||||||
|
lettings_log.irproduct = nil |
||||||
|
lettings_log.rent_type = 3 |
||||||
|
lettings_log.save!(validate: false) |
||||||
|
task.invoke |
||||||
|
lettings_log.reload |
||||||
|
expect(lettings_log.irproduct).to eq(1) |
||||||
|
expect(lettings_log.values_updated_at).not_to be_nil |
||||||
|
end |
||||||
|
|
||||||
|
it "updates irproduct to 1 if it's set to something else but rent type is 3" do |
||||||
|
lettings_log.irproduct = 2 |
||||||
|
lettings_log.rent_type = 3 |
||||||
|
lettings_log.save!(validate: false) |
||||||
|
task.invoke |
||||||
|
lettings_log.reload |
||||||
|
expect(lettings_log.irproduct).to eq(1) |
||||||
|
expect(lettings_log.values_updated_at).not_to be_nil |
||||||
|
end |
||||||
|
|
||||||
|
it "updates irproduct to 2 if it's set to nil but rent type is 4" do |
||||||
|
lettings_log.irproduct = nil |
||||||
|
lettings_log.rent_type = 4 |
||||||
|
lettings_log.save!(validate: false) |
||||||
|
task.invoke |
||||||
|
lettings_log.reload |
||||||
|
expect(lettings_log.irproduct).to eq(2) |
||||||
|
expect(lettings_log.values_updated_at).not_to be_nil |
||||||
|
end |
||||||
|
|
||||||
|
it "updates irproduct to 2 if it's set to something else but rent type is 4" do |
||||||
|
lettings_log.irproduct = 1 |
||||||
|
lettings_log.rent_type = 4 |
||||||
|
lettings_log.save!(validate: false) |
||||||
|
task.invoke |
||||||
|
lettings_log.reload |
||||||
|
expect(lettings_log.irproduct).to eq(2) |
||||||
|
expect(lettings_log.values_updated_at).not_to be_nil |
||||||
|
end |
||||||
|
|
||||||
|
it "updates irproduct to 3 if it's set to nil but rent type is 5" do |
||||||
|
lettings_log.irproduct = nil |
||||||
|
lettings_log.rent_type = 5 |
||||||
|
lettings_log.irproduct_other = "other" |
||||||
|
lettings_log.save!(validate: false) |
||||||
|
task.invoke |
||||||
|
lettings_log.reload |
||||||
|
expect(lettings_log.irproduct).to eq(3) |
||||||
|
expect(lettings_log.values_updated_at).not_to be_nil |
||||||
|
end |
||||||
|
|
||||||
|
it "updates irproduct to 3 if it's set to something else but rent type is 5" do |
||||||
|
lettings_log.irproduct = 2 |
||||||
|
lettings_log.rent_type = 5 |
||||||
|
lettings_log.irproduct_other = "other" |
||||||
|
lettings_log.save!(validate: false) |
||||||
|
task.invoke |
||||||
|
lettings_log.reload |
||||||
|
expect(lettings_log.irproduct).to eq(3) |
||||||
|
expect(lettings_log.values_updated_at).not_to be_nil |
||||||
|
end |
||||||
|
|
||||||
|
it "does not update irproduct if rent_type is not 3, 4 or 5 and irproduct is nil" do |
||||||
|
lettings_log.irproduct = nil |
||||||
|
lettings_log.rent_type = 2 |
||||||
|
lettings_log.save!(validate: false) |
||||||
|
task.invoke |
||||||
|
lettings_log.reload |
||||||
|
expect(lettings_log.irproduct).to eq(nil) |
||||||
|
expect(lettings_log.values_updated_at).to be_nil |
||||||
|
end |
||||||
|
|
||||||
|
it "does not update irproduct if a different validation is triggering" do |
||||||
|
lettings_log.irproduct = 2 |
||||||
|
lettings_log.rent_type = 5 |
||||||
|
lettings_log.postcode_full = "invalid" |
||||||
|
lettings_log.save!(validate: false) |
||||||
|
expect(Rails.logger).to receive(:info).with("Could not update irproduct for LettingsLog #{lettings_log.id}") |
||||||
|
task.invoke |
||||||
|
lettings_log.reload |
||||||
|
expect(lettings_log.irproduct).to eq(2) |
||||||
|
expect(lettings_log.values_updated_at).to be_nil |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,100 @@ |
|||||||
|
require "rails_helper" |
||||||
|
require "rake" |
||||||
|
|
||||||
|
RSpec.describe "recalculate_lar_values" do |
||||||
|
describe ":recalculate_lar_values", type: :task do |
||||||
|
subject(:task) { Rake::Task["recalculate_lar_values"] } |
||||||
|
|
||||||
|
before do |
||||||
|
Rake.application.rake_require("tasks/recalculate_lar_values") |
||||||
|
Rake::Task.define_task(:environment) |
||||||
|
task.reenable |
||||||
|
end |
||||||
|
|
||||||
|
context "when the rake task is run" do |
||||||
|
let!(:lettings_log) { create(:lettings_log, :completed, values_updated_at: nil) } |
||||||
|
|
||||||
|
it "updates lar to nil if it's not afordable rent or london afordable rent and lar is 1 but does not set it to export" do |
||||||
|
lettings_log.lar = 1 |
||||||
|
lettings_log.rent_type = 3 |
||||||
|
lettings_log.save!(validate: false) |
||||||
|
task.invoke |
||||||
|
lettings_log.reload |
||||||
|
expect(lettings_log.lar).to eq(nil) |
||||||
|
expect(lettings_log.values_updated_at).to be_nil |
||||||
|
end |
||||||
|
|
||||||
|
it "updates lar to nil if it's not afordable rent or london afordable rent and lar is 2 but does not set it to export" do |
||||||
|
lettings_log.lar = 2 |
||||||
|
lettings_log.rent_type = 4 |
||||||
|
lettings_log.save!(validate: false) |
||||||
|
task.invoke |
||||||
|
lettings_log.reload |
||||||
|
expect(lettings_log.lar).to eq(nil) |
||||||
|
expect(lettings_log.values_updated_at).to be_nil |
||||||
|
end |
||||||
|
|
||||||
|
it "does not update lar if it's not london afordable rent or affordable rent and lar is nil" do |
||||||
|
lettings_log.lar = nil |
||||||
|
lettings_log.rent_type = 3 |
||||||
|
lettings_log.save!(validate: false) |
||||||
|
task.invoke |
||||||
|
lettings_log.reload |
||||||
|
expect(lettings_log.lar).to eq(nil) |
||||||
|
expect(lettings_log.values_updated_at).to be_nil |
||||||
|
end |
||||||
|
|
||||||
|
it "updates lar to 1 if it's london afordable rent and lar is currently nil" do |
||||||
|
lettings_log.lar = nil |
||||||
|
lettings_log.rent_type = 2 |
||||||
|
lettings_log.save!(validate: false) |
||||||
|
task.invoke |
||||||
|
lettings_log.reload |
||||||
|
expect(lettings_log.lar).to eq(1) |
||||||
|
expect(lettings_log.values_updated_at).not_to be_nil |
||||||
|
end |
||||||
|
|
||||||
|
it "updates lar to 1 if it's london afordable rent and lar is currently 2" do |
||||||
|
lettings_log.lar = 2 |
||||||
|
lettings_log.rent_type = 2 |
||||||
|
lettings_log.save!(validate: false) |
||||||
|
task.invoke |
||||||
|
lettings_log.reload |
||||||
|
expect(lettings_log.lar).to eq(1) |
||||||
|
expect(lettings_log.values_updated_at).not_to be_nil |
||||||
|
end |
||||||
|
|
||||||
|
it "updates lar to 2 if it's afordable rent and lar is currently nil" do |
||||||
|
lettings_log.lar = nil |
||||||
|
lettings_log.rent_type = 1 |
||||||
|
lettings_log.save!(validate: false) |
||||||
|
task.invoke |
||||||
|
lettings_log.reload |
||||||
|
expect(lettings_log.lar).to eq(2) |
||||||
|
expect(lettings_log.values_updated_at).not_to be_nil |
||||||
|
end |
||||||
|
|
||||||
|
it "updates lar to 2 if it's afordable rent and lar is currently 1" do |
||||||
|
lettings_log.lar = 1 |
||||||
|
lettings_log.rent_type = 1 |
||||||
|
lettings_log.save!(validate: false) |
||||||
|
task.invoke |
||||||
|
lettings_log.reload |
||||||
|
expect(lettings_log.lar).to eq(2) |
||||||
|
expect(lettings_log.values_updated_at).not_to be_nil |
||||||
|
end |
||||||
|
|
||||||
|
it "does not update lar if a different validation is triggering" do |
||||||
|
lettings_log.lar = 1 |
||||||
|
lettings_log.rent_type = 1 |
||||||
|
lettings_log.postcode_full = "invalid" |
||||||
|
lettings_log.save!(validate: false) |
||||||
|
expect(Rails.logger).to receive(:info).with("Could not update lar for LettingsLog #{lettings_log.id}") |
||||||
|
task.invoke |
||||||
|
lettings_log.reload |
||||||
|
expect(lettings_log.lar).to eq(1) |
||||||
|
expect(lettings_log.values_updated_at).to be_nil |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
Loading…
Reference in new issue