Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NikitaAvvakumov/6dc9c81bab00c484ca82 to your computer and use it in GitHub Desktop.
Save NikitaAvvakumov/6dc9c81bab00c484ca82 to your computer and use it in GitHub Desktop.
Migration spec for adding enums to a model and converting from existing associations
#TODO Remove this brittle spec after it proves that the migration works
require 'rails_helper'
migration_file_name = '20150226095809_add_status_enums_to_orphan.rb'
migration_file_path = Rails.root.join 'db', 'migrate', migration_file_name
version = migration_file_name.split('_').first
require migration_file_path
describe AddStatusEnumsToOrphan do
let(:migration) { AddStatusEnumsToOrphan.new }
before(:each) do
if migration_has_been_run?(version)
migration.down
end
end
describe '#up' do
before(:each) do
Orphan.skip_callback :initialize, :after, :set_defaults
@active_unsponsored_orphan = create :orphan,
orphan_status: OrphanStatus.find_by_name('Active'),
orphan_sponsorship_status: OrphanSponsorshipStatus.find_by_name('Unsponsored'),
priority: 'Normal'
@inactive_sponsored_orphan = create :orphan,
orphan_status: OrphanStatus.find_by_name('Inactive'),
orphan_sponsorship_status: OrphanSponsorshipStatus.find_by_name('Sponsored'),
priority: 'Normal'
@on_hold_previously_sponsored_orphan = create :orphan,
orphan_status: OrphanStatus.find_by_name('On Hold'),
orphan_sponsorship_status: OrphanSponsorshipStatus.find_by_name('Previously Sponsored'),
priority: 'Normal'
@under_revision_on_hold_orphan = create :orphan,
orphan_status: OrphanStatus.find_by_name('Under Revision'),
orphan_sponsorship_status: OrphanSponsorshipStatus.find_by_name('On Hold'),
priority: 'Normal'
Orphan.set_callback :initialize, :after, :set_defaults
migration.up
Orphan.reset_column_information
end
describe 'setting status' do
it 'sets status & sponsorship_status enums from associations' do
expect(@active_unsponsored_orphan.reload.status).to eq 'active'
expect(@active_unsponsored_orphan.reload.sponsorship_status).to eq 'unsponsored'
expect(@inactive_sponsored_orphan.reload.status).to eq 'inactive'
expect(@inactive_sponsored_orphan.reload.sponsorship_status).to eq 'sponsored'
expect(@on_hold_previously_sponsored_orphan.reload.status).to eq 'on_hold'
expect(@on_hold_previously_sponsored_orphan.reload.sponsorship_status).to eq 'previously_sponsored'
expect(@under_revision_on_hold_orphan.reload.status).to eq 'under_revision'
expect(@under_revision_on_hold_orphan.reload.sponsorship_status).to eq 'sponsorship_on_hold'
end
end
end
def migration_has_been_run?(version)
query = "SELECT version FROM schema_migrations WHERE version = '#{version}'"
ActiveRecord::Base.connection.execute(query).any?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment