|
| 1 | +require_relative '../spec_helper' |
| 2 | + |
| 3 | +describe 'ActiveRecord migration rake task hooks' do |
| 4 | + before do |
| 5 | + Rake.application = Rake::Application.new |
| 6 | + |
| 7 | + # Stub migration tasks |
| 8 | + %w(db:migrate db:migrate:up db:migrate:down db:migrate:reset db:rollback).each do |task| |
| 9 | + Rake::Task.define_task(task) |
| 10 | + end |
| 11 | + Rake::Task.define_task('db:migrate:redo') do |
| 12 | + Rake::Task['db:rollback'].invoke |
| 13 | + Rake::Task['db:migrate'].invoke |
| 14 | + end |
| 15 | + |
| 16 | + Rake::Task.define_task('set_annotation_options') |
| 17 | + Rake.load_rakefile('tasks/annotate_models_migrate.rake') |
| 18 | + |
| 19 | + Rake.application.instance_variable_set(:@top_level_tasks, [subject]) |
| 20 | + end |
| 21 | + |
| 22 | + describe 'db:migrate' do |
| 23 | + it 'should update annotations' do |
| 24 | + expect(Annotate::Migration).to receive(:update_annotations) |
| 25 | + Rake.application.top_level |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + describe 'db:migrate:up' do |
| 30 | + it 'should update annotations' do |
| 31 | + expect(Annotate::Migration).to receive(:update_annotations) |
| 32 | + Rake.application.top_level |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + describe 'db:migrate:down' do |
| 37 | + it 'should update annotations' do |
| 38 | + expect(Annotate::Migration).to receive(:update_annotations) |
| 39 | + Rake.application.top_level |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + describe 'db:migrate:reset' do |
| 44 | + it 'should update annotations' do |
| 45 | + expect(Annotate::Migration).to receive(:update_annotations) |
| 46 | + Rake.application.top_level |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + describe 'db:rollback' do |
| 51 | + it 'should update annotations' do |
| 52 | + expect(Annotate::Migration).to receive(:update_annotations) |
| 53 | + Rake.application.top_level |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + describe 'db:migrate:redo' do |
| 58 | + it 'should update annotations after all migration tasks' do |
| 59 | + allow(Annotate::Migration).to receive(:update_annotations) |
| 60 | + |
| 61 | + # Confirm that update_annotations isn't called when the original redo task finishes |
| 62 | + Rake::Task[subject].enhance do |
| 63 | + expect(Annotate::Migration).not_to have_received(:update_annotations) |
| 64 | + end |
| 65 | + |
| 66 | + Rake.application.top_level |
| 67 | + |
| 68 | + # Hooked 3 times by db:rollback, db:migrate, and db:migrate:redo tasks |
| 69 | + expect(Annotate::Migration).to have_received(:update_annotations).exactly(3).times |
| 70 | + end |
| 71 | + end |
| 72 | +end |
0 commit comments