1
0
Fork 0
mirror of https://github.com/paper-trail-gem/paper_trail.git synced 2022-11-09 11:33:19 -05:00

Lint: Finish fixing RSpec/NestedGroups

This commit is contained in:
Jared Beck 2017-04-01 20:13:08 -04:00
parent eb4f346ee3
commit cf99fb3358
6 changed files with 186 additions and 212 deletions

View file

@ -27,8 +27,5 @@ RSpec/MessageSpies:
RSpec/NamedSubject:
Enabled: false
RSpec/NestedGroups:
Max: 4 # goal: 3
Security/YAMLLoad:
Enabled: false

View file

@ -1,48 +1,40 @@
require "rails_helper"
require Rails.root.join("..", "custom_json_serializer")
describe Boolit, type: :model do
it { is_expected.to be_versioned }
RSpec.describe Boolit, type: :model, versioning: true do
subject { Boolit.create! }
it "has a default scope" do
expect(subject.default_scopes).not_to be_empty
before { subject.update_attributes!(name: FFaker::Name.name) }
it "has two versions" do
expect(subject.versions.size).to eq(2)
end
describe "Versioning", versioning: true do
subject { Boolit.create! }
it "can be reified and persisted" do
expect { subject.versions.last.reify.save! }.not_to raise_error
end
before { subject.update_attributes!(name: FFaker::Name.name) }
context "Instance falls out of default scope" do
before { subject.update_attributes!(scoped: false) }
it "has two versions" do
expect(subject.versions.size).to eq(2)
it "is NOT scoped" do
expect(Boolit.first).to be_nil
end
it "can be reified and persisted" do
expect { subject.versions.last.reify.save! }.not_to raise_error
it "still can be reified and persisted" do
expect { subject.paper_trail.previous_version.save! }.not_to raise_error
end
context "Instance falls out of default scope" do
before { subject.update_attributes!(scoped: false) }
it "is NOT scoped" do
expect(Boolit.first).to be_nil
context "with `nil` attributes on the live instance" do
before do
PaperTrail.serializer = CustomJsonSerializer
subject.update_attributes!(name: nil)
subject.update_attributes!(name: FFaker::Name.name)
end
after { PaperTrail.serializer = PaperTrail::Serializers::YAML }
it "still can be reified and persisted" do
expect { subject.paper_trail.previous_version.save! }.not_to raise_error
end
context "with `nil` attributes on the live instance" do
before do
PaperTrail.serializer = CustomJsonSerializer
subject.update_attributes!(name: nil)
subject.update_attributes!(name: FFaker::Name.name)
end
after { PaperTrail.serializer = PaperTrail::Serializers::YAML }
it "does not overwrite that attribute during the reification process" do
expect(subject.paper_trail.previous_version.name).to be_nil
end
it "does not overwrite that attribute during the reification process" do
expect(subject.paper_trail.previous_version.name).to be_nil
end
end
end

View file

