|
1 | 1 | require "rails_helper" |
2 | 2 |
|
3 | 3 | RSpec.describe BatchSubmissionsSelector do |
4 | | - let(:form_document_with_batch_enabled) { create(:v2_form_document, send_daily_submission_batch: true) } |
5 | | - let(:form_document_with_batch_disabled) { create(:v2_form_document, send_daily_submission_batch: false) } |
6 | | - |
7 | | - let(:date) { Time.zone.local(2022, 12, 1) } |
8 | 4 | let(:form_id) { 101 } |
9 | 5 |
|
10 | 6 | describe ".daily_batches" do |
11 | 7 | subject(:daily_batches) { described_class.daily_batches(date) } |
12 | 8 |
|
| 9 | + let(:date) { Time.zone.local(2022, 12, 1) } |
| 10 | + let(:form_document_with_batch_enabled) { create(:v2_form_document, send_daily_submission_batch: true) } |
| 11 | + let(:form_document_with_batch_disabled) { create(:v2_form_document, send_daily_submission_batch: false) } |
| 12 | + |
13 | 13 | it "returns an enumerator" do |
14 | 14 | expect(daily_batches).to be_an(Enumerator) |
15 | 15 | end |
16 | 16 |
|
17 | | - context "when send_daily_submissions_batch is enabled for the form document" do |
| 17 | + context "when send_daily_submission_batch is enabled for the form document" do |
18 | 18 | context "when the date is during BST" do |
19 | 19 | let(:date) { Time.zone.local(2022, 6, 1) } |
20 | 20 |
|
|
35 | 35 | create(:submission, form_id: form_id, mode: "form", reference: "OMITTED4", created_at: Time.utc(2022, 6, 1, 23, 0, 0), form_document: form_document_with_batch_enabled) |
36 | 36 | end |
37 | 37 |
|
38 | | - it "includes only forms/modes with submissions on the date and their submissions" do |
| 38 | + it "includes only forms/modes with submissions on the date" do |
39 | 39 | expect(daily_batches.map(&:to_h)).to contain_exactly( |
40 | 40 | a_hash_including(form_id: form_id, mode: "form"), |
41 | 41 | a_hash_including(form_id: form_id, mode: "preview-draft"), |
|
82 | 82 | end |
83 | 83 | end |
84 | 84 |
|
85 | | - context "when send_daily_submissions_batch is enabled part-way through the day for the form document" do |
86 | | - let(:other_form_id) { 102 } |
87 | | - |
| 85 | + context "when send_daily_submission_batch is enabled part-way through the day for the form document" do |
88 | 86 | let!(:latest_submission) do |
89 | 87 | create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED1", created_at: Time.utc(2022, 12, 1, 10, 0, 0), form_document: form_document_with_batch_enabled) |
90 | 88 | end |
|
104 | 102 | end |
105 | 103 | end |
106 | 104 |
|
107 | | - context "when send_daily_submissions_batch is disabled for the form document" do |
| 105 | + context "when send_daily_submission_batch is disabled for the form document" do |
108 | 106 | before do |
109 | 107 | create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2022, 12, 1, 10, 0, 0), form_document: form_document_with_batch_disabled) |
110 | 108 | end |
|
114 | 112 | end |
115 | 113 | end |
116 | 114 |
|
117 | | - context "when send_daily_submissions_batch is disabled part-way through the day for the form document" do |
| 115 | + context "when send_daily_submission_batch is disabled part-way through the day for the form document" do |
118 | 116 | before do |
119 | 117 | create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2022, 12, 1, 10, 0, 0), form_document: form_document_with_batch_disabled) |
120 | 118 | create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2022, 12, 1, 9, 0, 0), form_document: form_document_with_batch_enabled) |
|
125 | 123 | end |
126 | 124 | end |
127 | 125 | end |
| 126 | + |
| 127 | + describe ".weekly_batches" do |
| 128 | + subject(:weekly_batches) { described_class.weekly_batches(date) } |
| 129 | + |
| 130 | + let(:date) { Time.zone.local(2025, 5, 19) } |
| 131 | + let(:form_document_with_batch_enabled) { create(:v2_form_document, send_weekly_submission_batch: true) } |
| 132 | + let(:form_document_with_batch_disabled) { create(:v2_form_document, send_weekly_submission_batch: false) } |
| 133 | + |
| 134 | + it "returns an enumerator" do |
| 135 | + expect(weekly_batches).to be_an(Enumerator) |
| 136 | + end |
| 137 | + |
| 138 | + context "when send_weekly_submission_batch is enabled for the form document" do |
| 139 | + context "when the week is during BST" do |
| 140 | + let(:date) { Time.zone.local(2025, 5, 19) } |
| 141 | + |
| 142 | + let!(:form_submission) do |
| 143 | + create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED1", created_at: Time.utc(2025, 5, 18, 23, 0, 0), form_document: form_document_with_batch_enabled) |
| 144 | + end |
| 145 | + let!(:preview_draft_submission) do |
| 146 | + create(:submission, form_id: form_id, mode: "preview-draft", reference: "INCLUDED2", created_at: Time.utc(2025, 5, 25, 22, 59, 59), form_document: form_document_with_batch_enabled) |
| 147 | + end |
| 148 | + |
| 149 | + before do |
| 150 | + # create form/mode combinations that only have submissions outside the BST week |
| 151 | + create(:submission, form_id: form_id, mode: "preview-archived", reference: "OMITTED1", created_at: Time.utc(2025, 5, 18, 22, 59, 59), form_document: form_document_with_batch_enabled) |
| 152 | + create(:submission, form_id: 102, mode: "form", reference: "OMITTED2", created_at: Time.utc(2025, 5, 25, 23, 0, 0), form_document: form_document_with_batch_enabled) |
| 153 | + |
| 154 | + # create submissions for the form/mode included in a batch outside the BST week to ensure they are excluded |
| 155 | + create(:submission, form_id: form_id, mode: "form", reference: "OMITTED3", created_at: Time.utc(2025, 5, 18, 22, 59, 59), form_document: form_document_with_batch_enabled) |
| 156 | + create(:submission, form_id: form_id, mode: "form", reference: "OMITTED4", created_at: Time.utc(2025, 5, 25, 23, 0, 0), form_document: form_document_with_batch_enabled) |
| 157 | + end |
| 158 | + |
| 159 | + it "includes only forms/modes with submissions in the week" do |
| 160 | + expect(weekly_batches.map(&:to_h)).to contain_exactly( |
| 161 | + a_hash_including(form_id: form_id, mode: "form"), |
| 162 | + a_hash_including(form_id: form_id, mode: "preview-draft"), |
| 163 | + ) |
| 164 | + end |
| 165 | + |
| 166 | + it "includes only submissions in the week in the batches" do |
| 167 | + expect(weekly_batches.to_a[0].submissions.pluck(:reference)).to contain_exactly(form_submission.reference) |
| 168 | + expect(weekly_batches.to_a[1].submissions.pluck(:reference)).to contain_exactly(preview_draft_submission.reference) |
| 169 | + end |
| 170 | + end |
| 171 | + |
| 172 | + context "when the week is not in BST" do |
| 173 | + let(:date) { Time.zone.local(2025, 11, 3) } |
| 174 | + |
| 175 | + let!(:form_submission) do |
| 176 | + create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED1", created_at: Time.utc(2025, 11, 3, 0, 0, 0), form_document: form_document_with_batch_enabled) |
| 177 | + end |
| 178 | + let!(:preview_draft_submission) do |
| 179 | + create(:submission, form_id: form_id, mode: "preview-draft", reference: "INCLUDED2", created_at: Time.utc(2025, 11, 9, 23, 59, 59), form_document: form_document_with_batch_enabled) |
| 180 | + end |
| 181 | + |
| 182 | + before do |
| 183 | + # create form/mode combinations that only have submissions outside the week |
| 184 | + create(:submission, form_id: form_id, mode: "preview-archived", reference: "OMITTED1", created_at: Time.utc(2025, 11, 2, 23, 59, 59), form_document: form_document_with_batch_enabled) |
| 185 | + create(:submission, form_id: 102, mode: "form", reference: "OMITTED2", created_at: Time.utc(2025, 11, 10, 0, 0, 0), form_document: form_document_with_batch_enabled) |
| 186 | + |
| 187 | + # create submissions for the form/mode included in a batch outside the week to ensure they are excluded |
| 188 | + create(:submission, form_id: form_id, mode: "form", reference: "OMITTED3", created_at: Time.utc(2025, 11, 2, 23, 59, 59), form_document: form_document_with_batch_enabled) |
| 189 | + create(:submission, form_id: form_id, mode: "form", reference: "OMITTED4", created_at: Time.utc(2025, 11, 10, 0, 0, 0), form_document: form_document_with_batch_enabled) |
| 190 | + end |
| 191 | + |
| 192 | + it "includes only forms/modes with submissions in the week" do |
| 193 | + expect(weekly_batches.map(&:to_h)).to contain_exactly( |
| 194 | + a_hash_including(form_id: form_id, mode: "form"), |
| 195 | + a_hash_including(form_id: form_id, mode: "preview-draft"), |
| 196 | + ) |
| 197 | + end |
| 198 | + |
| 199 | + it "includes only submissions in the week in the batches" do |
| 200 | + expect(weekly_batches.to_a[0].submissions.pluck(:reference)).to contain_exactly(form_submission.reference) |
| 201 | + expect(weekly_batches.to_a[1].submissions.pluck(:reference)).to contain_exactly(preview_draft_submission.reference) |
| 202 | + end |
| 203 | + end |
| 204 | + end |
| 205 | + |
| 206 | + context "when send_weekly_submission_batch is enabled part-way through the week for the form document" do |
| 207 | + let(:date) { Time.zone.local(2025, 11, 3) } |
| 208 | + |
| 209 | + let!(:latest_submission) do |
| 210 | + create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED1", created_at: Time.utc(2025, 11, 5, 0, 0, 0), form_document: form_document_with_batch_enabled) |
| 211 | + end |
| 212 | + let!(:earlier_submission) do |
| 213 | + create(:submission, form_id: form_id, mode: "form", reference: "INCLUDED2", created_at: Time.utc(2025, 11, 4, 0, 0, 0), form_document: form_document_with_batch_disabled) |
| 214 | + end |
| 215 | + |
| 216 | + it "includes a batch for the form and mode" do |
| 217 | + expect(weekly_batches.map(&:to_h)).to contain_exactly( |
| 218 | + a_hash_including(form_id: form_id, mode: "form"), |
| 219 | + ) |
| 220 | + end |
| 221 | + |
| 222 | + it "includes all the submissions in the batch" do |
| 223 | + submissions = weekly_batches.first.submissions |
| 224 | + expect(submissions.pluck(:reference)).to contain_exactly(latest_submission.reference, earlier_submission.reference) |
| 225 | + end |
| 226 | + end |
| 227 | + |
| 228 | + context "when send_weekly_submission_batch is disabled for the form document" do |
| 229 | + let(:date) { Time.zone.local(2025, 11, 3) } |
| 230 | + |
| 231 | + before do |
| 232 | + create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2025, 11, 5, 10, 0, 0), form_document: form_document_with_batch_disabled) |
| 233 | + end |
| 234 | + |
| 235 | + it "does not include a batch for the form and mode" do |
| 236 | + expect(weekly_batches.to_a).to be_empty |
| 237 | + end |
| 238 | + end |
| 239 | + |
| 240 | + context "when send_weekly_submission_batch is disabled part-way through the week for the form document" do |
| 241 | + let(:date) { Time.zone.local(2025, 11, 3) } |
| 242 | + |
| 243 | + before do |
| 244 | + create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2025, 11, 5, 0, 0, 0), form_document: form_document_with_batch_disabled) |
| 245 | + create(:submission, form_id: form_id, mode: "form", created_at: Time.utc(2025, 11, 4, 0, 0, 0), form_document: form_document_with_batch_enabled) |
| 246 | + end |
| 247 | + |
| 248 | + it "does not include a batch for the form and mode" do |
| 249 | + expect(weekly_batches.to_a).to be_empty |
| 250 | + end |
| 251 | + end |
| 252 | + end |
128 | 253 | end |
0 commit comments