test/unit/model_test.rb in paper_trail-5.2.3 vs test/unit/model_test.rb in paper_trail-6.0.0
- old
+ new
@@ -1,9 +1,11 @@
require "test_helper"
require "time_travel_helper"
class HasPaperTrailModelTest < ActiveSupport::TestCase
+ extend CleanupCallbacks
+
context "A record with defined 'only' and 'ignore' attributes" do
setup { @article = Article.create }
should "creation should change the number of versions" do
assert_equal(1, PaperTrail::Version.count)
@@ -133,11 +135,11 @@
)
@old_article = @article.versions.last
end
should "have removed the skipped attributes when saving the previous version" do
- assert_equal nil, PaperTrail.serializer.load(@old_article.object)["file_upload"]
+ assert_nil PaperTrail.serializer.load(@old_article.object)["file_upload"]
end
should "have kept the non-skipped attributes in the previous version" do
assert_equal "Some text here.", PaperTrail.serializer.load(@old_article.object)["content"]
end
@@ -515,20 +517,13 @@
assert @previous.a_boolean
end
context "after a column is removed from the record's schema" do
setup do
- change_schema
- Widget.connection.schema_cache.clear!
- Widget.reset_column_information
@last = @widget.versions.last
end
- teardown do
- restore_schema
- end
-
should "reify previous version" do
assert_kind_of Widget, @last.reify
end
should "restore all forward-compatible attributes" do
@@ -1289,62 +1284,79 @@
end
end
context "The `on` option" do
context "on create" do
+ cleanup_callbacks(Fluxor, :create)
+ cleanup_callbacks(Fluxor, :update)
+ cleanup_callbacks(Fluxor, :destroy)
+ cleanup_callbacks(Fluxor, :save)
+
setup do
Fluxor.instance_eval <<-END
has_paper_trail :on => [:create]
END
@fluxor = Fluxor.create
@fluxor.update_attributes name: "blah"
@fluxor.destroy
end
+
should "only have a version for the create event" do
assert_equal 1, @fluxor.versions.length
assert_equal "create", @fluxor.versions.last.event
end
end
+
context "on update" do
+ cleanup_callbacks(Fluxor, :create)
+ cleanup_callbacks(Fluxor, :update)
+ cleanup_callbacks(Fluxor, :destroy)
+ cleanup_callbacks(Fluxor, :save)
+
setup do
- Fluxor.reset_callbacks :create
- Fluxor.reset_callbacks :update
- Fluxor.reset_callbacks :destroy
Fluxor.instance_eval <<-END
has_paper_trail :on => [:update]
END
@fluxor = Fluxor.create
@fluxor.update_attributes name: "blah"
@fluxor.destroy
end
+
should "only have a version for the update event" do
assert_equal 1, @fluxor.versions.length
assert_equal "update", @fluxor.versions.last.event
end
end
+
context "on destroy" do
+ cleanup_callbacks(Fluxor, :create)
+ cleanup_callbacks(Fluxor, :update)
+ cleanup_callbacks(Fluxor, :destroy)
+ cleanup_callbacks(Fluxor, :save)
+
setup do
- Fluxor.reset_callbacks :create
- Fluxor.reset_callbacks :update
- Fluxor.reset_callbacks :destroy
Fluxor.instance_eval <<-END
has_paper_trail :on => [:destroy]
END
@fluxor = Fluxor.create
@fluxor.update_attributes name: "blah"
@fluxor.destroy
end
+
should "only have a version for the destroy event" do
assert_equal 1, @fluxor.versions.length
assert_equal "destroy", @fluxor.versions.last.event
end
end
+
context "on []" do
+ cleanup_callbacks(Fluxor, :create)
+ cleanup_callbacks(Fluxor, :update)
+ cleanup_callbacks(Fluxor, :destroy)
+ cleanup_callbacks(Fluxor, :save)
+
setup do
- Fluxor.reset_callbacks :create
- Fluxor.reset_callbacks :update
- Fluxor.reset_callbacks :destroy
Fluxor.instance_eval <<-END
has_paper_trail :on => []
END
@fluxor = Fluxor.create
@fluxor.update_attributes name: "blah"
@@ -1361,22 +1373,26 @@
should "still respond to touch_with_version" do
@fluxor.paper_trail.touch_with_version
assert_equal 1, @fluxor.versions.length
end
end
+
context "allows a symbol to be passed" do
+ cleanup_callbacks(Fluxor, :create)
+ cleanup_callbacks(Fluxor, :update)
+ cleanup_callbacks(Fluxor, :destroy)
+ cleanup_callbacks(Fluxor, :save)
+
setup do
- Fluxor.reset_callbacks :create
- Fluxor.reset_callbacks :update
- Fluxor.reset_callbacks :destroy
Fluxor.instance_eval <<-END
has_paper_trail :on => :create
END
@fluxor = Fluxor.create
@fluxor.update_attributes name: "blah"
@fluxor.destroy
end
+
should "only have a version for hte create event" do
assert_equal 1, @fluxor.versions.length
assert_equal "create", @fluxor.versions.last.event
end
end
@@ -1418,54 +1434,65 @@
end
end
context "custom events" do
context "on create" do
+ cleanup_callbacks(Fluxor, :create)
+ cleanup_callbacks(Fluxor, :update)
+ cleanup_callbacks(Fluxor, :destroy)
+ cleanup_callbacks(Fluxor, :save)
+
setup do
- Fluxor.reset_callbacks :create
- Fluxor.reset_callbacks :update
- Fluxor.reset_callbacks :destroy
Fluxor.instance_eval <<-END
has_paper_trail :on => [:create]
END
@fluxor = Fluxor.new.tap { |model| model.paper_trail_event = "created" }
@fluxor.update_attributes name: "blah"
@fluxor.destroy
end
+
should "only have a version for the created event" do
assert_equal 1, @fluxor.versions.length
assert_equal "created", @fluxor.versions.last.event
end
end
+
context "on update" do
+ cleanup_callbacks(Fluxor, :create)
+ cleanup_callbacks(Fluxor, :update)
+ cleanup_callbacks(Fluxor, :destroy)
+ cleanup_callbacks(Fluxor, :save)
+
setup do
- Fluxor.reset_callbacks :create
- Fluxor.reset_callbacks :update
- Fluxor.reset_callbacks :destroy
Fluxor.instance_eval <<-END
has_paper_trail :on => [:update]
END
@fluxor = Fluxor.create.tap { |model| model.paper_trail_event = "name_updated" }
@fluxor.update_attributes name: "blah"
@fluxor.destroy
end
+
should "only have a version for the name_updated event" do
assert_equal 1, @fluxor.versions.length
assert_equal "name_updated", @fluxor.versions.last.event
end
end
+
context "on destroy" do
+ cleanup_callbacks(Fluxor, :create)
+ cleanup_callbacks(Fluxor, :update)
+ cleanup_callbacks(Fluxor, :destroy)
+ cleanup_callbacks(Fluxor, :save)
+
setup do
- Fluxor.reset_callbacks :create
- Fluxor.reset_callbacks :update
- Fluxor.reset_callbacks :destroy
Fluxor.instance_eval <<-END
has_paper_trail :on => [:destroy]
END
@fluxor = Fluxor.create.tap { |model| model.paper_trail_event = "destroyed" }
@fluxor.update_attributes name: "blah"
@fluxor.destroy
end
+
should "only have a version for the destroy event" do
assert_equal 1, @fluxor.versions.length
assert_equal "destroyed", @fluxor.versions.last.event
end
end