Add test cases explicitly surrounding JSON type columns when the test suite is executed against Postgres

This commit is contained in:
Ben Atkins 2015-05-06 22:29:39 -04:00
parent 5a408e20da
commit 7834309857
4 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,78 @@
require 'rails_helper'
describe JsonVersion, :type => :model do
it "should include the `VersionConcern` module to get base functionality" do
expect(JsonVersion).to include(PaperTrail::VersionConcern)
end
if ENV['DB'] == 'postgres' || JsonVersion.table_exists?
describe "Methods" do
describe "Class" do
describe '#where_object' do
it { expect(JsonVersion).to respond_to(:where_object) }
context "invalid arguments" do
it "should raise an error" do
expect { JsonVersion.where_object(:foo) }.to raise_error(ArgumentError)
expect { JsonVersion.where_object([]) }.to raise_error(ArgumentError)
end
end
context "valid arguments", :versioning => true do
let(:fruit_names) { %w(apple orange lemon banana lime coconut strawberry blueberry) }
let(:fruit) { Fruit.new }
let(:name) { 'pomegranate' }
let(:color) { Faker::Color.name }
before do
fruit.update_attributes!(:name => name)
fruit.update_attributes!(:name => fruit_names.sample, :color => color)
fruit.update_attributes!(:name => fruit_names.sample, :color => Faker::Color.name)
end
it "should be able to locate versions according to their `object` contents" do
expect(JsonVersion.where_object(:name => name)).to eq([fruit.versions[1]])
expect(JsonVersion.where_object(:color => color)).to eq([fruit.versions[2]])
end
end
end
describe '#where_object_changes' do
it { expect(JsonVersion).to respond_to(:where_object_changes) }
context "invalid arguments" do
it "should raise an error" do
expect { JsonVersion.where_object_changes(:foo) }.to raise_error(ArgumentError)
expect { JsonVersion.where_object_changes([]) }.to raise_error(ArgumentError)
end
end
context "valid arguments", :versioning => true do
let(:fruit_names) { %w(apple orange lemon banana lime strawberry blueberry) }
let(:tropical_fruit_names) { %w(coconut pineapple kiwi mango melon) }
let(:fruit) { Fruit.new }
let(:name) { 'pomegranate' }
let(:color) { Faker::Color.name }
before do
fruit.update_attributes!(:name => name)
fruit.update_attributes!(:name => tropical_fruit_names.sample, :color => color)
fruit.update_attributes!(:name => fruit_names.sample, :color => Faker::Color.name)
end
it "should be able to locate versions according to their `object_changes` contents" do
expect(fruit.versions.where_object_changes(:name => name)).to eq(fruit.versions[0..1])
expect(fruit.versions.where_object_changes(:color => color)).to eq(fruit.versions[1..2])
end
it "should be able to handle queries for multiple attributes" do
expect(fruit.versions.where_object_changes(:color => color, :name => name)).to eq([fruit.versions[1]])
end
end
end
end
end
end
end

View File

@ -0,0 +1,5 @@
class Fruit < ActiveRecord::Base
if ENV['DB'] == 'postgres' || JsonVersion.table_exists?
has_paper_trail :class_name => 'JsonVersion'
end
end

View File

@ -0,0 +1,3 @@
class JsonVersion < PaperTrail::Version
self.table_name = 'json_versions'
end

View File

@ -60,6 +60,19 @@ class SetUpTestTables < ActiveRecord::Migration
end
add_index :post_versions, [:item_type, :item_id]
if ENV['DB'] == 'postgres'
create_table :json_versions, :force => true do |t|
t.string :item_type, :null => false
t.integer :item_id, :null => false
t.string :event, :null => false
t.string :whodunnit
t.json :object
t.json :object_changes
t.datetime :created_at
end
add_index :json_versions, [:item_type, :item_id]
end
create_table :wotsits, :force => true do |t|
t.integer :widget_id
t.string :name
@ -164,6 +177,11 @@ class SetUpTestTables < ActiveRecord::Migration
t.integer :order_id
t.string :product
end
create_table :fruits, :force => true do |t|
t.string :name
t.string :color
end
end
def self.down
@ -183,14 +201,20 @@ class SetUpTestTables < ActiveRecord::Migration
drop_table :post_versions
remove_index :versions, :column => [:item_type, :item_id]
drop_table :versions
if JsonVersion.table_exists?
remove_index :json_versions, :column => [:item_type, :item_id]
drop_table :json_versions
end
drop_table :widgets
drop_table :documents
drop_table :legacy_widgets
drop_table :things
drop_table :translations
drop_table :gadgets
drop_table :customers
drop_table :orders
drop_table :line_items
drop_table :fruits
remove_index :version_associations, :column => [:version_id]
remove_index :version_associations, :name => 'index_version_associations_on_foreign_key'
drop_table :version_associations