1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# frozen_string_literal: true
require "helper"
require "active_job/arguments"
require "models/person"
require "active_support/core_ext/hash/indifferent_access"
require "jobs/kwargs_job"
class ArgumentSerializationTest < ActiveSupport::TestCase
setup do
@person = Person.find("5")
end
[ nil, 1, 1.0, 1_000_000_000_000_000_000_000,
"a", true, false, BigDecimal(5),
:a, 1.day, Date.new(2001, 2, 3), Time.new(2002, 10, 31, 2, 2, 2, "+02:00"),
DateTime.new(2001, 2, 3, 4, 5, 6, "+03:00"),
ActiveSupport::TimeWithZone.new(Time.utc(1999, 12, 31, 23, 59, 59), ActiveSupport::TimeZone["UTC"]),
[ 1, "a" ],
{ "a" => 1 }
].each do |arg|
test "serializes #{arg.class} - #{arg} verbatim" do
assert_arguments_unchanged arg
end
end
[ Object.new, self, Person.find("5").to_gid ].each do |arg|
test "does not serialize #{arg.class}" do
assert_raises ActiveJob::SerializationError do
ActiveJob::Arguments.serialize [ arg ]
end
assert_raises ActiveJob::DeserializationError do
ActiveJob::Arguments.deserialize [ arg ]
end
end
end
test "should convert records to Global IDs" do
assert_arguments_roundtrip [@person]
end
test "should dive deep into arrays and hashes" do
assert_arguments_roundtrip [3, [@person]]
assert_arguments_roundtrip [{ "a" => @person }]
end
test "should maintain string and symbol keys" do
assert_arguments_roundtrip([a: 1, "b" => 2])
end
test "serialize a hash" do
symbol_key = { a: 1 }
string_key = { "a" => 1 }
indifferent_access = { a: 1 }.with_indifferent_access
assert_equal(
{ "a" => 1, "_aj_symbol_keys" => ["a"] },
ActiveJob::Arguments.serialize([symbol_key]).first
)
assert_equal(
{ "a" => 1, "_aj_symbol_keys" => [] },
ActiveJob::Arguments.serialize([string_key]).first
)
assert_equal(
{ "a" => 1, "_aj_hash_with_indifferent_access" => true },
ActiveJob::Arguments.serialize([indifferent_access]).first
)
end
test "deserialize a hash" do
symbol_key = { "a" => 1, "_aj_symbol_keys" => ["a"] }
string_key = { "a" => 1, "_aj_symbol_keys" => [] }
another_string_key = { "a" => 1 }
indifferent_access = { "a" => 1, "_aj_hash_with_indifferent_access" => true }
assert_equal(
{ a: 1 },
ActiveJob::Arguments.deserialize([symbol_key]).first
)
assert_equal(
{ "a" => 1 },
ActiveJob::Arguments.deserialize([string_key]).first
)
assert_equal(
{ "a" => 1 },
ActiveJob::Arguments.deserialize([another_string_key]).first
)
assert_equal(
{ "a" => 1 },
ActiveJob::Arguments.deserialize([indifferent_access]).first
)
end
test "should maintain hash with indifferent access" do
symbol_key = { a: 1 }
string_key = { "a" => 1 }
indifferent_access = { a: 1 }.with_indifferent_access
assert_not_instance_of ActiveSupport::HashWithIndifferentAccess, perform_round_trip([symbol_key]).first
assert_not_instance_of ActiveSupport::HashWithIndifferentAccess, perform_round_trip([string_key]).first
assert_instance_of ActiveSupport::HashWithIndifferentAccess, perform_round_trip([indifferent_access]).first
end
test "should maintain time with zone" do
Time.use_zone "Alaska" do
time_with_zone = Time.new(2002, 10, 31, 2, 2, 2).in_time_zone
assert_instance_of ActiveSupport::TimeWithZone, perform_round_trip([time_with_zone]).first
assert_arguments_unchanged time_with_zone
end
end
test "should disallow non-string/symbol hash keys" do
assert_raises ActiveJob::SerializationError do
ActiveJob::Arguments.serialize [ { 1 => 2 } ]
end
assert_raises ActiveJob::SerializationError do
ActiveJob::Arguments.serialize [ { a: [{ 2 => 3 }] } ]
end
end
test "should not allow reserved hash keys" do
["_aj_globalid", :_aj_globalid,
"_aj_symbol_keys", :_aj_symbol_keys,
"_aj_hash_with_indifferent_access", :_aj_hash_with_indifferent_access,
"_aj_serialized", :_aj_serialized].each do |key|
assert_raises ActiveJob::SerializationError do
ActiveJob::Arguments.serialize [key => 1]
end
end
end
test "should not allow non-primitive objects" do
assert_raises ActiveJob::SerializationError do
ActiveJob::Arguments.serialize [Object.new]
end
assert_raises ActiveJob::SerializationError do
ActiveJob::Arguments.serialize [1, [Object.new]]
end
end
test "allows for keyword arguments" do
KwargsJob.perform_later(argument: 2)
assert_equal "Job with argument: 2", JobBuffer.last_value
end
test "raises a friendly SerializationError for records without ids" do
err = assert_raises ActiveJob::SerializationError do
ActiveJob::Arguments.serialize [Person.new(nil)]
end
assert_match "Unable to serialize Person without an id.", err.message
end
private
def assert_arguments_unchanged(*args)
assert_arguments_roundtrip args
end
def assert_arguments_roundtrip(args)
assert_equal args, perform_round_trip(args)
end
def perform_round_trip(args)
ActiveJob::Arguments.deserialize(ActiveJob::Arguments.serialize(args))
end
end
|