Convert unit/cleaner_test to rspec

This commit is contained in:
Jared Beck 2017-02-05 18:58:45 -05:00
parent 9bed39c168
commit 01041df841
2 changed files with 147 additions and 151 deletions

View File

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

View File

@ -1,151 +0,0 @@
require "test_helper"
class PaperTrailCleanerTest < ActiveSupport::TestCase
def populate_db!
@animals = [@animal = Animal.new, @dog = Dog.new, @cat = Cat.new]
@animals.each do |animal|
3.times { animal.update_attribute(:name, FFaker::Name.name) }
end
end
context "`clean_versions!` method" do
setup { populate_db! }
should "Baseline" do
assert_equal 9, PaperTrail::Version.count
@animals.each { |animal| assert_equal 3, animal.versions.size }
end
should "be extended by `PaperTrail` module" do
assert_respond_to PaperTrail, :clean_versions!
end
context "No options provided" do
should "removes extra versions for each item" do
PaperTrail.clean_versions!
assert_equal 3, PaperTrail::Version.count
@animals.each { |animal| assert_equal 1, animal.versions.size }
end
should "removes the earliest version(s)" do
before = @animals.map { |animal| animal.versions.last.reify.name }
PaperTrail.clean_versions!
after = @animals.map { |animal| animal.versions.last.reify.name }
assert_equal before, after
end
end
context "`:keeping` option" do
should "modifies the number of versions ommitted from destruction" do
PaperTrail.clean_versions!(keeping: 2)
assert_equal 6, PaperTrail::Version.all.count
@animals.each { |animal| assert_equal 2, animal.versions.size }
end
end
context "`:date` option" do
setup do
@animal.versions.each { |ver| ver.update_attribute(:created_at, ver.created_at - 1.day) }
@date = @animal.versions.first.created_at.to_date
@animal.update_attribute(:name, FFaker::Name.name)
end
should "restrict the versions destroyed to those that were created on the date provided" do
assert_equal 10, PaperTrail::Version.count
assert_equal 4, @animal.versions.size
assert_equal 3, @animal.paper_trail.versions_between(@date, @date + 1.day).size
PaperTrail.clean_versions!(date: @date)
assert_equal 8, PaperTrail::Version.count
assert_equal 2, @animal.versions.reload.size
assert_equal @date, @animal.versions.first.created_at.to_date
assert_not_same @date, @animal.versions.last.created_at.to_date
end
end
context "`:item_id` option" do
context "single ID received" do
should "restrict the versions destroyed to the versions for the Item with that ID" do
PaperTrail.clean_versions!(item_id: @animal.id)
assert_equal 1, @animal.versions.size
assert_equal 7, PaperTrail::Version.count
end
end
context "collection of ID's received" do
should "restrict the versions destroyed to the versions for the Item with those ID's" do
PaperTrail.clean_versions!(item_id: [@animal.id, @dog.id])
assert_equal 1, @animal.versions.size
assert_equal 1, @dog.versions.size
assert_equal 5, PaperTrail::Version.count
end
end
end
context "options combinations" do # additional tests to cover combinations of options
context "`:date`" do
setup do
[@animal, @dog].each do |animal|
animal.versions.each { |ver| ver.update_attribute(:created_at, ver.created_at - 1.day) }
animal.update_attribute(:name, FFaker::Name.name)
end
@date = @animal.versions.first.created_at.to_date
end
should "Baseline" do
assert_equal 11, PaperTrail::Version.count
[@animal, @dog].each do |animal|
assert_equal 4, animal.versions.size
assert_equal 3, animal.versions.between(@date, @date + 1.day).size
end
end
context "and `:keeping`" do
should "restrict cleaning properly" do
PaperTrail.clean_versions!(date: @date, keeping: 2)
[@animal, @dog].each do |animal|
# reload the association to pick up the destructions made by the `Cleaner`
animal.versions.reload
assert_equal 3, animal.versions.size
assert_equal 2, animal.versions.between(@date, @date + 1.day).size
end
# ensure that the versions for the `@cat` instance wasn't touched
assert_equal 9, PaperTrail::Version.count
end
end
context "and `:item_id`" do
should "restrict cleaning properly" do
PaperTrail.clean_versions!(date: @date, item_id: @dog.id)
# reload the association to pick up the destructions made by the `Cleaner`
@dog.versions.reload
assert_equal 2, @dog.versions.size
assert_equal 1, @dog.versions.between(@date, @date + 1.day).size
# ensure the versions for other animals besides `@animal` weren't touched
assert_equal 9, PaperTrail::Version.count
end
end
context ", `:item_id`, and `:keeping`" do
should "restrict cleaning properly" do
PaperTrail.clean_versions!(date: @date, item_id: @dog.id, keeping: 2)
# reload the association to pick up the destructions made by the `Cleaner`
@dog.versions.reload
assert_equal 3, @dog.versions.size
assert_equal 2, @dog.versions.between(@date, @date + 1.day).size
# ensure the versions for other animals besides `@animal` weren't touched
assert_equal 10, PaperTrail::Version.count
end
end
end
context "`:keeping` and `:item_id`" do
should "restrict cleaning properly" do
PaperTrail.clean_versions!(keeping: 2, item_id: @animal.id)
assert_equal 2, @animal.versions.size
# ensure the versions for other animals besides `@animal` weren't touched
assert_equal 8, PaperTrail::Version.count
end
end
end
end # clean_versions! method
end