Add except option for ActiveJob::TestHelper methods

This commit is contained in:
posthumanism 2017-07-16 17:35:17 +09:00
parent 589adea81c
commit 4458b76756
3 changed files with 440 additions and 15 deletions

@ -12,7 +12,7 @@ module QueueAdapters
#
# Rails.application.config.active_job.queue_adapter = :test
class TestAdapter
attr_accessor(:perform_enqueued_jobs, :perform_enqueued_at_jobs, :filter)
attr_accessor(:perform_enqueued_jobs, :perform_enqueued_at_jobs, :filter, :reject)
attr_writer(:enqueued_jobs, :performed_jobs)
# Provides a store of all the enqueued jobs with the TestAdapter so you can check them.
@ -54,7 +54,13 @@ def enqueue_or_perform(perform, job, job_data)
end
def filtered?(job)
filter && !Array(filter).include?(job.class)
if filter
!Array(filter).include?(job.class)
elsif reject
Array(reject).include?(job.class)
else
false
end
end
end
end

@ -6,6 +6,7 @@
module ActiveJob
# Provides helper methods for testing Active Job
module TestHelper
class InvalidOptionsError < StandardError; end
delegate :enqueued_jobs, :enqueued_jobs=,
:performed_jobs, :performed_jobs=,
to: :queue_adapter
@ -89,7 +90,7 @@ def queue_adapter_for_test
# end
# end
#
# The number of times a specific job is enqueued can be asserted.
# The number of times a specific job was enqueued can be asserted.
#
# def test_logging_job
# assert_enqueued_jobs 1, only: LoggingJob do
@ -98,6 +99,15 @@ def queue_adapter_for_test
# end
# end
#
# The number of times a job except specific class was enqueued can be asserted.
#
# def test_logging_job
# assert_enqueued_jobs 1, except: HelloJob do
# LoggingJob.perform_later
# HelloJob.perform_later('jeremy')
# end
# end
#
# The number of times a job is enqueued to a specific queue can also be asserted.
#
# def test_logging_job
@ -106,14 +116,14 @@ def queue_adapter_for_test
# HelloJob.perform_later('elfassy')
# end
# end
def assert_enqueued_jobs(number, only: nil, queue: nil)
def assert_enqueued_jobs(number, only: nil, except: except, queue: nil)
if block_given?
original_count = enqueued_jobs_size(only: only, queue: queue)
original_count = enqueued_jobs_size(only: only, except: except, queue: queue)
yield
new_count = enqueued_jobs_size(only: only, queue: queue)
new_count = enqueued_jobs_size(only: only, except: except, queue: queue)
assert_equal number, new_count - original_count, "#{number} jobs expected, but #{new_count - original_count} were enqueued"
else
actual_count = enqueued_jobs_size(only: only, queue: queue)
actual_count = enqueued_jobs_size(only: only, except: except, queue: queue)
assert_equal number, actual_count, "#{number} jobs expected, but #{actual_count} were enqueued"
end
end
@ -142,11 +152,19 @@ def assert_enqueued_jobs(number, only: nil, queue: nil)
# end
# end
#
# It can be asserted that no jobs except specific class are enqueued:
#
# def test_no_logging
# assert_no_enqueued_jobs except: HelloJob do
# HelloJob.perform_later('jeremy')
# end
# end
#
# Note: This assertion is simply a shortcut for:
#
# assert_enqueued_jobs 0, &block
def assert_no_enqueued_jobs(only: nil, &block)
assert_enqueued_jobs 0, only: only, &block
def assert_no_enqueued_jobs(only: nil, except: except, &block)
assert_enqueued_jobs 0, only: only, except: except, &block
end
# Asserts that the number of performed jobs matches the given number.
@ -191,6 +209,16 @@ def assert_no_enqueued_jobs(only: nil, &block)
# end
# end
#
# Also if the :except option is specified,
# then the job(s) except specific class will be performed.
#
# def test_hello_job
# assert_performed_jobs 1, except: LoggingJob do
# HelloJob.perform_later('jeremy')
# LoggingJob.perform_later
# end
# end
#
# An array may also be specified, to support testing multiple jobs.
#
# def test_hello_and_logging_jobs
@ -202,10 +230,10 @@ def assert_no_enqueued_jobs(only: nil, &block)
# end
# end
# end
def assert_performed_jobs(number, only: nil)
def assert_performed_jobs(number, only: nil, except: nil)
if block_given?
original_count = performed_jobs.size
perform_enqueued_jobs(only: only) { yield }
perform_enqueued_jobs(only: only, except: except) { yield }
new_count = performed_jobs.size
assert_equal number, new_count - original_count,
"#{number} jobs expected, but #{new_count - original_count} were performed"
@ -243,11 +271,20 @@ def assert_performed_jobs(number, only: nil)
# end
# end
#
# Also if the :except option is specified,
# then the job(s) except specific class will not be performed.
#
# def test_no_logging
# assert_no_performed_jobs except: HelloJob do
# HelloJob.perform_later('jeremy')
# end
# end
#
# Note: This assertion is simply a shortcut for:
#
# assert_performed_jobs 0, &block
def assert_no_performed_jobs(only: nil, &block)
assert_performed_jobs 0, only: only, &block
def assert_no_performed_jobs(only: nil, except: nil, &block)
assert_performed_jobs 0, only: only, except: except, &block
end
# Asserts that the job passed in the block has been enqueued with the given arguments.
@ -317,20 +354,36 @@ def assert_performed_with(job: nil, args: nil, at: nil, queue: nil)
# end
# assert_performed_jobs 1
# end
def perform_enqueued_jobs(only: nil)
#
# Also if the +:except+ option is specified,
# then the job(s) except specific class will be performed.
#
# def test_perform_enqueued_jobs_with_except
# perform_enqueued_jobs(except: HelloJob) do
# MyJob.perform_later(1, 2, 3) # will be performed
# HelloJob.perform_later(1, 2, 3) # will not be performed
# end
# assert_performed_jobs 1
# end
#
def perform_enqueued_jobs(only: nil, except: nil)
validate_option(only: only, except: except)
old_perform_enqueued_jobs = queue_adapter.perform_enqueued_jobs
old_perform_enqueued_at_jobs = queue_adapter.perform_enqueued_at_jobs
old_filter = queue_adapter.filter
old_reject = queue_adapter.reject
begin
queue_adapter.perform_enqueued_jobs = true
queue_adapter.perform_enqueued_at_jobs = true
queue_adapter.filter = only
queue_adapter.reject = except
yield
ensure
queue_adapter.perform_enqueued_jobs = old_perform_enqueued_jobs
queue_adapter.perform_enqueued_at_jobs = old_perform_enqueued_at_jobs
queue_adapter.filter = old_filter
queue_adapter.reject = old_reject
end
end
@ -352,11 +405,14 @@ def clear_performed_jobs
performed_jobs.clear
end
def enqueued_jobs_size(only: nil, queue: nil)
def enqueued_jobs_size(only: nil, except: nil, queue: nil)
validate_option(only: only, except: except)
enqueued_jobs.count do |job|
job_class = job.fetch(:job)
if only
next false unless Array(only).include?(job_class)
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)
@ -385,5 +441,9 @@ def queue_adapter_changed_jobs
klass.singleton_class.public_instance_methods(false).include?(:_queue_adapter)
end
end
def validate_option(only: nil, except: nil)
raise InvalidOptionsError, "Cannot specify both `:only` and `:except` options." if only && except
end
end
end

