|
| 1 | +require "rails_helper" |
| 2 | +require Rails.root.join("db/migrate/20250826223940_backfill_fulfilled_requests") |
| 3 | + |
| 4 | +RSpec.describe BackfillFulfilledRequests, type: :migration do |
| 5 | + let(:migration) { described_class.new } |
| 6 | + let(:organization) { create(:organization, :with_items) } |
| 7 | + let(:partner) { create(:partner, organization: organization) } |
| 8 | + |
| 9 | + it "updates started requests with complete distributions to fulfilled" do |
| 10 | + # Create a distribution with complete state |
| 11 | + distribution = create(:distribution, organization: organization, partner: partner, state: "complete") |
| 12 | + |
| 13 | + # Create a request with started status and link it to the distribution |
| 14 | + request = create(:request, :started, organization: organization, partner: partner, distribution: distribution) |
| 15 | + |
| 16 | + expect { |
| 17 | + migration.up |
| 18 | + }.to change { request.reload.status }.from("started").to("fulfilled") |
| 19 | + end |
| 20 | + |
| 21 | + it "does not change requests without complete distributions" do |
| 22 | + # Create a distribution with scheduled state (not complete) |
| 23 | + distribution = create(:distribution, organization: organization, partner: partner, state: "scheduled") |
| 24 | + |
| 25 | + # Create a request with started status and link it to the distribution |
| 26 | + request = create(:request, :started, organization: organization, partner: partner, distribution: distribution) |
| 27 | + |
| 28 | + expect { |
| 29 | + migration.up |
| 30 | + }.not_to change { request.reload.status } |
| 31 | + end |
| 32 | + |
| 33 | + it "does not change requests without any distribution" do |
| 34 | + # Create a request with started status but no distribution |
| 35 | + request = create(:request, :started, organization: organization, partner: partner, distribution: nil) |
| 36 | + |
| 37 | + expect { |
| 38 | + migration.up |
| 39 | + }.not_to change { request.reload.status } |
| 40 | + end |
| 41 | + |
| 42 | + it "does not change fulfilled requests even if they have complete distributions" do |
| 43 | + # Create a distribution with complete state |
| 44 | + distribution = create(:distribution, organization: organization, partner: partner, state: "complete") |
| 45 | + |
| 46 | + # Create a request that's already fulfilled |
| 47 | + request = create(:request, :fulfilled, organization: organization, partner: partner, distribution: distribution) |
| 48 | + |
| 49 | + expect { |
| 50 | + migration.up |
| 51 | + }.not_to change { request.reload.status } |
| 52 | + end |
| 53 | + |
| 54 | + it "does not change pending requests even if they have complete distributions" do |
| 55 | + # Create a distribution with complete state |
| 56 | + distribution = create(:distribution, organization: organization, partner: partner, state: "complete") |
| 57 | + |
| 58 | + # Create a request with pending status |
| 59 | + request = create(:request, :pending, organization: organization, partner: partner, distribution: distribution) |
| 60 | + |
| 61 | + expect { |
| 62 | + migration.up |
| 63 | + }.not_to change { request.reload.status } |
| 64 | + end |
| 65 | +end |
0 commit comments