2016-03-05 17:07:32 -05:00
|
|
|
require "rails_helper"
|
2013-10-11 10:42:12 -04:00
|
|
|
|
2016-02-15 22:32:40 -05:00
|
|
|
describe PaperTrail::Version, type: :model do
|
2013-10-29 11:57:41 -04:00
|
|
|
it "should include the `VersionConcern` module to get base functionality" do
|
2014-10-09 15:04:17 -04:00
|
|
|
expect(PaperTrail::Version).to include(PaperTrail::VersionConcern)
|
2013-10-22 13:12:08 -04:00
|
|
|
end
|
|
|
|
|
2013-10-11 10:42:12 -04:00
|
|
|
describe "Attributes" do
|
2014-10-09 15:04:17 -04:00
|
|
|
it { is_expected.to have_db_column(:item_type).of_type(:string) }
|
|
|
|
it { is_expected.to have_db_column(:item_id).of_type(:integer) }
|
|
|
|
it { is_expected.to have_db_column(:event).of_type(:string) }
|
|
|
|
it { is_expected.to have_db_column(:whodunnit).of_type(:string) }
|
|
|
|
it { is_expected.to have_db_column(:object).of_type(:text) }
|
|
|
|
it { is_expected.to have_db_column(:created_at).of_type(:datetime) }
|
2014-10-21 15:05:16 -04:00
|
|
|
|
2016-02-15 22:32:40 -05:00
|
|
|
describe "object_changes column", versioning: true do
|
2016-03-05 17:07:32 -05:00
|
|
|
let(:widget) { Widget.create!(name: "Dashboard") }
|
2014-10-21 15:05:16 -04:00
|
|
|
let(:value) { widget.versions.last.object_changes }
|
|
|
|
|
|
|
|
context "serializer is YAML" do
|
|
|
|
specify { expect(PaperTrail.serializer).to be PaperTrail::Serializers::YAML }
|
|
|
|
|
|
|
|
it "should store out as a plain hash" do
|
2014-10-21 15:30:29 -04:00
|
|
|
expect(value =~ /HashWithIndifferentAccess/).to be_nil
|
2014-10-21 15:05:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "serializer is JSON" do
|
|
|
|
before(:all) { PaperTrail.serializer = PaperTrail::Serializers::JSON }
|
|
|
|
|
|
|
|
it "should store out as a plain hash" do
|
2014-10-21 15:30:29 -04:00
|
|
|
expect(value =~ /HashWithIndifferentAccess/).to be_nil
|
2014-10-21 15:05:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
after(:all) { PaperTrail.serializer = PaperTrail::Serializers::YAML }
|
|
|
|
end
|
|
|
|
end
|
2013-10-11 10:42:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "Indexes" do
|
2014-10-09 15:04:17 -04:00
|
|
|
it { is_expected.to have_db_index([:item_type, :item_id]) }
|
2013-10-11 10:42:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "Methods" do
|
|
|
|
describe "Instance" do
|
|
|
|
subject { PaperTrail::Version.new(attributes) rescue PaperTrail::Version.new }
|
|
|
|
|
2015-05-08 13:28:47 -04:00
|
|
|
describe '#paper_trail_originator' do
|
|
|
|
it { is_expected.to respond_to(:paper_trail_originator) }
|
|
|
|
|
|
|
|
context "No previous versions" do
|
|
|
|
specify { expect(subject.previous).to be_nil }
|
|
|
|
|
|
|
|
it "should return nil" do
|
|
|
|
expect(subject.paper_trail_originator).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-15 22:32:40 -05:00
|
|
|
context "Has previous version", versioning: true do
|
2015-12-19 19:01:58 -05:00
|
|
|
let(:name) { FFaker::Name.name }
|
2016-02-15 22:32:40 -05:00
|
|
|
let(:widget) { Widget.create!(name: FFaker::Name.name) }
|
2015-05-08 13:28:47 -04:00
|
|
|
before do
|
2016-02-15 22:32:40 -05:00
|
|
|
widget.versions.first.update_attributes!(whodunnit: name)
|
|
|
|
widget.update_attributes!(name: FFaker::Name.first_name)
|
2015-05-08 13:28:47 -04:00
|
|
|
end
|
|
|
|
subject { widget.versions.last }
|
|
|
|
|
|
|
|
specify { expect(subject.previous).to be_instance_of(PaperTrail::Version) }
|
|
|
|
|
|
|
|
it "should return nil" do
|
|
|
|
expect(subject.paper_trail_originator).to eq(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#originator" do
|
|
|
|
it { is_expected.to respond_to(:originator) }
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
it "should set the invoke `paper_trail_originator`" do
|
2015-07-12 23:43:25 -04:00
|
|
|
allow(ActiveSupport::Deprecation).to receive(:warn)
|
2015-05-08 13:28:47 -04:00
|
|
|
is_expected.to receive(:paper_trail_originator)
|
|
|
|
subject.originator
|
|
|
|
end
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
it "should display a deprecation warning" do
|
2015-07-12 23:43:25 -04:00
|
|
|
expect(ActiveSupport::Deprecation).to receive(:warn).
|
|
|
|
with(/Use paper_trail_originator instead of originator/)
|
2015-05-08 13:28:47 -04:00
|
|
|
subject.originator
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-09 15:04:17 -04:00
|
|
|
describe '#terminator' do
|
|
|
|
it { is_expected.to respond_to(:terminator) }
|
2013-10-11 10:42:12 -04:00
|
|
|
|
2016-03-05 17:11:08 -05:00
|
|
|
let(:attributes) { { whodunnit: FFaker::Name.first_name } }
|
2013-10-11 10:42:12 -04:00
|
|
|
|
|
|
|
it "is an alias for the `whodunnit` attribute" do
|
2015-05-08 13:28:47 -04:00
|
|
|
expect(subject.terminator).to eq(attributes[:whodunnit])
|
2013-10-11 10:42:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-09 15:04:17 -04:00
|
|
|
describe '#version_author' do
|
|
|
|
it { is_expected.to respond_to(:version_author) }
|
2013-10-11 10:42:12 -04:00
|
|
|
|
|
|
|
it "should be an alias for the `terminator` method" do
|
2014-10-09 15:04:17 -04:00
|
|
|
expect(subject.method(:version_author)).to eq(subject.method(:terminator))
|
2013-10-11 10:42:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-06-26 17:29:09 -04:00
|
|
|
|
|
|
|
describe "Class" do
|
2015-04-09 13:48:35 -04:00
|
|
|
column_overrides = [false]
|
2016-03-05 17:07:32 -05:00
|
|
|
if ENV["DB"] == "postgres" && ::ActiveRecord::VERSION::MAJOR >= 4
|
|
|
|
column_overrides << "json"
|
2015-05-07 14:45:03 -04:00
|
|
|
# 'jsonb' column types are only supported for ActiveRecord 4.2+
|
2016-03-05 17:07:32 -05:00
|
|
|
column_overrides << "jsonb" if ::ActiveRecord::VERSION::STRING >= "4.2"
|
2015-05-07 14:45:03 -04:00
|
|
|
end
|
2014-06-26 17:29:09 -04:00
|
|
|
|
2015-04-09 15:27:15 -04:00
|
|
|
column_overrides.shuffle.each do |override|
|
2015-04-09 13:48:35 -04:00
|
|
|
context "with a #{override || 'text'} column" do
|
2014-06-26 17:29:09 -04:00
|
|
|
before do
|
2015-04-09 13:48:35 -04:00
|
|
|
if override
|
|
|
|
ActiveRecord::Base.connection.execute("SAVEPOINT pgtest;")
|
2016-03-05 17:22:29 -05:00
|
|
|
%w(object object_changes).each do |column|
|
2016-02-15 18:27:57 -05:00
|
|
|
ActiveRecord::Base.connection.execute(
|
|
|
|
"ALTER TABLE versions DROP COLUMN #{column};"
|
|
|
|
)
|
|
|
|
ActiveRecord::Base.connection.execute(
|
|
|
|
"ALTER TABLE versions ADD COLUMN #{column} #{override};"
|
|
|
|
)
|
2015-04-09 14:56:26 -04:00
|
|
|
end
|
2015-04-09 13:48:35 -04:00
|
|
|
PaperTrail::Version.reset_column_information
|
|
|
|
end
|
2014-06-26 17:29:09 -04:00
|
|
|
end
|
2015-04-09 13:48:35 -04:00
|
|
|
after do
|
|
|
|
if override
|
|
|
|
ActiveRecord::Base.connection.execute("ROLLBACK TO SAVEPOINT pgtest;")
|
|
|
|
PaperTrail::Version.reset_column_information
|
2014-06-26 17:29:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-09 13:48:35 -04:00
|
|
|
describe '#where_object' do
|
|
|
|
it { expect(PaperTrail::Version).to respond_to(:where_object) }
|
2014-06-26 17:29:09 -04:00
|
|
|
|
2015-04-09 13:48:35 -04:00
|
|
|
context "invalid arguments" do
|
|
|
|
it "should raise an error" do
|
2016-02-15 18:27:57 -05:00
|
|
|
expect {
|
|
|
|
PaperTrail::Version.where_object(:foo)
|
|
|
|
}.to raise_error(ArgumentError)
|
|
|
|
expect {
|
|
|
|
PaperTrail::Version.where_object([])
|
|
|
|
}.to raise_error(ArgumentError)
|
2015-04-09 13:48:35 -04:00
|
|
|
end
|
2014-06-26 17:29:09 -04:00
|
|
|
end
|
2014-10-21 15:05:16 -04:00
|
|
|
|
2016-02-15 22:32:40 -05:00
|
|
|
context "valid arguments", versioning: true do
|
2015-04-09 13:48:35 -04:00
|
|
|
let(:widget) { Widget.new }
|
2015-12-19 19:01:58 -05:00
|
|
|
let(:name) { FFaker::Name.first_name }
|
2015-04-09 13:48:35 -04:00
|
|
|
let(:int) { rand(10) + 1 }
|
|
|
|
|
|
|
|
before do
|
2016-02-15 22:32:40 -05:00
|
|
|
widget.update_attributes!(name: name, an_integer: int)
|
2016-03-05 17:07:32 -05:00
|
|
|
widget.update_attributes!(name: "foobar", an_integer: 100)
|
2016-02-15 22:32:40 -05:00
|
|
|
widget.update_attributes!(name: FFaker::Name.last_name, an_integer: 15)
|
2015-04-09 13:48:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "`serializer == YAML`" do
|
2016-02-15 18:27:57 -05:00
|
|
|
specify do
|
|
|
|
expect(PaperTrail.serializer).to be PaperTrail::Serializers::YAML
|
|
|
|
end
|
2015-04-09 13:48:35 -04:00
|
|
|
|
|
|
|
it "should be able to locate versions according to their `object` contents" do
|
2016-02-15 18:27:57 -05:00
|
|
|
expect(
|
2016-02-15 22:32:40 -05:00
|
|
|
PaperTrail::Version.where_object(name: name)
|
2016-02-15 18:27:57 -05:00
|
|
|
).to eq([widget.versions[1]])
|
|
|
|
expect(
|
2016-02-15 22:32:40 -05:00
|
|
|
PaperTrail::Version.where_object(an_integer: 100)
|
2016-02-15 18:27:57 -05:00
|
|
|
).to eq([widget.versions[2]])
|
2015-04-09 13:48:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-15 18:27:57 -05:00
|
|
|
context "JSON serializer" do
|
|
|
|
before(:all) do
|
|
|
|
PaperTrail.serializer = PaperTrail::Serializers::JSON
|
|
|
|
end
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect(PaperTrail.serializer).to be PaperTrail::Serializers::JSON
|
|
|
|
end
|
2015-04-09 13:48:35 -04:00
|
|
|
|
|
|
|
it "should be able to locate versions according to their `object` contents" do
|
2016-02-15 18:27:57 -05:00
|
|
|
expect(
|
2016-02-15 22:32:40 -05:00
|
|
|
PaperTrail::Version.where_object(name: name)
|
2016-02-15 18:27:57 -05:00
|
|
|
).to eq([widget.versions[1]])
|
|
|
|
expect(
|
2016-02-15 22:32:40 -05:00
|
|
|
PaperTrail::Version.where_object(an_integer: 100)
|
2016-02-15 18:27:57 -05:00
|
|
|
).to eq([widget.versions[2]])
|
2015-04-09 13:48:35 -04:00
|
|
|
end
|
|
|
|
|
2016-02-15 18:27:57 -05:00
|
|
|
after(:all) do
|
|
|
|
PaperTrail.serializer = PaperTrail::Serializers::YAML
|
|
|
|
end
|
2015-04-09 13:48:35 -04:00
|
|
|
end
|
|
|
|
end
|
2014-06-26 17:29:09 -04:00
|
|
|
end
|
2015-01-07 16:57:45 -05:00
|
|
|
|
2015-04-09 14:56:26 -04:00
|
|
|
describe '#where_object_changes' do
|
|
|
|
context "invalid arguments" do
|
|
|
|
it "should raise an error" do
|
2016-02-15 18:27:57 -05:00
|
|
|
expect {
|
|
|
|
PaperTrail::Version.where_object_changes(:foo)
|
|
|
|
}.to raise_error(ArgumentError)
|
|
|
|
expect {
|
|
|
|
PaperTrail::Version.where_object_changes([])
|
|
|
|
}.to raise_error(ArgumentError)
|
2015-04-09 14:56:26 -04:00
|
|
|
end
|
|
|
|
end
|
2015-01-07 16:57:45 -05:00
|
|
|
|
2016-02-15 22:32:40 -05:00
|
|
|
context "valid arguments", versioning: true do
|
2015-04-09 14:56:26 -04:00
|
|
|
let(:widget) { Widget.new }
|
2015-12-19 19:01:58 -05:00
|
|
|
let(:name) { FFaker::Name.first_name }
|
2015-04-09 14:56:26 -04:00
|
|
|
let(:int) { rand(5) + 2 }
|
2015-01-07 16:57:45 -05:00
|
|
|
|
2015-04-09 14:56:26 -04:00
|
|
|
before do
|
2016-02-15 22:32:40 -05:00
|
|
|
widget.update_attributes!(name: name, an_integer: 0)
|
2016-03-05 17:07:32 -05:00
|
|
|
widget.update_attributes!(name: "foobar", an_integer: 77)
|
2016-02-15 22:32:40 -05:00
|
|
|
widget.update_attributes!(name: FFaker::Name.last_name, an_integer: int)
|
2015-04-09 14:56:26 -04:00
|
|
|
end
|
|
|
|
|
2016-02-15 18:27:57 -05:00
|
|
|
context "YAML serializer" do
|
2015-04-09 14:56:26 -04:00
|
|
|
specify { expect(PaperTrail.serializer).to be PaperTrail::Serializers::YAML }
|
2015-01-07 16:57:45 -05:00
|
|
|
|
2016-02-15 18:27:57 -05:00
|
|
|
it "locates versions according to their `object_changes` contents" do
|
|
|
|
expect(
|
2016-02-15 22:32:40 -05:00
|
|
|
widget.versions.where_object_changes(name: name)
|
2016-02-15 18:27:57 -05:00
|
|
|
).to eq(widget.versions[0..1])
|
|
|
|
expect(
|
2016-02-15 22:32:40 -05:00
|
|
|
widget.versions.where_object_changes(an_integer: 77)
|
2016-02-15 18:27:57 -05:00
|
|
|
).to eq(widget.versions[1..2])
|
|
|
|
expect(
|
2016-02-15 22:32:40 -05:00
|
|
|
widget.versions.where_object_changes(an_integer: int)
|
2016-02-15 18:27:57 -05:00
|
|
|
).to eq([widget.versions.last])
|
2015-04-09 14:56:26 -04:00
|
|
|
end
|
2015-01-07 16:57:45 -05:00
|
|
|
|
2016-02-15 18:27:57 -05:00
|
|
|
it "handles queries for multiple attributes" do
|
|
|
|
expect(
|
2016-03-05 17:07:32 -05:00
|
|
|
widget.versions.where_object_changes(an_integer: 77, name: "foobar")
|
2016-02-15 18:27:57 -05:00
|
|
|
).to eq(widget.versions[1..2])
|
2015-04-09 14:56:26 -04:00
|
|
|
end
|
|
|
|
end
|
2015-01-07 16:57:45 -05:00
|
|
|
|
2016-02-15 18:27:57 -05:00
|
|
|
context "JSON serializer" do
|
2015-04-09 14:56:26 -04:00
|
|
|
before(:all) { PaperTrail.serializer = PaperTrail::Serializers::JSON }
|
|
|
|
specify { expect(PaperTrail.serializer).to be PaperTrail::Serializers::JSON }
|
2015-01-07 16:57:45 -05:00
|
|
|
|
2016-02-15 18:27:57 -05:00
|
|
|
it "locates versions according to their `object_changes` contents" do
|
|
|
|
expect(
|
2016-02-15 22:32:40 -05:00
|
|
|
widget.versions.where_object_changes(name: name)
|
2016-02-15 18:27:57 -05:00
|
|
|
).to eq(widget.versions[0..1])
|
|
|
|
expect(
|
2016-02-15 22:32:40 -05:00
|
|
|
widget.versions.where_object_changes(an_integer: 77)
|
2016-02-15 18:27:57 -05:00
|
|
|
).to eq(widget.versions[1..2])
|
|
|
|
expect(
|
2016-02-15 22:32:40 -05:00
|
|
|
widget.versions.where_object_changes(an_integer: int)
|
2016-02-15 18:27:57 -05:00
|
|
|
).to eq([widget.versions.last])
|
2015-04-09 14:56:26 -04:00
|
|
|
end
|
2015-01-07 16:57:45 -05:00
|
|
|
|
2016-02-15 18:27:57 -05:00
|
|
|
it "handles queries for multiple attributes" do
|
|
|
|
expect(
|
2016-03-05 17:07:32 -05:00
|
|
|
widget.versions.where_object_changes(an_integer: 77, name: "foobar")
|
2016-02-15 18:27:57 -05:00
|
|
|
).to eq(widget.versions[1..2])
|
2015-04-09 14:56:26 -04:00
|
|
|
end
|
2015-01-07 16:57:45 -05:00
|
|
|
|
2015-04-09 14:56:26 -04:00
|
|
|
after(:all) { PaperTrail.serializer = PaperTrail::Serializers::YAML }
|
|
|
|
end
|
2015-01-07 16:57:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-06-26 17:29:09 -04:00
|
|
|
end
|
2013-10-11 10:42:12 -04:00
|
|
|
end
|
|
|
|
end
|