Browse Source

create rent periods in factories as default, with an option to skip. skip automatic creation in tests specifically related to rent periods

pull/2389/head
Arthur Campbell 2 years ago
parent
commit
2ef0b0573c
  1. 12
      spec/factories/organisation.rb
  2. 2
      spec/helpers/organisations_helper_spec.rb
  3. 2
      spec/models/organisation_spec.rb
  4. 7
      spec/models/validations/financial_validations_spec.rb
  5. 16
      spec/requests/OrganisationsController/organisations_controller_rent_periods_spec.rb
  6. 6
      spec/services/merge/merge_organisations_service_spec.rb

12
spec/factories/organisation.rb

@ -14,6 +14,18 @@ FactoryBot.define do
with_dsa { true } with_dsa { true }
end 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| after(:create) do |org, evaluator|
if evaluator.with_dsa if evaluator.with_dsa
create( create(

2
spec/helpers/organisations_helper_spec.rb

@ -3,7 +3,7 @@ require "rails_helper"
RSpec.describe OrganisationsHelper do RSpec.describe OrganisationsHelper do
include TagHelper include TagHelper
describe "display_organisation_attributes" do describe "display_organisation_attributes" do
let(:organisation) { create(:organisation) } let(:organisation) { create(:organisation, skip_rent_period_creation: true) }
it "has the correct values" do it "has the correct values" do
expect(display_organisation_attributes(organisation)).to eq( expect(display_organisation_attributes(organisation)).to eq(

2
spec/models/organisation_spec.rb

@ -109,6 +109,7 @@ RSpec.describe Organisation, type: :model do
end end
context "with associated rent periods" do context "with associated rent periods" do
let(:organisation) { create(:organisation, skip_rent_period_creation: true) }
let(:period_1_label) { "Every minute" } let(:period_1_label) { "Every minute" }
let(:fake_rent_periods) do let(:fake_rent_periods) do
{ {
@ -242,6 +243,7 @@ RSpec.describe Organisation, type: :model do
describe "scopes" do describe "scopes" do
before do before do
described_class.destroy_all
create(:organisation, name: "Joe Bloggs", active: false) create(:organisation, name: "Joe Bloggs", active: false)
create(:organisation, name: "Tom Smith", active: true) create(:organisation, name: "Tom Smith", active: true)
end end

7
spec/models/validations/financial_validations_spec.rb

@ -139,12 +139,13 @@ RSpec.describe Validations::FinancialValidations do
end end
describe "rent period validations" do describe "rent period validations" do
let(:user) { create(:user) } let(:organisation) { create(:organisation, skip_rent_period_creation: true) }
let(:record) { create(:lettings_log, owning_organisation: user.organisation, managing_organisation: user.organisation, assigned_to: user) } let(:user) { create(:user, organisation:) }
let(:record) { create(:lettings_log, owning_organisation: organisation, managing_organisation: organisation, assigned_to: user) }
let(:used_period) { 2 } let(:used_period) { 2 }
before do 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 record.period = period
end end

16
spec/requests/OrganisationsController/organisations_controller_rent_periods_spec.rb

@ -44,8 +44,8 @@ RSpec.describe OrganisationsController, type: :request do
end end
it "creates organisation rent periods with the correct rent period and organisation id" do it "creates organisation rent periods with the correct rent period and organisation id" do
org = Organisation.find_by_name org_name org = Organisation.includes(:organisation_rent_periods).find_by_name(org_name)
org_rent_periods = OrganisationRentPeriod.all org_rent_periods = org.organisation_rent_periods
expect(org_rent_periods.count).to be expected_rent_periods.count 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(&:rent_period)).to match_array expected_rent_periods
expect(org_rent_periods.map(&:organisation_id)).to all be org.id expect(org_rent_periods.map(&:organisation_id)).to all be org.id
@ -53,7 +53,7 @@ RSpec.describe OrganisationsController, type: :request do
end end
describe "#edit" do describe "#edit" do
let(:organisation) { create(:organisation) } let(:organisation) { create(:organisation, skip_rent_period_creation: true) }
let(:fake_rent_periods) do let(:fake_rent_periods) do
{ {
"1" => { "value" => "Every minute" }, "1" => { "value" => "Every minute" },
@ -83,7 +83,7 @@ RSpec.describe OrganisationsController, type: :request do
end end
describe "#update" do 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_checked_rent_period_id) { "1" }
let(:initially_unchecked_rent_period_id) { "2" } let(:initially_unchecked_rent_period_id) { "2" }
let(:params) do let(:params) do
@ -101,13 +101,17 @@ RSpec.describe OrganisationsController, type: :request do
end end
it "creates and destroys organisation rent periods as appropriate" do 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.count).to be 1
expect(rent_periods.first.rent_period.to_s).to eq initially_checked_rent_period_id expect(rent_periods.first.rent_period.to_s).to eq initially_checked_rent_period_id
patch organisation_path(organisation, headers:, params:) 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.count).to be 1
expect(rent_periods.first.rent_period.to_s).to eq initially_unchecked_rent_period_id expect(rent_periods.first.rent_period.to_s).to eq initially_unchecked_rent_period_id
end end

6
spec/services/merge/merge_organisations_service_spec.rb

@ -75,6 +75,9 @@ RSpec.describe Merge::MergeOrganisationsService do
end end
context "and merging organisation rent periods" do 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 before do
OrganisationRentPeriod.create!(organisation: absorbing_organisation, rent_period: 1) OrganisationRentPeriod.create!(organisation: absorbing_organisation, rent_period: 1)
OrganisationRentPeriod.create!(organisation: absorbing_organisation, rent_period: 3) OrganisationRentPeriod.create!(organisation: absorbing_organisation, rent_period: 3)
@ -1157,6 +1160,9 @@ RSpec.describe Merge::MergeOrganisationsService do
end end
context "and merging organisation rent periods" do 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 before do
OrganisationRentPeriod.create!(organisation: new_absorbing_organisation, rent_period: 1) OrganisationRentPeriod.create!(organisation: new_absorbing_organisation, rent_period: 1)
OrganisationRentPeriod.create!(organisation: new_absorbing_organisation, rent_period: 3) OrganisationRentPeriod.create!(organisation: new_absorbing_organisation, rent_period: 3)

Loading…
Cancel
Save