paper-trail-gem--paper_trail/spec/paper_trail/cleaner_spec.rb

155 lines
5.7 KiB
Ruby
Raw Normal View History

2017-12-11 04:05:11 +00:00
# frozen_string_literal: true
require "spec_helper"
2017-02-05 23:58:45 +00:00
module PaperTrail
::RSpec.describe Cleaner, versioning: true do
2017-02-05 23:58:45 +00:00
describe "clean_versions!" do
2017-04-01 02:11:08 +00:00
let(:animal) { ::Animal.new }
let(:dog) { ::Dog.new }
let(:cat) { ::Cat.new }
let(:animals) { [animal, dog, cat] }
2017-02-05 23:58:45 +00:00
before do
2017-04-01 02:11:08 +00:00
animals.each do |animal|
3.times do
animal.update_attribute(:name, FFaker::Name.name)
end
end
2017-02-05 23:58:45 +00:00
end
it "baseline test setup" do
expect(PaperTrail::Version.count).to(eq(9))
2017-04-01 02:11:08 +00:00
animals.each { |animal| expect(animal.versions.size).to(eq(3)) }
2017-02-05 23:58:45 +00:00
end
2021-08-30 02:22:39 +00:00
context "with no options provided" do
2017-02-05 23:58:45 +00:00
it "removes extra versions for each item" do
PaperTrail.clean_versions!
expect(PaperTrail::Version.count).to(eq(3))
2017-04-01 02:11:08 +00:00
animals.each { |animal| expect(animal.versions.size).to(eq(1)) }
2017-02-05 23:58:45 +00:00
end
it "removes the earliest version(s)" do
2017-04-01 02:11:08 +00:00
before = animals.map { |animal| animal.versions.last.reify.name }
2017-02-05 23:58:45 +00:00
PaperTrail.clean_versions!
2017-04-01 02:11:08 +00:00
after = animals.map { |animal| animal.versions.last.reify.name }
2017-02-05 23:58:45 +00:00
expect(after).to(eq(before))
end
end
2021-08-30 02:22:39 +00:00
context "when keeping 2" do
2017-02-05 23:58:45 +00:00
it "keeps two records, instead of the usual one" do
PaperTrail.clean_versions!(keeping: 2)
expect(PaperTrail::Version.all.count).to(eq(6))
2017-04-01 02:11:08 +00:00
animals.each { |animal| expect(animal.versions.size).to(eq(2)) }
2017-02-05 23:58:45 +00:00
end
end
context "with the :date option" do
it "only deletes versions created on the given date" do
2017-04-01 02:11:08 +00:00
animal.versions.each do |ver|
2017-02-05 23:58:45 +00:00
ver.update_attribute(:created_at, (ver.created_at - 1.day))
end
2017-04-01 02:11:08 +00:00
date = animal.versions.first.created_at.to_date
animal.update_attribute(:name, FFaker::Name.name)
2017-02-05 23:58:45 +00:00
expect(PaperTrail::Version.count).to(eq(10))
2017-04-01 02:11:08 +00:00
expect(animal.versions.size).to(eq(4))
expect(animal.paper_trail.versions_between(date, (date + 1.day)).size).to(eq(3))
PaperTrail.clean_versions!(date: date)
2017-02-05 23:58:45 +00:00
expect(PaperTrail::Version.count).to(eq(8))
2017-04-01 02:11:08 +00:00
expect(animal.versions.reload.size).to(eq(2))
expect(animal.versions.first.created_at.to_date).to(eq(date))
2017-02-05 23:58:45 +00:00
# Why use `equal?` here instead of something less strict?
# Doesn't `to_date` always produce a new date object?
2017-04-01 02:11:08 +00:00
expect(date.equal?(animal.versions.last.created_at.to_date)).to eq(false)
2017-02-05 23:58:45 +00:00
end
end
context "with the :item_id option" do
2021-08-30 02:22:39 +00:00
context "when a single ID is received" do
2017-02-05 23:58:45 +00:00
it "only deletes the versions for the Item with that ID" do
2017-04-01 02:11:08 +00:00
PaperTrail.clean_versions!(item_id: animal.id)
expect(animal.versions.size).to(eq(1))
2017-02-05 23:58:45 +00:00
expect(PaperTrail::Version.count).to(eq(7))
end
end
2021-08-30 02:22:39 +00:00
context "when a collection of IDs is received" do
2017-02-05 23:58:45 +00:00
it "only deletes versions for the Item(s) with those IDs" do
2017-04-01 02:11:08 +00:00
PaperTrail.clean_versions!(item_id: [animal.id, dog.id])
expect(animal.versions.size).to(eq(1))
expect(dog.versions.size).to(eq(1))
2017-02-05 23:58:45 +00:00
expect(PaperTrail::Version.count).to(eq(5))
end
end
end
2021-08-30 02:22:39 +00:00
context "with options combinations" do
context "with :date" do
2017-02-05 23:58:45 +00:00
before do
2017-04-01 02:11:08 +00:00
[animal, dog].each do |animal|
2017-02-05 23:58:45 +00:00
animal.versions.each do |ver|
ver.update_attribute(:created_at, (ver.created_at - 1.day))
end
animal.update_attribute(:name, FFaker::Name.name)
end
end
it "baseline test setup" do
2017-04-01 02:11:08 +00:00
date = animal.versions.first.created_at.to_date
2017-02-05 23:58:45 +00:00
expect(PaperTrail::Version.count).to(eq(11))
2017-04-01 02:11:08 +00:00
[animal, dog].each do |animal|
2017-02-05 23:58:45 +00:00
expect(animal.versions.size).to(eq(4))
2017-04-01 02:11:08 +00:00
expect(animal.versions.between(date, (date + 1.day)).size).to(eq(3))
2017-02-05 23:58:45 +00:00
end
end
2021-08-30 02:22:39 +00:00
context "with :keeping" do
2017-02-05 23:58:45 +00:00
it "restrict cleaning properly" do
2017-04-01 02:11:08 +00:00
date = animal.versions.first.created_at.to_date
PaperTrail.clean_versions!(date: date, keeping: 2)
[animal, dog].each do |animal|
2017-02-05 23:58:45 +00:00
animal.versions.reload
expect(animal.versions.size).to(eq(3))
2017-04-01 02:11:08 +00:00
expect(animal.versions.between(date, (date + 1.day)).size).to(eq(2))
2017-02-05 23:58:45 +00:00
end
expect(PaperTrail::Version.count).to(eq(9))
end
end
2021-08-30 02:22:39 +00:00
context "with :item_id" do
2017-02-05 23:58:45 +00:00
it "restrict cleaning properly" do
2017-04-01 02:11:08 +00:00
date = animal.versions.first.created_at.to_date
PaperTrail.clean_versions!(date: date, item_id: dog.id)
dog.versions.reload
expect(dog.versions.size).to(eq(2))
expect(dog.versions.between(date, (date + 1.day)).size).to(eq(1))
2017-02-05 23:58:45 +00:00
expect(PaperTrail::Version.count).to(eq(9))
end
end
2021-08-30 02:22:39 +00:00
context "with :item_id, and :keeping" do
2017-02-05 23:58:45 +00:00
it "restrict cleaning properly" do
2017-04-01 02:11:08 +00:00
date = animal.versions.first.created_at.to_date
PaperTrail.clean_versions!(date: date, item_id: dog.id, keeping: 2)
dog.versions.reload
expect(dog.versions.size).to(eq(3))
expect(dog.versions.between(date, (date + 1.day)).size).to(eq(2))
2017-02-05 23:58:45 +00:00
expect(PaperTrail::Version.count).to(eq(10))
end
end
end
2021-08-30 02:22:39 +00:00
context "with :keeping and :item_id" do
2017-02-05 23:58:45 +00:00
it "restrict cleaning properly" do
2017-04-01 02:11:08 +00:00
PaperTrail.clean_versions!(keeping: 2, item_id: animal.id)
expect(animal.versions.size).to(eq(2))
2017-02-05 23:58:45 +00:00
expect(PaperTrail::Version.count).to(eq(8))
end
end
end
end
end
end