2024-09-09 23:14:39 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-11-20 06:54:37 +08:00
|
|
|
RSpec.describe ::Migrations::Converters::Base::StepTracker do
|
|
|
|
subject(:tracker) { described_class.new }
|
|
|
|
|
|
|
|
before { allow(::Migrations::Database::IntermediateDB::LogEntry).to receive(:create!) }
|
2024-09-09 23:14:39 +08:00
|
|
|
|
|
|
|
describe "#initialize" do
|
|
|
|
it "starts at the correct values" do
|
2024-11-20 06:54:37 +08:00
|
|
|
stats = tracker.stats
|
2024-09-09 23:14:39 +08:00
|
|
|
expect(stats.progress).to eq(1)
|
|
|
|
expect(stats.warning_count).to eq(0)
|
|
|
|
expect(stats.error_count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-11-20 06:54:37 +08:00
|
|
|
describe "#progress=" do
|
|
|
|
it "allows setting progress" do
|
|
|
|
tracker.progress = 10
|
|
|
|
expect(tracker.stats.progress).to eq(10)
|
2024-09-09 23:14:39 +08:00
|
|
|
end
|
2024-11-20 06:54:37 +08:00
|
|
|
end
|
2024-09-09 23:14:39 +08:00
|
|
|
|
2024-11-20 06:54:37 +08:00
|
|
|
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,
|
|
|
|
),
|
|
|
|
)
|
2024-09-09 23:14:39 +08:00
|
|
|
|
2024-11-20 06:54:37 +08:00
|
|
|
tracker.progress = 5
|
|
|
|
2.times { tracker.log_warning("Foo") }
|
|
|
|
3.times { tracker.log_error("Foo") }
|
2024-09-09 23:14:39 +08:00
|
|
|
|
2024-11-20 06:54:37 +08:00
|
|
|
expect(tracker.stats).to eq(
|
|
|
|
::Migrations::Converters::Base::StepStats.new(
|
|
|
|
progress: 5,
|
|
|
|
warning_count: 2,
|
|
|
|
error_count: 3,
|
|
|
|
),
|
|
|
|
)
|
2024-09-09 23:14:39 +08:00
|
|
|
end
|
2024-11-20 06:54:37 +08:00
|
|
|
end
|
2024-09-09 23:14:39 +08:00
|
|
|
|
2024-11-20 06:54:37 +08:00
|
|
|
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,
|
|
|
|
),
|
|
|
|
)
|
2024-09-09 23:14:39 +08:00
|
|
|
|
2024-11-20 06:54:37 +08:00
|
|
|
tracker.reset_stats!
|
2024-09-09 23:14:39 +08:00
|
|
|
|
2024-11-20 06:54:37 +08:00
|
|
|
expect(tracker.stats).to eq(
|
|
|
|
::Migrations::Converters::Base::StepStats.new(
|
|
|
|
progress: 1,
|
|
|
|
warning_count: 0,
|
|
|
|
error_count: 0,
|
|
|
|
),
|
|
|
|
)
|
2024-09-09 23:14:39 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#log_info" do
|
|
|
|
it "logs an info message" do
|
2024-11-20 06:54:37 +08:00
|
|
|
tracker.log_info("Info message")
|
2024-09-09 23:14:39 +08:00
|
|
|
|
|
|
|
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
|
2024-11-20 06:54:37 +08:00
|
|
|
tracker.log_info("Info message", details: { key: "value" })
|
2024-09-09 23:14:39 +08:00
|
|
|
|
|
|
|
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
|
2024-11-20 06:54:37 +08:00
|
|
|
expect { tracker.log_warning("Warning message") }.to change {
|
|
|
|
tracker.stats.warning_count
|
|
|
|
}.by(1)
|
2024-09-09 23:14:39 +08:00
|
|
|
|
|
|
|
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 {
|
2024-11-20 06:54:37 +08:00
|
|
|
tracker.log_warning("Warning message", exception: exception, details: { key: "value" })
|
|
|
|
}.to change { tracker.stats.warning_count }.by(1)
|
2024-09-09 23:14:39 +08:00
|
|
|
|
|
|
|
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
|
2024-11-20 06:54:37 +08:00
|
|
|
expect { tracker.log_error("Error message") }.to change { tracker.stats.error_count }.by(1)
|
2024-09-09 23:14:39 +08:00
|
|
|
|
|
|
|
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 {
|
2024-11-20 06:54:37 +08:00
|
|
|
tracker.log_error("Error message", exception: exception, details: { key: "value" })
|
|
|
|
}.to change { tracker.stats.error_count }.by(1)
|
2024-09-09 23:14:39 +08:00
|
|
|
|
|
|
|
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
|