diff --git a/spec/models/boolit_spec.rb b/spec/models/boolit_spec.rb index eafccfb4..df7c1708 100644 --- a/spec/models/boolit_spec.rb +++ b/spec/models/boolit_spec.rb @@ -1,5 +1,5 @@ require "rails_helper" -require Rails.root.join("..", "custom_json_serializer") +require "support/custom_json_serializer" RSpec.describe Boolit, type: :model, versioning: true do subject { Boolit.create! } diff --git a/spec/paper_trail/serializer_spec.rb b/spec/paper_trail/serializer_spec.rb new file mode 100644 index 00000000..d6c708ad --- /dev/null +++ b/spec/paper_trail/serializer_spec.rb @@ -0,0 +1,85 @@ +require "rails_helper" +require "support/custom_json_serializer" + +RSpec.describe(PaperTrail, versioning: true) do + context "YAML serializer" do + it "saves the expected YAML in the object column" do + customer = Customer.create(name: "Some text.") + original_attributes = customer.paper_trail.attributes_before_change + customer.update(name: "Some more text.") + expect(customer.versions.length).to(eq(2)) + expect(customer.versions[0].reify).to(be_nil) + expect(customer.versions[1].reify.name).to(eq("Some text.")) + expect(YAML.load(customer.versions[1].object)).to(eq(original_attributes)) + expect(customer.versions[1].object).to(eq(YAML.dump(original_attributes))) + end + end + + context "JSON Serializer" do + before do + PaperTrail.configure do |config| + config.serializer = PaperTrail::Serializers::JSON + end + end + + after do + PaperTrail.config.serializer = PaperTrail::Serializers::YAML + end + + it "reify with JSON serializer" do + customer = Customer.create(name: "Some text.") + original_attributes = customer.paper_trail.attributes_before_change + customer.update(name: "Some more text.") + expect(customer.versions.length).to(eq(2)) + expect(customer.versions[0].reify).to(be_nil) + expect(customer.versions[1].reify.name).to(eq("Some text.")) + expect(ActiveSupport::JSON.decode(customer.versions[1].object)).to(eq(original_attributes)) + expect(customer.versions[1].object).to(eq(ActiveSupport::JSON.encode(original_attributes))) + end + + describe "#changeset" do + it "returns the expected hash" do + customer = Customer.create(name: "Some text.") + customer.update(name: "Some more text.") + initial_changeset = { "name" => [nil, "Some text."], "id" => [nil, customer.id] } + second_changeset = { "name" => ["Some text.", "Some more text."] } + expect(customer.versions[0].changeset).to(eq(initial_changeset)) + expect(customer.versions[1].changeset).to(eq(second_changeset)) + end + end + end + + context "Custom Serializer" do + before do + PaperTrail.configure { |config| config.serializer = CustomJsonSerializer } + end + + after do + PaperTrail.config.serializer = PaperTrail::Serializers::YAML + end + + it "reify with custom serializer" do + customer = Customer.create + original_attributes = customer.paper_trail.attributes_before_change.reject { |_k, v| v.nil? } + customer.update(name: "Some more text.") + expect(customer.versions.length).to(eq(2)) + expect(customer.versions[0].reify).to(be_nil) + expect(customer.versions[1].reify.name).to(be_nil) + expect( + ActiveSupport::JSON.decode(customer.versions[1].object) + ).to eq(original_attributes) + expect( + customer.versions[1].object + ).to eq(ActiveSupport::JSON.encode(original_attributes)) + end + + describe "#changeset" do + it "store object_changes" do + customer = Customer.create + customer.update(name: "banana") + expect(customer.versions[0].changeset).to eq("id" => [nil, customer.id]) + expect(customer.versions[1].changeset).to eq("name" => [nil, "banana"]) + end + end + end +end diff --git a/test/custom_json_serializer.rb b/spec/support/custom_json_serializer.rb similarity index 100% rename from test/custom_json_serializer.rb rename to spec/support/custom_json_serializer.rb diff --git a/test/unit/serializer_test.rb b/test/unit/serializer_test.rb deleted file mode 100644 index 2b8594d5..00000000 --- a/test/unit/serializer_test.rb +++ /dev/null @@ -1,107 +0,0 @@ -require "test_helper" -require "custom_json_serializer" - -class SerializerTest < ActiveSupport::TestCase - context "YAML Serializer" do - setup do - @customer = Customer.create name: "Some text." - - # this is exactly what PaperTrail serializes - @original_attributes = @customer.paper_trail.attributes_before_change - - @customer.update_attributes name: "Some more text." - end - - should "work with the default `YAML` serializer" do - # Normal behaviour - assert_equal 2, @customer.versions.length - assert_nil @customer.versions[0].reify - assert_equal "Some text.", @customer.versions[1].reify.name - - # Check values are stored as `YAML`. - assert_equal @original_attributes, YAML.load(@customer.versions[1].object) - assert_equal YAML.dump(@original_attributes), @customer.versions[1].object - end - end - - context "JSON Serializer" do - setup do - PaperTrail.configure do |config| - config.serializer = PaperTrail::Serializers::JSON - end - - @customer = Customer.create name: "Some text." - - # this is exactly what PaperTrail serializes - @original_attributes = @customer.paper_trail.attributes_before_change - - @customer.update_attributes name: "Some more text." - end - - teardown do - PaperTrail.config.serializer = PaperTrail::Serializers::YAML - end - - should "reify with JSON serializer" do - # Normal behaviour - assert_equal 2, @customer.versions.length - assert_nil @customer.versions[0].reify - assert_equal "Some text.", @customer.versions[1].reify.name - - # Check values are stored as JSON. - assert_equal @original_attributes, - ActiveSupport::JSON.decode(@customer.versions[1].object) - assert_equal ActiveSupport::JSON.encode(@original_attributes), - @customer.versions[1].object - end - - should "store object_changes" do - initial_changeset = { "name" => [nil, "Some text."], "id" => [nil, @customer.id] } - second_changeset = { "name" => ["Some text.", "Some more text."] } - assert_equal initial_changeset, @customer.versions[0].changeset - assert_equal second_changeset, @customer.versions[1].changeset - end - end - - context "Custom Serializer" do - setup do - PaperTrail.configure do |config| - config.serializer = CustomJsonSerializer - end - - @customer = Customer.create - - # this is exactly what PaperTrail serializes - @original_attributes = @customer. - paper_trail. - attributes_before_change. - reject { |_k, v| v.nil? } - - @customer.update_attributes name: "Some more text." - end - - teardown do - PaperTrail.config.serializer = PaperTrail::Serializers::YAML - end - - should "reify with custom serializer" do - # Normal behaviour - assert_equal 2, @customer.versions.length - assert_nil @customer.versions[0].reify - assert_nil @customer.versions[1].reify.name - - # Check values are stored as JSON. - assert_equal @original_attributes, - ActiveSupport::JSON.decode(@customer.versions[1].object) - assert_equal ActiveSupport::JSON.encode(@original_attributes), - @customer.versions[1].object - end - - should "store object_changes" do - initial_changeset = { "id" => [nil, @customer.id] } - second_changeset = { "name" => [nil, "Some more text."] } - assert_equal initial_changeset, @customer.versions[0].changeset - assert_equal second_changeset, @customer.versions[1].changeset - end - end -end diff --git a/test/unit/serializers/mixin_json_test.rb b/test/unit/serializers/mixin_json_test.rb index 72518d2c..7a560a75 100644 --- a/test/unit/serializers/mixin_json_test.rb +++ b/test/unit/serializers/mixin_json_test.rb @@ -1,5 +1,5 @@ require "test_helper" -require "custom_json_serializer" +require_relative "../../../spec/support/custom_json_serializer" class MixinJsonTest < ActiveSupport::TestCase setup do