2012-03-12 19:22:21 -04:00
|
|
|
require 'test_helper'
|
|
|
|
|
2013-05-29 17:19:05 -04:00
|
|
|
class PaperTrail::VersionTest < ActiveSupport::TestCase
|
2013-08-27 12:16:01 -04:00
|
|
|
setup do
|
2012-03-12 19:22:21 -04:00
|
|
|
change_schema
|
2013-08-27 11:17:20 -04:00
|
|
|
@animal = Animal.create
|
2013-05-29 17:19:05 -04:00
|
|
|
assert PaperTrail::Version.creates.present?
|
2013-08-27 12:16:01 -04:00
|
|
|
end
|
2012-03-12 19:22:21 -04:00
|
|
|
|
2014-03-14 23:52:35 -04:00
|
|
|
teardown do
|
|
|
|
restore_schema
|
|
|
|
Animal.connection.schema_cache.clear!
|
|
|
|
Animal.reset_column_information
|
|
|
|
end
|
|
|
|
|
2013-05-29 17:19:05 -04:00
|
|
|
context "PaperTrail::Version.creates" do
|
2012-03-12 19:22:21 -04:00
|
|
|
should "return only create events" do
|
2013-05-29 17:19:05 -04:00
|
|
|
PaperTrail::Version.creates.each do |version|
|
2012-03-12 19:22:21 -04:00
|
|
|
assert_equal "create", version.event
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-29 17:19:05 -04:00
|
|
|
context "PaperTrail::Version.updates" do
|
2012-03-12 19:22:21 -04:00
|
|
|
setup {
|
2013-08-27 11:17:20 -04:00
|
|
|
@animal.update_attributes(:name => 'Animal')
|
2013-05-29 17:19:05 -04:00
|
|
|
assert PaperTrail::Version.updates.present?
|
2012-03-12 19:22:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
should "return only update events" do
|
2013-05-29 17:19:05 -04:00
|
|
|
PaperTrail::Version.updates.each do |version|
|
2012-03-12 19:22:21 -04:00
|
|
|
assert_equal "update", version.event
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-29 17:19:05 -04:00
|
|
|
context "PaperTrail::Version.destroys" do
|
2012-03-12 19:22:21 -04:00
|
|
|
setup {
|
2013-08-27 11:17:20 -04:00
|
|
|
@animal.destroy
|
2013-05-29 17:19:05 -04:00
|
|
|
assert PaperTrail::Version.destroys.present?
|
2012-03-12 19:22:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
should "return only destroy events" do
|
2013-05-29 17:19:05 -04:00
|
|
|
PaperTrail::Version.destroys.each do |version|
|
2012-03-12 19:22:21 -04:00
|
|
|
assert_equal "destroy", version.event
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-05-23 15:56:19 -04:00
|
|
|
|
2013-05-29 17:19:05 -04:00
|
|
|
context "PaperTrail::Version.not_creates" do
|
2013-05-23 15:56:19 -04:00
|
|
|
setup {
|
2013-08-27 11:17:20 -04:00
|
|
|
@animal.update_attributes(:name => 'Animal')
|
|
|
|
@animal.destroy
|
2013-05-29 17:19:05 -04:00
|
|
|
assert PaperTrail::Version.not_creates.present?
|
2013-05-23 15:56:19 -04:00
|
|
|
}
|
|
|
|
|
2013-10-10 13:59:53 -04:00
|
|
|
should "return all versions except create events" do
|
2013-05-29 17:19:05 -04:00
|
|
|
PaperTrail::Version.not_creates.each do |version|
|
2013-05-23 15:56:19 -04:00
|
|
|
assert_not_equal "create", version.event
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-10-10 13:59:53 -04:00
|
|
|
|
|
|
|
context "PaperTrail::Version.subsequent" do
|
|
|
|
setup { 2.times { @animal.update_attributes(:name => Faker::Lorem.word) } }
|
|
|
|
|
|
|
|
context "receiving a TimeStamp" do
|
2014-05-09 11:01:33 -04:00
|
|
|
should "return all versions that were created before the Timestamp" do
|
2014-04-01 13:38:53 -04:00
|
|
|
value = PaperTrail::Version.subsequent(1.hour.ago, true)
|
2013-10-10 13:59:53 -04:00
|
|
|
assert_equal value, @animal.versions.to_a
|
2014-05-28 19:18:53 -04:00
|
|
|
assert_not_nil value.to_sql.match(/ORDER BY #{PaperTrail::Version.arel_table[:created_at].asc.to_sql}/)
|
2013-10-10 13:59:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "receiving a `PaperTrail::Version`" do
|
|
|
|
should "grab the Timestamp from the version and use that as the value" do
|
|
|
|
value = PaperTrail::Version.subsequent(@animal.versions.first)
|
|
|
|
assert_equal value, @animal.versions.to_a.tap { |assoc| assoc.shift }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "PaperTrail::Version.preceding" do
|
|
|
|
setup { 2.times { @animal.update_attributes(:name => Faker::Lorem.word) } }
|
|
|
|
|
|
|
|
context "receiving a TimeStamp" do
|
2014-05-09 11:01:33 -04:00
|
|
|
should "return all versions that were created before the Timestamp" do
|
|
|
|
value = PaperTrail::Version.preceding(5.seconds.from_now, true)
|
2013-10-10 13:59:53 -04:00
|
|
|
assert_equal value, @animal.versions.reverse
|
2014-05-28 19:18:53 -04:00
|
|
|
assert_not_nil value.to_sql.match(/ORDER BY #{PaperTrail::Version.arel_table[:created_at].desc.to_sql}/)
|
2013-10-10 13:59:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "receiving a `PaperTrail::Version`" do
|
|
|
|
should "grab the Timestamp from the version and use that as the value" do
|
|
|
|
value = PaperTrail::Version.preceding(@animal.versions.last)
|
|
|
|
assert_equal value, @animal.versions.to_a.tap { |assoc| assoc.pop }.reverse
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-03-12 19:22:21 -04:00
|
|
|
end
|