2012-07-03 23:22:52 -04:00
|
|
|
require 'test_helper'
|
2013-05-10 20:43:56 -04:00
|
|
|
require 'custom_json_serializer'
|
2012-07-03 23:22:52 -04:00
|
|
|
|
|
|
|
class SerializerTest < ActiveSupport::TestCase
|
|
|
|
|
|
|
|
context 'YAML Serializer' do
|
|
|
|
setup do
|
|
|
|
Fluxor.instance_eval <<-END
|
|
|
|
has_paper_trail
|
|
|
|
END
|
|
|
|
|
|
|
|
@fluxor = Fluxor.create :name => 'Some text.'
|
2013-01-31 17:32:36 -05:00
|
|
|
@original_fluxor_attributes = @fluxor.send(:item_before_change).attributes # this is exactly what PaperTrail serializes
|
2012-07-03 23:22:52 -04:00
|
|
|
@fluxor.update_attributes :name => 'Some more text.'
|
|
|
|
end
|
|
|
|
|
2013-10-17 22:05:24 -04:00
|
|
|
should 'work with the default `YAML` serializer' do
|
2012-07-03 23:22:52 -04:00
|
|
|
# Normal behaviour
|
|
|
|
assert_equal 2, @fluxor.versions.length
|
|
|
|
assert_nil @fluxor.versions[0].reify
|
|
|
|
assert_equal 'Some text.', @fluxor.versions[1].reify.name
|
|
|
|
|
2013-10-17 22:05:24 -04:00
|
|
|
# Check values are stored as `YAML`.
|
2013-01-31 16:39:52 -05:00
|
|
|
assert_equal @original_fluxor_attributes, YAML.load(@fluxor.versions[1].object)
|
2013-02-14 12:09:14 -05:00
|
|
|
# This test can't consistently pass in Ruby1.8 because hashes do no preserve order, which means the order of the
|
2013-10-17 22:05:24 -04:00
|
|
|
# attributes in the `YAML` can't be ensured.
|
2013-02-14 12:09:14 -05:00
|
|
|
if RUBY_VERSION.to_f >= 1.9
|
|
|
|
assert_equal YAML.dump(@original_fluxor_attributes), @fluxor.versions[1].object
|
|
|
|
end
|
2012-07-03 23:22:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-10 20:43:56 -04:00
|
|
|
context 'JSON Serializer' do
|
2012-07-03 23:22:52 -04:00
|
|
|
setup do
|
2013-01-07 14:44:12 -05:00
|
|
|
PaperTrail.configure do |config|
|
2013-10-17 22:05:24 -04:00
|
|
|
config.serializer = PaperTrail::Serializers::JSON
|
2013-01-07 14:44:12 -05:00
|
|
|
end
|
2012-07-03 23:22:52 -04:00
|
|
|
|
|
|
|
Fluxor.instance_eval <<-END
|
|
|
|
has_paper_trail
|
|
|
|
END
|
|
|
|
|
|
|
|
@fluxor = Fluxor.create :name => 'Some text.'
|
2013-01-31 17:32:36 -05:00
|
|
|
@original_fluxor_attributes = @fluxor.send(:item_before_change).attributes # this is exactly what PaperTrail serializes
|
2012-07-03 23:22:52 -04:00
|
|
|
@fluxor.update_attributes :name => 'Some more text.'
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
2013-10-17 22:05:24 -04:00
|
|
|
PaperTrail.config.serializer = PaperTrail::Serializers::YAML
|
2012-07-03 23:22:52 -04:00
|
|
|
end
|
|
|
|
|
2013-05-10 20:43:56 -04:00
|
|
|
should 'reify with JSON serializer' do
|
2012-07-03 23:22:52 -04:00
|
|
|
# Normal behaviour
|
|
|
|
assert_equal 2, @fluxor.versions.length
|
|
|
|
assert_nil @fluxor.versions[0].reify
|
|
|
|
assert_equal 'Some text.', @fluxor.versions[1].reify.name
|
|
|
|
|
|
|
|
# Check values are stored as JSON.
|
2013-01-31 16:39:52 -05:00
|
|
|
assert_equal @original_fluxor_attributes, ActiveSupport::JSON.decode(@fluxor.versions[1].object)
|
2013-02-14 12:09:14 -05:00
|
|
|
# This test can't consistently pass in Ruby1.8 because hashes do no preserve order, which means the order of the
|
|
|
|
# attributes in the JSON can't be ensured.
|
|
|
|
if RUBY_VERSION.to_f >= 1.9
|
|
|
|
assert_equal ActiveSupport::JSON.encode(@original_fluxor_attributes), @fluxor.versions[1].object
|
|
|
|
end
|
2013-01-10 05:48:06 -05:00
|
|
|
end
|
2012-07-03 23:22:52 -04:00
|
|
|
|
2013-01-10 05:48:06 -05:00
|
|
|
should 'store object_changes' do
|
2014-03-15 00:01:31 -04:00
|
|
|
initial_changeset = {"name" => [nil, "Some text."], "id" => [nil, @fluxor.id]}
|
2013-01-10 05:48:06 -05:00
|
|
|
second_changeset = {"name"=>["Some text.", "Some more text."]}
|
|
|
|
assert_equal initial_changeset, @fluxor.versions[0].changeset
|
|
|
|
assert_equal second_changeset, @fluxor.versions[1].changeset
|
2012-07-03 23:22:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-10 20:43:56 -04:00
|
|
|
context 'Custom Serializer' do
|
|
|
|
setup do
|
|
|
|
PaperTrail.configure do |config|
|
|
|
|
config.serializer = CustomJsonSerializer
|
|
|
|
end
|
|
|
|
|
|
|
|
Fluxor.instance_eval <<-END
|
|
|
|
has_paper_trail
|
|
|
|
END
|
|
|
|
|
|
|
|
@fluxor = Fluxor.create
|
|
|
|
@original_fluxor_attributes = @fluxor.send(:item_before_change).attributes.reject { |k,v| v.nil? } # this is exactly what PaperTrail serializes
|
|
|
|
@fluxor.update_attributes :name => 'Some more text.'
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
2013-10-17 22:05:24 -04:00
|
|
|
PaperTrail.config.serializer = PaperTrail::Serializers::YAML
|
2013-05-10 20:43:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
should 'reify with custom serializer' do
|
|
|
|
# Normal behaviour
|
|
|
|
assert_equal 2, @fluxor.versions.length
|
|
|
|
assert_nil @fluxor.versions[0].reify
|
|
|
|
assert_nil @fluxor.versions[1].reify.name
|
|
|
|
|
|
|
|
# Check values are stored as JSON.
|
|
|
|
assert_equal @original_fluxor_attributes, ActiveSupport::JSON.decode(@fluxor.versions[1].object)
|
|
|
|
# This test can't consistently pass in Ruby1.8 because hashes do no preserve order, which means the order of the
|
|
|
|
# attributes in the JSON can't be ensured.
|
|
|
|
if RUBY_VERSION.to_f >= 1.9
|
|
|
|
assert_equal ActiveSupport::JSON.encode(@original_fluxor_attributes), @fluxor.versions[1].object
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
should 'store object_changes' do
|
2014-03-15 00:01:31 -04:00
|
|
|
initial_changeset = {"id" => [nil, @fluxor.id]}
|
2013-05-10 20:43:56 -04:00
|
|
|
second_changeset = {"name"=>[nil, "Some more text."]}
|
|
|
|
assert_equal initial_changeset, @fluxor.versions[0].changeset
|
|
|
|
assert_equal second_changeset, @fluxor.versions[1].changeset
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-03 23:22:52 -04:00
|
|
|
end
|