discourse/migrations/spec/lib/converters/base/step_tracker_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

162 lines
4.6 KiB
Ruby

# frozen_string_literal: true
RSpec.describe ::Migrations::Converters::Base::StepTracker do
subject(:tracker) { described_class.new }
before { allow(::Migrations::Database::IntermediateDB::LogEntry).to receive(:create!) }
describe "#initialize" do
it "starts at the correct values" do
stats = tracker.stats
expect(stats.progress).to eq(1)
expect(stats.warning_count).to eq(0)
expect(stats.error_count).to eq(0)
end
end
describe "#progress=" do
it "allows setting progress" do
tracker.progress = 10
expect(tracker.stats.progress).to eq(10)
end
end
describe "#stats" do
it "returns correct stats" do
expect(tracker.stats).to eq(
::Migrations::Converters::Base::StepStats.new(
progress: 1,
warning_count: 0,
error_count: 0,
),
)
tracker.progress = 5
2.times { tracker.log_warning("Foo") }
3.times { tracker.log_error("Foo") }
expect(tracker.stats).to eq(
::Migrations::Converters::Base::StepStats.new(
progress: 5,
warning_count: 2,
error_count: 3,
),
)
end
end
describe "#reset_stats!" do
it "correctly resets stats" do
tracker.progress = 5
2.times { tracker.log_warning("Foo") }
3.times { tracker.log_error("Foo") }
expect(tracker.stats).to eq(
::Migrations::Converters::Base::StepStats.new(
progress: 5,
warning_count: 2,
error_count: 3,
),
)
tracker.reset_stats!
expect(tracker.stats).to eq(
::Migrations::Converters::Base::StepStats.new(
progress: 1,
warning_count: 0,
error_count: 0,
),
)
end
end
describe "#log_info" do
it "logs an info message" do
tracker.log_info("Info message")
expect(::Migrations::Database::IntermediateDB::LogEntry).to have_received(:create!).with(
type: ::Migrations::Database::IntermediateDB::LogEntry::INFO,
message: "Info message",
exception: nil,
details: nil,
)
end
it "logs an info message with details" do
tracker.log_info("Info message", details: { key: "value" })
expect(::Migrations::Database::IntermediateDB::LogEntry).to have_received(:create!).with(
type: ::Migrations::Database::IntermediateDB::LogEntry::INFO,
message: "Info message",
exception: nil,
details: {
key: "value",
},
)
end
end
describe "#log_warning" do
it "logs a warning message and increments warning_count" do
expect { tracker.log_warning("Warning message") }.to change {
tracker.stats.warning_count
}.by(1)
expect(::Migrations::Database::IntermediateDB::LogEntry).to have_received(:create!).with(
type: ::Migrations::Database::IntermediateDB::LogEntry::WARNING,
message: "Warning message",
exception: nil,
details: nil,
)
end
it "logs a warning message with exception and details and increments warning_count" do
exception = StandardError.new("Warning exception")
expect {
tracker.log_warning("Warning message", exception: exception, details: { key: "value" })
}.to change { tracker.stats.warning_count }.by(1)
expect(::Migrations::Database::IntermediateDB::LogEntry).to have_received(:create!).with(
type: ::Migrations::Database::IntermediateDB::LogEntry::WARNING,
message: "Warning message",
exception: exception,
details: {
key: "value",
},
)
end
end
describe "#log_error" do
it "logs an error message and increments error_count" do
expect { tracker.log_error("Error message") }.to change { tracker.stats.error_count }.by(1)
expect(::Migrations::Database::IntermediateDB::LogEntry).to have_received(:create!).with(
type: ::Migrations::Database::IntermediateDB::LogEntry::ERROR,
message: "Error message",
exception: nil,
details: nil,
)
end
it "logs an error message with exception and details and increments error_count" do
exception = StandardError.new("Error exception")
expect {
tracker.log_error("Error message", exception: exception, details: { key: "value" })
}.to change { tracker.stats.error_count }.by(1)
expect(::Migrations::Database::IntermediateDB::LogEntry).to have_received(:create!).with(
type: ::Migrations::Database::IntermediateDB::LogEntry::ERROR,
message: "Error message",
exception: exception,
details: {
key: "value",
},
)
end
end
end