Browse Source

removed unrequired foreign keys

pull/863/head
Ted-U 3 years ago committed by baarkerlounger
parent
commit
187e839b97
  1. 4
      db/migrate/20220826093411_add_sales_log.rb
  2. 2
      db/schema.rb
  3. 33
      spec/features/organisation_spec.rb
  4. 29
      spec/models/organisation_spec.rb
  5. 29
      spec/models/user_spec.rb

4
db/migrate/20220826093411_add_sales_log.rb

@ -5,8 +5,8 @@ class AddSalesLog < ActiveRecord::Migration[7.0]
t.datetime :saledate
t.timestamps
t.references :owning_organisation, class_name: "Organisation", foreign_key: { to_table: :organisations, on_delete: :cascade }
t.references :managing_organisation, class_name: "Organisation", foreign_key: { to_table: :organisations }
t.references :created_by, class_name: "User", foreign_key: { to_table: :users }
t.references :managing_organisation, class_name: "Organisation"
t.references :created_by, class_name: "User"
end
end
end

2
db/schema.rb

@ -408,9 +408,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_09_02_082245) do
add_foreign_key "lettings_logs", "organisations", column: "owning_organisation_id", on_delete: :cascade
add_foreign_key "lettings_logs", "schemes"
add_foreign_key "locations", "schemes"
add_foreign_key "sales_logs", "organisations", column: "managing_organisation_id"
add_foreign_key "sales_logs", "organisations", column: "owning_organisation_id", on_delete: :cascade
add_foreign_key "sales_logs", "users", column: "created_by_id"
add_foreign_key "schemes", "organisations", column: "managing_organisation_id"
add_foreign_key "schemes", "organisations", column: "owning_organisation_id", on_delete: :cascade
add_foreign_key "users", "organisations", on_delete: :cascade

33
spec/features/organisation_spec.rb

@ -252,39 +252,6 @@ RSpec.describe "User Features" do
end
end
end
describe "delete cascade" do
context "when the organisation is deleted" do
let!(:organisation) { FactoryBot.create(:organisation) }
let!(:user) { FactoryBot.create(:user, :support, last_sign_in_at: Time.zone.now, organisation:) }
let!(:scheme_to_delete) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
let!(:log_to_delete) { FactoryBot.create(:lettings_log, owning_organisation: user.organisation) }
context "when organisation is deleted" do
it "child relationships ie logs, schemes and users are deleted too - application" do
organisation.destroy!
expect { organisation.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect { LettingsLog.find(log_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound)
expect { Scheme.find(scheme_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound)
expect { User.find(user.id) }.to raise_error(ActiveRecord::RecordNotFound)
end
context "when the organisation is deleted" do
let!(:organisation) { FactoryBot.create(:organisation) }
let!(:user) { FactoryBot.create(:user, :support, last_sign_in_at: Time.zone.now, organisation:) }
let!(:scheme_to_delete) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
let!(:log_to_delete) { FactoryBot.create(:lettings_log, :in_progress, needstype: 1, owning_organisation: user.organisation) }
it "child relationships ie logs, schemes and users are deleted too - database" do
ActiveRecord::Base.connection.exec_query("DELETE FROM organisations WHERE id = #{organisation.id};")
expect { LettingsLog.find(log_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound)
expect { Scheme.find(scheme_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound)
expect { User.find(user.id) }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
end
end
end
end
end

29
spec/models/organisation_spec.rb

@ -131,6 +131,35 @@ RSpec.describe Organisation, type: :model do
end
end
describe "delete cascade" do
context "when the organisation is deleted" do
let!(:organisation) { FactoryBot.create(:organisation) }
let!(:user) { FactoryBot.create(:user, :support, last_sign_in_at: Time.zone.now, organisation:) }
let!(:scheme_to_delete) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
let!(:log_to_delete) { FactoryBot.create(:lettings_log, owning_organisation: user.organisation) }
let!(:sales_log_to_delete) { FactoryBot.create(:sales_log, owning_organisation: user.organisation, managing_organisation: user.organisation) }
context "when organisation is deleted" do
it "child relationships ie logs, schemes and users are deleted too - application" do
organisation.destroy!
expect { organisation.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect { LettingsLog.find(log_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound)
expect { Scheme.find(scheme_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound)
expect { User.find(user.id) }.to raise_error(ActiveRecord::RecordNotFound)
expect { SalesLog.find(sales_log_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound)
end
it "child relationships ie logs, schemes and users are deleted too - database" do
ActiveRecord::Base.connection.exec_query("DELETE FROM organisations WHERE id = #{organisation.id};")
expect { LettingsLog.find(log_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound)
expect { Scheme.find(scheme_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound)
expect { User.find(user.id) }.to raise_error(ActiveRecord::RecordNotFound)
expect { SalesLog.find(sales_log_to_delete.id) }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
end
describe "scopes" do
before do
FactoryBot.create(:organisation, name: "Joe Bloggs")

29
spec/models/user_spec.rb

@ -272,7 +272,8 @@ RSpec.describe User, type: :model do
describe "delete" do
let(:user) { FactoryBot.create(:user) }
let!(:owned_lettings_log) do
before do
FactoryBot.create(
:lettings_log,
:completed,
@ -280,25 +281,25 @@ RSpec.describe User, type: :model do
managing_organisation: user.organisation,
created_by: user,
)
FactoryBot.create(
:sales_log,
owning_organisation: user.organisation,
created_by: user,
)
end
context "when the user is deleted" do
it "owned lettings logs are not deleted as a result" do
expect(described_class.count).to eq(1)
expect(LettingsLog.count).to eq(1)
user.destroy!
expect(described_class.count).to eq(0)
expect(LettingsLog.count).to eq(1)
expect { user.destroy! }
.to change(described_class, :count).from(1).to(0)
.and change(LettingsLog, :count).by(0)
end
it "owned lettings logs are not deleted as a result" do
expect(described_class.count).to eq(1)
expect(LettingsLog.count).to eq(1)
expect(Organisation.count).to eq(1)
user.organisation.destroy!
expect(described_class.count).to eq(0)
expect(Organisation.count).to eq(0)
expect(LettingsLog.count).to eq(0)
it "owned sales logs are not deleted as a result" do
expect { user.destroy! }
.to change(described_class, :count).from(1).to(0)
.and change(SalesLog, :count).by(0)
end
end
end

Loading…
Cancel
Save