aboutsummaryrefslogtreecommitdiffstats
path: root/activejob
diff options
context:
space:
mode:
authorSharang Dashputre <sharang.d@gmail.com>2018-09-12 10:52:17 +0530
committerSharang Dashputre <sharang.d@gmail.com>2018-09-21 00:09:40 +0530
commit14e278f876bb0def5378db78f53024c2a7553fa0 (patch)
tree75355b024595f58e351415a541a79b52d63e960c /activejob
parentd99d4a58fa5c1344196ab1cb1b162348597f67bf (diff)
downloadrails-14e278f876bb0def5378db78f53024c2a7553fa0.tar.gz
rails-14e278f876bb0def5378db78f53024c2a7553fa0.tar.bz2
rails-14e278f876bb0def5378db78f53024c2a7553fa0.zip
Make `assert_<enqueued|performed>_with()` compare hashes ignoring order of keys
The test helpers now treat `{ a: 1, b: 2 }` and `{ b: 2, a: 1 }` as equals
Diffstat (limited to 'activejob')
-rw-r--r--activejob/lib/active_job/test_helper.rb25
-rw-r--r--activejob/test/cases/test_helper_test.rb13
-rw-r--r--activejob/test/jobs/multiple_kwargs_job.rb9
3 files changed, 38 insertions, 9 deletions
diff --git a/activejob/lib/active_job/test_helper.rb b/activejob/lib/active_job/test_helper.rb
index bb9e3e6ca4..984fa5e318 100644
--- a/activejob/lib/active_job/test_helper.rb
+++ b/activejob/lib/active_job/test_helper.rb
@@ -345,7 +345,7 @@ module ActiveJob
# end
def assert_enqueued_with(job: nil, args: nil, at: nil, queue: nil)
expected = { job: job, args: args, at: at, queue: queue }.compact
- serialized_args = serialize_args_for_assertion(expected)
+ expected_args = prepare_args_for_assertion(expected)
if block_given?
original_enqueued_jobs_count = enqueued_jobs.count
@@ -358,7 +358,8 @@ module ActiveJob
end
matching_job = jobs.find do |enqueued_job|
- serialized_args.all? { |key, value| value == enqueued_job[key] }
+ deserialized_job = deserialize_args_for_assertion(enqueued_job)
+ expected_args.all? { |key, value| value == deserialized_job[key] }
end
assert matching_job, "No enqueued job found with #{expected}"
@@ -396,7 +397,7 @@ module ActiveJob
# end
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)
+ expected_args = prepare_args_for_assertion(expected)
if block_given?
original_performed_jobs_count = performed_jobs.count
@@ -408,8 +409,9 @@ module ActiveJob
jobs = performed_jobs
end
- matching_job = jobs.find do |performed_job|
- serialized_args.all? { |key, value| value == performed_job[key] }
+ matching_job = jobs.find do |enqueued_job|
+ deserialized_job = deserialize_args_for_assertion(enqueued_job)
+ expected_args.all? { |key, value| value == deserialized_job[key] }
end
assert matching_job, "No performed job found with #{expected}"
@@ -552,10 +554,15 @@ module ActiveJob
end
end
- def serialize_args_for_assertion(args)
- args.dup.tap do |serialized_args|
- serialized_args[:args] = ActiveJob::Arguments.serialize(serialized_args[:args]) if serialized_args[:args]
- serialized_args[:at] = serialized_args[:at].to_f if serialized_args[:at]
+ def prepare_args_for_assertion(args)
+ args.dup.tap do |arguments|
+ arguments[:at] = arguments[:at].to_f if arguments[:at]
+ end
+ end
+
+ def deserialize_args_for_assertion(job)
+ job.dup.tap do |job|
+ job[:args] = ActiveJob::Arguments.deserialize(job[:args]) if job[:args]
end
end
diff --git a/activejob/test/cases/test_helper_test.rb b/activejob/test/cases/test_helper_test.rb
index 160b876e85..1e3fcf1fc2 100644
--- a/activejob/test/cases/test_helper_test.rb
+++ b/activejob/test/cases/test_helper_test.rb
@@ -8,6 +8,7 @@ require "jobs/logging_job"
require "jobs/nested_job"
require "jobs/rescue_job"
require "jobs/inherited_job"
+require "jobs/multiple_kwargs_job"
require "models/person"
class EnqueuedJobsTest < ActiveJob::TestCase
@@ -555,6 +556,12 @@ class EnqueuedJobsTest < ActiveJob::TestCase
assert_enqueued_with(job: HelloJob, at: Date.tomorrow.noon)
end
+ def test_assert_enqueued_with_with_hash_arg
+ assert_enqueued_with(job: MultipleKwargsJob, args: [{ argument1: 1, argument2: { a: 1, b: 2 } }]) do
+ MultipleKwargsJob.perform_later(argument2: { b: 2, a: 1 }, argument1: 1)
+ end
+ end
+
def test_assert_enqueued_with_with_global_id_args
ricardo = Person.new(9)
assert_enqueued_with(job: HelloJob, args: [ricardo]) do
@@ -1566,6 +1573,12 @@ class PerformedJobsTest < ActiveJob::TestCase
end
end
+ def test_assert_performed_with_with_hash_arg
+ assert_performed_with(job: MultipleKwargsJob, args: [{ argument1: 1, argument2: { a: 1, b: 2 } }]) do
+ MultipleKwargsJob.perform_later(argument2: { b: 2, a: 1 }, argument1: 1)
+ end
+ end
+
def test_assert_performed_with_with_global_id_args
ricardo = Person.new(9)
assert_performed_with(job: HelloJob, args: [ricardo]) do
diff --git a/activejob/test/jobs/multiple_kwargs_job.rb b/activejob/test/jobs/multiple_kwargs_job.rb
new file mode 100644
index 0000000000..b355c4ce1a
--- /dev/null
+++ b/activejob/test/jobs/multiple_kwargs_job.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+require_relative "../support/job_buffer"
+
+class MultipleKwargsJob < ActiveJob::Base
+ def perform(argument1:, argument2:)
+ JobBuffer.add("Job with argument1: #{argument1}, argument2: #{argument2}")
+ end
+end