From 2ef0b0573cd357fdca5a96947332376a5b0628ae Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Wed, 22 May 2024 13:21:46 +0100 Subject: [PATCH] create rent periods in factories as default, with an option to skip. skip automatic creation in tests specifically related to rent periods --- spec/factories/organisation.rb | 12 ++++++++++++ spec/helpers/organisations_helper_spec.rb | 2 +- spec/models/organisation_spec.rb | 2 ++ .../validations/financial_validations_spec.rb | 7 ++++--- ...organisations_controller_rent_periods_spec.rb | 16 ++++++++++------ .../merge/merge_organisations_service_spec.rb | 6 ++++++ 6 files changed, 35 insertions(+), 10 deletions(-) diff --git a/spec/factories/organisation.rb b/spec/factories/organisation.rb index c65533b3d..6a0aac85f 100644 --- a/spec/factories/organisation.rb +++ b/spec/factories/organisation.rb @@ -14,6 +14,18 @@ FactoryBot.define do with_dsa { true } end + transient do + skip_rent_period_creation { false } + end + + after(:build) do |organisation, evaluator| + unless evaluator.skip_rent_period_creation + (1..11).each do |rent_period| + organisation.organisation_rent_periods << build(:organisation_rent_period, organisation:, rent_period:) + end + end + end + after(:create) do |org, evaluator| if evaluator.with_dsa create( diff --git a/spec/helpers/organisations_helper_spec.rb b/spec/helpers/organisations_helper_spec.rb index 9ebe12d1f..bdd774b18 100644 --- a/spec/helpers/organisations_helper_spec.rb +++ b/spec/helpers/organisations_helper_spec.rb @@ -3,7 +3,7 @@ require "rails_helper" RSpec.describe OrganisationsHelper do include TagHelper describe "display_organisation_attributes" do - let(:organisation) { create(:organisation) } + let(:organisation) { create(:organisation, skip_rent_period_creation: true) } it "has the correct values" do expect(display_organisation_attributes(organisation)).to eq( diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index 71359e251..11eb8eab1 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -109,6 +109,7 @@ RSpec.describe Organisation, type: :model do end context "with associated rent periods" do + let(:organisation) { create(:organisation, skip_rent_period_creation: true) } let(:period_1_label) { "Every minute" } let(:fake_rent_periods) do { @@ -242,6 +243,7 @@ RSpec.describe Organisation, type: :model do describe "scopes" do before do + described_class.destroy_all create(:organisation, name: "Joe Bloggs", active: false) create(:organisation, name: "Tom Smith", active: true) end diff --git a/spec/models/validations/financial_validations_spec.rb b/spec/models/validations/financial_validations_spec.rb index acd8ae388..5dd738f0d 100644 --- a/spec/models/validations/financial_validations_spec.rb +++ b/spec/models/validations/financial_validations_spec.rb @@ -139,12 +139,13 @@ RSpec.describe Validations::FinancialValidations do end describe "rent period validations" do - let(:user) { create(:user) } - let(:record) { create(:lettings_log, owning_organisation: user.organisation, managing_organisation: user.organisation, assigned_to: user) } + let(:organisation) { create(:organisation, skip_rent_period_creation: true) } + let(:user) { create(:user, organisation:) } + let(:record) { create(:lettings_log, owning_organisation: organisation, managing_organisation: organisation, assigned_to: user) } let(:used_period) { 2 } before do - create(:organisation_rent_period, organisation: user.organisation, rent_period: used_period) + create(:organisation_rent_period, organisation:, rent_period: used_period) record.period = period end diff --git a/spec/requests/OrganisationsController/organisations_controller_rent_periods_spec.rb b/spec/requests/OrganisationsController/organisations_controller_rent_periods_spec.rb index b69c8d6c5..390b747b8 100644 --- a/spec/requests/OrganisationsController/organisations_controller_rent_periods_spec.rb +++ b/spec/requests/OrganisationsController/organisations_controller_rent_periods_spec.rb @@ -44,8 +44,8 @@ RSpec.describe OrganisationsController, type: :request do end it "creates organisation rent periods with the correct rent period and organisation id" do - org = Organisation.find_by_name org_name - org_rent_periods = OrganisationRentPeriod.all + org = Organisation.includes(:organisation_rent_periods).find_by_name(org_name) + org_rent_periods = org.organisation_rent_periods expect(org_rent_periods.count).to be expected_rent_periods.count expect(org_rent_periods.map(&:rent_period)).to match_array expected_rent_periods expect(org_rent_periods.map(&:organisation_id)).to all be org.id @@ -53,7 +53,7 @@ RSpec.describe OrganisationsController, type: :request do end describe "#edit" do - let(:organisation) { create(:organisation) } + let(:organisation) { create(:organisation, skip_rent_period_creation: true) } let(:fake_rent_periods) do { "1" => { "value" => "Every minute" }, @@ -83,7 +83,7 @@ RSpec.describe OrganisationsController, type: :request do end describe "#update" do - let(:organisation) { create(:organisation) } + let(:organisation) { create(:organisation, skip_rent_period_creation: true) } let(:initially_checked_rent_period_id) { "1" } let(:initially_unchecked_rent_period_id) { "2" } let(:params) do @@ -101,13 +101,17 @@ RSpec.describe OrganisationsController, type: :request do end it "creates and destroys organisation rent periods as appropriate" do - rent_periods = OrganisationRentPeriod.all + rent_periods = Organisation.includes(:organisation_rent_periods) + .find(organisation.id) + .organisation_rent_periods expect(rent_periods.count).to be 1 expect(rent_periods.first.rent_period.to_s).to eq initially_checked_rent_period_id patch organisation_path(organisation, headers:, params:) - rent_periods = OrganisationRentPeriod.all + rent_periods = Organisation.includes(:organisation_rent_periods) + .find(organisation.id) + .organisation_rent_periods expect(rent_periods.count).to be 1 expect(rent_periods.first.rent_period.to_s).to eq initially_unchecked_rent_period_id end diff --git a/spec/services/merge/merge_organisations_service_spec.rb b/spec/services/merge/merge_organisations_service_spec.rb index 59b34e525..5c582a95f 100644 --- a/spec/services/merge/merge_organisations_service_spec.rb +++ b/spec/services/merge/merge_organisations_service_spec.rb @@ -75,6 +75,9 @@ RSpec.describe Merge::MergeOrganisationsService do end context "and merging organisation rent periods" do + let(:absorbing_organisation) { create(:organisation, holds_own_stock: false, name: "absorbing org", skip_rent_period_creation: true) } + let(:merging_organisation) { create(:organisation, holds_own_stock: true, name: "fake org", skip_rent_period_creation: true) } + before do OrganisationRentPeriod.create!(organisation: absorbing_organisation, rent_period: 1) OrganisationRentPeriod.create!(organisation: absorbing_organisation, rent_period: 3) @@ -1157,6 +1160,9 @@ RSpec.describe Merge::MergeOrganisationsService do end context "and merging organisation rent periods" do + let(:new_absorbing_organisation) { create(:organisation, :without_dpc, holds_own_stock: false, skip_rent_period_creation: true) } + let(:merging_organisation) { create(:organisation, holds_own_stock: true, name: "fake org", skip_rent_period_creation: true) } + before do OrganisationRentPeriod.create!(organisation: new_absorbing_organisation, rent_period: 1) OrganisationRentPeriod.create!(organisation: new_absorbing_organisation, rent_period: 3)