aboutsummaryrefslogtreecommitdiffstats
path: root/activejob
diff options
context:
space:
mode:
authorGannon McGibbon <gannon.mcgibbon@gmail.com>2019-03-22 15:52:36 -0400
committerGannon McGibbon <gannon.mcgibbon@gmail.com>2019-03-22 17:04:56 -0400
commit7f038621dfd7eba316b601b010cbf442b63ea17e (patch)
tree6dd343257cb2e2e91981e258c6a91ab297d2f3af /activejob
parent4c8a333ac1278ec49cc78097b1c64d00338c01f2 (diff)
downloadrails-7f038621dfd7eba316b601b010cbf442b63ea17e.tar.gz
rails-7f038621dfd7eba316b601b010cbf442b63ea17e.tar.bz2
rails-7f038621dfd7eba316b601b010cbf442b63ea17e.zip
Drop microseconds in job argument assertions
Diffstat (limited to 'activejob')
-rw-r--r--activejob/CHANGELOG.md5
-rw-r--r--activejob/lib/active_job/test_helper.rb14
-rw-r--r--activejob/test/cases/test_helper_test.rb54
3 files changed, 73 insertions, 0 deletions
diff --git a/activejob/CHANGELOG.md b/activejob/CHANGELOG.md
index de375baa9c..e09ba4b055 100644
--- a/activejob/CHANGELOG.md
+++ b/activejob/CHANGELOG.md
@@ -1,3 +1,8 @@
+* Make job argument assertions with `Time`, `ActiveSupport::TimeWithZone`, and `DateTime` work by dropping microseconds. Microsecond precision is lost during serialization.
+
+ *Gannon McGibbon*
+
+
## Rails 6.0.0.beta3 (March 11, 2019) ##
* No changes.
diff --git a/activejob/lib/active_job/test_helper.rb b/activejob/lib/active_job/test_helper.rb
index f03780b91e..e5e2b086bc 100644
--- a/activejob/lib/active_job/test_helper.rb
+++ b/activejob/lib/active_job/test_helper.rb
@@ -631,6 +631,20 @@ module ActiveJob
def prepare_args_for_assertion(args)
args.dup.tap do |arguments|
arguments[:at] = arguments[:at].to_f if arguments[:at]
+ arguments[:args] = round_time_arguments(arguments[:args]) if arguments[:args]
+ end
+ end
+
+ def round_time_arguments(argument)
+ case argument
+ when Time, ActiveSupport::TimeWithZone, DateTime
+ argument.change(usec: 0)
+ when Hash
+ argument.transform_values { |value| round_time_arguments(value) }
+ when Array
+ argument.map { |element| round_time_arguments(element) }
+ else
+ argument
end
end
diff --git a/activejob/test/cases/test_helper_test.rb b/activejob/test/cases/test_helper_test.rb
index 4d934df31b..d6607cb6b6 100644
--- a/activejob/test/cases/test_helper_test.rb
+++ b/activejob/test/cases/test_helper_test.rb
@@ -581,6 +581,33 @@ class EnqueuedJobsTest < ActiveJob::TestCase
end
end
+ def test_assert_enqueued_with_time
+ now = Time.now
+ args = [{ argument1: [now] }]
+
+ assert_enqueued_with(job: MultipleKwargsJob, args: args) do
+ MultipleKwargsJob.perform_later(argument1: [now])
+ end
+ end
+
+ def test_assert_enqueued_with_date_time
+ now = DateTime.now
+ args = [{ argument1: [now] }]
+
+ assert_enqueued_with(job: MultipleKwargsJob, args: args) do
+ MultipleKwargsJob.perform_later(argument1: [now])
+ end
+ end
+
+ def test_assert_enqueued_with_time_with_zone
+ now = Time.now.in_time_zone("Tokyo")
+ args = [{ argument1: [now] }]
+
+ assert_enqueued_with(job: MultipleKwargsJob, args: args) do
+ MultipleKwargsJob.perform_later(argument1: [now])
+ end
+ end
+
def test_assert_enqueued_with_with_no_block_args
assert_raise ArgumentError do
NestedJob.set(wait_until: Date.tomorrow.noon).perform_later
@@ -1681,6 +1708,33 @@ class PerformedJobsTest < ActiveJob::TestCase
end
end
+ def test_assert_performed_with_time
+ now = Time.now
+ args = [{ argument1: { now: now } }]
+
+ assert_enqueued_with(job: MultipleKwargsJob, args: args) do
+ MultipleKwargsJob.perform_later(argument1: { now: now })
+ end
+ end
+
+ def test_assert_performed_with_date_time
+ now = DateTime.now
+ args = [{ argument1: { now: now } }]
+
+ assert_enqueued_with(job: MultipleKwargsJob, args: args) do
+ MultipleKwargsJob.perform_later(argument1: { now: now })
+ end
+ end
+
+ def test_assert_performed_with_time_with_zone
+ now = Time.now.in_time_zone("Tokyo")
+ args = [{ argument1: { now: now } }]
+
+ assert_enqueued_with(job: MultipleKwargsJob, args: args) do
+ MultipleKwargsJob.perform_later(argument1: { now: now })
+ end
+ end
+
def test_assert_performed_with_with_global_id_args
ricardo = Person.new(9)
assert_performed_with(job: HelloJob, args: [ricardo]) do