|
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 |
|
|
75 | 71 | expect(event).to be_valid |
76 | 72 | end |
77 | 73 | end |
| 74 | + |
| 75 | + context 'when patient belongs to a different user' do |
| 76 | + it 'is invalid when patient and event_procedure have different users' do |
| 77 | + user = create(:user) |
| 78 | + other_user = create(:user) |
| 79 | + patient = create(:patient, user: other_user) |
| 80 | + |
| 81 | + event_procedure = build(:event_procedure, user: user, patient: patient) |
| 82 | + |
| 83 | + expect(event_procedure).not_to be_valid |
| 84 | + expect(event_procedure.errors[:base]).to include("The patient must be associated with the same procedure user") |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + context 'when patient belongs to the same user' do |
| 89 | + it 'is valid when patient and event_procedure have the same user' do |
| 90 | + user = create(:user) |
| 91 | + patient = create(:patient, user: user) |
| 92 | + event_procedure = build(:event_procedure, user: user, patient: patient) |
| 93 | + |
| 94 | + expect(event_procedure).to be_valid |
| 95 | + end |
| 96 | + end |
78 | 97 | end |
79 | 98 |
|
80 | 99 | describe ".enumerations" do |
|
96 | 115 | end |
97 | 116 |
|
98 | 117 | describe "nested attributes for patient" do |
| 118 | + let(:user) { create(:user) } |
| 119 | + |
99 | 120 | context "when patient_attributes are provided" do |
100 | 121 | it "creates patient" do |
101 | | - event_procedure = build(:event_procedure, patient_attributes: { id: nil, name: "John Doe" }) |
| 122 | + event_procedure = build( |
| 123 | + :event_procedure, |
| 124 | + user: user, |
| 125 | + patient: nil, |
| 126 | + patient_attributes: { name: "John Doe", user_id: user.id } |
| 127 | + ) |
102 | 128 |
|
103 | 129 | expect { event_procedure.save }.to change(Patient, :count).by(1) |
104 | 130 | expect(event_procedure.patient).to be_persisted |
|
108 | 134 |
|
109 | 135 | context "when patient_attributes are not provided" do |
110 | 136 | it "does not create patient" do |
111 | | - event_procedure = build(:event_procedure, patient_attributes: { id: nil, name: nil }) |
| 137 | + event_procedure = build( |
| 138 | + :event_procedure, |
| 139 | + patient: nil, |
| 140 | + patient_attributes: { name: nil, user_id: nil } |
| 141 | + ) |
112 | 142 |
|
113 | 143 | expect(event_procedure.save).to be_falsey |
114 | 144 | expect(event_procedure.errors[:"patient.name"]).to include("can't be blank") |
|
120 | 150 | context "when procedure_attributes are provided" do |
121 | 151 | it "creates procedure" do |
122 | 152 | user = create(:user) |
| 153 | + patient = create(:patient, user: user) |
123 | 154 | procedure_attributes = { |
124 | 155 | id: nil, |
125 | 156 | name: "procedure name", |
|
129 | 160 | custom: true, |
130 | 161 | user_id: user.id |
131 | 162 | } |
132 | | - event_procedure = build(:event_procedure, procedure_attributes: procedure_attributes) |
| 163 | + event_procedure = build( |
| 164 | + :event_procedure, user: user, patient: patient, |
| 165 | + procedure_attributes: procedure_attributes |
| 166 | + ) |
133 | 167 |
|
134 | 168 | expect { event_procedure.save }.to change(Procedure, :count).by(1) |
135 | 169 | expect(event_procedure.procedure).to be_persisted |
|
163 | 197 | context "when health_insurance_attributes are provided" do |
164 | 198 | it "creates health_insurance" do |
165 | 199 | user = create(:user) |
| 200 | + patient = create(:patient, user: user) |
166 | 201 | health_insurance_attributes = { |
167 | 202 | id: nil, |
168 | 203 | name: "health_insurance name", |
169 | 204 | custom: true, |
170 | 205 | user_id: user.id |
171 | 206 | } |
172 | | - event_procedure = build(:event_procedure, health_insurance_attributes: health_insurance_attributes) |
| 207 | + event_procedure = build( |
| 208 | + :event_procedure, user: user, patient: patient, |
| 209 | + health_insurance_attributes: health_insurance_attributes |
| 210 | + ) |
173 | 211 |
|
174 | 212 | expect { event_procedure.save }.to change(HealthInsurance, :count).by(1) |
175 | 213 | expect(event_procedure.health_insurance).to be_persisted |
|
0 commit comments