2014-10-09 15:04:17 -04:00
|
|
|
require 'rails_helper'
|
2013-10-11 10:42:12 -04:00
|
|
|
|
2014-10-09 15:04:17 -04: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
|
|
|
|
|
|
|
describe "object_changes column", :versioning => true do
|
|
|
|
let(:widget) { Widget.create!(:name => 'Dashboard') }
|
|
|
|
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
|
|
|
|
|
|
|
|
context "Has previous version", :versioning => true do
|
2015-12-19 19:01:58 -05:00
|
|
|
let(:name) { FFaker::Name.name }
|
|
|
|
let(:widget) { Widget.create!(:name => FFaker::Name.name) }
|
2015-05-08 13:28:47 -04:00
|
|
|
before do
|
|
|
|
widget.versions.first.update_attributes!(:whodunnit => name)
|
2015-12-19 19:01:58 -05:00
|
|
|
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) }
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-12-19 19:01:58 -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]
|
2015-05-07 15:04:15 -04:00
|
|
|
if ENV['DB'] == 'postgres' && ::ActiveRecord::VERSION::MAJOR >= 4
|
2015-05-07 14:45:03 -04:00
|
|
|
column_overrides << 'json'
|
|
|
|
# 'jsonb' column types are only supported for ActiveRecord 4.2+
|
|
|
|
column_overrides << 'jsonb' if ::ActiveRecord::VERSION::STRING >= '4.2'
|
|
|
|
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;")
|
2015-04-09 14:56:26 -04:00
|
|
|
%w[object object_changes].each do |column|
|
|
|
|
ActiveRecord::Base.connection.execute("ALTER TABLE versions DROP COLUMN #{column};")
|
|
|
|
ActiveRecord::Base.connection.execute("ALTER TABLE versions ADD COLUMN #{column} #{override};")
|
|
|
|
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
|
|
|
|
expect { PaperTrail::Version.where_object(:foo) }.to raise_error(ArgumentError)
|
|
|
|
expect { PaperTrail::Version.where_object([]) }.to raise_error(ArgumentError)
|
|
|
|
end
|
2014-06-26 17:29:09 -04:00
|
|
|
end
|
2014-10-21 15:05:16 -04:00
|
|
|
|
2015-04-09 13:48:35 -04:00
|
|
|
context "valid arguments", :versioning => true do
|
|
|
|
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
|
|
|
|
widget.update_attributes!(:name => name, :an_integer => int)
|
|
|
|
widget.update_attributes!(:name => 'foobar', :an_integer => 100)
|
2015-12-19 19:01:58 -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
|
|
|
|
specify { expect(PaperTrail.serializer).to be PaperTrail::Serializers::YAML }
|
|
|
|
|
|
|
|
it "should be able to locate versions according to their `object` contents" do
|
|
|
|
expect(PaperTrail::Version.where_object(:name => name)).to eq([widget.versions[1]])
|
|
|
|
expect(PaperTrail::Version.where_object(:an_integer => 100)).to eq([widget.versions[2]])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "`serializer == JSON`" do
|
|
|
|
before(:all) { PaperTrail.serializer = PaperTrail::Serializers::JSON }
|
|
|
|
specify { expect(PaperTrail.serializer).to be PaperTrail::Serializers::JSON }
|
|
|
|
|
|
|
|
it "should be able to locate versions according to their `object` contents" do
|
|
|
|
expect(PaperTrail::Version.where_object(:name => name)).to eq([widget.versions[1]])
|
|
|
|
expect(PaperTrail::Version.where_object(:an_integer => 100)).to eq([widget.versions[2]])
|
|
|
|
end
|
|
|
|
|
|
|
|
after(:all) { PaperTrail.serializer = PaperTrail::Serializers::YAML }
|
|
|
|
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
|
|
|
|
it { expect(PaperTrail::Version).to respond_to(:where_object_changes) }
|
2015-01-07 16:57:45 -05:00
|
|
|
|
2015-04-09 14:56:26 -04:00
|
|
|
context "invalid arguments" do
|
|
|
|
it "should raise an error" do
|
|
|
|
expect { PaperTrail::Version.where_object_changes(:foo) }.to raise_error(ArgumentError)
|
|
|
|
expect { PaperTrail::Version.where_object_changes([]) }.to raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
end
|
2015-01-07 16:57:45 -05:00
|
|
|
|
2015-04-09 14:56:26 -04:00
|
|
|
context "valid arguments", :versioning => true do
|
|
|
|
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
|
|
|
|
widget.update_attributes!(:name => name, :an_integer => 0)
|
|
|
|
widget.update_attributes!(:name => 'foobar', :an_integer => 77)
|
2015-12-19 19:01:58 -05:00
|
|
|
widget.update_attributes!(:name => FFaker::Name.last_name, :an_integer => int)
|
2015-04-09 14:56:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "`serializer == YAML`" do
|
|
|
|
specify { expect(PaperTrail.serializer).to be PaperTrail::Serializers::YAML }
|
2015-01-07 16:57:45 -05:00
|
|
|
|
2015-04-09 14:56:26 -04:00
|
|
|
it "should be able to locate versions according to their `object_changes` contents" do
|
|
|
|
expect(widget.versions.where_object_changes(:name => name)).to eq(widget.versions[0..1])
|
|
|
|
expect(widget.versions.where_object_changes(:an_integer => 77)).to eq(widget.versions[1..2])
|
|
|
|
expect(widget.versions.where_object_changes(:an_integer => int)).to eq([widget.versions.last])
|
|
|
|
end
|
2015-01-07 16:57:45 -05:00
|
|
|
|
2015-04-09 14:56:26 -04:00
|
|
|
it "should be able to handle queries for multiple attributes" do
|
|
|
|
expect(widget.versions.where_object_changes(:an_integer => 77, :name => 'foobar')).to eq(widget.versions[1..2])
|
|
|
|
end
|
|
|
|
end
|
2015-01-07 16:57:45 -05:00
|
|
|
|
2015-04-09 14:56:26 -04:00
|
|
|
context "`serializer == JSON`" do
|
|
|
|
before(:all) { PaperTrail.serializer = PaperTrail::Serializers::JSON }
|
|
|
|
specify { expect(PaperTrail.serializer).to be PaperTrail::Serializers::JSON }
|
2015-01-07 16:57:45 -05:00
|
|
|
|
2015-04-09 14:56:26 -04:00
|
|
|
it "should be able to locate versions according to their `object_changes` contents" do
|
|
|
|
expect(widget.versions.where_object_changes(:name => name)).to eq(widget.versions[0..1])
|
|
|
|
expect(widget.versions.where_object_changes(:an_integer => 77)).to eq(widget.versions[1..2])
|
|
|
|
expect(widget.versions.where_object_changes(:an_integer => int)).to eq([widget.versions.last])
|
|
|
|
end
|
2015-01-07 16:57:45 -05:00
|
|
|
|
2015-04-09 14:56:26 -04:00
|
|
|
it "should be able to handle queries for multiple attributes" do
|
|
|
|
expect(widget.versions.where_object_changes(:an_integer => 77, :name => 'foobar')).to eq(widget.versions[1..2])
|
|
|
|
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
|