diff --git a/spec/models/validations/date_validations_spec.rb b/spec/models/validations/date_validations_spec.rb index 5b9d1a436..75907b5bc 100644 --- a/spec/models/validations/date_validations_spec.rb +++ b/spec/models/validations/date_validations_spec.rb @@ -9,15 +9,6 @@ RSpec.describe Validations::DateValidations do let(:scheme_no_end_date) { create(:scheme, end_date: nil) } describe "tenancy start date" do - before do - Timecop.freeze(Time.zone.local(2023, 11, 10)) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - end - it "must be a valid date" do record.startdate = Time.zone.local(0, 7, 1) date_validator.validate_startdate(record) diff --git a/spec/models/validations/household_validations_spec.rb b/spec/models/validations/household_validations_spec.rb index 4254ede6d..20df63fa5 100644 --- a/spec/models/validations/household_validations_spec.rb +++ b/spec/models/validations/household_validations_spec.rb @@ -4,16 +4,8 @@ RSpec.describe Validations::HouseholdValidations do subject(:household_validator) { validator_class.new } let(:validator_class) { Class.new { include Validations::HouseholdValidations } } - let(:log_date) { Time.zone.now } - let(:record) { FactoryBot.create(:lettings_log, :setup_completed, startdate: log_date) } - - before do - Timecop.freeze(log_date + 1) - end - - after do - Timecop.return - end + let(:startdate) { Time.zone.now } + let(:record) { FactoryBot.build(:lettings_log, :setup_completed, startdate:) } describe "reasonable preference validations" do context "when reasonable preference is not given" do @@ -59,7 +51,7 @@ RSpec.describe Validations::HouseholdValidations do end context "when form year is before 2024" do - let(:log_date) { Time.zone.local(2024, 1, 1) } + let(:startdate) { Time.zone.local(2024, 1, 1) } it "does not validate the content of reasonother for phrases indicating homelessness" do record.reason = 20 @@ -70,7 +62,7 @@ RSpec.describe Validations::HouseholdValidations do end context "when form year is >= 2024" do - let(:log_date) { Time.zone.local(2024, 4, 1) } + let(:startdate) { Time.zone.local(2024, 4, 1) } context "when checking the content of reasonother" do it "validates that the reason doesn't match phrase indicating homelessness" do @@ -277,7 +269,7 @@ RSpec.describe Validations::HouseholdValidations do end describe "#validate_partner_count" do - let(:log_date) { Time.zone.local(2023, 4, 1) } + let(:startdate) { Time.zone.local(2023, 4, 1) } it "validates that only 1 partner exists" do record.relat2 = "P" @@ -300,17 +292,7 @@ RSpec.describe Validations::HouseholdValidations do describe "#validate_person_age_matches_relationship" do context "with 2023 logs" do - let(:log_date) { Time.zone.local(2023, 4, 1) } - - before do - Timecop.freeze(log_date) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - Singleton.__init__(FormHandler) - end + let(:startdate) { Time.zone.local(2023, 4, 1) } context "when the household contains a person under 16" do it "validates that person must be a child of the tenant" do @@ -334,17 +316,7 @@ RSpec.describe Validations::HouseholdValidations do end context "with 2024 logs" do - let(:log_date) { Time.zone.local(2024, 4, 1) } - - before do - Timecop.freeze(log_date) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - Singleton.__init__(FormHandler) - end + let(:startdate) { Time.zone.local(2024, 4, 1) } it "does not add an error is person under 16 is a partner" do record.age2 = 14 @@ -366,17 +338,7 @@ RSpec.describe Validations::HouseholdValidations do describe "#validate_person_age_matches_economic_status" do context "with 2023 logs" do - let(:log_date) { Time.zone.local(2023, 4, 1) } - - before do - Timecop.freeze(log_date) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - Singleton.__init__(FormHandler) - end + let(:startdate) { Time.zone.local(2023, 4, 1) } context "when the household contains a person under 16" do it "validates that person's economic status must be Child" do @@ -411,17 +373,7 @@ RSpec.describe Validations::HouseholdValidations do end context "with 2024 logs" do - let(:log_date) { Time.zone.local(2024, 4, 1) } - - before do - Timecop.freeze(log_date) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - Singleton.__init__(FormHandler) - end + let(:startdate) { Time.zone.local(2024, 4, 1) } it "does not run the validation" do record.age2 = 14 @@ -437,17 +389,7 @@ RSpec.describe Validations::HouseholdValidations do describe "#validate_person_age_and_relationship_matches_economic_status" do context "with 2023 logs" do - let(:log_date) { Time.zone.local(2023, 4, 1) } - - before do - Timecop.freeze(log_date) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - Singleton.__init__(FormHandler) - end + let(:startdate) { Time.zone.local(2023, 4, 1) } context "when the household contains a tenant’s child between the ages of 16 and 19" do it "validates that person's economic status must be full time student or refused" do @@ -519,17 +461,7 @@ RSpec.describe Validations::HouseholdValidations do end context "with 2024 logs" do - let(:log_date) { Time.zone.local(2024, 4, 1) } - - before do - Timecop.freeze(log_date) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - Singleton.__init__(FormHandler) - end + let(:startdate) { Time.zone.local(2024, 4, 1) } context "when the household contains a tenant’s child between the ages of 16 and 19" do it "does not add an error" do diff --git a/spec/models/validations/sales/financial_validations_spec.rb b/spec/models/validations/sales/financial_validations_spec.rb index 316c2d175..6ae34c3df 100644 --- a/spec/models/validations/sales/financial_validations_spec.rb +++ b/spec/models/validations/sales/financial_validations_spec.rb @@ -6,7 +6,7 @@ RSpec.describe Validations::Sales::FinancialValidations do let(:validator_class) { Class.new { include Validations::Sales::FinancialValidations } } describe "income validations for shared ownership" do - let(:record) { FactoryBot.create(:sales_log, ownershipsch: 1) } + let(:record) { FactoryBot.build(:sales_log, ownershipsch: 1) } context "when buying in a non london borough" do before do @@ -150,7 +150,7 @@ RSpec.describe Validations::Sales::FinancialValidations do end describe "#validate_mortgage" do - let(:record) { FactoryBot.create(:sales_log) } + let(:record) { FactoryBot.build(:sales_log) } it "adds an error is the mortgage is zero" do record.mortgageused = 1 @@ -168,7 +168,7 @@ RSpec.describe Validations::Sales::FinancialValidations do end describe "#validate_percentage_bought_not_greater_than_percentage_owned" do - let(:record) { FactoryBot.create(:sales_log) } + let(:record) { FactoryBot.build(:sales_log) } it "does not add an error if the percentage bought is less than the percentage owned" do record.stairbought = 20 @@ -202,7 +202,7 @@ RSpec.describe Validations::Sales::FinancialValidations do end describe "#validate_percentage_bought_not_equal_percentage_owned" do - let(:record) { FactoryBot.create(:sales_log) } + let(:record) { FactoryBot.build(:sales_log) } context "with 24/25 logs" do before do @@ -249,7 +249,7 @@ RSpec.describe Validations::Sales::FinancialValidations do end describe "#validate_monthly_leasehold_charges" do - let(:record) { FactoryBot.create(:sales_log) } + let(:record) { FactoryBot.build(:sales_log) } it "does not add an error if monthly leasehold charges are positive" do record.mscharge = 2345 @@ -265,7 +265,7 @@ RSpec.describe Validations::Sales::FinancialValidations do end describe "#validate_percentage_bought_at_least_threshold" do - let(:record) { FactoryBot.create(:sales_log) } + let(:record) { FactoryBot.build(:sales_log) } it "adds an error to stairbought and type if the percentage bought is less than the threshold (which is 1% by default, but higher for some shared ownership types)" do record.stairbought = 9 @@ -307,7 +307,7 @@ RSpec.describe Validations::Sales::FinancialValidations do end describe "#validate_child_income" do - let(:record) { FactoryBot.create(:sales_log) } + let(:record) { FactoryBot.build(:sales_log) } context "when buyer 2 is not a child" do before do @@ -352,17 +352,10 @@ RSpec.describe Validations::Sales::FinancialValidations do end describe "#validate_equity_in_range_for_year_and_type" do - let(:record) { FactoryBot.create(:sales_log, saledate: now) } - - around do |example| - Timecop.freeze(now) do - example.run - end - Timecop.return - end + let(:record) { FactoryBot.build(:sales_log, saledate:) } context "with a log in the 22/23 collection year" do - let(:now) { Time.zone.local(2023, 1, 1) } + let(:saledate) { Time.zone.local(2023, 1, 1) } it "adds an error for type 2, equity below min with the correct percentage" do record.type = 2 @@ -397,7 +390,7 @@ RSpec.describe Validations::Sales::FinancialValidations do end context "with a log in 23/24 collection year" do - let(:now) { Time.zone.local(2024, 1, 1) } + let(:saledate) { Time.zone.local(2024, 1, 1) } it "adds an error for type 2, equity below min with the correct percentage" do record.type = 2 @@ -433,19 +426,10 @@ RSpec.describe Validations::Sales::FinancialValidations do end describe "#validate_equity_less_than_staircase_difference" do - let(:record) { FactoryBot.create(:sales_log, saledate: now) } - - around do |example| - Timecop.freeze(now) do - Singleton.__init__(FormHandler) - example.run - end - Timecop.return - Singleton.__init__(FormHandler) - end + let(:record) { FactoryBot.build(:sales_log, saledate:) } context "with a log in the 23/24 collection year" do - let(:now) { Time.zone.local(2023, 4, 1) } + let(:saledate) { Time.zone.local(2023, 4, 1) } it "does not add an error" do record.stairbought = 2 @@ -457,7 +441,7 @@ RSpec.describe Validations::Sales::FinancialValidations do end context "with a log in 24/25 collection year" do - let(:now) { Time.zone.local(2024, 4, 1) } + let(:saledate) { Time.zone.local(2024, 4, 1) } it "adds errors if equity is more than stairowned - stairbought for joint purchase" do record.stairbought = 2 diff --git a/spec/models/validations/sales/household_validations_spec.rb b/spec/models/validations/sales/household_validations_spec.rb index 89680fc1a..48ab425c5 100644 --- a/spec/models/validations/sales/household_validations_spec.rb +++ b/spec/models/validations/sales/household_validations_spec.rb @@ -4,8 +4,8 @@ RSpec.describe Validations::Sales::HouseholdValidations do subject(:household_validator) { validator_class.new } let(:validator_class) { Class.new { include Validations::Sales::HouseholdValidations } } - let(:record) { build(:sales_log, saledate: log_date) } - let(:log_date) { Time.zone.local(2023, 4, 1) } + let(:record) { build(:sales_log, saledate:) } + let(:saledate) { Time.zone.local(2023, 4, 1) } describe "#validate_partner_count" do it "validates that only 1 partner exists" do @@ -28,18 +28,8 @@ RSpec.describe Validations::Sales::HouseholdValidations do end describe "#validate_person_age_matches_relationship" do - before do - Timecop.freeze(log_date) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - Singleton.__init__(FormHandler) - end - context "with 2023 logs" do - let(:log_date) { Time.zone.local(2023, 4, 1) } + let(:saledate) { Time.zone.local(2023, 4, 1) } context "when the household contains a person under 16" do it "expects that person is a child of the tenant" do @@ -73,7 +63,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do end context "with 2024 logs" do - let(:log_date) { Time.zone.local(2024, 4, 1) } + let(:saledate) { Time.zone.local(2024, 4, 1) } it "does not add error if person under 16 is a partner" do record.age2 = 14 @@ -94,18 +84,8 @@ RSpec.describe Validations::Sales::HouseholdValidations do end describe "#validate_person_age_matches_economic_status" do - before do - Timecop.freeze(log_date) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - Singleton.__init__(FormHandler) - end - context "with 2023 logs" do - let(:log_date) { Time.zone.local(2023, 4, 1) } + let(:saledate) { Time.zone.local(2023, 4, 1) } it "validates that person's economic status must be Child" do record.age2 = 14 @@ -137,7 +117,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do end context "with 2024 logs" do - let(:log_date) { Time.zone.local(2024, 4, 1) } + let(:saledate) { Time.zone.local(2024, 4, 1) } it "does not run the validation" do record.age2 = 14 @@ -152,18 +132,8 @@ RSpec.describe Validations::Sales::HouseholdValidations do end describe "#validate_child_12_years_younger" do - before do - Timecop.freeze(log_date) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - Singleton.__init__(FormHandler) - end - context "with 2023 logs" do - let(:log_date) { Time.zone.local(2023, 4, 1) } + let(:saledate) { Time.zone.local(2023, 4, 1) } it "validates the child is at least 12 years younger than buyer 1" do record.age1 = 30 @@ -190,7 +160,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do end context "with 2024 logs" do - let(:log_date) { Time.zone.local(2024, 4, 1) } + let(:saledate) { Time.zone.local(2024, 4, 1) } it "does not validate that child is at least 12 year younger than buyer" do record.age1 = 20 @@ -205,18 +175,8 @@ RSpec.describe Validations::Sales::HouseholdValidations do end describe "#validate_person_age_and_relationship_matches_economic_status" do - before do - Timecop.freeze(log_date) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - Singleton.__init__(FormHandler) - end - context "with 2023 logs" do - let(:log_date) { Time.zone.local(2023, 4, 1) } + let(:saledate) { Time.zone.local(2023, 4, 1) } it "does not add an error for a person aged 16-19 who is a student but not a child of the buyer" do record.age2 = 18 @@ -266,7 +226,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do end context "with 2024 logs" do - let(:log_date) { Time.zone.local(2024, 4, 1) } + let(:saledate) { Time.zone.local(2024, 4, 1) } context "when the household contains a tenant’s child between the ages of 16 and 19" do it "does not add an error" do @@ -316,7 +276,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do end describe "validating fields about buyers living in the property" do - let(:sales_log) { FactoryBot.create(:sales_log, :outright_sale_setup_complete, saledate: log_date, noint: 1, companybuy: 2, buylivein:, jointpur:, jointmore:, buy1livein:) } + let(:sales_log) { FactoryBot.create(:sales_log, :outright_sale_setup_complete, saledate:, noint: 1, companybuy: 2, buylivein:, jointpur:, jointmore:, buy1livein:) } context "when buyers will live in the property and the sale is a joint purchase" do let(:buylivein) { 1 } @@ -349,7 +309,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do end context "with 2023 logs" do - let(:log_date) { Time.zone.local(2023, 4, 1) } + let(:saledate) { Time.zone.local(2023, 4, 1) } it "triggers a validation if buyer two will also not live in the property" do sales_log.buy2livein = 2 @@ -377,21 +337,8 @@ RSpec.describe Validations::Sales::HouseholdValidations do end describe "#validate_buyer1_previous_tenure" do - let(:record) { build(:sales_log) } - - let(:now) { Time.zone.local(2024, 4, 4) } - - before do - Timecop.freeze(now) - Singleton.__init__(FormHandler) - record.ownershipsch = 2 - record.saledate = now - end - - after do - Timecop.return - Singleton.__init__(FormHandler) - end + let(:saledate) { Time.zone.local(2024, 4, 4) } + let(:record) { build(:sales_log, saledate:, ownershipsch: 2) } it "adds an error when previous tenure is not valid" do [3, 4, 5, 6, 7, 9, 0].each do |prevten| @@ -437,7 +384,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do end context "with 23/24 logs" do - let(:now) { Time.zone.local(2023, 4, 4) } + let(:saledate) { Time.zone.local(2023, 4, 4) } it "does not add an error for outright sale" do record.ownershipsch = 2 @@ -452,18 +399,8 @@ RSpec.describe Validations::Sales::HouseholdValidations do end describe "#validate_buyer_not_child" do - before do - Timecop.freeze(log_date) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - Singleton.__init__(FormHandler) - end - context "with 2023 logs" do - let(:log_date) { Time.zone.local(2023, 4, 1) } + let(:saledate) { Time.zone.local(2023, 4, 1) } it "does not add an error if either buyer is a child" do record.jointpur = 1 @@ -476,7 +413,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do end context "with 2024 logs" do - let(:log_date) { Time.zone.local(2024, 4, 1) } + let(:saledate) { Time.zone.local(2024, 4, 1) } it "validates buyer 1 isn't a child" do record.ecstat1 = 9 diff --git a/spec/models/validations/sales/sale_information_validations_spec.rb b/spec/models/validations/sales/sale_information_validations_spec.rb index 123122a0c..44245f73f 100644 --- a/spec/models/validations/sales/sale_information_validations_spec.rb +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -725,17 +725,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end describe "#validate_stairbought" do - let(:now) { Time.zone.local(2024, 4, 4) } - - before do - Timecop.freeze(now) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - Singleton.__init__(FormHandler) - end + let(:saledate) { Time.zone.local(2024, 4, 4) } [ ["Shared Ownership (new model lease)", 30, 90], @@ -748,7 +738,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do ["Older Persons Shared Ownership", 24, 50], ].each do |label, type, max| context "when ownership type is #{label}" do - let(:record) { build(:sales_log, ownershipsch: 1, type:, saledate: now) } + let(:record) { build(:sales_log, ownershipsch: 1, type:, saledate:) } it "does not add an error if stairbought is under #{max}%" do record.stairbought = max - 1 @@ -781,8 +771,8 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end end context "when the collection year is before 2024" do - let(:record) { build(:sales_log, ownershipsch: 1, type: 24, saledate: now, stairbought: 90) } - let(:now) { Time.zone.local(2023, 4, 4) } + let(:record) { build(:sales_log, ownershipsch: 1, type: 24, saledate:, stairbought: 90) } + let(:saledate) { Time.zone.local(2023, 4, 4) } it "does not add an error" do sale_information_validator.validate_stairbought(record) @@ -793,15 +783,8 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end describe "#validate_discount_and_value" do - let(:record) { FactoryBot.build(:sales_log, value: 200_000, discount: 50, ownershipsch: 2, type: 9, saledate: now) } - let(:now) { Time.zone.local(2024, 4, 1) } - - around do |example| - Timecop.freeze(now) do - example.run - end - Timecop.return - end + let(:record) { FactoryBot.build(:sales_log, value: 200_000, discount: 50, ownershipsch: 2, type: 9, saledate:) } + let(:saledate) { Time.zone.local(2024, 4, 1) } context "with a log in the 24/25 collection year" do context "when in London" do @@ -870,19 +853,10 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end describe "#validate_non_staircasing_mortgage" do - let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, deposit: 5_000, value: 30_000, equity: 28, ownershipsch: 1, type: 30, saledate: now) } - - around do |example| - Timecop.freeze(now) do - Singleton.__init__(FormHandler) - example.run - end - Timecop.return - Singleton.__init__(FormHandler) - end + let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, deposit: 5_000, value: 30_000, equity: 28, ownershipsch: 1, type: 30, saledate:) } context "with a log in the 24/25 collection year" do - let(:now) { Time.zone.local(2024, 4, 4) } + let(:saledate) { Time.zone.local(2024, 4, 4) } context "when MORTGAGE + DEPOSIT does not equal VALUE * EQUITY/100 " do context "and it is not a staircase transaction" do @@ -952,7 +926,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end context "when MORTGAGE + DEPOSIT equals VALUE * EQUITY/100" do - let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, staircase: 2, deposit: 5_000, value: 30_000, equity: 50, ownershipsch: 1, type: 30, saledate: now) } + let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, staircase: 2, deposit: 5_000, value: 30_000, equity: 50, ownershipsch: 1, type: 30, saledate:) } it "does not add an error" do sale_information_validator.validate_non_staircasing_mortgage(record) @@ -966,7 +940,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end context "when MORTGAGE + DEPOSIT is within 1£ tolerance of VALUE * EQUITY/100" do - let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, staircase: 2, deposit: 50_000, value: 120_001, equity: 50, ownershipsch: 1, type: 30, saledate: now) } + let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, staircase: 2, deposit: 50_000, value: 120_001, equity: 50, ownershipsch: 1, type: 30, saledate:) } it "does not add an error" do sale_information_validator.validate_non_staircasing_mortgage(record) @@ -1052,7 +1026,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end context "when DEPOSIT equals VALUE * EQUITY/100" do - let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 2, deposit: 15_000, value: 30_000, equity: 50, ownershipsch: 1, type: 30, saledate: now) } + let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 2, deposit: 15_000, value: 30_000, equity: 50, ownershipsch: 1, type: 30, saledate:) } it "does not add an error" do sale_information_validator.validate_non_staircasing_mortgage(record) @@ -1066,7 +1040,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end context "when DEPOSIT is within 1£ tolerance of VALUE * EQUITY/100" do - let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 2, deposit: 15_000, value: 30_001, equity: 50, ownershipsch: 1, type: 30, saledate: now) } + let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 2, deposit: 15_000, value: 30_001, equity: 50, ownershipsch: 1, type: 30, saledate:) } it "does not add an error" do sale_information_validator.validate_non_staircasing_mortgage(record) @@ -1082,8 +1056,8 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end context "when it is a 2023 log" do - let(:now) { Time.zone.local(2023, 4, 1) } - let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, staircase: 2, deposit: 5_000, value: 30_000, equity: 28, ownershipsch: 1, type: 30, saledate: now) } + let(:saledate) { Time.zone.local(2023, 4, 1) } + let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, staircase: 2, deposit: 5_000, value: 30_000, equity: 28, ownershipsch: 1, type: 30, saledate:) } it "does not add an error" do sale_information_validator.validate_non_staircasing_mortgage(record) @@ -1098,19 +1072,10 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end describe "#validate_staircasing_mortgage" do - let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, deposit: 5_000, value: 30_000, stairbought: 28, ownershipsch: 1, type: 30, saledate: now) } - - around do |example| - Timecop.freeze(now) do - Singleton.__init__(FormHandler) - example.run - end - Timecop.return - Singleton.__init__(FormHandler) - end + let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, deposit: 5_000, value: 30_000, stairbought: 28, ownershipsch: 1, type: 30, saledate:) } context "with a log in the 24/25 collection year" do - let(:now) { Time.zone.local(2024, 4, 4) } + let(:saledate) { Time.zone.local(2024, 4, 4) } context "when MORTGAGE + DEPOSIT does not equal STAIRBOUGHT/100 * VALUE" do context "and it is a staircase transaction" do @@ -1180,7 +1145,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end context "when MORTGAGE + DEPOSIT equals STAIRBOUGHT/100 * VALUE" do - let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, staircase: 1, deposit: 5_000, value: 30_000, stairbought: 50, ownershipsch: 1, type: 30, saledate: now) } + let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, staircase: 1, deposit: 5_000, value: 30_000, stairbought: 50, ownershipsch: 1, type: 30, saledate:) } it "does not add an error" do sale_information_validator.validate_staircasing_mortgage(record) @@ -1194,7 +1159,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end context "when MORTGAGE + DEPOSIT is within 1£ tolerance of STAIRBOUGHT/100 * VALUE" do - let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, staircase: 1, deposit: 5_000, value: 30_001, stairbought: 50, ownershipsch: 1, type: 30, saledate: now) } + let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, staircase: 1, deposit: 5_000, value: 30_001, stairbought: 50, ownershipsch: 1, type: 30, saledate:) } it "does not add an error" do sale_information_validator.validate_staircasing_mortgage(record) @@ -1209,8 +1174,8 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end context "when it is a 2023 log" do - let(:now) { Time.zone.local(2023, 4, 1) } - let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, staircase: 1, deposit: 5_000, value: 30_000, stairbought: 28, ownershipsch: 1, type: 30, saledate: now) } + let(:saledate) { Time.zone.local(2023, 4, 1) } + let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, staircase: 1, deposit: 5_000, value: 30_000, stairbought: 28, ownershipsch: 1, type: 30, saledate:) } it "does not add an error" do sale_information_validator.validate_staircasing_mortgage(record) @@ -1225,7 +1190,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do context "when mortgage is not used" do context "with a log in the 24/25 collection year" do - let(:now) { Time.zone.local(2024, 4, 4) } + let(:saledate) { Time.zone.local(2024, 4, 4) } before do record.mortgageused = 2 @@ -1299,7 +1264,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end context "when DEPOSIT equals STAIRBOUGHT/100 * VALUE" do - let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 1, deposit: 15_000, value: 30_000, stairbought: 50, ownershipsch: 1, type: 30, saledate: now) } + let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 1, deposit: 15_000, value: 30_000, stairbought: 50, ownershipsch: 1, type: 30, saledate:) } it "does not add an error" do sale_information_validator.validate_staircasing_mortgage(record) @@ -1313,7 +1278,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end context "when DEPOSIT is within 1£ tolerance of STAIRBOUGHT/100 * VALUE" do - let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 1, deposit: 15_000, value: 30_001, stairbought: 50, ownershipsch: 1, type: 30, saledate: now) } + let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 1, deposit: 15_000, value: 30_001, stairbought: 50, ownershipsch: 1, type: 30, saledate:) } it "does not add an error" do sale_information_validator.validate_staircasing_mortgage(record) @@ -1328,8 +1293,8 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end context "when it is a 2023 log" do - let(:now) { Time.zone.local(2023, 4, 1) } - let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 1, deposit: 5_000, value: 30_000, stairbought: 28, ownershipsch: 1, type: 30, saledate: now) } + let(:saledate) { Time.zone.local(2023, 4, 1) } + let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 1, deposit: 5_000, value: 30_000, stairbought: 28, ownershipsch: 1, type: 30, saledate:) } it "does not add an error" do sale_information_validator.validate_staircasing_mortgage(record) diff --git a/spec/models/validations/sales/setup_validations_spec.rb b/spec/models/validations/sales/setup_validations_spec.rb index 526024ae3..77c04f8c6 100644 --- a/spec/models/validations/sales/setup_validations_spec.rb +++ b/spec/models/validations/sales/setup_validations_spec.rb @@ -7,15 +7,6 @@ RSpec.describe Validations::Sales::SetupValidations do describe "#validate_saledate_collection_year" do context "with sales_in_crossover_period == false" do - before do - Timecop.freeze(Time.zone.local(2023, 1, 10)) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - end - context "when saledate is blank" do let(:record) { build(:sales_log, saledate: nil) } @@ -26,9 +17,13 @@ RSpec.describe Validations::Sales::SetupValidations do end end - context "when saledate is in the 22/23 collection year" do + context "when saledate is in the open collection year" do let(:record) { build(:sales_log, saledate: Time.zone.local(2023, 1, 1)) } + before do + allow(Time).to receive(:now).and_return(Time.zone.local(2023, 1, 10)) + end + it "does not add an error" do setup_validator.validate_saledate_collection_year(record) @@ -36,9 +31,13 @@ RSpec.describe Validations::Sales::SetupValidations do end end - context "when saledate is before the 22/23 collection year" do + context "when saledate is before the open collection year" do let(:record) { build(:sales_log, saledate: Time.zone.local(2020, 1, 1)) } + before do + allow(Time).to receive(:now).and_return(Time.zone.local(2023, 1, 10)) + end + it "adds error" do setup_validator.validate_saledate_collection_year(record) @@ -46,9 +45,13 @@ RSpec.describe Validations::Sales::SetupValidations do end end - context "when saledate is after the 22/23 collection year" do + context "when saledate is after the open collection year" do let(:record) { build(:sales_log, saledate: Time.zone.local(2025, 4, 1)) } + before do + allow(Time).to receive(:now).and_return(Time.zone.local(2023, 1, 10)) + end + it "adds error" do setup_validator.validate_saledate_collection_year(record) @@ -58,14 +61,6 @@ RSpec.describe Validations::Sales::SetupValidations do end context "with sales_in_crossover_period == true" do - around do |example| - Timecop.freeze(Time.zone.local(2024, 5, 1)) do - Singleton.__init__(FormHandler) - example.run - end - Timecop.return - end - context "when saledate is blank" do let(:record) { build(:sales_log, saledate: nil) } @@ -191,12 +186,6 @@ RSpec.describe Validations::Sales::SetupValidations do let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1), available_from: Time.zone.local(2023, 2, 1), name: "Absorbing org") } let(:merged_organisation) { create(:organisation, name: "Merged org") } - around do |example| - Timecop.freeze(Time.zone.local(2023, 5, 1)) - example.run - Timecop.return - end - before do merged_organisation.update!(absorbing_organisation:, merge_date: Time.zone.local(2023, 2, 2)) end @@ -249,11 +238,8 @@ RSpec.describe Validations::Sales::SetupValidations do let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1), available_from: Time.zone.local(2023, 2, 1), name: "Absorbing org") } let(:merged_organisation) { create(:organisation, name: "Merged org") } - around do |example| - Timecop.freeze(Time.zone.local(2023, 5, 1)) + before do merged_organisation.update!(merge_date: Time.zone.local(2023, 2, 2), absorbing_organisation:) - example.run - Timecop.return end context "and owning organisation is no longer active" do diff --git a/spec/models/validations/setup_validations_spec.rb b/spec/models/validations/setup_validations_spec.rb index aca3eea7d..5e7705c6d 100644 --- a/spec/models/validations/setup_validations_spec.rb +++ b/spec/models/validations/setup_validations_spec.rb @@ -143,7 +143,7 @@ RSpec.describe Validations::SetupValidations do context "when attempted startdate is more than 14 days from the current date" do before do - Timecop.freeze(2024, 3, 1) + allow(Time).to receive(:now).and_return(Time.zone.local(2024, 3, 1)) end it "adds an error to startdate" do @@ -164,18 +164,13 @@ RSpec.describe Validations::SetupValidations do end context "when organisations were merged" do - around do |example| - Timecop.freeze(Time.zone.local(2023, 5, 1)) - example.run - Timecop.return - end - let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 1, 30, 4, 5, 6), available_from: Time.zone.local(2023, 2, 1, 4, 5, 6), name: "Absorbing org") } let(:absorbing_organisation_2) { create(:organisation, created_at: Time.zone.local(2023, 1, 30), available_from: Time.zone.local(2023, 2, 1), name: "Absorbing org 2") } let(:merged_organisation) { create(:organisation, name: "Merged org") } let(:merged_organisation_2) { create(:organisation, name: "Merged org 2") } before do + allow(Time).to receive(:now).and_return(Time.zone.local(2023, 5, 1)) merged_organisation.update!(absorbing_organisation:, merge_date: Time.zone.local(2023, 2, 2)) merged_organisation_2.update!(absorbing_organisation:, merge_date: Time.zone.local(2023, 2, 2)) end @@ -431,9 +426,8 @@ RSpec.describe Validations::SetupValidations do before do create(:location, scheme:) - Timecop.freeze(Time.zone.local(2023, 11, 10)) - create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), scheme:) - Timecop.return + scheme_deactivation_period = build(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), scheme:) + scheme_deactivation_period.save!(validate: false) scheme.reload end @@ -461,9 +455,8 @@ RSpec.describe Validations::SetupValidations do before do create(:location, scheme:) - Timecop.freeze(Time.zone.local(2023, 11, 10)) - create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:) - Timecop.return + scheme_deactivation_period = build(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:) + scheme_deactivation_period.save!(validate: false) scheme.reload end @@ -491,11 +484,12 @@ RSpec.describe Validations::SetupValidations do before do create(:location, scheme:) - Timecop.freeze(Time.zone.local(2023, 11, 10)) - create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 1), reactivation_date: Time.zone.local(2022, 9, 4), scheme:) - create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:) - create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), scheme:) - Timecop.return + scheme_deactivation_period = build(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), scheme:) + scheme_deactivation_period_2 = build(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:) + scheme_deactivation_period_3 = build(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 1), reactivation_date: Time.zone.local(2022, 9, 4), scheme:) + scheme_deactivation_period.save!(validate: false) + scheme_deactivation_period_2.save!(validate: false) + scheme_deactivation_period_3.save!(validate: false) scheme.reload end @@ -525,9 +519,8 @@ RSpec.describe Validations::SetupValidations do let(:location) { create(:location, scheme:) } before do - Timecop.freeze(Time.zone.local(2023, 11, 10)) - create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), location:) - Timecop.return + location_deactivation_period = build(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), location:) + location_deactivation_period.save!(validate: false) location.reload end @@ -555,9 +548,8 @@ RSpec.describe Validations::SetupValidations do let(:location) { create(:location, scheme:) } before do - Timecop.freeze(Time.zone.local(2023, 11, 10)) - create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:) - Timecop.return + location_deactivation_period = build(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:) + location_deactivation_period.save!(validate: false) location.reload end @@ -585,11 +577,12 @@ RSpec.describe Validations::SetupValidations do let(:location) { create(:location, scheme:) } before do - Timecop.freeze(Time.zone.local(2023, 11, 10)) - create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 1), reactivation_date: Time.zone.local(2022, 9, 4), location:) - create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:) - create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), location:) - Timecop.return + location_deactivation_period = build(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), location:) + location_deactivation_period_2 = build(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:) + location_deactivation_period_3 = build(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 1), reactivation_date: Time.zone.local(2022, 9, 4), location:) + location_deactivation_period.save!(validate: false) + location_deactivation_period_2.save!(validate: false) + location_deactivation_period_3.save!(validate: false) location.reload end @@ -719,11 +712,10 @@ RSpec.describe Validations::SetupValidations do let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1, 4, 5, 6), available_from: Time.zone.local(2023, 2, 1, 4, 5, 6), name: "Absorbing org") } let(:merged_organisation) { create(:organisation, name: "Merged org") } - around do |example| - Timecop.freeze(Time.zone.local(2023, 5, 1)) - merged_organisation.update!(merge_date: Time.zone.local(2023, 2, 2), absorbing_organisation:) - example.run - Timecop.return + before do + merged_organisation.merge_date = Time.zone.local(2023, 2, 2) + merged_organisation.absorbing_organisation = absorbing_organisation + merged_organisation.save!(validate: false) end context "and owning organisation is no longer active" do diff --git a/spec/models/validations/soft_validations_spec.rb b/spec/models/validations/soft_validations_spec.rb index 601c5640e..242ff4d1b 100644 --- a/spec/models/validations/soft_validations_spec.rb +++ b/spec/models/validations/soft_validations_spec.rb @@ -2,16 +2,7 @@ require "rails_helper" RSpec.describe Validations::SoftValidations do let(:organisation) { FactoryBot.create(:organisation, provider_type: "PRP") } - let(:record) { FactoryBot.create(:lettings_log, owning_organisation: organisation) } - - before do - Timecop.freeze(Time.zone.local(2021, 10, 10)) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - end + let(:record) { FactoryBot.build(:lettings_log, owning_organisation: organisation) } describe "rent min max validations" do before do @@ -32,7 +23,7 @@ RSpec.describe Validations::SoftValidations do record.rent_type = 0 record.beds = 1 record.period = 1 - record.startdate = Time.zone.today + record.startdate = Time.zone.local(2021, 10, 10) end context "when validating soft min" do @@ -84,7 +75,8 @@ RSpec.describe Validations::SoftValidations do describe "retirement soft validations" do before do - record.update!(age1:, ecstat1:) + record.age1 = age1 + record.ecstat1 = ecstat1 end context "when the tenant is under the expected retirement age" do @@ -163,35 +155,57 @@ RSpec.describe Validations::SoftValidations do describe "pregnancy soft validations" do context "when there are no female tenants" do it "shows the interruption screen" do - record.update!(age1: 43, sex1: "M", preg_occ: 1, hhmemb: 1, age1_known: 0) + record.age1 = 43 + record.sex1 = "M" + record.preg_occ = 1 + record.hhmemb = 1 + record.age1_known = 0 expect(record.no_females_in_a_pregnant_household?).to be true end end context "when there are no female tenants and age of other tenants is unknown" do it "shows the interruption screen" do - record.update!(sex1: "M", preg_occ: 1, hhmemb: 1, age1_known: 1) + record.sex1 = "M" + record.preg_occ = 1 + record.hhmemb = 1 + record.age1_known = 1 expect(record.no_females_in_a_pregnant_household?).to be true end end context "when female tenants are under 16" do it "shows the interruption screen" do - record.update!(age2: 14, sex2: "F", preg_occ: 1, hhmemb: 2, details_known_2: 0, age2_known: 0, age1: 18, sex1: "M", age1_known: 0) + record.age2 = 14 + record.sex2 = "F" + record.preg_occ = 1 + record.hhmemb = 2 + record.details_known_2 = 0 + record.age2_known = 0 + record.age1 = 18 + record.sex1 = "M" + record.age1_known = 0 expect(record.female_in_pregnant_household_in_soft_validation_range?).to be true end end context "when female tenants are over 50" do it "shows the interruption screen" do - record.update!(age1: 54, sex1: "F", preg_occ: 1, hhmemb: 1, age1_known: 0) + record.age1 = 54 + record.sex1 = "F" + record.preg_occ = 1 + record.hhmemb = 1 + record.age1_known = 0 expect(record.female_in_pregnant_household_in_soft_validation_range?).to be true end end context "when female tenants are outside of soft validation ranges" do it "does not show the interruption screen" do - record.update!(age1: 44, sex1: "F", preg_occ: 1, hhmemb: 1) + record.age1 = 44 + record.sex1 = "F" + record.preg_occ = 1 + record.hhmemb = 1 expect(record.no_females_in_a_pregnant_household?).to be false expect(record.female_in_pregnant_household_in_soft_validation_range?).to be false end @@ -199,7 +213,8 @@ RSpec.describe Validations::SoftValidations do context "when the information about the tenants is not given" do it "does not show the interruption screen" do - record.update!(preg_occ: 1, hhmemb: 2) + record.preg_occ = 1 + record.hhmemb = 2 expect(record.no_females_in_a_pregnant_household?).to be false expect(record.female_in_pregnant_household_in_soft_validation_range?).to be false end @@ -207,48 +222,36 @@ RSpec.describe Validations::SoftValidations do end describe "major repairs date soft validations" do - before do - Timecop.freeze(Time.zone.local(2022, 2, 1)) - end - - after do - Timecop.unfreeze - end - context "when the major repairs date is within 10 years of the tenancy start date" do it "shows the interruption screen" do - record.update!(startdate: Time.zone.local(2022, 2, 1), mrcdate: Time.zone.local(2013, 2, 1)) + record.startdate = Time.zone.local(2022, 2, 1) + record.mrcdate = Time.zone.local(2013, 2, 1) expect(record.major_repairs_date_in_soft_range?).to be true end end context "when the major repairs date is less than 2 years before the tenancy start date" do it "does not show the interruption screen" do - record.update!(startdate: Time.zone.local(2022, 2, 1), mrcdate: Time.zone.local(2021, 2, 1)) + record.startdate = Time.zone.local(2022, 2, 1) + record.mrcdate = Time.zone.local(2021, 2, 1) expect(record.major_repairs_date_in_soft_range?).to be false end end end describe "void date soft validations" do - before do - Timecop.freeze(Time.zone.local(2022, 2, 1)) - end - - after do - Timecop.unfreeze - end - context "when the void date is within 10 years of the tenancy start date" do it "shows the interruption screen" do - record.update!(startdate: Time.zone.local(2022, 2, 1), voiddate: Time.zone.local(2013, 2, 1)) + record.startdate = Time.zone.local(2022, 2, 1) + record.voiddate = Time.zone.local(2013, 2, 1) expect(record.voiddate_in_soft_range?).to be true end end context "when the void date is less than 2 years before the tenancy start date" do it "does not show the interruption screen" do - record.update!(startdate: Time.zone.local(2022, 2, 1), voiddate: Time.zone.local(2021, 2, 1)) + record.startdate = Time.zone.local(2022, 2, 1) + record.voiddate = Time.zone.local(2021, 2, 1) expect(record.voiddate_in_soft_range?).to be false end end