aboutsummaryrefslogtreecommitdiffstats
path: root/activejob
diff options
context:
space:
mode:
authorbogdanvlviv <bogdanvlviv@gmail.com>2018-08-16 21:54:26 +0300
committerbogdanvlviv <bogdanvlviv@gmail.com>2018-08-20 13:05:29 +0300
commit11634e8ef85870535bad61b999e0f9ec36f6f963 (patch)
treeb5710303e045cab7479846bfa57e6f5faedfb3d9 /activejob
parent2bf8b4eb0eceb37fc278c71371a659979844dfb2 (diff)
downloadrails-11634e8ef85870535bad61b999e0f9ec36f6f963.tar.gz
rails-11634e8ef85870535bad61b999e0f9ec36f6f963.tar.bz2
rails-11634e8ef85870535bad61b999e0f9ec36f6f963.zip
Fix `assert_performed_jobs` and `assert_no_performed_jobs`
Execution of `assert_performed_jobs`, and `assert_no_performed_jobs` without a block should respect passed `:except`, `:only`, and `:queue` options.
Diffstat (limited to 'activejob')
-rw-r--r--activejob/CHANGELOG.md5
-rw-r--r--activejob/lib/active_job/test_helper.rb42
-rw-r--r--activejob/test/cases/test_helper_test.rb266
3 files changed, 289 insertions, 24 deletions
diff --git a/activejob/CHANGELOG.md b/activejob/CHANGELOG.md
index 69a5d469e4..0b440e5056 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 4dae1a9969..d3a3e0a156 100644
--- a/activejob/lib/active_job/test_helper.rb
+++ b/activejob/lib/active_job/test_helper.rb
@@ -176,7 +176,7 @@ module ActiveJob
# Asserts that the number of performed jobs matches the given number.
# If no block is passed, <tt>perform_enqueued_jobs</tt>
- # 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 @@ module ActiveJob
# 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 @@ module ActiveJob
# 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 @@ module ActiveJob
# 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 @@ module ActiveJob
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 @@ module ActiveJob
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 eade285412..f64b886d67 100644
--- a/activejob/test/cases/test_helper_test.rb
+++ b/activejob/test/cases/test_helper_test.rb
@@ -677,7 +677,7 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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 @@ class PerformedJobsTest < ActiveJob::TestCase
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