Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Internal RFC] Create RSpec helpers for migrations specs #27

Draft
wants to merge 1 commit into
base: kennyadsl/move-promo-calculator
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions core/spec/support/migrations.rb
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions core/spec/support/migrations/migrator_helper.rb
Original file line number Diff line number Diff line change
@@ -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