2017-10-23 09:29:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "helper"
|
|
|
|
require "active_job/serializers"
|
|
|
|
|
|
|
|
class SerializersTest < ActiveSupport::TestCase
|
|
|
|
class DummyValueObject
|
|
|
|
attr_accessor :value
|
|
|
|
|
|
|
|
def initialize(value)
|
|
|
|
@value = value
|
|
|
|
end
|
2018-02-09 15:23:05 -05:00
|
|
|
|
|
|
|
def ==(other)
|
|
|
|
self.value == other.value
|
|
|
|
end
|
2017-10-23 09:29:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class DummySerializer < ActiveJob::Serializers::ObjectSerializer
|
2018-02-09 16:32:35 -05:00
|
|
|
def serialize(object)
|
|
|
|
super({ "value" => object.value })
|
|
|
|
end
|
2017-10-23 09:29:28 -04:00
|
|
|
|
2018-02-09 16:32:35 -05:00
|
|
|
def deserialize(hash)
|
|
|
|
DummyValueObject.new(hash["value"])
|
|
|
|
end
|
2017-10-23 09:29:28 -04:00
|
|
|
|
2018-02-09 16:32:35 -05:00
|
|
|
private
|
2017-10-23 09:29:28 -04:00
|
|
|
def klass
|
|
|
|
DummyValueObject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
setup do
|
|
|
|
@value_object = DummyValueObject.new 123
|
2018-02-09 14:24:55 -05:00
|
|
|
@original_serializers = ActiveJob::Serializers.serializers
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
|
|
|
ActiveJob::Serializers._additional_serializers = @original_serializers
|
2017-10-23 09:29:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "can't serialize unknown object" do
|
|
|
|
assert_raises ActiveJob::SerializationError do
|
|
|
|
ActiveJob::Serializers.serialize @value_object
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-09 15:23:05 -05:00
|
|
|
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
|
|
|
|
|
2017-10-23 09:29:28 -04:00
|
|
|
test "won't deserialize unknown hash" do
|
|
|
|
hash = { "_dummy_serializer" => 123, "_aj_symbol_keys" => [] }
|
2018-02-12 14:16:41 -05:00
|
|
|
error = assert_raises(ArgumentError) do
|
|
|
|
ActiveJob::Serializers.deserialize(hash)
|
|
|
|
end
|
|
|
|
assert_equal(
|
|
|
|
'Serializer name is not present in the argument: {"_dummy_serializer"=>123, "_aj_symbol_keys"=>[]}',
|
|
|
|
error.message
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "won't deserialize unknown serializer" do
|
|
|
|
hash = { "_aj_serialized" => "DoNotExist", "value" => 123 }
|
|
|
|
error = assert_raises(ArgumentError) do
|
|
|
|
ActiveJob::Serializers.deserialize(hash)
|
|
|
|
end
|
|
|
|
assert_equal(
|
2018-02-16 17:45:17 -05:00
|
|
|
"Serializer DoNotExist is not known",
|
2018-02-12 14:16:41 -05:00
|
|
|
error.message
|
|
|
|
)
|
2018-02-09 15:23:05 -05:00
|
|
|
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)
|
2017-10-23 09:29:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "adds new serializer" do
|
2018-02-09 14:24:55 -05:00
|
|
|
ActiveJob::Serializers.add_serializers DummySerializer
|
|
|
|
assert ActiveJob::Serializers.serializers.include?(DummySerializer)
|
2017-10-23 09:29:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "can't add serializer with the same key twice" do
|
2018-02-09 14:24:55 -05:00
|
|
|
ActiveJob::Serializers.add_serializers DummySerializer
|
2018-02-12 14:16:41 -05:00
|
|
|
assert_no_difference(-> { ActiveJob::Serializers.serializers.size }) do
|
2018-02-09 14:24:55 -05:00
|
|
|
ActiveJob::Serializers.add_serializers DummySerializer
|
2017-10-23 09:29:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|