2017-12-10 23:05:11 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-30 00:58:26 -04:00
|
|
|
require "spec_helper"
|
2015-05-06 22:29:39 -04:00
|
|
|
|
2015-11-17 23:59:35 -05:00
|
|
|
# The `json_versions` table tests postgres' `json` data type. So, that
|
|
|
|
# table is only created when testing against postgres and ActiveRecord >= 4.
|
|
|
|
if JsonVersion.table_exists?
|
2017-04-01 01:50:13 -04:00
|
|
|
RSpec.describe JsonVersion, type: :model do
|
|
|
|
it "includes the VersionConcern module" do
|
|
|
|
expect(described_class).to include(PaperTrail::VersionConcern)
|
2015-05-07 14:56:28 -04:00
|
|
|
end
|
2015-05-06 22:29:39 -04:00
|
|
|
|
|
|
|
describe "Methods" do
|
|
|
|
describe "Class" do
|
2016-06-27 03:02:33 -04:00
|
|
|
describe "#where_object" do
|
2017-04-01 01:50:13 -04:00
|
|
|
it { expect(described_class).to respond_to(:where_object) }
|
2015-05-06 22:29:39 -04:00
|
|
|
|
2016-01-18 14:21:27 -05:00
|
|
|
it "escapes values" do
|
2016-03-05 17:07:32 -05:00
|
|
|
f = Fruit.create(name: "Bobby")
|
2016-01-18 14:21:27 -05:00
|
|
|
expect(
|
|
|
|
f.
|
|
|
|
versions.
|
2016-02-15 22:32:40 -05:00
|
|
|
where_object(name: "Robert'; DROP TABLE Students;--").
|
2016-01-18 14:21:27 -05:00
|
|
|
count
|
|
|
|
).to eq(0)
|
|
|
|
end
|
|
|
|
|
2015-05-06 22:29:39 -04:00
|
|
|
context "invalid arguments" do
|
2017-04-01 01:50:13 -04:00
|
|
|
it "raises an error" do
|
|
|
|
expect { described_class.where_object(:foo) }.to raise_error(ArgumentError)
|
|
|
|
expect { described_class.where_object([]) }.to raise_error(ArgumentError)
|
2015-05-06 22:29:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-15 22:32:40 -05:00
|
|
|
context "valid arguments", versioning: true do
|
2017-05-21 02:40:23 -04:00
|
|
|
let(:fruit_names) { %w[apple orange lemon banana lime coconut strawberry blueberry] }
|
2015-05-06 22:29:39 -04:00
|
|
|
let(:fruit) { Fruit.new }
|
2016-03-05 17:07:32 -05:00
|
|
|
let(:name) { "pomegranate" }
|
2015-12-19 19:01:58 -05:00
|
|
|
let(:color) { FFaker::Color.name }
|
2015-05-06 22:29:39 -04:00
|
|
|
|
|
|
|
before do
|
2016-02-15 22:32:40 -05:00
|
|
|
fruit.update_attributes!(name: name)
|
|
|
|
fruit.update_attributes!(name: fruit_names.sample, color: color)
|
|
|
|
fruit.update_attributes!(name: fruit_names.sample, color: FFaker::Color.name)
|
2015-05-06 22:29:39 -04:00
|
|
|
end
|
|
|
|
|
2017-04-01 01:50:13 -04:00
|
|
|
it "locates versions according to their `object` contents" do
|
|
|
|
expect(described_class.where_object(name: name)).to eq([fruit.versions[1]])
|
|
|
|
expect(described_class.where_object(color: color)).to eq([fruit.versions[2]])
|
2015-05-06 22:29:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-27 03:02:33 -04:00
|
|
|
describe "#where_object_changes" do
|
2017-04-01 01:50:13 -04:00
|
|
|
it { expect(described_class).to respond_to(:where_object_changes) }
|
2015-05-06 22:29:39 -04:00
|
|
|
|
2016-01-18 14:21:27 -05:00
|
|
|
it "escapes values" do
|
2016-03-05 17:07:32 -05:00
|
|
|
f = Fruit.create(name: "Bobby")
|
2016-01-18 14:21:27 -05:00
|
|
|
expect(
|
|
|
|
f.
|
|
|
|
versions.
|
2016-02-15 22:32:40 -05:00
|
|
|
where_object_changes(name: "Robert'; DROP TABLE Students;--").
|
2016-01-18 14:21:27 -05:00
|
|
|
count
|
|
|
|
).to eq(0)
|
|
|
|
end
|
|
|
|
|
2015-05-06 22:29:39 -04:00
|
|
|
context "invalid arguments" do
|
2017-04-01 01:50:13 -04:00
|
|
|
it "raises an error" do
|
|
|
|
expect { described_class.where_object_changes(:foo) }.to raise_error(ArgumentError)
|
|
|
|
expect { described_class.where_object_changes([]) }.to raise_error(ArgumentError)
|
2015-05-06 22:29:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-15 22:32:40 -05:00
|
|
|
context "valid arguments", versioning: true do
|
2017-05-21 02:40:23 -04:00
|
|
|
let(:color) { %w[red green] }
|
2015-12-19 21:16:25 -05:00
|
|
|
let(:fruit) { Fruit.create!(name: name[0]) }
|
2017-05-21 02:40:23 -04:00
|
|
|
let(:name) { %w[banana kiwi mango] }
|
2015-05-06 22:29:39 -04:00
|
|
|
|
|
|
|
before do
|
2015-12-19 21:16:25 -05:00
|
|
|
fruit.update_attributes!(name: name[1], color: color[0])
|
|
|
|
fruit.update_attributes!(name: name[2], color: color[1])
|
2015-05-06 22:29:39 -04:00
|
|
|
end
|
|
|
|
|
2015-12-19 19:58:49 -05:00
|
|
|
it "finds versions according to their `object_changes` contents" do
|
|
|
|
expect(
|
2015-12-19 21:16:25 -05:00
|
|
|
fruit.versions.where_object_changes(name: name[0])
|
2015-12-19 19:58:49 -05:00
|
|
|
).to match_array(fruit.versions[0..1])
|
|
|
|
expect(
|
|
|
|
fruit.versions.where_object_changes(color: color[0])
|
|
|
|
).to match_array(fruit.versions[1..2])
|
2015-05-06 22:29:39 -04:00
|
|
|
end
|
|
|
|
|
2015-12-19 19:58:49 -05:00
|
|
|
it "finds versions with multiple attributes changed" do
|
|
|
|
expect(
|
2015-12-19 21:16:25 -05:00
|
|
|
fruit.versions.where_object_changes(color: color[0], name: name[0])
|
2015-12-19 19:58:49 -05:00
|
|
|
).to match_array([fruit.versions[1]])
|
2015-05-06 22:29:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|