1# frozen_string_literal: true 2 3require 'fast_spec_helper' 4 5require_relative '../../../../rubocop/cop/migration/schedule_async' 6 7RSpec.describe RuboCop::Cop::Migration::ScheduleAsync do 8 let(:cop) { described_class.new } 9 let(:source) do 10 <<~SOURCE 11 def up 12 BackgroundMigrationWorker.perform_async(ClazzName, "Bar", "Baz") 13 end 14 SOURCE 15 end 16 17 shared_examples 'a disabled cop' do 18 it 'does not register any offenses' do 19 expect_no_offenses(source) 20 end 21 end 22 23 context 'outside of a migration' do 24 it_behaves_like 'a disabled cop' 25 end 26 27 context 'in a migration' do 28 before do 29 allow(cop).to receive(:in_migration?).and_return(true) 30 end 31 32 context 'in an old migration' do 33 before do 34 allow(cop).to receive(:version).and_return(described_class::ENFORCED_SINCE - 5) 35 end 36 37 it_behaves_like 'a disabled cop' 38 end 39 40 context 'that is recent' do 41 before do 42 allow(cop).to receive(:version).and_return(described_class::ENFORCED_SINCE + 5) 43 end 44 45 context 'BackgroundMigrationWorker.perform_async' do 46 it 'adds an offense when calling `BackgroundMigrationWorker.peform_async` and corrects', :aggregate_failures do 47 expect_offense(<<~RUBY) 48 def up 49 BackgroundMigrationWorker.perform_async(ClazzName, "Bar", "Baz") 50 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't call [...] 51 end 52 RUBY 53 54 expect_correction(<<~RUBY) 55 def up 56 migrate_async(ClazzName, "Bar", "Baz") 57 end 58 RUBY 59 end 60 end 61 62 context 'BackgroundMigrationWorker.perform_in' do 63 it 'adds an offense and corrects', :aggregate_failures do 64 expect_offense(<<~RUBY) 65 def up 66 BackgroundMigrationWorker 67 ^^^^^^^^^^^^^^^^^^^^^^^^^ Don't call [...] 68 .perform_in(delay, ClazzName, "Bar", "Baz") 69 end 70 RUBY 71 72 expect_correction(<<~RUBY) 73 def up 74 migrate_in(delay, ClazzName, "Bar", "Baz") 75 end 76 RUBY 77 end 78 end 79 80 context 'BackgroundMigrationWorker.bulk_perform_async' do 81 it 'adds an offense and corrects', :aggregate_failures do 82 expect_offense(<<~RUBY) 83 def up 84 BackgroundMigrationWorker 85 ^^^^^^^^^^^^^^^^^^^^^^^^^ Don't call [...] 86 .bulk_perform_async(jobs) 87 end 88 RUBY 89 90 expect_correction(<<~RUBY) 91 def up 92 bulk_migrate_async(jobs) 93 end 94 RUBY 95 end 96 end 97 98 context 'BackgroundMigrationWorker.bulk_perform_in' do 99 it 'adds an offense and corrects', :aggregate_failures do 100 expect_offense(<<~RUBY) 101 def up 102 BackgroundMigrationWorker 103 ^^^^^^^^^^^^^^^^^^^^^^^^^ Don't call [...] 104 .bulk_perform_in(5.minutes, jobs) 105 end 106 RUBY 107 108 expect_correction(<<~RUBY) 109 def up 110 bulk_migrate_in(5.minutes, jobs) 111 end 112 RUBY 113 end 114 end 115 end 116 end 117end 118