discourse/migrations/spec/lib/converters/base/serial_job_spec.rb
Gerhard Schlager 5ac69076c1
REFACTOR: Simplify converter steps in migration tooling (#29779)
* Remove unused `report_progress_in_percent` option from step
* Remove `use_custom_progress_increment` option from the step because we can figure it out by looking at the progress
* Introduce `StepTracker` to for logging warnings and errors and tracking step progress
* Make it easier to log warnings and errors in all methods of `Step` without the need to pass around a `stats` object
2024-11-19 23:54:37 +01:00

49 lines
1.4 KiB
Ruby

# frozen_string_literal: true
RSpec.describe ::Migrations::Converters::Base::SerialJob do
subject(:job) { described_class.new(step) }
let(:step) { instance_double(::Migrations::Converters::Base::ProgressStep) }
let(:item) { "Item" }
let(:tracker) { instance_double(::Migrations::Converters::Base::StepTracker) }
let(:stats) { ::Migrations::Converters::Base::StepStats.new }
before do
allow(step).to receive(:tracker).and_return(tracker)
allow(tracker).to receive(:reset_stats!)
allow(tracker).to receive(:log_error)
allow(tracker).to receive(:stats).and_return(stats)
end
describe "#run" do
it "resets stats and processes item" do
allow(step).to receive(:process_item).and_return(stats)
result = job.run(item)
expect(result).to eq(stats)
expect(tracker).to have_received(:reset_stats!)
expect(step).to have_received(:process_item).with(item)
end
it "logs error if processing item raises an exception" do
allow(step).to receive(:process_item).and_raise(StandardError)
job.run(item)
expect(tracker).to have_received(:log_error).with(
"Failed to process item",
exception: an_instance_of(StandardError),
details: item,
)
end
end
describe "#cleanup" do
it "can be called without errors" do
expect { job.cleanup }.not_to raise_error
end
end
end