From 05d3299aeca3db09a7fb5dc21f72ac9a32f163e7 Mon Sep 17 00:00:00 2001 From: Andrei Kaleshka Date: Wed, 1 May 2024 17:15:40 +0200 Subject: [PATCH] improve specs --- test/rake_task_test.rb | 192 +++++++++++-------------------------- test/support/test_utils.rb | 90 +++++++++++++++++ test/test_helper.rb | 1 + 3 files changed, 146 insertions(+), 137 deletions(-) create mode 100644 test/support/test_utils.rb diff --git a/test/rake_task_test.rb b/test/rake_task_test.rb index ced50fb..2311577 100644 --- a/test/rake_task_test.rb +++ b/test/rake_task_test.rb @@ -2,154 +2,72 @@ require "test_helper" -def app_file(path) - Rails.application.config.root.join(path) -end - -def remove_app_dir(name) - FileUtils.rm_rf(app_file(name)) -end - -def run_migrations - Rake::Task["db:migrate"].invoke - Rake::Task["db:migrate"].reenable - Rake::Task["db:rollback_branches"].reenable -end - -def run_sql(sql) - ActiveRecord::Base.connection.execute(sql) -end - -def applied_migrations - run_sql("select * from schema_migrations").map do |row| - row["version"] - end -end - -def clear_schema - run_sql("delete from schema_migrations") -end +describe "single db" do + let(:utils) { TestUtils.new } -def delete_migrations_files - Dir.glob(app_file("db/migrate/*.rb")).each do |file| - remove_app_dir(file) - end -end - -def define_migration_file(filename, content) - File.write(app_file("db/migrate/#{filename}"), content, mode: "w") -end - -def define_migrations - { - first: "20130906111511_first.rb", - second: "20130906111512_second.rb" - }.each do |key, file_name| - define_migration_file(file_name, <<~RUBY) - class #{key.to_s.camelize} < ActiveRecord::Migration[6.0] - def up - TestingState.up << :#{key} - end - - def down - TestingState.down << :#{key} - end - end - RUBY - end -end + before { utils.cleanup } -def prepare_phantom_migrations - run_migrations - delete_migrations_files # simulate switching branches -end - -def cleanup - delete_migrations_files - if ActiveRecord::SchemaMigration.respond_to?(:create_table) - ActiveRecord::SchemaMigration.create_table - else - ActiveRecord::SchemaMigration.new(ActiveRecord::Base.connection).create_table - end - run_sql("delete from schema_migrations") - remove_app_dir("tmp/migrated") - define_migrations - Rails.application.load_tasks - TestingState.reset -end - -def migrated_files - Dir.glob(app_file("tmp/migrated/*.rb")).map { |f| File.basename(f) }.sort -end - -describe "db:rollback_branches" do - before { cleanup } - - it "creates the tmp/migrated folder" do - refute File.exist?(app_file("tmp/migrated")) - run_migrations - assert File.exist?(app_file("tmp/migrated")) - end - - it "migrates the migrations" do - assert_empty applied_migrations - run_migrations - assert_equal %w[20130906111511 20130906111512], applied_migrations - end - - it "keeps migrated migrations in tmp/migrated folder" do - run_migrations - assert_equal %w[20130906111511_first.rb 20130906111512_second.rb], migrated_files - end - - it "rolls back the migrations in the reversed order" do - prepare_phantom_migrations - assert_empty TestingState.down - run_migrations - assert_equal %i[second first], TestingState.down - end + describe "db:rollback_branches" do + it "creates the tmp/migrated folder" do + refute File.exist?(utils.app_file("tmp/migrated")) + utils.run_migrations + assert File.exist?(utils.app_file("tmp/migrated")) + end - describe "with irreversible migration" do - before do - define_migration_file("20130906111513_irreversible.rb", <<~RUBY) - class Irreversible < ActiveRecord::Migration[6.0] - def up - TestingState.up << :irreversible - end + it "migrates the migrations" do + assert_empty utils.applied_migrations + utils.run_migrations + assert_equal %w[20130906111511 20130906111512], utils.applied_migrations + end - def down - raise ActiveRecord::IrreversibleMigration - end - end - RUBY + it "keeps migrated migrations in tmp/migrated folder" do + utils.run_migrations + assert_equal %w[20130906111511_first.rb 20130906111512_second.rb], utils.migrated_files end - it "keeps track of the irreversible migrations" do - prepare_phantom_migrations - assert_equal %i[first second irreversible], TestingState.up - assert_empty ActualDbSchema.failed - run_migrations - assert_equal(%w[20130906111513_irreversible.rb], ActualDbSchema.failed.map { |m| File.basename(m.filename) }) + it "rolls back the migrations in the reversed order" do + utils.prepare_phantom_migrations + assert_empty TestingState.down + utils.run_migrations + assert_equal %i[second first], TestingState.down end - end -end -describe "db:phantom_migrations" do - before { cleanup } + describe "with irreversible migration" do + before do + utils.define_migration_file("20130906111513_irreversible.rb", <<~RUBY) + class Irreversible < ActiveRecord::Migration[6.0] + def up + TestingState.up << :irreversible + end + + def down + raise ActiveRecord::IrreversibleMigration + end + end + RUBY + end - def run_task - Rake::Task["db:phantom_migrations"].invoke - Rake::Task["db:phantom_migrations"].reenable + it "keeps track of the irreversible migrations" do + utils.prepare_phantom_migrations + assert_equal %i[first second irreversible], TestingState.up + assert_empty ActualDbSchema.failed + utils.run_migrations + assert_equal(%w[20130906111513_irreversible.rb], ActualDbSchema.failed.map { |m| File.basename(m.filename) }) + end + end end - it "shows the list of phantom migrations" do - ActualDbSchema::Git.stub(:current_branch, "fix-bug") do - prepare_phantom_migrations - run_task - assert_match(/ Status Migration ID Branch Migration File/, TestingState.output) - assert_match(/---------------------------------------------------/, TestingState.output) - assert_match(%r{ up 20130906111511 fix-bug tmp/migrated/20130906111511_first.rb}, TestingState.output) - assert_match(%r{ up 20130906111512 fix-bug tmp/migrated/20130906111512_second.rb}, TestingState.output) + describe "db:phantom_migrations" do + it "shows the list of phantom migrations" do + ActualDbSchema::Git.stub(:current_branch, "fix-bug") do + utils.prepare_phantom_migrations + Rake::Task["db:phantom_migrations"].invoke + Rake::Task["db:phantom_migrations"].reenable + assert_match(/ Status Migration ID Branch Migration File/, TestingState.output) + assert_match(/---------------------------------------------------/, TestingState.output) + assert_match(%r{ up 20130906111511 fix-bug tmp/migrated/20130906111511_first.rb}, TestingState.output) + assert_match(%r{ up 20130906111512 fix-bug tmp/migrated/20130906111512_second.rb}, TestingState.output) + end end end end diff --git a/test/support/test_utils.rb b/test/support/test_utils.rb new file mode 100644 index 0000000..f94fa8d --- /dev/null +++ b/test/support/test_utils.rb @@ -0,0 +1,90 @@ +# frozen_string_literal: true + +class TestUtils + attr_accessor :migrations_path, :migrated_path + + def initialize(migrations_path: "db/migrate", migrated_path: "tmp/migrated") + @migrations_path = migrations_path + @migrated_path = migrated_path + end + + def app_file(path) + Rails.application.config.root.join(path) + end + + def remove_app_dir(name) + FileUtils.rm_rf(app_file(name)) + end + + def run_migrations + Rake::Task["db:migrate"].invoke + Rake::Task["db:migrate"].reenable + Rake::Task["db:rollback_branches"].reenable + end + + def run_sql(sql) + ActiveRecord::Base.connection.execute(sql) + end + + def applied_migrations + run_sql("select * from schema_migrations").map do |row| + row["version"] + end + end + + def clear_schema + run_sql("delete from schema_migrations") + end + + def delete_migrations_files + Dir.glob(app_file("#{migrations_path}/**/*.rb")).each do |file| + remove_app_dir(file) + end + end + + def define_migration_file(filename, content) + File.write(app_file("#{migrations_path}/#{filename}"), content, mode: "w") + end + + def define_migrations + { + first: "20130906111511_first.rb", + second: "20130906111512_second.rb" + }.each do |key, file_name| + define_migration_file(file_name, <<~RUBY) + class #{key.to_s.camelize} < ActiveRecord::Migration[6.0] + def up + TestingState.up << :#{key} + end + + def down + TestingState.down << :#{key} + end + end + RUBY + end + end + + def prepare_phantom_migrations + run_migrations + delete_migrations_files # simulate switching branches + end + + def cleanup + delete_migrations_files + if ActiveRecord::SchemaMigration.respond_to?(:create_table) + ActiveRecord::SchemaMigration.create_table + else + ActiveRecord::SchemaMigration.new(ActiveRecord::Base.connection).create_table + end + run_sql("delete from schema_migrations") + remove_app_dir(migrated_path) + define_migrations + Rails.application.load_tasks + TestingState.reset + end + + def migrated_files + Dir.glob(app_file("#{migrated_path}/*.rb")).map { |f| File.basename(f) }.sort + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 06f2a87..1db5699 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -5,6 +5,7 @@ require "actual_db_schema" require "minitest/autorun" require "debug" +require "support/test_utils" Rails.env = "test"