@ -1,96 +1,92 @@
require "rails_helper"
describe CallbackModifier, type: :model do
with_versioning do
describe "callback-methods", versioning: true do
describe "paper_trail_on_destroy" do
it "adds :destroy to paper_trail_options[:on]" do
modifier = NoArgDestroyModifier.create!(some_content: FFaker::Lorem.sentence)
expect(modifier.paper_trail_options[:on]).to eq [:destroy]
end
RSpec.describe CallbackModifier, type: :model, versioning: true do
describe "paper_trail_on_destroy" do
it "adds :destroy to paper_trail_options[:on]" do
modifier = NoArgDestroyModifier.create!(some_content: FFaker::Lorem.sentence)
expect(modifier.paper_trail_options[:on]).to eq([:destroy])
end
context "when :before" do
it "creates the version before destroy" do
modifier = BeforeDestroyModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.test_destroy
expect(modifier.versions.last.reify).not_to be_flagged_deleted
end
end
context "when :after" do
it "creates the version after destroy" do
modifier = AfterDestroyModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.test_destroy
expect(modifier.versions.last.reify).to be_flagged_deleted
end
end
context "when no argument" do
it "defaults to before destroy" do
modifier = NoArgDestroyModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.test_destroy
expect(modifier.versions.last.reify).not_to be_flagged_deleted
end
end
context "when :before" do
it "creates the version before destroy" do
modifier = BeforeDestroyModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.test_destroy
expect(modifier.versions.last.reify).not_to be_flagged_deleted
end
end
describe "paper_trail_on_update" do
it "adds :update to paper_trail_options[:on]" do
modifier = UpdateModifier.create!(some_content: FFaker::Lorem.sentence)
expect(modifier.paper_trail_options[:on]).to eq [:update]
end
it "creates a version" do
modifier = UpdateModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.update_attributes! some_content: "modified"
expect(modifier.versions.last.event).to eq "update"
end
context "when :after" do
it "creates the version after destroy" do
modifier = AfterDestroyModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.test_destroy
expect(modifier.versions.last.reify).to be_flagged_deleted
end
end
describe "paper_trail_on_create" do
it "adds :create to paper_trail_options[:on]" do
modifier = CreateModifier.create!(some_content: FFaker::Lorem.sentence)
expect(modifier.paper_trail_options[:on]).to eq [:create]
end
it "creates a version" do
modifier = CreateModifier.create!(some_content: FFaker::Lorem.sentence)
expect(modifier.versions.last.event).to eq "create"
end
end
context "when no callback-method used" do
it "sets paper_trail_options[:on] to [:create, :update, :destroy]" do
modifier = DefaultModifier.create!(some_content: FFaker::Lorem.sentence)
expect(modifier.paper_trail_options[:on]).to eq %i(create update destroy)
end
it "tracks destroy" do
modifier = DefaultModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.destroy
expect(modifier.versions.last.event).to eq "destroy"
end
it "tracks update" do
modifier = DefaultModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.update_attributes! some_content: "modified"
expect(modifier.versions.last.event).to eq "update"
end
it "tracks create" do
modifier = DefaultModifier.create!(some_content: FFaker::Lorem.sentence)
expect(modifier.versions.last.event).to eq "create"
end
end
context "when only one callback-method" do
it "does only track the corresponding event" do
modifier = CreateModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.update_attributes!(some_content: "modified")
modifier.test_destroy
expect(modifier.versions.collect(&:event)).to eq ["create"]
end
context "when no argument" do
it "defaults to before destroy" do
modifier = NoArgDestroyModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.test_destroy
expect(modifier.versions.last.reify).not_to be_flagged_deleted
end
end
end
describe "paper_trail_on_update" do
it "adds :update to paper_trail_options[:on]" do
modifier = UpdateModifier.create!(some_content: FFaker::Lorem.sentence)
expect(modifier.paper_trail_options[:on]).to eq [:update]
end
it "creates a version" do
modifier = UpdateModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.update_attributes! some_content: "modified"
expect(modifier.versions.last.event).to eq "update"
end
end
describe "paper_trail_on_create" do
it "adds :create to paper_trail_options[:on]" do
modifier = CreateModifier.create!(some_content: FFaker::Lorem.sentence)
expect(modifier.paper_trail_options[:on]).to eq [:create]
end
it "creates a version" do
modifier = CreateModifier.create!(some_content: FFaker::Lorem.sentence)
expect(modifier.versions.last.event).to eq "create"
end
end
context "when no callback-method used" do
it "sets paper_trail_options[:on] to [:create, :update, :destroy]" do
modifier = DefaultModifier.create!(some_content: FFaker::Lorem.sentence)
expect(modifier.paper_trail_options[:on]).to eq %i(create update destroy)
end
it "tracks destroy" do
modifier = DefaultModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.destroy
expect(modifier.versions.last.event).to eq "destroy"
end
it "tracks update" do
modifier = DefaultModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.update_attributes! some_content: "modified"
expect(modifier.versions.last.event).to eq "update"
end
it "tracks create" do
modifier = DefaultModifier.create!(some_content: FFaker::Lorem.sentence)
expect(modifier.versions.last.event).to eq "create"
end
end
context "when only one callback-method" do
it "does only track the corresponding event" do
modifier = CreateModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.update_attributes!(some_content: "modified")
modifier.test_destroy
expect(modifier.versions.collect(&:event)).to eq ["create"]
end
end
end

View file

@ -23,41 +23,40 @@ describe Gadget, type: :model do
end
describe "#changed_notably?", versioning: true do
subject { Gadget.new(created_at: Time.now) }
context "create events" do
context "new record" do
it "returns true" do
expect(subject.paper_trail.changed_notably?).to eq(true)
g = Gadget.new(created_at: Time.now)
expect(g.paper_trail.changed_notably?).to eq(true)
end
end
context "update events" do
before { subject.save! }
context "without update timestamps" do
it "only acknowledges non-ignored attrs" do
subject.name = "Wrench"
expect(subject.paper_trail.changed_notably?).to be true
end
it "does not acknowledge ignored attr (brand)" do
subject.brand = "Acme"
expect(subject.paper_trail.changed_notably?).to be false
end
context "persisted record without update timestamps" do
it "only acknowledges non-ignored attrs" do
subject = Gadget.create!(created_at: Time.now)
subject.name = "Wrench"
expect(subject.paper_trail.changed_notably?).to be true
end
context "with update timestamps" do
it "only acknowledges non-ignored attrs" do
subject.name = "Wrench"
subject.updated_at = Time.now
expect(subject.paper_trail.changed_notably?).to be true
end
it "does not acknowledge ignored attr (brand)" do
subject = Gadget.create!(created_at: Time.now)
subject.brand = "Acme"
expect(subject.paper_trail.changed_notably?).to be false
end
end
it "does not acknowledge ignored attrs and timestamps only" do
subject.brand = "Acme"
subject.updated_at = Time.now
expect(subject.paper_trail.changed_notably?).to be false
end
context "persisted record with update timestamps" do
it "only acknowledges non-ignored attrs" do
subject = Gadget.create!(created_at: Time.now)
subject.name = "Wrench"
subject.updated_at = Time.now
expect(subject.paper_trail.changed_notably?).to be true
end
it "does not acknowledge ignored attrs and timestamps only" do
subject = Gadget.create!(created_at: Time.now)
subject.brand = "Acme"
subject.updated_at = Time.now
expect(subject.paper_trail.changed_notably?).to be false
end
end
end

