1
0
Fork 0
mirror of https://github.com/paper-trail-gem/paper_trail.git synced 2022-11-09 11:33:19 -05:00
paper-trail-gem--paper_trail/spec/paper_trail/version_spec.rb
Edouard Chin 2b479a7f08 Support rails 6.0.0 (#1172)
* Change update_attributes to update

In Rails 6.0 update_attributes/update_attributes! is considered deprecated. Method update/update! is the replacement.

* CI: Don't use Bundler 1.16.1

- Bundler 1.16.1 has bug where dependencies can't be resolved properly
  when a gem is a release candidate or an alpha version.
  The underlying bundler issue can be found here https://github.com/bundler/bundler/issues/6449

* Disable eager_load in test env:

- In Rails 6.0, rails/rails@3b95478 made a change to eagerly define
  attribute methods of a Model when `eager_load` is enabled.
  This breaks our test suite because of the way we run migration.

  The TL;DR is that doing `People.attribute_names` will return an
  empty array instead of `[:id, time_zone, ...]`.
  You can find a failing build here https://travis-ci.org/paper-trail-gem/paper_trail/jobs/463369634

  Basically what happens is:

  1) The dummy app boot, attribute methods of each model are defined
     but since migration didn't run yet, the tables aren't even
     created resulting in a empty attribute set.
  2) Migration runs, but it's already too late.

  In this commit I disabled eager_loading in test, AFAIT there isn't
  much benefit in eager_loading the dummy app anyway.
  Also renaming the `user.rb` file to `postgres_user.rb` in order for
  rails autoloading to work correctly.
2018-12-04 16:10:35 -05:00

98 lines
2.9 KiB
Ruby

# frozen_string_literal: true
require "spec_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(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(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(name: "Animal")
animal.destroy
expect(
described_class.not_creates.pluck(:event)
).to match_array(%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(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(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(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(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