Skip to content

Commit 4ffe25d

Browse files
committed
refactoring tests to validate match_patient_event_procedure_user
1 parent 109b3ae commit 4ffe25d

File tree

11 files changed

+53
-46
lines changed

11 files changed

+53
-46
lines changed

app/models/event_procedure.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ class EventProcedure < ApplicationRecord
3232
validates :urgency, inclusion: [true, false], if: -> { health_insurance? }
3333
validates :payment, presence: true
3434

35-
validate :unique_user
35+
validate :match_patient_event_procedure_user
3636

3737
private
3838

39-
def unique_user
40-
return unless patient.present? && user.present?
41-
return if user == patient.user
39+
def match_patient_event_procedure_user
40+
return if patient.nil? || user.nil?
41+
42+
return unless patient.user_id != user_id
4243

4344
errors.add(:base, "The patient must be associated with the same procedure user")
4445
end

spec/factories/event_procedures.rb

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
cbhpm
66
health_insurance
77
hospital
8-
patient
9-
procedure
108
user
9+
patient { build(:patient, user: user) }
10+
procedure
1111

1212
patient_service_number { "202312150001" }
1313
date { "2023-12-15 16:02:00" }
@@ -22,24 +22,19 @@
2222

2323
transient do
2424
health_insurance_attributes { nil }
25-
end
26-
27-
transient do
2825
patient_attributes { nil }
29-
end
30-
31-
transient do
3226
procedure_attributes { nil }
3327
end
3428

3529
after(:build) do |event_procedure, evaluator|
36-
event_procedure.patient = build(:patient, evaluator.patient_attributes) if evaluator.patient_attributes
30+
event_procedure.user ||= build(:user)
31+
unless event_procedure.patient
32+
event_procedure.patient = build(:patient, evaluator.patient_attributes || { user: event_procedure.user })
33+
end
3734
event_procedure.procedure = build(:procedure, evaluator.procedure_attributes) if evaluator.procedure_attributes
35+
3836
if evaluator.health_insurance_attributes
39-
event_procedure.health_insurance = build(
40-
:health_insurance,
41-
evaluator.health_insurance_attributes
42-
)
37+
event_procedure.health_insurance = build(:health_insurance, evaluator.health_insurance_attributes)
4338
end
4439
end
4540
end

spec/models/event_procedure_spec.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@
9797
context "when patient_attributes are provided" do
9898
it "creates patient" do
9999
event_procedure = build(
100-
:event_procedure, user: user,
100+
:event_procedure,
101+
user: user,
102+
patient: nil,
101103
patient_attributes: { name: "John Doe", user_id: user.id }
102104
)
103105

@@ -109,7 +111,11 @@
109111

110112
context "when patient_attributes are not provided" do
111113
it "does not create patient" do
112-
event_procedure = build(:event_procedure, patient_attributes: { id: nil, name: nil })
114+
event_procedure = build(
115+
:event_procedure,
116+
patient: nil,
117+
patient_attributes: { name: nil, user_id: nil }
118+
)
113119

114120
expect(event_procedure.save).to be_falsey
115121
expect(event_procedure.errors[:"patient.name"]).to include("can't be blank")

spec/models/patient_spec.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515
describe "deletable?" do
1616
subject { patient.deletable? }
1717

18-
let(:patient) { create(:patient, event_procedures:) }
18+
let(:user) { create(:user) }
19+
let(:patient) { create(:patient, user: user) }
1920

2021
context "when patient has no event_procedures" do
21-
let(:event_procedures) { [] }
22-
2322
it { is_expected.to be_truthy }
2423
end
2524

2625
context "when patient has event_procedures" do
27-
let(:event_procedures) { create_list(:event_procedure, 1) }
26+
before do
27+
create(:event_procedure, patient: patient, user: user)
28+
end
2829

2930
it { is_expected.to be_falsy }
3031
end

