|
14 | 14 |
|
15 | 15 | before { allow(SearchVaccinationRecordsInNHSJob).to receive(:perform_bulk) }
|
16 | 16 |
|
17 |
| - let(:send_consent_requests_at) {} |
18 |
| - let(:days_before_consent_reminders) { 7 } |
19 |
| - let!(:session) do |
20 |
| - create( |
21 |
| - :session, |
22 |
| - programmes: [flu], |
23 |
| - academic_year: AcademicYear.pending, |
24 |
| - dates:, |
25 |
| - send_consent_requests_at:, |
26 |
| - days_before_consent_reminders:, |
27 |
| - team:, |
28 |
| - location:, |
29 |
| - patients: [patient] |
30 |
| - ) |
31 |
| - end |
| 17 | + context "with a normal school session" do |
| 18 | + let(:send_consent_requests_at) {} |
| 19 | + let(:days_before_consent_reminders) { 7 } |
| 20 | + let!(:session) do |
| 21 | + create( |
| 22 | + :session, |
| 23 | + programmes: [flu], |
| 24 | + academic_year: AcademicYear.pending, |
| 25 | + dates:, |
| 26 | + send_consent_requests_at:, |
| 27 | + days_before_consent_reminders:, |
| 28 | + team:, |
| 29 | + location:, |
| 30 | + patients: [patient] |
| 31 | + ) |
| 32 | + end |
32 | 33 |
|
33 |
| - context "with an specific, unscheduled session" do |
34 |
| - before { described_class.perform_now([session]) } |
| 34 | + context "with an specific, unscheduled session" do |
| 35 | + before { described_class.perform_now([session]) } |
35 | 36 |
|
36 |
| - let(:dates) { [] } |
37 |
| - let(:days_before_consent_reminders) { nil } |
| 37 | + let(:dates) { [] } |
| 38 | + let(:days_before_consent_reminders) { nil } |
38 | 39 |
|
39 |
| - it { should have_received(:perform_bulk).once.with([[patient.id]]) } |
40 |
| - end |
| 40 | + it { should have_received(:perform_bulk).once.with([[patient.id]]) } |
| 41 | + end |
41 | 42 |
|
42 |
| - context "session with dates in the future" do |
43 |
| - before { described_class.perform_now } |
| 43 | + context "session with dates in the future" do |
| 44 | + before { described_class.perform_now } |
44 | 45 |
|
45 |
| - let(:dates) { [7.days.from_now] } |
46 |
| - let(:send_consent_requests_at) { 14.days.ago } |
| 46 | + let(:dates) { [7.days.from_now] } |
| 47 | + let(:send_consent_requests_at) { 14.days.ago } |
47 | 48 |
|
48 |
| - it { should have_received(:perform_bulk).once.with([[patient.id]]) } |
| 49 | + it { should have_received(:perform_bulk).once.with([[patient.id]]) } |
49 | 50 |
|
50 |
| - context "community clinic session" do |
51 |
| - let(:location) { create(:community_clinic, team:, programmes: [flu]) } |
52 |
| - let(:school) { create(:school, team:, programmes: [flu]) } |
| 51 | + context "community clinic session" do |
| 52 | + let(:location) { create(:community_clinic, team:, programmes: [flu]) } |
| 53 | + let(:school) { create(:school, team:, programmes: [flu]) } |
53 | 54 |
|
54 |
| - it { should have_received(:perform_bulk).exactly(:once) } |
| 55 | + it { should have_received(:perform_bulk).exactly(:once) } |
| 56 | + end |
| 57 | + |
| 58 | + context "generic clinic session" do |
| 59 | + let(:location) { create(:generic_clinic, team:, programmes: [flu]) } |
| 60 | + let(:school) { create(:school, team:, programmes: [flu]) } |
| 61 | + |
| 62 | + it { should have_received(:perform_bulk).exactly(:once) } |
| 63 | + end |
55 | 64 | end
|
56 | 65 |
|
57 |
| - context "generic clinic session" do |
58 |
| - let(:location) { create(:generic_clinic, team:, programmes: [flu]) } |
59 |
| - let(:school) { create(:school, team:, programmes: [flu]) } |
| 66 | + context "session with dates in the past", |
| 67 | + within_academic_year: { |
| 68 | + from_start: 7.days |
| 69 | + } do |
| 70 | + before { described_class.perform_now } |
| 71 | + |
| 72 | + let(:dates) { [7.days.ago] } |
| 73 | + let(:send_consent_requests_at) { 30.days.ago } |
| 74 | + |
| 75 | + it { should_not have_received(:perform_bulk) } |
| 76 | + end |
| 77 | + |
| 78 | + context "session with dates in the past and the future", |
| 79 | + within_academic_year: { |
| 80 | + from_start: 7.days |
| 81 | + } do |
| 82 | + before { described_class.perform_now } |
| 83 | + |
| 84 | + let(:send_consent_requests_at) { 28.days.ago } |
| 85 | + let(:dates) { [7.days.ago, 7.days.from_now] } |
60 | 86 |
|
61 | 87 | it { should have_received(:perform_bulk).exactly(:once) }
|
62 | 88 | end
|
63 |
| - end |
64 | 89 |
|
65 |
| - context "session with dates in the past", |
66 |
| - within_academic_year: { |
67 |
| - from_start: 7.days |
68 |
| - } do |
69 |
| - before { described_class.perform_now } |
| 90 | + context "session with send_invitations_at in the future" do |
| 91 | + before { described_class.perform_now } |
70 | 92 |
|
71 |
| - let(:dates) { [7.days.ago] } |
72 |
| - let(:send_consent_requests_at) { 30.days.ago } |
| 93 | + let(:send_consent_requests_at) { 2.days.from_now } |
| 94 | + let(:dates) { [17.days.from_now] } |
| 95 | + |
| 96 | + it { should have_received(:perform_bulk).exactly(:once) } |
| 97 | + end |
| 98 | + |
| 99 | + context "session with send_consent_requests_at too far in the future" do |
| 100 | + let(:send_consent_requests_at) { 3.days.from_now } |
| 101 | + let(:dates) { [17.days.from_now] } |
73 | 102 |
|
74 | 103 | it { should_not have_received(:perform_bulk) }
|
| 104 | + end |
75 | 105 | end
|
76 | 106 |
|
77 |
| - context "session with dates in the past and the future", |
78 |
| - within_academic_year: { |
79 |
| - from_start: 7.days |
80 |
| - } do |
81 |
| - before { described_class.perform_now } |
| 107 | + shared_examples "notification date logic" do |notification_date_field| |
| 108 | + context "with notification date within threshold" do |
| 109 | + let!(:session) do |
| 110 | + session_attributes = { |
| 111 | + programmes: [flu], |
| 112 | + academic_year: AcademicYear.pending, |
| 113 | + dates:, |
| 114 | + team:, |
| 115 | + location:, |
| 116 | + patients: [patient] |
| 117 | + } |
| 118 | + |
| 119 | + # Set the appropriate notification date field |
| 120 | + if notification_date_field == :send_invitations_at |
| 121 | + session_attributes[:send_invitations_at] = 1.day.from_now |
| 122 | + session_attributes[:send_consent_requests_at] = nil |
| 123 | + else |
| 124 | + session_attributes[:send_invitations_at] = nil |
| 125 | + session_attributes[:send_consent_requests_at] = 1.day.from_now |
| 126 | + session_attributes[:days_before_consent_reminders] = 7 |
| 127 | + end |
| 128 | + |
| 129 | + create(:session, session_attributes) |
| 130 | + end |
82 | 131 |
|
83 |
| - let(:send_consent_requests_at) { 28.days.ago } |
84 |
| - let(:dates) { [7.days.ago, 7.days.from_now] } |
| 132 | + before { described_class.perform_now } |
85 | 133 |
|
86 |
| - it { should have_received(:perform_bulk).exactly(:once) } |
87 |
| - end |
| 134 | + it "includes the session" do |
| 135 | + expect(SearchVaccinationRecordsInNHSJob).to have_received(:perform_bulk).with( |
| 136 | + [patient.id].zip |
| 137 | + ) |
| 138 | + end |
| 139 | + end |
88 | 140 |
|
89 |
| - context "session with send_invitations_at in the future" do |
90 |
| - before { described_class.perform_now } |
| 141 | + context "with notification date too far in future" do |
| 142 | + let!(:session) do |
| 143 | + session_attributes = { |
| 144 | + programmes: [flu], |
| 145 | + academic_year: AcademicYear.pending, |
| 146 | + dates:, |
| 147 | + team:, |
| 148 | + location:, |
| 149 | + patients: [patient] |
| 150 | + } |
| 151 | + |
| 152 | + # Set the appropriate notification date field |
| 153 | + if notification_date_field == :send_invitations_at |
| 154 | + session_attributes[:send_invitations_at] = 3.days.from_now |
| 155 | + session_attributes[:send_consent_requests_at] = nil |
| 156 | + else |
| 157 | + session_attributes[:send_invitations_at] = nil |
| 158 | + session_attributes[:send_consent_requests_at] = 3.days.from_now |
| 159 | + session_attributes[:days_before_consent_reminders] = 7 |
| 160 | + end |
| 161 | + |
| 162 | + create(:session, session_attributes) |
| 163 | + end |
91 | 164 |
|
92 |
| - let(:send_consent_requests_at) { 2.days.from_now } |
93 |
| - let(:dates) { [17.days.from_now] } |
| 165 | + before { described_class.perform_now } |
94 | 166 |
|
95 |
| - it { should have_received(:perform_bulk).exactly(:once) } |
| 167 | + it "excludes the session" do |
| 168 | + expect(SearchVaccinationRecordsInNHSJob).not_to have_received(:perform_bulk) |
| 169 | + end |
| 170 | + end |
96 | 171 | end
|
97 | 172 |
|
98 |
| - context "session with send_consent_requests_at too far in the future" do |
99 |
| - let(:send_consent_requests_at) { 3.days.from_now } |
100 |
| - let(:dates) { [17.days.from_now] } |
| 173 | + context "testing notification date logic for different location types" do |
| 174 | + let(:dates) { [30.days.from_now] } |
101 | 175 |
|
102 |
| - it { should_not have_received(:perform_bulk) } |
| 176 | + context "generic clinic sessions" do |
| 177 | + let(:location) { create(:generic_clinic, team:, programmes: [flu]) } |
| 178 | + let(:school) { create(:school, team:, programmes: [flu]) } |
| 179 | + |
| 180 | + include_examples "notification date logic", :send_invitations_at |
| 181 | + end |
| 182 | + |
| 183 | + context "community clinic sessions" do |
| 184 | + let(:location) { create(:community_clinic, team:, programmes: [flu]) } |
| 185 | + let(:school) { create(:school, team:, programmes: [flu]) } |
| 186 | + |
| 187 | + include_examples "notification date logic", :send_consent_requests_at |
| 188 | + end |
| 189 | + |
| 190 | + context "school sessions" do |
| 191 | + let(:location) { create(:school, team:, programmes: [flu]) } |
| 192 | + let(:school) { location } |
| 193 | + |
| 194 | + include_examples "notification date logic", :send_consent_requests_at |
| 195 | + end |
| 196 | + |
| 197 | + context "mixed session types" do |
| 198 | + let(:generic_clinic) { create(:generic_clinic, team:, programmes: [flu]) } |
| 199 | + let(:community_clinic) { create(:community_clinic, team:, programmes: [flu]) } |
| 200 | + let(:school_location) { create(:school, team:, programmes: [flu]) } |
| 201 | + |
| 202 | + let!(:first_patient) { create(:patient, team:, school:) } |
| 203 | + let!(:second_patient) { create(:patient, team:, school:) } |
| 204 | + let!(:third_patient) { create(:patient, team:, school:) } |
| 205 | + |
| 206 | + let!(:generic_session) do |
| 207 | + create( |
| 208 | + :session, |
| 209 | + programmes: [flu], |
| 210 | + academic_year: AcademicYear.pending, |
| 211 | + dates:, |
| 212 | + send_invitations_at: 1.day.from_now, |
| 213 | + send_consent_requests_at: nil, |
| 214 | + days_before_consent_reminders: nil, |
| 215 | + team:, |
| 216 | + location: generic_clinic, |
| 217 | + patients: [first_patient] |
| 218 | + ) |
| 219 | + end |
| 220 | + |
| 221 | + let!(:community_session) do |
| 222 | + create( |
| 223 | + :session, |
| 224 | + programmes: [flu], |
| 225 | + academic_year: AcademicYear.pending, |
| 226 | + dates:, |
| 227 | + send_invitations_at: nil, |
| 228 | + send_consent_requests_at: 1.day.from_now, |
| 229 | + days_before_consent_reminders: 7, |
| 230 | + team:, |
| 231 | + location: community_clinic, |
| 232 | + patients: [second_patient] |
| 233 | + ) |
| 234 | + end |
| 235 | + |
| 236 | + let!(:school_session) do |
| 237 | + create( |
| 238 | + :session, |
| 239 | + programmes: [flu], |
| 240 | + academic_year: AcademicYear.pending, |
| 241 | + dates:, |
| 242 | + send_invitations_at: nil, |
| 243 | + send_consent_requests_at: 1.day.from_now, |
| 244 | + days_before_consent_reminders: 7, |
| 245 | + team:, |
| 246 | + location: school_location, |
| 247 | + patients: [third_patient] |
| 248 | + ) |
| 249 | + end |
| 250 | + |
| 251 | + before { described_class.perform_now } |
| 252 | + |
| 253 | + it "includes all sessions when their respective notification dates are within threshold" do |
| 254 | + expect(SearchVaccinationRecordsInNHSJob).to have_received(:perform_bulk).once |
| 255 | + |
| 256 | + expect(SearchVaccinationRecordsInNHSJob).to have_received(:perform_bulk).with( |
| 257 | + [first_patient.id, second_patient.id, third_patient.id].zip |
| 258 | + ) |
| 259 | + end |
| 260 | + end |
103 | 261 | end
|
104 | 262 | end
|
105 | 263 | end
|
0 commit comments