diff --git a/spec/paper_trail/version_spec.rb b/spec/paper_trail/version_spec.rb new file mode 100644 index 00000000..b7317c34 --- /dev/null +++ b/spec/paper_trail/version_spec.rb @@ -0,0 +1,94 @@ +require "rails_helper" + +module PaperTrail + ::RSpec.describe(Version, versioning: true) do + describe ".creates" do + it "returns only create events" do + animal = Animal.create(name: "Foo") + animal.update_attributes(name: "Bar") + expect(described_class.creates.pluck(:event)).to eq(["create"]) + end + end + + describe ".updates" do + it "returns only update events" do + animal = Animal.create + animal.update_attributes(name: "Animal") + expect(described_class.updates.pluck(:event)).to eq(["update"]) + end + end + + describe ".destroys" do + it "returns only destroy events" do + animal = Animal.create + animal.destroy + expect(described_class.destroys.pluck(:event)).to eq(["destroy"]) + end + end + + describe ".not_creates" do + it "returns all versions except create events" do + animal = Animal.create + animal.update_attributes(name: "Animal") + animal.destroy + expect(described_class.not_creates.pluck(:event)).to eq(%w(update destroy)) + end + end + + describe ".subsequent" do + context "given a timestamp" do + it "returns all versions that were created after the timestamp" do + animal = Animal.create + 2.times do + animal.update_attributes(name: FFaker::Lorem.word) + end + value = described_class.subsequent(1.hour.ago, true) + expect(value).to eq(animal.versions.to_a) + expect(value.to_sql).to match( + /ORDER BY #{described_class.arel_table[:created_at].asc.to_sql}/ + ) + end + end + + context "given a Version" do + it "grab the timestamp from the version and use that as the value" do + animal = Animal.create + 2.times do + animal.update_attributes(name: FFaker::Lorem.word) + end + expect(described_class.subsequent(animal.versions.first)).to eq( + animal.versions.to_a.drop(1) + ) + end + end + end + + describe ".preceding" do + context "given a timestamp" do + it "returns all versions that were created before the timestamp" do + animal = Animal.create + 2.times do + animal.update_attributes(name: FFaker::Lorem.word) + end + value = described_class.preceding(5.seconds.from_now, true) + expect(value).to eq(animal.versions.reverse) + expect(value.to_sql).to match( + /ORDER BY #{described_class.arel_table[:created_at].desc.to_sql}/ + ) + end + end + + context "given a Version" do + it "grab the timestamp from the version and use that as the value" do + animal = Animal.create + 2.times do + animal.update_attributes(name: FFaker::Lorem.word) + end + expect(described_class.preceding(animal.versions.last)).to eq( + animal.versions.to_a.tap(&:pop).reverse + ) + end + end + end + end +end diff --git a/test/unit/version_test.rb b/test/unit/version_test.rb deleted file mode 100644 index e8b578b0..00000000 --- a/test/unit/version_test.rb +++ /dev/null @@ -1,112 +0,0 @@ -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