diff --git a/core/spec/migrate/20190106184413_remove_code_from_spree_promotions_spec.rb b/core/spec/migrate/20190106184413_remove_code_from_spree_promotions_spec.rb index 600a5cbf64b..1362a11d96c 100644 --- a/core/spec/migrate/20190106184413_remove_code_from_spree_promotions_spec.rb +++ b/core/spec/migrate/20190106184413_remove_code_from_spree_promotions_spec.rb @@ -3,69 +3,12 @@ require 'rails_helper' require Spree::Core::Engine.root.join('db/migrate/20190106184413_remove_code_from_spree_promotions.rb') -RSpec.describe RemoveCodeFromSpreePromotions do - let(:migrations_paths) { ActiveRecord::Migrator.migrations_paths } - let(:migrations) do - if Rails.gem_version >= Gem::Version.new('6.0.0') - ActiveRecord::MigrationContext.new( - migrations_paths, - ActiveRecord::SchemaMigration - ).migrations - elsif Rails.gem_version >= Gem::Version.new('5.2.0') - ActiveRecord::MigrationContext.new(migrations_paths).migrations - else - ActiveRecord::Migrator.migrations(migrations_paths) - end - end - let(:previous_version) { 20180710170104 } - let(:current_version) { 20190106184413 } - - subject do - if Rails.gem_version >= Gem::Version.new('6.0.0') - ActiveRecord::Migrator.new(:up, migrations, ActiveRecord::SchemaMigration, current_version).migrate - else - ActiveRecord::Migrator.new(:up, migrations, current_version).migrate - end - end - - # This is needed for MySQL since it is not able to rollback to the previous - # state when database schema changes within that transaction. - before(:all) { self.use_transactional_tests = false } - after(:all) { self.use_transactional_tests = true } - - around do |example| - DatabaseCleaner.clean_with(:truncation) - # Silence migrations output in specs report. - ActiveRecord::Migration.suppress_messages do - # Migrate back to the previous version - if Rails.gem_version >= Gem::Version.new('6.0.0') - ActiveRecord::Migrator.new(:down, migrations, ActiveRecord::SchemaMigration, previous_version).migrate - else - ActiveRecord::Migrator.new(:down, migrations, previous_version).migrate - end - # If other tests using Spree::Promotion ran before this one, Rails has - # stored information about table's columns and we need to reset those - # since the migration changed the database structure. - Spree::Promotion.reset_column_information - - example.run - - # Re-update column information after the migration has been executed - # again in the example. This will make the promotion attributes cache - # ready for other tests. - Spree::Promotion.delete_all - Spree::Promotion.reset_column_information - if Rails.gem_version >= Gem::Version.new('6.0.0') - ActiveRecord::Migrator.new(:up, migrations, ActiveRecord::SchemaMigration).migrate - else - ActiveRecord::Migrator.new(:up, migrations).migrate - end - end - DatabaseCleaner.clean_with(:truncation) - end - +RSpec.describe RemoveCodeFromSpreePromotions, type: :migration, + described: 20190106184413, + previous: 20180710170104, + reset_tables: [Spree::Promotion] do + subject { |example| described_migration(example) } let(:promotion_with_code) { create(:promotion) } - before do # We can't set code via factory since `code=` is currently raising # an error, see more at: diff --git a/core/spec/migrate/20191001071110_convert_calculator_type_to_promotion_calculators_spec.rb b/core/spec/migrate/20191001071110_convert_calculator_type_to_promotion_calculators_spec.rb index 8063a85b423..ebbb9d42372 100644 --- a/core/spec/migrate/20191001071110_convert_calculator_type_to_promotion_calculators_spec.rb +++ b/core/spec/migrate/20191001071110_convert_calculator_type_to_promotion_calculators_spec.rb @@ -3,57 +3,10 @@ require 'rails_helper' require Spree::Core::Engine.root.join('db/migrate/20191001071110_convert_calculator_type_to_promotion_calculators.rb') -RSpec.describe ConvertCalculatorTypeToPromotionCalculators do - let(:migrations_paths) { ActiveRecord::Migrator.migrations_paths } - let(:migrations) do - if Rails.gem_version >= Gem::Version.new('6.0.0') - ActiveRecord::MigrationContext.new( - migrations_paths, - ActiveRecord::SchemaMigration - ).migrations - elsif Rails.gem_version >= Gem::Version.new('5.2.0') - ActiveRecord::MigrationContext.new(migrations_paths).migrations - else - ActiveRecord::Migrator.migrations(migrations_paths) - end - end - let(:previous_version) { 20190220093635 } - let(:current_version) { 20191001071110 } - - subject do - if Rails.gem_version >= Gem::Version.new('6.0.0') - ActiveRecord::Migrator.new(:up, migrations, ActiveRecord::SchemaMigration, current_version).migrate - else - ActiveRecord::Migrator.new(:up, migrations, current_version).migrate - end - end - - # This is needed for MySQL since it is not able to rollback to the previous - # state when database schema changes within that transaction. - before(:all) { self.use_transactional_tests = false } - after(:all) { self.use_transactional_tests = true } - - around do |example| - DatabaseCleaner.clean_with(:truncation) - # Silence migrations output in specs report. - ActiveRecord::Migration.suppress_messages do - # Migrate back to the previous version - if Rails.gem_version >= Gem::Version.new('6.0.0') - ActiveRecord::Migrator.new(:down, migrations, ActiveRecord::SchemaMigration, previous_version).migrate - else - ActiveRecord::Migrator.new(:down, migrations, previous_version).migrate - end - - example.run - - if Rails.gem_version >= Gem::Version.new('6.0.0') - ActiveRecord::Migrator.new(:up, migrations, ActiveRecord::SchemaMigration).migrate - else - ActiveRecord::Migrator.new(:up, migrations).migrate - end - end - DatabaseCleaner.clean_with(:truncation) - end +RSpec.describe ConvertCalculatorTypeToPromotionCalculators, type: :migration, + described: 20191001071110, + previous: 20190220093635 do + subject { |example| described_migration(example) } # This promotion factory creates a promotion action with # the flat rate calculator. diff --git a/core/spec/support/migrations.rb b/core/spec/support/migrations.rb new file mode 100644 index 00000000000..71ad8c2c20e --- /dev/null +++ b/core/spec/support/migrations.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require_relative 'migrations/migrator_helper' + +RSpec.configure do |config| + config.include Migrations::MigratorHelpers, type: :migration + + # This is needed for MySQL since it is not able to rollback to the previous + # state when database schema changes within that transaction. + config.before(:all, type: :migration) { self.use_transactional_tests = false } + config.after(:all, type: :migration) { self.use_transactional_tests = true } + + config.around(:each, type: :migration) do |example| + # Silence migrations output in specs report. + ActiveRecord::Migration.suppress_messages do + migrate_to_previous_migration_for(example) + clear_tables_cache(example) + + example.run + + DatabaseCleaner.clean_with(:truncation) + clear_tables_cache(example) + migrate_to_last_migration + end + end +end diff --git a/core/spec/support/migrations/migrator_helper.rb b/core/spec/support/migrations/migrator_helper.rb new file mode 100644 index 00000000000..ab568f1a5aa --- /dev/null +++ b/core/spec/support/migrations/migrator_helper.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +module Migrations + module MigratorHelpers + def migrate_to_described_migration_for(example) + migrate(example.metadata[:described]) + end + alias :described_migration :migrate_to_described_migration_for + + def migrate_to_previous_migration_for(example) + migrate(example.metadata[:previous], :down) + end + + def migrate(version = nil, direction = :up) + if Rails.gem_version >= Gem::Version.new('6.0.0') + ActiveRecord::Migrator.new(direction, migrations, ActiveRecord::SchemaMigration, version).migrate + else + ActiveRecord::Migrator.new(direction, migrations, version).migrate + end + end + + def migrate_to_last_migration + migrate + end + + def migrations_paths + ActiveRecord::Migrator.migrations_paths + end + + def migrations + if Rails.gem_version >= Gem::Version.new('6.0.0') + ActiveRecord::MigrationContext.new( + migrations_paths, + ActiveRecord::SchemaMigration + ).migrations + elsif Rails.gem_version >= Gem::Version.new('5.2.0') + ActiveRecord::MigrationContext.new(migrations_paths).migrations + else + ActiveRecord::Migrator.migrations(migrations_paths) + end + end + + def clear_tables_cache(example) + (example.metadata[:reset_tables] || []).each(&:reset_column_information) + end + end +end