|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +describe "rake outbox:update_status_items" do |
| 4 | + subject(:task) { Rake::Task["outbox:update_status_items"] } |
| 5 | + |
| 6 | + let(:klass) { "OutboxItem" } |
| 7 | + let(:status) { 1 } |
| 8 | + let(:new_status) { 3 } |
| 9 | + |
| 10 | + let(:created_at_a) { 6.hours.ago } |
| 11 | + let(:created_at_b) { 8.hours.ago } |
| 12 | + let(:created_at_c) { 4.hours.ago } |
| 13 | + |
| 14 | + let!(:outbox_item_a) { create(:outbox_item, status: :failed, errors_count: 1, created_at: created_at_a) } |
| 15 | + let!(:outbox_item_b) { create(:outbox_item, status: :failed, errors_count: 1, created_at: created_at_b) } |
| 16 | + let!(:outbox_item_c) { create(:outbox_item, status: :delivered, errors_count: 0, created_at: created_at_c) } |
| 17 | + |
| 18 | + before do |
| 19 | + task.reenable |
| 20 | + allow(Rails.logger).to receive(:info) |
| 21 | + end |
| 22 | + |
| 23 | + context "when filtering records by status" do |
| 24 | + let(:created_at_a) { Time.zone.now } |
| 25 | + let(:created_at_b) { 6.hours.ago } |
| 26 | + let(:created_at_c) { Time.zone.now } |
| 27 | + |
| 28 | + it "updates records matching the given status" do |
| 29 | + expect { |
| 30 | + task.invoke(klass, status, new_status) |
| 31 | + outbox_item_a.reload |
| 32 | + outbox_item_b.reload |
| 33 | + outbox_item_c.reload |
| 34 | + }.to change(outbox_item_b, :status).from("failed").to("discarded") |
| 35 | + .and not_change { outbox_item_a.status } |
| 36 | + .and not_change { outbox_item_c.status } |
| 37 | + |
| 38 | + expect(Rails.logger).to have_received(:info).with(/Batch items updated: 1/) |
| 39 | + expect(Rails.logger).to have_received(:info).with(/Total items updated: 1/) |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + context "when filtering records by time range" do |
| 44 | + let(:start_time) { 7.hours.ago } |
| 45 | + let(:end_time) { 5.hours.ago } |
| 46 | + |
| 47 | + it "updates records within the specified time range" do |
| 48 | + expect { |
| 49 | + task.invoke(klass, status, new_status, start_time, end_time) |
| 50 | + outbox_item_a.reload |
| 51 | + outbox_item_b.reload |
| 52 | + outbox_item_c.reload |
| 53 | + }.to change(outbox_item_a, :status).from("failed").to("discarded") |
| 54 | + .and not_change { outbox_item_b.status } |
| 55 | + .and not_change { outbox_item_c.status } |
| 56 | + |
| 57 | + expect(Rails.logger).to have_received(:info).with(/Batch items updated: 1/) |
| 58 | + expect(Rails.logger).to have_received(:info).with(/Total items updated: 1/) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + context "when required parameters are missing" do |
| 63 | + it "raises an error" do |
| 64 | + expect { |
| 65 | + task.invoke(nil, status, new_status) |
| 66 | + }.to raise_error("Error: Class, current status, and new status must be specified. Example: rake outbox:update_status_items[OutboxItem,0,3]") |
| 67 | + |
| 68 | + expect(Rails.logger).not_to have_received(:info) |
| 69 | + end |
| 70 | + end |
| 71 | +end |
0 commit comments