@ -108,10 +108,33 @@ def test_assert_enqueued_jobs_with_only_option
assert_enqueued_jobs 1, only: HelloJob do
HelloJob.perform_later("jeremy")
LoggingJob.perform_later
LoggingJob.perform_later
end
end
end
def test_assert_enqueued_jobs_with_except_option
assert_nothing_raised do
assert_enqueued_jobs 1, except: LoggingJob do
HelloJob.perform_later("jeremy")
LoggingJob.perform_later
LoggingJob.perform_later
end
end
end
def test_assert_enqueued_jobs_with_only_and_except_option
error = assert_raise ActiveJob::TestHelper::InvalidOptionsError do
assert_enqueued_jobs 1, only: HelloJob, except: HelloJob do
HelloJob.perform_later("jeremy")
LoggingJob.perform_later
LoggingJob.perform_later
end
end
assert_match(/`:only` and `:except`/, error.message)
end
def test_assert_enqueued_jobs_with_only_and_queue_option
assert_nothing_raised do
assert_enqueued_jobs 1, only: HelloJob, queue: :some_queue do
@ -122,6 +145,28 @@ def test_assert_enqueued_jobs_with_only_and_queue_option
end
end
def test_assert_enqueued_jobs_with_except_and_queue_option
assert_nothing_raised do
assert_enqueued_jobs 1, except: LoggingJob, queue: :some_queue do
HelloJob.set(queue: :some_queue).perform_later
HelloJob.set(queue: :other_queue).perform_later
LoggingJob.perform_later
end
end
end
def test_assert_enqueued_jobs_with_only_and_except_and_queue_option
error = assert_raise ActiveJob::TestHelper::InvalidOptionsError do
assert_enqueued_jobs 1, only: HelloJob, except: HelloJob, queue: :some_queue do
HelloJob.set(queue: :some_queue).perform_later
HelloJob.set(queue: :other_queue).perform_later
LoggingJob.perform_later
end
end
assert_match(/`:only` and `:except`/, error.message)
end
def test_assert_enqueued_jobs_with_queue_option
assert_nothing_raised do
assert_enqueued_jobs 2, queue: :default do
@ -143,6 +188,26 @@ def test_assert_enqueued_jobs_with_only_option_and_none_sent
assert_match(/1 .* but 0/, error.message)
end
def test_assert_enqueued_jobs_with_except_option_and_none_sent
error = assert_raise ActiveSupport::TestCase::Assertion do
assert_enqueued_jobs 1, except: LoggingJob do
LoggingJob.perform_later
end
end
assert_match(/1 .* but 0/, error.message)
end
def test_assert_enqueued_jobs_with_only_and_except_option_and_none_sent
error = assert_raise ActiveJob::TestHelper::InvalidOptionsError do
assert_enqueued_jobs 1, only: HelloJob, except: HelloJob do
LoggingJob.perform_later
end
end
assert_match(/`:only` and `:except`/, error.message)
end
def test_assert_enqueued_jobs_with_only_option_and_too_few_sent
error = assert_raise ActiveSupport::TestCase::Assertion do
assert_enqueued_jobs 5, only: HelloJob do
@ -154,6 +219,28 @@ def test_assert_enqueued_jobs_with_only_option_and_too_few_sent
assert_match(/5 .* but 1/, error.message)
end
def test_assert_enqueued_jobs_with_except_option_and_too_few_sent
error = assert_raise ActiveSupport::TestCase::Assertion do
assert_enqueued_jobs 5, except: LoggingJob do
HelloJob.perform_later("jeremy")
4.times { LoggingJob.perform_later }
end
end
assert_match(/5 .* but 1/, error.message)
end
def test_assert_enqueued_jobs_with_only_and_except_option_and_too_few_sent
error = assert_raise ActiveJob::TestHelper::InvalidOptionsError do
assert_enqueued_jobs 5, only: HelloJob, except: HelloJob do
HelloJob.perform_later("jeremy")
4.times { LoggingJob.perform_later }
end
end
assert_match(/`:only` and `:except`/, error.message)
end
def test_assert_enqueued_jobs_with_only_option_and_too_many_sent
error = assert_raise ActiveSupport::TestCase::Assertion do
assert_enqueued_jobs 1, only: HelloJob do
@ -164,6 +251,26 @@ def test_assert_enqueued_jobs_with_only_option_and_too_many_sent
assert_match(/1 .* but 2/, error.message)
end
def test_assert_enqueued_jobs_with_except_option_and_too_many_sent
error = assert_raise ActiveSupport::TestCase::Assertion do
assert_enqueued_jobs 1, except: LoggingJob do
2.times { HelloJob.perform_later("jeremy") }
end
end
assert_match(/1 .* but 2/, error.message)
end
def test_assert_enqueued_jobs_with_only_and_except_option_and_too_many_sent
error = assert_raise ActiveJob::TestHelper::InvalidOptionsError do
assert_enqueued_jobs 1, only: HelloJob, except: HelloJob do
2.times { HelloJob.perform_later("jeremy") }
end
end
assert_match(/`:only` and `:except`/, error.message)
end
def test_assert_enqueued_jobs_with_only_option_as_array
assert_nothing_raised do
assert_enqueued_jobs 2, only: [HelloJob, LoggingJob] do
@ -174,6 +281,28 @@ def test_assert_enqueued_jobs_with_only_option_as_array
end
end
def test_assert_enqueued_jobs_with_except_option_as_array
assert_nothing_raised do
assert_enqueued_jobs 1, except: [HelloJob, LoggingJob] do
HelloJob.perform_later("jeremy")
LoggingJob.perform_later("stewie")
RescueJob.perform_later("david")
end
end
end
def test_assert_enqueued_jobs_with_only_and_except_option_as_array
error = assert_raise ActiveJob::TestHelper::InvalidOptionsError do
assert_enqueued_jobs 2, only: [HelloJob, LoggingJob], except: [HelloJob, LoggingJob] do
HelloJob.perform_later("jeremy")
LoggingJob.perform_later("stewie")
RescueJob.perform_later("david")
end
end
assert_match(/`:only` and `:except`/, error.message)
end
def test_assert_no_enqueued_jobs_with_only_option
assert_nothing_raised do
assert_no_enqueued_jobs only: HelloJob do
@ -182,6 +311,24 @@ def test_assert_no_enqueued_jobs_with_only_option
end
end
def test_assert_no_enqueued_jobs_with_except_option
assert_nothing_raised do
assert_no_enqueued_jobs except: LoggingJob do
LoggingJob.perform_later
end
end
end
def test_assert_no_enqueued_jobs_with_only_and_except_option
error = assert_raise ActiveJob::TestHelper::InvalidOptionsError do
assert_no_enqueued_jobs only: HelloJob, except: HelloJob do
LoggingJob.perform_later
end
end
assert_match(/`:only` and `:except`/, error.message)
end
def test_assert_no_enqueued_jobs_with_only_option_failure
error = assert_raise ActiveSupport::TestCase::Assertion do
assert_no_enqueued_jobs only: HelloJob do
@ -193,6 +340,28 @@ def test_assert_no_enqueued_jobs_with_only_option_failure
assert_match(/0 .* but 1/, error.message)
end
def test_assert_no_enqueued_jobs_with_except_option_failure
error = assert_raise ActiveSupport::TestCase::Assertion do
assert_no_enqueued_jobs except: LoggingJob do
HelloJob.perform_later("jeremy")
LoggingJob.perform_later
end
end
assert_match(/0 .* but 1/, error.message)
end
def test_assert_no_enqueued_jobs_with_only_and_except_option_failure
error = assert_raise ActiveJob::TestHelper::InvalidOptionsError do
assert_no_enqueued_jobs only: HelloJob, except: HelloJob do
HelloJob.perform_later("jeremy")
LoggingJob.perform_later
end
end
assert_match(/`:only` and `:except`/, error.message)
end
def test_assert_no_enqueued_jobs_with_only_option_as_array
assert_nothing_raised do
assert_no_enqueued_jobs only: [HelloJob, RescueJob] do
@ -201,6 +370,25 @@ def test_assert_no_enqueued_jobs_with_only_option_as_array
end
end
def test_assert_no_enqueued_jobs_with_except_option_as_array
assert_nothing_raised do
assert_no_enqueued_jobs except: [HelloJob, RescueJob] do
HelloJob.perform_later
RescueJob.perform_later
end
end
end
def test_assert_no_enqueued_jobs_with_only_and_except_option_as_array
error = assert_raise ActiveJob::TestHelper::InvalidOptionsError do
assert_no_enqueued_jobs only: [HelloJob, RescueJob], except: [HelloJob, RescueJob] do
LoggingJob.perform_later
end
end
assert_match(/`:only` and `:except`/, error.message)
end
def test_assert_enqueued_job
assert_enqueued_with(job: LoggingJob, queue: "default") do
LoggingJob.set(wait_until: Date.tomorrow.noon).perform_later
@ -286,6 +474,14 @@ def test_performed_enqueue_jobs_with_only_option_doesnt_leak_outside_the_block
assert_nil queue_adapter.filter
end
def test_performed_enqueue_jobs_with_except_option_doesnt_leak_outside_the_block
assert_nil queue_adapter.reject
perform_enqueued_jobs except: HelloJob do
assert_equal HelloJob, queue_adapter.reject
end
assert_nil queue_adapter.reject
end
def test_assert_performed_jobs
assert_nothing_raised do
assert_performed_jobs 1 do
@ -391,6 +587,26 @@ def test_assert_performed_jobs_with_only_option
end
end
def test_assert_performed_jobs_with_except_option
assert_nothing_raised do
assert_performed_jobs 1, except: LoggingJob do
HelloJob.perform_later("jeremy")
LoggingJob.perform_later
end
end
end
def test_assert_performed_jobs_with_only_and_except_option
error = assert_raise ActiveJob::TestHelper::InvalidOptionsError do
assert_performed_jobs 1, only: HelloJob, except: HelloJob do
HelloJob.perform_later("jeremy")
LoggingJob.perform_later
end
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
@ -401,6 +617,28 @@ def test_assert_performed_jobs_with_only_option_as_array
end
end
def test_assert_performed_jobs_with_except_option_as_array
assert_nothing_raised do
assert_performed_jobs 1, except: [LoggingJob, RescueJob] do
HelloJob.perform_later("jeremy")
LoggingJob.perform_later("stewie")
RescueJob.perform_later("david")
end
end
end
def test_assert_performed_jobs_with_only_and_except_option_as_array
error = assert_raise ActiveJob::TestHelper::InvalidOptionsError do
assert_performed_jobs 2, only: [HelloJob, LoggingJob], except: [HelloJob, LoggingJob] do
HelloJob.perform_later("jeremy")
LoggingJob.perform_later("stewie")
RescueJob.perform_later("david")
end
end
assert_match(/`:only` and `:except`/, error.message)
end
def test_assert_performed_jobs_with_only_option_and_none_sent
error = assert_raise ActiveSupport::TestCase::Assertion do
assert_performed_jobs 1, only: HelloJob do
@ -411,6 +649,26 @@ def test_assert_performed_jobs_with_only_option_and_none_sent
assert_match(/1 .* but 0/, error.message)
end
def test_assert_performed_jobs_with_except_option_and_none_sent
error = assert_raise ActiveSupport::TestCase::Assertion do
assert_performed_jobs 1, except: LoggingJob do
LoggingJob.perform_later
end
end
assert_match(/1 .* but 0/, error.message)
end
def test_assert_performed_jobs_with_only_and_except_option_and_none_sent
error = assert_raise ActiveJob::TestHelper::InvalidOptionsError do
assert_performed_jobs 1, only: HelloJob, except: HelloJob do
LoggingJob.perform_later
end
end
assert_match(/`:only` and `:except`/, error.message)
end
def test_assert_performed_jobs_with_only_option_and_too_few_sent
error = assert_raise ActiveSupport::TestCase::Assertion do
assert_performed_jobs 5, only: HelloJob do
@ -422,6 +680,28 @@ def test_assert_performed_jobs_with_only_option_and_too_few_sent
assert_match(/5 .* but 1/, error.message)
end
def test_assert_performed_jobs_with_except_option_and_too_few_sent
error = assert_raise ActiveSupport::TestCase::Assertion do
assert_performed_jobs 5, except: LoggingJob do
HelloJob.perform_later("jeremy")
4.times { LoggingJob.perform_later }
end
end
assert_match(/5 .* but 1/, error.message)
end
def test_assert_performed_jobs_with_only_and_except_option_and_too_few_sent
error = assert_raise ActiveJob::TestHelper::InvalidOptionsError do
assert_performed_jobs 5, only: HelloJob, except: HelloJob do
HelloJob.perform_later("jeremy")
4.times { LoggingJob.perform_later }
end
end
assert_match(/`:only` and `:except`/, error.message)
end
def test_assert_performed_jobs_with_only_option_and_too_many_sent
error = assert_raise ActiveSupport::TestCase::Assertion do
assert_performed_jobs 1, only: HelloJob do
@ -432,6 +712,26 @@ def test_assert_performed_jobs_with_only_option_and_too_many_sent
assert_match(/1 .* but 2/, error.message)
end
def test_assert_performed_jobs_with_except_option_and_too_many_sent
error = assert_raise ActiveSupport::TestCase::Assertion do
assert_performed_jobs 1, except: LoggingJob do
2.times { HelloJob.perform_later("jeremy") }
end
end
assert_match(/1 .* but 2/, error.message)
end
def test_assert_performed_jobs_with_only_and_except_option_and_too_many_sent
error = assert_raise ActiveJob::TestHelper::InvalidOptionsError do
assert_performed_jobs 1, only: HelloJob, except: HelloJob do
2.times { HelloJob.perform_later("jeremy") }
end
end
assert_match(/`:only` and `:except`/, error.message)
end
def test_assert_no_performed_jobs_with_only_option
assert_nothing_raised do
assert_no_performed_jobs only: HelloJob do
@ -440,6 +740,24 @@ def test_assert_no_performed_jobs_with_only_option
end
end
def test_assert_no_performed_jobs_with_except_option
assert_nothing_raised do
assert_no_performed_jobs except: LoggingJob do
LoggingJob.perform_later
end
end
end
def test_assert_no_performed_jobs_with_only_and_except_option
error = assert_raise ActiveJob::TestHelper::InvalidOptionsError do
assert_no_performed_jobs only: HelloJob, except: HelloJob do
LoggingJob.perform_later
end
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
@ -448,6 +766,25 @@ def test_assert_no_performed_jobs_with_only_option_as_array
end
end
def test_assert_no_performed_jobs_with_except_option_as_array
assert_nothing_raised do
assert_no_performed_jobs except: [HelloJob, RescueJob] do
HelloJob.perform_later
RescueJob.perform_later
end
end
end
def test_assert_no_performed_jobs_with_only_and_except_option_as_array
error = assert_raise ActiveJob::TestHelper::InvalidOptionsError do
assert_no_performed_jobs only: [HelloJob, RescueJob], except: [HelloJob, RescueJob] do
LoggingJob.perform_later
end
end
assert_match(/`:only` and `:except`/, error.message)
end
def test_assert_no_performed_jobs_with_only_option_failure
error = assert_raise ActiveSupport::TestCase::Assertion do
assert_no_performed_jobs only: HelloJob do
@ -459,6 +796,28 @@ def test_assert_no_performed_jobs_with_only_option_failure
assert_match(/0 .* but 1/, error.message)
end
def test_assert_no_performed_jobs_with_except_option_failure
error = assert_raise ActiveSupport::TestCase::Assertion do
assert_no_performed_jobs except: LoggingJob do
HelloJob.perform_later("jeremy")
LoggingJob.perform_later
end
end
assert_match(/0 .* but 1/, error.message)
end
def test_assert_no_performed_jobs_with_only_and_except_option_failure
error = assert_raise ActiveJob::TestHelper::InvalidOptionsError do
assert_no_performed_jobs only: HelloJob, except: HelloJob do
HelloJob.perform_later("jeremy")
LoggingJob.perform_later
end
end
assert_match(/`:only` and `:except`/, error.message)
end
def test_assert_performed_job
assert_performed_with(job: NestedJob, queue: "default") do
NestedJob.perform_later