spec/operations/event_procedures/build_total_amount_cents_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
RSpec.describe EventProcedures::BuildTotalAmountCents, type: :operation do
66
describe ".result" do
77
it "returns a success" do
8+
user = create(:user)
89
cbhpm = create(:cbhpm)
910
procedure = create(:procedure)
1011
create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A")
1112
create(:port_value, cbhpm: cbhpm, anesthetic_port: "1A", amount_cents: 1000)
12-
event_procedure = create(:event_procedure, procedure: procedure, cbhpm: cbhpm)
13-
13+
patient = create(:patient, user: user)
14+
event_procedure = create(:event_procedure, user: user, procedure: procedure, cbhpm: cbhpm, patient: patient)
1415
expect(described_class.result(event_procedure: event_procedure)).to be_success
1516
end
1617

spec/operations/event_procedures/create_spec.rb

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
require "rails_helper"
44

55
RSpec.describe EventProcedures::Create, type: :operation do
6+
let(:user) { create(:user) }
7+
let(:patient) { create(:patient, user: user) }
8+
69
describe ".result" do
710
context "when params are valid" do
811
it "is successful" do
9-
user = create(:user)
1012
cbhpm = create(:cbhpm)
1113
procedure = create(:procedure)
1214
create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A")
@@ -19,7 +21,7 @@
1921
urgency: false,
2022
room_type: EventProcedures::RoomTypes::WARD,
2123
payment: EventProcedures::Payments::HEALTH_INSURANCE,
22-
patient_attributes: { id: create(:patient).id },
24+
patient_attributes: { id: patient.id, user_id: user.id },
2325
procedure_attributes: { id: procedure.id },
2426
health_insurance_attributes: { id: create(:health_insurance).id }
2527
}
@@ -30,7 +32,6 @@
3032
end
3133

3234
it "creates a new event_procedure" do
33-
user = create(:user)
3435
cbhpm = create(:cbhpm)
3536
procedure = create(:procedure)
3637
create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A")
@@ -43,7 +44,7 @@
4344
urgency: true,
4445
room_type: EventProcedures::RoomTypes::WARD,
4546
payment: EventProcedures::Payments::HEALTH_INSURANCE,
46-
patient_attributes: { id: create(:patient).id },
47+
patient_attributes: { id: patient.id, user_id: user.id },
4748
procedure_attributes: { id: procedure.id },
4849
health_insurance_attributes: { id: create(:health_insurance).id }
4950
}
@@ -68,7 +69,6 @@
6869

6970
context "when create a new patient" do
7071
it "creates and does not duplicate the creation" do
71-
user = create(:user)
7272
cbhpm = create(:cbhpm)
7373
procedure = create(:procedure)
7474
create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A")
@@ -94,7 +94,6 @@
9494
context "when create a new procedure" do
9595
context "when procedure attributes are valid" do
9696
it "does not duplicate the creation" do
97-
user = create(:user)
9897
cbhpm = create(:cbhpm)
9998
procedure = create(:procedure)
10099
create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A")
@@ -117,7 +116,7 @@
117116
urgency: true,
118117
room_type: EventProcedures::RoomTypes::WARD,
119118
payment: EventProcedures::Payments::HEALTH_INSURANCE,
120-
patient_attributes: { id: create(:patient).id },
119+
patient_attributes: { id: patient.id, user_id: user.id },
121120
procedure_attributes: procedure_attributes,
122121
health_insurance_attributes: { id: create(:health_insurance).id }
123122
}
@@ -163,7 +162,6 @@
163162
context "when create a new health_insurance" do
164163
context "when health_insurance attributes are valid" do
165164
it "does not duplicate the creation" do
166-
user = create(:user)
167165
cbhpm = create(:cbhpm)
168166
procedure = create(:procedure)
169167
create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A")
@@ -182,7 +180,7 @@
182180
urgency: nil,
183181
room_type: nil,
184182
payment: EventProcedures::Payments::OTHERS,
185-
patient_attributes: { id: create(:patient).id },
183+
patient_attributes: { id: patient.id, user_id: user.id },
186184
procedure_attributes: { id: procedure.id },
187185
health_insurance_attributes: health_insurance_attributes
188186
}
@@ -249,9 +247,7 @@
249247
end
250248

251249
it "returns errors" do
252-
user = create(:user)
253250
procedure = create(:procedure)
254-
patient = create(:patient)
255251
health_insurance = create(:health_insurance)
256252
attributes = {
257253
patient_attributes: { id: patient.id },

spec/operations/event_procedures/list_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
it "includes the associations" do # rubocop:disable RSpec/MultipleExpectations
2525
user = create(:user)
2626
procedure = create(:procedure, name: "Tireoidectomia")
27-
patient = create(:patient, name: "John Doe")
27+
patient = create(:patient, name: "John Doe", user: user)
2828
hospital = create(:hospital, name: "General Hospital")
2929
health_insurance = create(:health_insurance, name: "Insurance Corp")
3030
create_list(

spec/operations/event_procedures/total_amount_cents_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
describe ".result" do
77
it "is successful" do
88
user = create(:user)
9-
event_procedure = create(:event_procedure, user_id: user.id)
9+
patient = create(:patient, user: user)
10+
event_procedure = create(:event_procedure, user_id: user.id, patient: patient)
1011
expect(described_class.result(event_procedures: [event_procedure])).to be_success
1112
end
1213

spec/operations/event_procedures/update_spec.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,21 @@
2222

2323
context "when patient_attributes are provided" do
2424
it "update event_procedure association patient" do
25-
old_patient = create(:patient, name: "Old Patient")
26-
new_patient = create(:patient, name: "New Patient name")
25+
user = create(:user)
26+
old_patient = create(:patient, name: "Old Patient", user: user)
27+
new_patient = create(:patient, name: "New Patient name", user: user)
2728
cbhpm = create(:cbhpm)
2829
procedure = create(:procedure)
2930
create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A")
3031
create(:port_value, cbhpm: cbhpm, anesthetic_port: "1A", amount_cents: 1000)
31-
event_procedure = create(:event_procedure, procedure: procedure, cbhpm: cbhpm, patient: old_patient)
32+
event_procedure = create(
33+
:event_procedure, procedure: procedure, cbhpm: cbhpm, patient: old_patient,
34+
user: user
35+
)
3236
attributes = {
3337
urgency: true,
3438
room_type: EventProcedures::RoomTypes::WARD,
35-
patient_attributes: { id: new_patient.id, name: nil }
39+
patient_attributes: { id: new_patient.id, name: nil, user: user }
3640
}
3741
result = described_class.result(id: event_procedure.id.to_s, attributes: attributes)
3842

@@ -41,13 +45,13 @@
4145
end
4246

4347
it "creates a new patient and does not duplicate the creation" do
44-
patient = create(:patient, name: "Old Patient")
4548
user = create(:user)
49+
patient = create(:patient, name: "Old Patient", user: user)
4650
cbhpm = create(:cbhpm)
4751
procedure = create(:procedure)
4852
create(:cbhpm_procedure, procedure: procedure, cbhpm: cbhpm, anesthetic_port: "1A")
4953
create(:port_value, cbhpm: cbhpm, anesthetic_port: "1A", amount_cents: 1000)
50-
event_procedure = create(:event_procedure, procedure: procedure, cbhpm: cbhpm, patient: patient)
54+
event_procedure = create(:event_procedure, procedure: procedure, cbhpm: cbhpm, patient: patient, user: user)
5155
attributes = {
5256
urgency: true,
5357
room_type: EventProcedures::RoomTypes::WARD,

spec/pdfs/event_procedures_report_pdf_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
RSpec.describe EventProceduresReportPdf, type: :pdf do
66
it "generates a report with the correct content" do
77
user = create(:user)
8+
patient = create(:patient, user: user)
89
pdf = Prawn::Document.new
9-
event_procedures = create_list(:event_procedure, 3, user_id: user.id)
10+
event_procedures = create_list(:event_procedure, 3, user_id: user.id, patient: patient)
1011
amount = EventProcedures::TotalAmountCents.call(event_procedures: event_procedures)
1112

1213
described_class.new(

0 commit comments

Comments
 (0)