diff --git a/spec/lib/dump_cleaner/cleaners/mysql_shell_dump_cleaner_spec.rb b/spec/lib/dump_cleaner/cleaners/mysql_shell_dump_cleaner_spec.rb new file mode 100644 index 0000000..fdef3a3 --- /dev/null +++ b/spec/lib/dump_cleaner/cleaners/mysql_shell_dump_cleaner_spec.rb @@ -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 diff --git a/spec/lib/dump_cleaner/cleaners/mysql_shell_table_cleaner_spec.rb b/spec/lib/dump_cleaner/cleaners/mysql_shell_table_cleaner_spec.rb index c1cca6f..454f256 100644 --- a/spec/lib/dump_cleaner/cleaners/mysql_shell_table_cleaner_spec.rb +++ b/spec/lib/dump_cleaner/cleaners/mysql_shell_table_cleaner_spec.rb @@ -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),