diff --git a/activejob/CHANGELOG.md b/activejob/CHANGELOG.md index 69a5d469e44ff514279a4523967cbfa4087b0cb3..0b440e50560ccf1d7e50f6e960c2eb8c91b3ddf4 100644 --- a/activejob/CHANGELOG.md +++ b/activejob/CHANGELOG.md @@ -1,3 +1,8 @@ +* Execution of `assert_performed_jobs`, and `assert_no_performed_jobs` + without a block should respect passed `:except`, `:only`, and `:queue` options. + + *bogdanvlviv* + * Allow `:queue` option to job assertions and helpers. *bogdanvlviv* diff --git a/activejob/lib/active_job/test_helper.rb b/activejob/lib/active_job/test_helper.rb index 4dae1a996985060a7c6e55c18cd93c733375b560..d3a3e0a156e5bcc4e6f6ec543881618452b81a9a 100644 --- a/activejob/lib/active_job/test_helper.rb +++ b/activejob/lib/active_job/test_helper.rb @@ -176,7 +176,7 @@ def assert_no_enqueued_jobs(only: nil, except: nil, queue: nil, &block) # Asserts that the number of performed jobs matches the given number. # If no block is passed, perform_enqueued_jobs - # must be called around the job call. + # must be called around or after the job call. # # def test_jobs # assert_performed_jobs 0 @@ -186,10 +186,11 @@ def assert_no_enqueued_jobs(only: nil, except: nil, queue: nil, &block) # end # assert_performed_jobs 1 # - # perform_enqueued_jobs do - # HelloJob.perform_later('yves') - # assert_performed_jobs 2 - # end + # HelloJob.perform_later('yves') + # + # perform_enqueued_jobs + # + # assert_performed_jobs 2 # end # # If a block is passed, that block should cause the specified number of @@ -206,7 +207,7 @@ def assert_no_enqueued_jobs(only: nil, except: nil, queue: nil, &block) # end # end # - # The block form supports filtering. If the :only option is specified, + # This method also supports filtering. If the +:only+ option is specified, # then only the listed job(s) will be performed. # # def test_hello_job @@ -247,17 +248,20 @@ def assert_no_enqueued_jobs(only: nil, except: nil, queue: nil, &block) # HelloJob.set(queue: :other_queue).perform_later("bogdan") # end # end - def assert_performed_jobs(number, only: nil, except: nil, queue: nil) + def assert_performed_jobs(number, only: nil, except: nil, queue: nil, &block) if block_given? original_count = performed_jobs.size - perform_enqueued_jobs(only: only, except: except, queue: queue) { yield } + + perform_enqueued_jobs(only: only, except: except, queue: queue, &block) + new_count = performed_jobs.size - assert_equal number, new_count - original_count, - "#{number} jobs expected, but #{new_count - original_count} were performed" + + performed_jobs_size = new_count - original_count else - performed_jobs_size = performed_jobs.size - assert_equal number, performed_jobs_size, "#{number} jobs expected, but #{performed_jobs_size} were performed" + performed_jobs_size = performed_jobs_with(only: only, except: except, queue: queue) end + + assert_equal number, performed_jobs_size, "#{number} jobs expected, but #{performed_jobs_size} were performed" end # Asserts that no jobs have been performed. @@ -479,10 +483,10 @@ def clear_performed_jobs performed_jobs.clear end - def enqueued_jobs_with(only: nil, except: nil, queue: nil) + def jobs_with(jobs, only: nil, except: nil, queue: nil) validate_option(only: only, except: except) - enqueued_jobs.count do |job| + jobs.count do |job| job_class = job.fetch(:job) if only @@ -490,15 +494,25 @@ def enqueued_jobs_with(only: nil, except: nil, queue: nil) elsif except next false if Array(except).include?(job_class) end + if queue next false unless queue.to_s == job.fetch(:queue, job_class.queue_name) end yield job if block_given? + true end end + def enqueued_jobs_with(only: nil, except: nil, queue: nil, &block) + jobs_with(enqueued_jobs, only: only, except: except, queue: queue, &block) + end + + def performed_jobs_with(only: nil, except: nil, queue: nil, &block) + jobs_with(performed_jobs, only: only, except: except, queue: queue, &block) + end + def flush_enqueued_jobs(only: nil, except: nil, queue: nil) enqueued_jobs_with(only: only, except: except, queue: queue) do |payload| args = ActiveJob::Arguments.deserialize(payload[:args]) diff --git a/activejob/test/cases/test_helper_test.rb b/activejob/test/cases/test_helper_test.rb index eade2854123ceb1a5f29fd4144812d0ab278586a..f64b886d676fdbb59c5e675df3e3a12a4a1e664a 100644 --- a/activejob/test/cases/test_helper_test.rb +++ b/activejob/test/cases/test_helper_test.rb @@ -677,7 +677,7 @@ def test_perform_enqueued_jobs_with_block_with_only_option end assert_performed_jobs 1 - # TODO assert_performed_jobs 1, only: LoggingJob + assert_performed_jobs 1, only: LoggingJob end def test_perform_enqueued_jobs_without_block_with_only_option @@ -687,7 +687,7 @@ def test_perform_enqueued_jobs_without_block_with_only_option perform_enqueued_jobs only: LoggingJob assert_performed_jobs 1 - # TODO assert_performed_jobs 1, only: LoggingJob + assert_performed_jobs 1, only: LoggingJob end def test_perform_enqueued_jobs_with_block_with_except_option @@ -697,7 +697,7 @@ def test_perform_enqueued_jobs_with_block_with_except_option end assert_performed_jobs 1 - # TODO assert_performed_jobs 1, only: LoggingJob + assert_performed_jobs 1, only: LoggingJob end def test_perform_enqueued_jobs_without_block_with_except_option @@ -707,7 +707,7 @@ def test_perform_enqueued_jobs_without_block_with_except_option perform_enqueued_jobs except: HelloJob assert_performed_jobs 1 - # TODO assert_performed_jobs 1, only: LoggingJob + assert_performed_jobs 1, only: LoggingJob end def test_perform_enqueued_jobs_with_block_with_queue_option @@ -718,7 +718,7 @@ def test_perform_enqueued_jobs_with_block_with_queue_option end assert_performed_jobs 1 - # TODO assert_performed_jobs 1, only: HelloJob, queue: :some_queue + assert_performed_jobs 1, only: HelloJob, queue: :some_queue end def test_perform_enqueued_jobs_without_block_with_queue_option @@ -729,7 +729,7 @@ def test_perform_enqueued_jobs_without_block_with_queue_option perform_enqueued_jobs queue: :some_queue assert_performed_jobs 1 - # TODO assert_performed_jobs 1, only: HelloJob, queue: :some_queue + assert_performed_jobs 1, only: HelloJob, queue: :some_queue end def test_perform_enqueued_jobs_with_block_with_only_and_queue_options @@ -740,7 +740,7 @@ def test_perform_enqueued_jobs_with_block_with_only_and_queue_options end assert_performed_jobs 1 - # TODO assert_performed_jobs 1, only: HelloJob, queue: :other_queue + assert_performed_jobs 1, only: HelloJob, queue: :other_queue end def test_perform_enqueued_jobs_without_block_with_only_and_queue_options @@ -751,7 +751,7 @@ def test_perform_enqueued_jobs_without_block_with_only_and_queue_options perform_enqueued_jobs only: HelloJob, queue: :other_queue assert_performed_jobs 1 - # TODO assert_performed_jobs 1, only: HelloJob, queue: :other_queue + assert_performed_jobs 1, only: HelloJob, queue: :other_queue end def test_perform_enqueued_jobs_with_block_with_except_and_queue_options @@ -762,7 +762,7 @@ def test_perform_enqueued_jobs_with_block_with_except_and_queue_options end assert_performed_jobs 1 - # TODO assert_performed_jobs 1, only: LoggingJob, queue: :other_queue + assert_performed_jobs 1, only: LoggingJob, queue: :other_queue end def test_perform_enqueued_jobs_without_block_with_except_and_queue_options @@ -773,7 +773,7 @@ def test_perform_enqueued_jobs_without_block_with_except_and_queue_options perform_enqueued_jobs except: HelloJob, queue: :other_queue assert_performed_jobs 1 - # TODO assert_performed_jobs 1, only: LoggingJob, queue: :other_queue + assert_performed_jobs 1, only: LoggingJob, queue: :other_queue end def test_assert_performed_jobs @@ -881,6 +881,28 @@ def test_assert_performed_jobs_with_only_option end end + def test_assert_performed_jobs_without_block_with_only_option + HelloJob.perform_later("jeremy") + LoggingJob.perform_later("bogdan") + + perform_enqueued_jobs + + assert_performed_jobs 1, only: HelloJob + end + + def test_assert_performed_jobs_without_block_with_only_option_failure + LoggingJob.perform_later("jeremy") + LoggingJob.perform_later("bogdan") + + perform_enqueued_jobs + + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_performed_jobs 1, only: HelloJob + end + + assert_match(/1 .* but 0/, error.message) + end + def test_assert_performed_jobs_with_except_option assert_nothing_raised do assert_performed_jobs 1, except: LoggingJob do @@ -890,6 +912,28 @@ def test_assert_performed_jobs_with_except_option end end + def test_assert_performed_jobs_without_block_with_except_option + HelloJob.perform_later("jeremy") + LoggingJob.perform_later("bogdan") + + perform_enqueued_jobs + + assert_performed_jobs 1, except: HelloJob + end + + def test_assert_performed_jobs_without_block_with_except_option_failure + HelloJob.perform_later("jeremy") + HelloJob.perform_later("bogdan") + + perform_enqueued_jobs + + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_performed_jobs 1, except: HelloJob + end + + assert_match(/1 .* but 0/, error.message) + end + def test_assert_performed_jobs_with_only_and_except_option error = assert_raise ArgumentError do assert_performed_jobs 1, only: HelloJob, except: HelloJob do @@ -901,6 +945,19 @@ def test_assert_performed_jobs_with_only_and_except_option assert_match(/`:only` and `:except`/, error.message) end + def test_assert_performed_jobs_without_block_with_only_and_except_options + error = assert_raise ArgumentError do + HelloJob.perform_later("jeremy") + LoggingJob.perform_later("bogdan") + + perform_enqueued_jobs + + assert_performed_jobs 1, only: HelloJob, except: HelloJob + end + + assert_match(/`:only` and `:except`/, error.message) + end + def test_assert_performed_jobs_with_only_option_as_array assert_nothing_raised do assert_performed_jobs 2, only: [HelloJob, LoggingJob] do @@ -1044,6 +1101,28 @@ def test_assert_performed_jobs_with_queue_option_failure assert_match(/1 .* but 0/, error.message) end + def test_assert_performed_jobs_without_block_with_queue_option + HelloJob.set(queue: :some_queue).perform_later("jeremy") + HelloJob.set(queue: :other_queue).perform_later("bogdan") + + perform_enqueued_jobs + + assert_performed_jobs 1, queue: :some_queue + end + + def test_assert_performed_jobs_without_block_with_queue_option_failure + HelloJob.set(queue: :other_queue).perform_later("jeremy") + HelloJob.set(queue: :other_queue).perform_later("bogdan") + + perform_enqueued_jobs + + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_performed_jobs 1, queue: :some_queue + end + + assert_match(/1 .* but 0/, error.message) + end + def test_assert_performed_jobs_with_only_and_queue_options assert_performed_jobs 1, only: HelloJob, queue: :some_queue do HelloJob.set(queue: :some_queue).perform_later("jeremy") @@ -1064,6 +1143,30 @@ def test_assert_performed_jobs_with_only_and_queue_options_failure assert_match(/1 .* but 0/, error.message) end + def test_assert_performed_jobs_without_block_with_only_and_queue_options + HelloJob.set(queue: :some_queue).perform_later("jeremy") + HelloJob.set(queue: :other_queue).perform_later("bogdan") + LoggingJob.set(queue: :some_queue).perform_later("jeremy") + + perform_enqueued_jobs + + assert_performed_jobs 1, only: HelloJob, queue: :some_queue + end + + def test_assert_performed_jobs_without_block_with_only_and_queue_options_failure + HelloJob.set(queue: :other_queue).perform_later("jeremy") + HelloJob.set(queue: :other_queue).perform_later("bogdan") + LoggingJob.set(queue: :some_queue).perform_later("jeremy") + + perform_enqueued_jobs + + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_performed_jobs 1, only: HelloJob, queue: :some_queue + end + + assert_match(/1 .* but 0/, error.message) + end + def test_assert_performed_jobs_with_except_and_queue_options assert_performed_jobs 1, except: HelloJob, queue: :other_queue do HelloJob.set(queue: :other_queue).perform_later("jeremy") @@ -1084,6 +1187,30 @@ def test_assert_performed_jobs_with_except_and_queue_options_failuree assert_match(/1 .* but 0/, error.message) end + def test_assert_performed_jobs_without_block_with_except_and_queue_options + HelloJob.set(queue: :other_queue).perform_later("jeremy") + LoggingJob.set(queue: :some_queue).perform_later("bogdan") + LoggingJob.set(queue: :other_queue).perform_later("jeremy") + + perform_enqueued_jobs + + assert_performed_jobs 1, except: HelloJob, queue: :other_queue + end + + def test_assert_performed_jobs_with_except_and_queue_options_failuree + HelloJob.set(queue: :other_queue).perform_later("jeremy") + LoggingJob.set(queue: :some_queue).perform_later("bogdan") + LoggingJob.set(queue: :some_queue).perform_later("jeremy") + + perform_enqueued_jobs + + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_performed_jobs 1, except: HelloJob, queue: :other_queue + end + + assert_match(/1 .* but 0/, error.message) + end + def test_assert_no_performed_jobs_with_only_option assert_nothing_raised do assert_no_performed_jobs only: HelloJob do @@ -1092,6 +1219,26 @@ def test_assert_no_performed_jobs_with_only_option end end + def test_assert_no_performed_jobs_without_block_with_only_option + LoggingJob.perform_later("bogdan") + + perform_enqueued_jobs + + assert_no_performed_jobs only: HelloJob + end + + def test_assert_no_performed_jobs_without_block_with_only_option_failure + HelloJob.perform_later("bogdan") + + perform_enqueued_jobs + + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_no_performed_jobs only: HelloJob + end + + assert_match(/0 .* but 1/, error.message) + end + def test_assert_no_performed_jobs_with_except_option assert_nothing_raised do assert_no_performed_jobs except: LoggingJob do @@ -1100,6 +1247,26 @@ def test_assert_no_performed_jobs_with_except_option end end + def test_assert_no_performed_jobs_without_block_with_except_option + HelloJob.perform_later("jeremy") + + perform_enqueued_jobs + + assert_no_performed_jobs except: HelloJob + end + + def test_assert_no_performed_jobs_without_block_with_except_option_failure + LoggingJob.perform_later("jeremy") + + perform_enqueued_jobs + + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_no_performed_jobs except: HelloJob + end + + assert_match(/0 .* but 1/, error.message) + end + def test_assert_no_performed_jobs_with_only_and_except_option error = assert_raise ArgumentError do assert_no_performed_jobs only: HelloJob, except: HelloJob do @@ -1110,6 +1277,19 @@ def test_assert_no_performed_jobs_with_only_and_except_option assert_match(/`:only` and `:except`/, error.message) end + def test_assert_no_performed_jobs_without_block_with_only_and_except_options + error = assert_raise ArgumentError do + HelloJob.perform_later("jeremy") + LoggingJob.perform_later("bogdan") + + perform_enqueued_jobs + + assert_no_performed_jobs only: HelloJob, except: HelloJob + end + + assert_match(/`:only` and `:except`/, error.message) + end + def test_assert_no_performed_jobs_with_only_option_as_array assert_nothing_raised do assert_no_performed_jobs only: [HelloJob, RescueJob] do @@ -1186,6 +1366,26 @@ def test_assert_no_performed_jobs_with_queue_option_failure assert_match(/0 .* but 1/, error.message) end + def test_assert_no_performed_jobs_without_block_with_queue_option + HelloJob.set(queue: :other_queue).perform_later("jeremy") + + perform_enqueued_jobs + + assert_no_performed_jobs queue: :some_queue + end + + def test_assert_no_performed_jobs_without_block_with_queue_option_failure + HelloJob.set(queue: :some_queue).perform_later("jeremy") + + perform_enqueued_jobs + + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_no_performed_jobs queue: :some_queue + end + + assert_match(/0 .* but 1/, error.message) + end + def test_assert_no_performed_jobs_with_only_and_queue_options assert_no_performed_jobs only: HelloJob, queue: :some_queue do HelloJob.set(queue: :other_queue).perform_later("bogdan") @@ -1204,6 +1404,28 @@ def test_assert_no_performed_jobs_with_only_and_queue_options_failure assert_match(/0 .* but 1/, error.message) end + def test_assert_no_performed_jobs_without_block_with_only_and_queue_options + HelloJob.set(queue: :other_queue).perform_later("bogdan") + LoggingJob.set(queue: :some_queue).perform_later("jeremy") + + perform_enqueued_jobs + + assert_no_performed_jobs only: HelloJob, queue: :some_queue + end + + def test_assert_no_performed_jobs_without_block_with_only_and_queue_options_failure + HelloJob.set(queue: :some_queue).perform_later("bogdan") + LoggingJob.set(queue: :some_queue).perform_later("jeremy") + + perform_enqueued_jobs + + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_no_performed_jobs only: HelloJob, queue: :some_queue + end + + assert_match(/0 .* but 1/, error.message) + end + def test_assert_no_performed_jobs_with_except_and_queue_options assert_no_performed_jobs except: HelloJob, queue: :some_queue do HelloJob.set(queue: :other_queue).perform_later("bogdan") @@ -1224,6 +1446,30 @@ def test_assert_no_performed_jobs_with_except_and_queue_options_failure assert_match(/0 .* but 1/, error.message) end + def test_assert_no_performed_jobs_without_block_with_except_and_queue_options + HelloJob.set(queue: :other_queue).perform_later("bogdan") + HelloJob.set(queue: :some_queue).perform_later("bogdan") + LoggingJob.set(queue: :other_queue).perform_later("jeremy") + + perform_enqueued_jobs + + assert_no_performed_jobs except: HelloJob, queue: :some_queue + end + + def test_assert_no_performed_jobs_without_block_with_except_and_queue_options_failure + HelloJob.set(queue: :other_queue).perform_later("bogdan") + HelloJob.set(queue: :some_queue).perform_later("bogdan") + LoggingJob.set(queue: :some_queue).perform_later("jeremy") + + perform_enqueued_jobs + + error = assert_raise ActiveSupport::TestCase::Assertion do + assert_no_performed_jobs except: HelloJob, queue: :some_queue + end + + assert_match(/0 .* but 1/, error.message) + end + def test_assert_performed_job assert_performed_with(job: NestedJob, queue: "default") do NestedJob.perform_later