Skip to content

Commit

Permalink
Add MysqlShellDumpCleaner specs
Browse files Browse the repository at this point in the history
  • Loading branch information
borama committed Jun 8, 2024
1 parent 632c135 commit 0f5889c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
69 changes: 69 additions & 0 deletions spec/lib/dump_cleaner/cleaners/mysql_shell_dump_cleaner_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true

require "spec_helper"
require "tempfile"
require "fileutils"

RSpec.describe DumpCleaner::Cleaners::MysqlShellDumpCleaner do
let(:config) do
instance_double(DumpCleaner::Config, cleanup_tables: [%w[db table1], %w[db table2]])
end

describe "#pre_cleanup" do
it "creates the destination dump directory" do
Dir.mktmpdir do |dir|
options = instance_double(DumpCleaner::Options, source_dump_path: "#{dir}/source_dump",
destination_dump_path: "#{dir}/dest_dump")
expect(Dir.exist?("#{dir}/dest_dump")).to be false

described_class.new(config:, options:).pre_cleanup
expect(Dir.exist?("#{dir}/dest_dump")).to be true
end
end
end

describe "#clean" do
it "calls the MysqlShellTableCleaner hooks for each table" do
options = instance_double(DumpCleaner::Options, source_dump_path: "source_dump",
destination_dump_path: "dest_dump")

cleaner1 = instance_double(DumpCleaner::Cleaners::MysqlShellTableCleaner)
expect(DumpCleaner::Cleaners::MysqlShellTableCleaner)
.to receive(:new).with(db: "db", table: "table1", config:, options:)
.and_return(cleaner1)
expect(cleaner1).to receive(:pre_cleanup)
expect(cleaner1).to receive(:clean)
expect(cleaner1).to receive(:post_cleanup)

cleaner2 = instance_double(DumpCleaner::Cleaners::MysqlShellTableCleaner)
expect(DumpCleaner::Cleaners::MysqlShellTableCleaner)
.to receive(:new).with(db: "db", table: "table2", config:, options:)
.and_return(cleaner2)
expect(cleaner2).to receive(:pre_cleanup)
expect(cleaner2).to receive(:clean)
expect(cleaner2).to receive(:post_cleanup)

described_class.new(config:, options:).clean
end
end

describe "#post_cleanup" do
it "copies all non-existent files to destination" do
Dir.mktmpdir do |dir|
options = instance_double(DumpCleaner::Options, source_dump_path: "#{dir}/source_dump",
destination_dump_path: "#{dir}/dest_dump")
Dir.mkdir("#{dir}/source_dump")
FileUtils.touch("#{dir}/source_dump/file1")
FileUtils.touch("#{dir}/source_dump/file2")
FileUtils.touch("#{dir}/source_dump/file3")

Dir.mkdir("#{dir}/dest_dump")
FileUtils.touch("#{dir}/dest_dump/file2")

described_class.new(config:, options:).post_cleanup
expect(File.exist?("#{dir}/dest_dump/file1")).to be true
expect(File.exist?("#{dir}/dest_dump/file3")).to be true
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
RSpec.describe DumpCleaner::Cleaners::MysqlShellTableCleaner do
let(:table_config) do
instance_double(DumpCleaner::Config::CleanupTableConfig,
db: "db",
table: "table",
columns: [
DumpCleaner::Config::CleanupTableColumnConfig.new(name: "name", cleanup_type: "name",
unique: false),
Expand Down

0 comments on commit 0f5889c

Please sign in to comment.