View file

@ -1,47 +1,41 @@
require "rails_helper"
describe JoinedVersion, type: :model, versioning: true do
it { expect(JoinedVersion.superclass).to be PaperTrail::Version }
RSpec.describe JoinedVersion, type: :model, versioning: true do
let(:widget) { Widget.create!(name: FFaker::Name.name) }
let(:version) { JoinedVersion.first }
describe "Scopes" do
describe "default_scope" do
it { expect(JoinedVersion.default_scopes).not_to be_empty }
describe "default_scope" do
it { expect(JoinedVersion.default_scopes).not_to be_empty }
end
describe "VersionConcern::ClassMethods" do
before { widget } # persist a widget
describe "#subsequent" do
it "does not raise error when there is a default_scope that joins" do
JoinedVersion.subsequent(version).first
end
end
describe "VersionConcern::ClassMethods" do
before { widget } # persist a widget
describe "#subsequent" do
it "does not raise error when there is a default_scope that joins" do
JoinedVersion.subsequent(version).first
end
describe "#preceding" do
it "does not raise error when there is a default scope that joins" do
JoinedVersion.preceding(version).first
end
end
describe "#preceding" do
it "does not raise error when there is a default scope that joins" do
JoinedVersion.preceding(version).first
end
end
describe "#between" do
it "does not raise error when there is a default scope that joins" do
JoinedVersion.between(Time.now, 1.minute.from_now).first
end
describe "#between" do
it "does not raise error when there is a default scope that joins" do
JoinedVersion.between(Time.now, 1.minute.from_now).first
end
end
end
describe "Methods" do
describe "#index" do
it { is_expected.to respond_to(:index) }
describe "#index" do
it { is_expected.to respond_to(:index) }
it "does not raise error when there is a default scope that joins" do
widget # persist a widget
version.index
end
it "does not raise error when there is a default scope that joins" do
widget # persist a widget
version.index
end
end
end

View file

@ -1,45 +1,41 @@
require "rails_helper"
describe Skipper, type: :model do
with_versioning do
it { is_expected.to be_versioned }
RSpec.describe Skipper, type: :model, versioning: true do
it { is_expected.to be_versioned }
describe "#update_attributes!", versioning: true do
context "updating a skipped attribute" do
let(:t1) { Time.zone.local(2015, 7, 15, 20, 34, 0) }
let(:t2) { Time.zone.local(2015, 7, 15, 20, 34, 30) }
describe "#update_attributes!", versioning: true do
context "updating a skipped attribute" do
let(:t1) { Time.zone.local(2015, 7, 15, 20, 34, 0) }
let(:t2) { Time.zone.local(2015, 7, 15, 20, 34, 30) }
it "does not create a version" do
skipper = Skipper.create!(another_timestamp: t1)
expect {
skipper.update_attributes!(another_timestamp: t2)
}.not_to(change { skipper.versions.length })
end
it "does not create a version" do
skipper = Skipper.create!(another_timestamp: t1)
expect {
skipper.update_attributes!(another_timestamp: t2)
}.not_to(change { skipper.versions.length })
end
end
end
describe "#reify" do
let(:t1) { Time.zone.local(2015, 7, 15, 20, 34, 0) }
let(:t2) { Time.zone.local(2015, 7, 15, 20, 34, 30) }
context "without preserve (default)" do
it "has no timestamp" do
skipper = Skipper.create!(another_timestamp: t1)
skipper.update_attributes!(another_timestamp: t2, name: "Foobar")
skipper = skipper.versions.last.reify
expect(skipper.another_timestamp).to be(nil)
end
end
describe "reify" do
context "reifying a with a skipped attribute" do
let(:t1) { Time.zone.local(2015, 7, 15, 20, 34, 0) }
let(:t2) { Time.zone.local(2015, 7, 15, 20, 34, 30) }
context "without preserve (default)" do
it "has no timestamp" do
skipper = Skipper.create!(another_timestamp: t1)
skipper.update_attributes!(another_timestamp: t2, name: "Foobar")
skipper = skipper.versions.last.reify
expect(skipper.another_timestamp).to be(nil)
end
end
context "with preserve" do
it "preserves its timestamp" do
skipper = Skipper.create!(another_timestamp: t1)
skipper.update_attributes!(another_timestamp: t2, name: "Foobar")
skipper = skipper.versions.last.reify(unversioned_attributes: :preserve)
expect(skipper.another_timestamp).to eq(t2)
end
end
context "with preserve" do
it "preserves its timestamp" do
skipper = Skipper.create!(another_timestamp: t1)
skipper.update_attributes!(another_timestamp: t2, name: "Foobar")
skipper = skipper.versions.last.reify(unversioned_attributes: :preserve)
expect(skipper.another_timestamp).to eq(t2)
end
end
end