|
4 | 4 |
|
5 | 5 | RSpec.describe EventProcedure do |
6 | 6 | describe "acts_as_paranoid" do |
7 | | - it "soft deletes the record" do |
8 | | - event_procedure = create(:event_procedure) |
| 7 | + let(:user) { create(:user) } |
| 8 | + let(:patient) { create(:patient, user: user) } |
| 9 | + let(:event_procedure) { create(:event_procedure, user: user, patient: patient) } |
9 | 10 |
|
| 11 | + it "soft deletes the record" do |
10 | 12 | event_procedure.destroy |
11 | 13 |
|
12 | 14 | expect(event_procedure.deleted_at).to be_present |
13 | 15 | expect(described_class.with_deleted).to include(event_procedure) |
14 | 16 | end |
15 | 17 |
|
16 | 18 | it "does not include the record in the default scope" do |
17 | | - event_procedure = create(:event_procedure) |
18 | | - |
19 | 19 | event_procedure.destroy |
20 | 20 |
|
21 | 21 | expect(described_class.all).not_to include(event_procedure) |
22 | 22 | end |
23 | 23 |
|
24 | 24 | it "includes the record in the default scope when with_deleted is called" do |
25 | | - event_procedure = create(:event_procedure) |
26 | | - |
27 | 25 | event_procedure.destroy |
28 | 26 |
|
29 | 27 | expect(described_class.with_deleted).to include(event_procedure) |
30 | 28 | end |
31 | 29 |
|
32 | 30 | it "restores a soft deleted record" do |
33 | | - event_procedure = create(:event_procedure) |
34 | | - |
35 | 31 | event_procedure.destroy |
36 | 32 | event_procedure.recover! |
37 | 33 |
|
|
54 | 50 | it { is_expected.to validate_presence_of(:patient_service_number) } |
55 | 51 | it { is_expected.to validate_presence_of(:room_type) } |
56 | 52 | it { is_expected.to validate_presence_of(:payment) } |
| 53 | + |
| 54 | + context "when patient belongs to a different user" do |
| 55 | + it "is invalid when has different users" do |
| 56 | + user = create(:user) |
| 57 | + other_user = create(:user) |
| 58 | + patient = create(:patient, user: other_user) |
| 59 | + |
| 60 | + event_procedure = build(:event_procedure, user: user, patient: patient) |
| 61 | + |
| 62 | + expect(event_procedure).not_to be_valid |
| 63 | + expect(event_procedure.errors[:base]).to include("The patient must be associated with the same procedure user") |
| 64 | + end |
| 65 | + |
| 66 | + it "is valid when has equal user" do |
| 67 | + user = create(:user) |
| 68 | + patient = create(:patient, user: user) |
| 69 | + event_procedure = build(:event_procedure, user: user, patient: patient) |
| 70 | + |
| 71 | + expect(event_procedure).to be_valid |
| 72 | + end |
| 73 | + end |
57 | 74 | end |
58 | 75 |
|
59 | 76 | describe ".enumerations" do |
|
75 | 92 | end |
76 | 93 |
|
77 | 94 | describe "nested attributes for patient" do |
| 95 | + let(:user) { create(:user) } |
| 96 | + |
78 | 97 | context "when patient_attributes are provided" do |
79 | 98 | it "creates patient" do |
80 | | - event_procedure = build(:event_procedure, patient_attributes: { id: nil, name: "John Doe" }) |
| 99 | + event_procedure = build( |
| 100 | + :event_procedure, user: user, |
| 101 | + patient_attributes: { name: "John Doe", user_id: user.id } |
| 102 | + ) |
81 | 103 |
|
82 | 104 | expect { event_procedure.save }.to change(Patient, :count).by(1) |
83 | 105 | expect(event_procedure.patient).to be_persisted |
|
99 | 121 | context "when procedure_attributes are provided" do |
100 | 122 | it "creates procedure" do |
101 | 123 | user = create(:user) |
| 124 | + patient = create(:patient, user: user) |
102 | 125 | procedure_attributes = { |
103 | 126 | id: nil, |
104 | 127 | name: "procedure name", |
|
108 | 131 | custom: true, |
109 | 132 | user_id: user.id |
110 | 133 | } |
111 | | - event_procedure = build(:event_procedure, procedure_attributes: procedure_attributes) |
| 134 | + event_procedure = build( |
| 135 | + :event_procedure, user: user, patient: patient, |
| 136 | + procedure_attributes: procedure_attributes |
| 137 | + ) |
112 | 138 |
|
113 | 139 | expect { event_procedure.save }.to change(Procedure, :count).by(1) |
114 | 140 | expect(event_procedure.procedure).to be_persisted |
|
142 | 168 | context "when health_insurance_attributes are provided" do |
143 | 169 | it "creates health_insurance" do |
144 | 170 | user = create(:user) |
| 171 | + patient = create(:patient, user: user) |
145 | 172 | health_insurance_attributes = { |
146 | 173 | id: nil, |
147 | 174 | name: "health_insurance name", |
148 | 175 | custom: true, |
149 | 176 | user_id: user.id |
150 | 177 | } |
151 | | - event_procedure = build(:event_procedure, health_insurance_attributes: health_insurance_attributes) |
| 178 | + event_procedure = build( |
| 179 | + :event_procedure, user: user, patient: patient, |
| 180 | + health_insurance_attributes: health_insurance_attributes |
| 181 | + ) |
152 | 182 |
|
153 | 183 | expect { event_procedure.save }.to change(HealthInsurance, :count).by(1) |
154 | 184 | expect(event_procedure.health_insurance).to be_persisted |
|
0 commit comments