Allow assert_performed_with to be called without a block.

Example:
```
def test_assert_performed_with
  MyJob.perform_later(1,2,3)

  perform_enqueued_jobs

  assert_performed_with(job: MyJob, args: [1,2,3], queue: 'high')
end
```

Follow up #33626.
This commit is contained in:
bogdanvlviv 2018-08-16 22:38:35 +03:00
parent 11634e8ef8
commit 2ec60fb818
No known key found for this signature in database
GPG Key ID: E4ACD76A6DB6DFDD
3 changed files with 129 additions and 14 deletions

@ -1,3 +1,7 @@
* Allow `assert_performed_with` to be called without a block.
*bogdanvlviv*
* Execution of `assert_performed_jobs`, and `assert_no_performed_jobs`
without a block should respect passed `:except`, `:only`, and `:queue` options.

@ -361,7 +361,25 @@ def assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil)
instantiate_job(matching_job)
end
# Asserts that the job passed in the block has been performed with the given arguments.
# Asserts that the job has been performed with the given arguments.
#
# def test_assert_performed_with
# MyJob.perform_later(1,2,3)
#
# perform_enqueued_jobs
#
# assert_performed_with(job: MyJob, args: [1,2,3], queue: 'high')
#
# MyJob.set(wait_until: Date.tomorrow.noon).perform_later
#
# perform_enqueued_jobs
#
# assert_performed_with(job: MyJob, at: Date.tomorrow.noon)
# end
#
# If a block is passed, that block performs all of the jobs that were
# enqueued throughout the duration of the block and asserts that
# the job has been performed with the given arguments in the block.
#
# def test_assert_performed_with
# assert_performed_with(job: MyJob, args: [1,2,3], queue: 'high') do
@ -372,15 +390,24 @@ def assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil)
# MyJob.set(wait_until: Date.tomorrow.noon).perform_later
# end
# end
def assert_performed_with(job: nil, args: nil, at: nil, queue: nil)
original_performed_jobs_count = performed_jobs.count
def assert_performed_with(job: nil, args: nil, at: nil, queue: nil, &block)
expected = { job: job, args: args, at: at, queue: queue }.compact
serialized_args = serialize_args_for_assertion(expected)
perform_enqueued_jobs { yield }
in_block_jobs = performed_jobs.drop(original_performed_jobs_count)
matching_job = in_block_jobs.find do |in_block_job|
serialized_args.all? { |key, value| value == in_block_job[key] }
if block_given?
original_performed_jobs_count = performed_jobs.count
perform_enqueued_jobs(&block)
jobs = performed_jobs.drop(original_performed_jobs_count)
else
jobs = performed_jobs
end
matching_job = jobs.find do |performed_job|
serialized_args.all? { |key, value| value == performed_job[key] }
end
assert matching_job, "No performed job found with #{expected}"
instantiate_job(matching_job)
end

@ -1470,13 +1470,21 @@ def test_assert_no_performed_jobs_without_block_with_except_and_queue_options_fa
assert_match(/0 .* but 1/, error.message)
end
def test_assert_performed_job
def test_assert_performed_with
assert_performed_with(job: NestedJob, queue: "default") do
NestedJob.perform_later
end
end
def test_assert_performed_job_returns
def test_assert_performed_with_without_block
NestedJob.perform_later
perform_enqueued_jobs
assert_performed_with(job: NestedJob, queue: "default")
end
def test_assert_performed_with_returns
job = assert_performed_with(job: NestedJob, queue: "default") do
NestedJob.perform_later
end
@ -1487,7 +1495,20 @@ def test_assert_performed_job_returns
assert_equal "default", job.queue_name
end
def test_assert_performed_job_failure
def test_assert_performed_with_without_block_returns
NestedJob.perform_later
perform_enqueued_jobs
job = assert_performed_with(job: NestedJob, queue: "default")
assert_instance_of NestedJob, job
assert_nil job.scheduled_at
assert_equal [], job.arguments
assert_equal "default", job.queue_name
end
def test_assert_performed_with_failure
assert_raise ActiveSupport::TestCase::Assertion do
assert_performed_with(job: LoggingJob) do
HelloJob.perform_later
@ -1501,7 +1522,23 @@ def test_assert_performed_job_failure
end
end
def test_assert_performed_job_with_at_option
def test_assert_performed_with_without_block_failure
HelloJob.perform_later
perform_enqueued_jobs
assert_raise ActiveSupport::TestCase::Assertion do
assert_performed_with(job: LoggingJob)
end
HelloJob.set(queue: "important").perform_later
assert_raise ActiveSupport::TestCase::Assertion do
assert_performed_with(job: HelloJob, queue: "low")
end
end
def test_assert_performed_with_with_at_option
assert_performed_with(job: HelloJob, at: Date.tomorrow.noon) do
HelloJob.set(wait_until: Date.tomorrow.noon).perform_later
end
@ -1513,14 +1550,37 @@ def test_assert_performed_job_with_at_option
end
end
def test_assert_performed_job_with_global_id_args
def test_assert_performed_with_without_block_with_at_option
HelloJob.set(wait_until: Date.tomorrow.noon).perform_later
perform_enqueued_jobs
assert_performed_with(job: HelloJob, at: Date.tomorrow.noon)
HelloJob.set(wait_until: Date.tomorrow.noon).perform_later
perform_enqueued_jobs
assert_raise ActiveSupport::TestCase::Assertion do
assert_performed_with(job: HelloJob, at: Date.today.noon)
end
end
def test_assert_performed_wiht_with_global_id_args
ricardo = Person.new(9)
assert_performed_with(job: HelloJob, args: [ricardo]) do
HelloJob.perform_later(ricardo)
end
end
def test_assert_performed_job_failure_with_global_id_args
def test_assert_performed_with_without_bllock_with_global_id_args
ricardo = Person.new(9)
HelloJob.perform_later(ricardo)
perform_enqueued_jobs
assert_performed_with(job: HelloJob, args: [ricardo])
end
def test_assert_performed_with_failure_with_global_id_args
ricardo = Person.new(9)
wilma = Person.new(11)
error = assert_raise ActiveSupport::TestCase::Assertion do
@ -1532,7 +1592,19 @@ def test_assert_performed_job_failure_with_global_id_args
assert_equal "No performed job found with {:job=>HelloJob, :args=>[#{wilma.inspect}]}", error.message
end
def test_assert_performed_job_does_not_change_jobs_count
def test_assert_performed_with_without_block_failure_with_global_id_args
ricardo = Person.new(9)
wilma = Person.new(11)
HelloJob.perform_later(ricardo)
perform_enqueued_jobs
error = assert_raise ActiveSupport::TestCase::Assertion do
assert_performed_with(job: HelloJob, args: [wilma])
end
assert_equal "No performed job found with {:job=>HelloJob, :args=>[#{wilma.inspect}]}", error.message
end
def test_assert_performed_with_does_not_change_jobs_count
assert_performed_with(job: HelloJob) do
HelloJob.perform_later
end
@ -1543,6 +1615,18 @@ def test_assert_performed_job_does_not_change_jobs_count
assert_equal 2, queue_adapter.performed_jobs.count
end
def test_assert_performed_with_without_block_does_not_change_jobs_count
HelloJob.perform_later
perform_enqueued_jobs
assert_performed_with(job: HelloJob)
perform_enqueued_jobs
HelloJob.perform_later
assert_performed_with(job: HelloJob)
assert_equal 2, queue_adapter.performed_jobs.count
end
end
class OverrideQueueAdapterTest < ActiveJob::TestCase