aboutsummaryrefslogtreecommitdiffstats
path: root/activejob/test
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2018-02-09 15:23:05 -0500
committerRafael Mendonça França <rafaelmfranca@gmail.com>2018-02-14 13:10:07 -0500
commitea615332452e6860872020aa161c5d34e81f1eea (patch)
tree2ba5478b1e48b56f722aed17c3f6d2d5f9d1d7d9 /activejob/test
parent9bc8b4bbde4634e0e4bddcffa25e0bf8d74d19cd (diff)
downloadrails-ea615332452e6860872020aa161c5d34e81f1eea.tar.gz
rails-ea615332452e6860872020aa161c5d34e81f1eea.tar.bz2
rails-ea615332452e6860872020aa161c5d34e81f1eea.zip
Only add one more custom key in the serialized hash
Now custom serialziers can register itself in the serialized hash using the "_aj_serialized" key that constains the serializer name. This way we can avoid poluting the hash with many reserved keys.
Diffstat (limited to 'activejob/test')
-rw-r--r--activejob/test/cases/serializers_test.rb31
1 files changed, 23 insertions, 8 deletions
diff --git a/activejob/test/cases/serializers_test.rb b/activejob/test/cases/serializers_test.rb
index 3b526c932b..fb0e6ecae6 100644
--- a/activejob/test/cases/serializers_test.rb
+++ b/activejob/test/cases/serializers_test.rb
@@ -10,20 +10,20 @@ class SerializersTest < ActiveSupport::TestCase
def initialize(value)
@value = value
end
+
+ def ==(other)
+ self.value == other.value
+ end
end
class DummySerializer < ActiveJob::Serializers::ObjectSerializer
class << self
def serialize(object)
- { key => object.value }
+ super({ "value" => object.value })
end
def deserialize(hash)
- DummyValueObject.new(hash[key])
- end
-
- def key
- "_dummy_serializer"
+ DummyValueObject.new(hash["value"])
end
private
@@ -49,9 +49,24 @@ class SerializersTest < ActiveSupport::TestCase
end
end
+ test "will serialize objects with serializers registered" do
+ ActiveJob::Serializers.add_serializers DummySerializer
+
+ assert_equal(
+ { "_aj_serialized" => "SerializersTest::DummySerializer", "value" => 123 },
+ ActiveJob::Serializers.serialize(@value_object)
+ )
+ end
+
test "won't deserialize unknown hash" do
hash = { "_dummy_serializer" => 123, "_aj_symbol_keys" => [] }
- assert ActiveJob::Serializers.deserialize(hash), hash.except("_aj_symbol_keys")
+ assert_equal({ "_dummy_serializer" => 123 }, ActiveJob::Serializers.deserialize(hash))
+ end
+
+ test "will deserialize know serialized objects" do
+ ActiveJob::Serializers.add_serializers DummySerializer
+ hash = { "_aj_serialized" => "SerializersTest::DummySerializer", "value" => 123 }
+ assert_equal DummyValueObject.new(123), ActiveJob::Serializers.deserialize(hash)
end
test "adds new serializer" do
@@ -61,7 +76,7 @@ class SerializersTest < ActiveSupport::TestCase
test "can't add serializer with the same key twice" do
ActiveJob::Serializers.add_serializers DummySerializer
- assert_raises ArgumentError do
+ assert_no_difference(-> { ActiveJob::Serializers.serializers.size } ) do
ActiveJob::Serializers.add_serializers DummySerializer
end
end