diff --git a/spec/models/event_procedure_spec.rb b/spec/models/event_procedure_spec.rb index 49ece5f..3a20023 100644 --- a/spec/models/event_procedure_spec.rb +++ b/spec/models/event_procedure_spec.rb @@ -3,37 +3,12 @@ require "rails_helper" RSpec.describe EventProcedure do - describe "acts_as_paranoid" do + describe "soft delete behavior" do let(:user) { create(:user) } let(:patient) { create(:patient, user: user) } let(:event_procedure) { create(:event_procedure, user: user, patient: patient) } - it "soft deletes the record" do - event_procedure.destroy - - expect(event_procedure.deleted_at).to be_present - expect(described_class.with_deleted).to include(event_procedure) - end - - it "does not include the record in the default scope" do - event_procedure.destroy - - expect(described_class.all).not_to include(event_procedure) - end - - it "includes the record in the default scope when with_deleted is called" do - event_procedure.destroy - - expect(described_class.with_deleted).to include(event_procedure) - end - - it "restores a soft deleted record" do - event_procedure.destroy - event_procedure.recover! - - expect(event_procedure.deleted_at).to be_nil - expect(described_class.all).to include(event_procedure) - end + it_behaves_like "acts as paranoid", :event_procedure end describe "associations" do diff --git a/spec/models/health_insurance_spec.rb b/spec/models/health_insurance_spec.rb index 6028a4b..49c87e3 100644 --- a/spec/models/health_insurance_spec.rb +++ b/spec/models/health_insurance_spec.rb @@ -3,6 +3,13 @@ require "rails_helper" RSpec.describe HealthInsurance do + describe "soft delete behavior" do + let(:user) { create(:user) } + let(:health_insurance) { create(:health_insurance, user: user) } + + it_behaves_like "acts as paranoid", :health_insurance + end + describe "associations" do it { is_expected.to belong_to(:user).optional(true) } diff --git a/spec/models/hospital_spec.rb b/spec/models/hospital_spec.rb index 0af9d6d..268d2de 100644 --- a/spec/models/hospital_spec.rb +++ b/spec/models/hospital_spec.rb @@ -3,6 +3,12 @@ require "rails_helper" RSpec.describe Hospital do + describe "soft delete behavior" do + let(:hospital) { create(:hospital) } + + it_behaves_like "acts as paranoid", :hospital + end + describe "associations" do it { is_expected.to have_many(:event_procedures).dependent(:destroy) } end diff --git a/spec/models/medical_shift_spec.rb b/spec/models/medical_shift_spec.rb index efc2538..4421ebd 100644 --- a/spec/models/medical_shift_spec.rb +++ b/spec/models/medical_shift_spec.rb @@ -3,6 +3,13 @@ require "rails_helper" RSpec.describe MedicalShift do + describe "soft delete behavior" do + let(:user) { create(:user) } + let(:medical_shift) { create(:medical_shift, user: user) } + + it_behaves_like "acts as paranoid", :medical_shift + end + describe "associations" do it { is_expected.to belong_to(:user) } end diff --git a/spec/models/patient_spec.rb b/spec/models/patient_spec.rb index 92a1e7b..4925e37 100644 --- a/spec/models/patient_spec.rb +++ b/spec/models/patient_spec.rb @@ -3,6 +3,13 @@ require "rails_helper" RSpec.describe Patient do + describe "soft delete behavior" do + let(:user) { create(:user) } + let(:patient) { create(:patient, user: user) } + + it_behaves_like "acts as paranoid", :patient + end + describe "associations" do it { is_expected.to have_many(:event_procedures).dependent(:destroy) } it { is_expected.to belong_to(:user) } diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index c742ae0..3c32141 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -3,6 +3,13 @@ require "rails_helper" RSpec.describe Procedure do + describe "soft delete behavior" do + let(:user) { create(:user) } + let(:procedure) { create(:procedure, user: user) } + + it_behaves_like "acts as paranoid", :procedure + end + describe "associations" do it { is_expected.to belong_to(:user).optional(true) } diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 7f39f61..1dff491 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -5,6 +5,12 @@ RSpec.describe User do subject { build(:user) } + describe "soft delete behavior" do + let(:user) { create(:user) } + + it_behaves_like "acts as paranoid", :user + end + describe "associations" do it { is_expected.to have_many(:event_procedures).dependent(:destroy) } it { is_expected.to have_many(:medical_shifts).dependent(:destroy) } diff --git a/spec/support/shared_contexts/soft_delete.rb b/spec/support/shared_contexts/soft_delete.rb new file mode 100644 index 0000000..041a1d0 --- /dev/null +++ b/spec/support/shared_contexts/soft_delete.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +RSpec.shared_examples "acts as paranoid" do |record_name| + let(:record) { send(record_name) } + + before { record.destroy } + + it "soft deletes the record" do + expect(record.deleted_at).to be_present + expect(described_class.with_deleted).to include(record) + end + + it "does not include the record in the default scope" do + expect(described_class.all).not_to include(record) + end + + it "includes the record in the default scope when with_deleted is called" do + expect(described_class.with_deleted).to include(record) + end + + it "restores a soft deleted record" do + record.recover! + expect(record.deleted_at).to be_nil + expect(described_class.all).to include(record) + end +end