mirror of
https://github.com/paper-trail-gem/paper_trail.git
synced 2022-11-09 11:33:19 -05:00
f963752703
A little over four years ago, a feature was added to allow users to change the name of the `versions.created_at` column. No reason was given at the time. https://github.com/airblade/paper_trail/pull/129 There are three reasons why this should not be configurable: 1. The standard column name in rails is `created_at`. 2. The majority of the `versions` table is, and should be, an implementation detail of PT. 3. This configuration option added moderate complexity to the library, and severe complexity to the test suite.
112 lines
2.9 KiB
Ruby
112 lines
2.9 KiB
Ruby
require "test_helper"
|
|
|
|
module PaperTrail
|
|
class VersionTest < ActiveSupport::TestCase
|
|
setup do
|
|
@animal = Animal.create
|
|
assert Version.creates.present?
|
|
end
|
|
|
|
context ".creates" do
|
|
should "return only create events" do
|
|
Version.creates.each do |version|
|
|
assert_equal "create", version.event
|
|
end
|
|
end
|
|
end
|
|
|
|
context ".updates" do
|
|
setup {
|
|
@animal.update_attributes(name: "Animal")
|
|
assert Version.updates.present?
|
|
}
|
|
|
|
should "return only update events" do
|
|
Version.updates.each do |version|
|
|
assert_equal "update", version.event
|
|
end
|
|
end
|
|
end
|
|
|
|
context ".destroys" do
|
|
setup {
|
|
@animal.destroy
|
|
assert Version.destroys.present?
|
|
}
|
|
|
|
should "return only destroy events" do
|
|
Version.destroys.each do |version|
|
|
assert_equal "destroy", version.event
|
|
end
|
|
end
|
|
end
|
|
|
|
context ".not_creates" do
|
|
setup {
|
|
@animal.update_attributes(name: "Animal")
|
|
@animal.destroy
|
|
assert Version.not_creates.present?
|
|
}
|
|
|
|
should "return all versions except create events" do
|
|
Version.not_creates.each do |version|
|
|
assert_not_equal "create", version.event
|
|
end
|
|
end
|
|
end
|
|
|
|
context ".subsequent" do
|
|
setup do
|
|
2.times do
|
|
@animal.update_attributes(name: FFaker::Lorem.word)
|
|
end
|
|
end
|
|
|
|
context "given a timestamp" do
|
|
should "return all versions that were created after the timestamp" do
|
|
value = Version.subsequent(1.hour.ago, true)
|
|
assert_equal @animal.versions.to_a, value
|
|
assert_match(
|
|
/ORDER BY #{Version.arel_table[:created_at].asc.to_sql}/,
|
|
value.to_sql
|
|
)
|
|
end
|
|
end
|
|
|
|
context "given a Version" do
|
|
should "grab the timestamp from the version and use that as the value" do
|
|
expected = @animal.versions.to_a.tap(&:shift)
|
|
actual = Version.subsequent(@animal.versions.first)
|
|
assert_equal expected, actual
|
|
end
|
|
end
|
|
end
|
|
|
|
context ".preceding" do
|
|
setup do
|
|
2.times do
|
|
@animal.update_attributes(name: FFaker::Lorem.word)
|
|
end
|
|
end
|
|
|
|
context "given a timestamp" do
|
|
should "return all versions that were created before the timestamp" do
|
|
value = Version.preceding(5.seconds.from_now, true)
|
|
assert_equal @animal.versions.reverse, value
|
|
assert_match(
|
|
/ORDER BY #{Version.arel_table[:created_at].desc.to_sql}/,
|
|
value.to_sql
|
|
)
|
|
end
|
|
end
|
|
|
|
context "given a Version" do
|
|
should "grab the timestamp from the version and use that as the value" do
|
|
expected = @animal.versions.to_a.tap(&:pop).reverse
|
|
actual = Version.preceding(@animal.versions.last)
|
|
assert_equal expected, actual
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|