mirror of
https://github.com/paper-trail-gem/paper_trail.git
synced 2022-11-09 11:33:19 -05:00
Convert unit/inheritance_column_test to rspec
This commit is contained in:
parent
01041df841
commit
379d9187e9
2 changed files with 50 additions and 66 deletions
|
@ -1,19 +1,45 @@
|
|||
require "rails_helper"
|
||||
|
||||
describe Animal, type: :model do
|
||||
it { is_expected.to be_versioned }
|
||||
|
||||
describe "STI", versioning: true do
|
||||
it { expect(Animal.inheritance_column).to eq("species") }
|
||||
|
||||
describe "updates to the `inheritance_column`" do
|
||||
subject { Cat.create!(name: "Leo") }
|
||||
|
||||
it "should be allowed" do
|
||||
subject.update_attributes(name: "Spike", species: "Dog")
|
||||
dog = Animal.find(subject.id)
|
||||
expect(dog).to be_instance_of(Dog)
|
||||
describe Animal, type: :model, versioning: true do
|
||||
it "baseline test setup" do
|
||||
expect(Animal.new).to be_versioned
|
||||
expect(Animal.inheritance_column).to eq("species")
|
||||
end
|
||||
|
||||
it "works with custom STI inheritance column" do
|
||||
animal = Animal.create(name: "Animal")
|
||||
animal.update_attributes(name: "Animal from the Muppets")
|
||||
animal.update_attributes(name: "Animal Muppet")
|
||||
animal.destroy
|
||||
dog = Dog.create(name: "Snoopy")
|
||||
dog.update_attributes(name: "Scooby")
|
||||
dog.update_attributes(name: "Scooby Doo")
|
||||
dog.destroy
|
||||
cat = Cat.create(name: "Garfield")
|
||||
cat.update_attributes(name: "Garfield (I hate Mondays)")
|
||||
cat.update_attributes(name: "Garfield The Cat")
|
||||
cat.destroy
|
||||
expect(PaperTrail::Version.count).to(eq(12))
|
||||
expect(animal.versions.count).to(eq(4))
|
||||
expect(animal.versions.first.reify).to(be_nil)
|
||||
animal.versions[(1..-1)].each do |v|
|
||||
expect(v.reify.class.name).to(eq("Animal"))
|
||||
end
|
||||
dog_versions = PaperTrail::Version.where(item_id: dog.id).order(:created_at)
|
||||
expect(dog_versions.count).to(eq(4))
|
||||
expect(dog_versions.first.reify).to(be_nil)
|
||||
expect(dog_versions.map { |v| v.reify.class.name }).to eq(%w(NilClass Dog Dog Dog))
|
||||
cat_versions = PaperTrail::Version.where(item_id: cat.id).order(:created_at)
|
||||
expect(cat_versions.count).to(eq(4))
|
||||
expect(cat_versions.first.reify).to(be_nil)
|
||||
expect(cat_versions.map { |v| v.reify.class.name }).to eq(%w(NilClass Cat Cat Cat))
|
||||
end
|
||||
|
||||
it "allows the inheritance_column (species) to be updated" do
|
||||
cat = Cat.create!(name: "Leo")
|
||||
cat.update_attributes(name: "Spike", species: "Dog")
|
||||
dog = Animal.find(cat.id)
|
||||
expect(dog).to be_instance_of(Dog)
|
||||
end
|
||||
|
||||
context "with callback-methods" do
|
||||
|
@ -32,5 +58,4 @@ describe Animal, type: :model do
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
require "test_helper"
|
||||
|
||||
class InheritanceColumnTest < ActiveSupport::TestCase
|
||||
context "STI models" do
|
||||
setup do
|
||||
@animal = Animal.create name: "Animal"
|
||||
@animal.update_attributes name: "Animal from the Muppets"
|
||||
@animal.update_attributes name: "Animal Muppet"
|
||||
@animal.destroy
|
||||
|
||||
@dog = Dog.create name: "Snoopy"
|
||||
@dog.update_attributes name: "Scooby"
|
||||
@dog.update_attributes name: "Scooby Doo"
|
||||
@dog.destroy
|
||||
|
||||
@cat = Cat.create name: "Garfield"
|
||||
@cat.update_attributes name: "Garfield (I hate Mondays)"
|
||||
@cat.update_attributes name: "Garfield The Cat"
|
||||
@cat.destroy
|
||||
end
|
||||
|
||||
should "work with custom STI inheritance column" do
|
||||
assert_equal 12, PaperTrail::Version.count
|
||||
assert_equal 4, @animal.versions.count
|
||||
assert_nil @animal.versions.first.reify
|
||||
@animal.versions[1..-1].each { |v| assert_equal "Animal", v.reify.class.name }
|
||||
|
||||
# For some reason `@dog.versions` doesn't include the final `destroy` version.
|
||||
# Neither do `@dog.versions.scoped` nor `@dog.versions(true)` nor `@dog.versions.reload`.
|
||||
dog_versions = PaperTrail::Version.where(item_id: @dog.id).order(:created_at)
|
||||
assert_equal 4, dog_versions.count
|
||||
assert_nil dog_versions.first.reify
|
||||
assert_equal %w(NilClass Dog Dog Dog), dog_versions.map { |v| v.reify.class.name }
|
||||
|
||||
cat_versions = PaperTrail::Version.where(item_id: @cat.id).order(:created_at)
|
||||
assert_equal 4, cat_versions.count
|
||||
assert_nil cat_versions.first.reify
|
||||
assert_equal %w(NilClass Cat Cat Cat), cat_versions.map { |v| v.reify.class.name }
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue