Add test within VersionConcern spec with persistence to simulate functionality in an app with multiple DB's

This commit is contained in:
Ben Atkins 2013-12-03 15:55:45 -05:00
parent 422ca91b54
commit 1e5b75d26d
2 changed files with 61 additions and 36 deletions

View File

@ -2,49 +2,31 @@ require 'spec_helper'
describe PaperTrail::VersionConcern do
before(:all) do
module Foo
class Base < ActiveRecord::Base
self.abstract_class = true
end
class Document < Base
has_paper_trail :class_name => 'Foo::Version'
end
class Version < Base
include PaperTrail::VersionConcern
end
end
Foo::Base.establish_connection(:adapter => 'sqlite3', :database => File.expand_path('../../../test/dummy/db/test-foo.sqlite3', __FILE__))
module Bar
class Base < ActiveRecord::Base
self.abstract_class = true
end
class Document < Base
has_paper_trail :class_name => 'Bar::Version'
end
class Version < Base
include PaperTrail::VersionConcern
end
end
Bar::Base.establish_connection(:adapter => 'sqlite3', :database => File.expand_path('../../../test/dummy/db/test-bar.sqlite3', __FILE__))
end
before(:all) { require 'support/alt_db_init' }
it 'allows included class to have different connections' do
Foo::Version.connection.should_not eq Bar::Version.connection
Foo::Version.connection.should_not == Bar::Version.connection
end
it 'allows custom version class to share connection with superclass' do
Foo::Version.connection.should eq Foo::Document.connection
Bar::Version.connection.should eq Bar::Document.connection
Foo::Version.connection.should == Foo::Document.connection
Bar::Version.connection.should == Bar::Document.connection
end
it 'can be used with class_name option' do
Foo::Document.version_class_name.should eq 'Foo::Version'
Bar::Document.version_class_name.should eq 'Bar::Version'
Foo::Document.version_class_name.should == 'Foo::Version'
Bar::Document.version_class_name.should == 'Bar::Version'
end
describe 'persistence', :versioning => true do
before do
@foo_doc = Foo::Document.create!(:name => 'foobar')
@bar_doc = Bar::Document.create!(:name => 'raboof')
end
it 'should store versions in the correct corresponding db location' do
@foo_doc.versions.first.should be_instance_of(Foo::Version)
@bar_doc.versions.first.should be_instance_of(Bar::Version)
end
end
end

View File

@ -0,0 +1,43 @@
# This file copies the test database into locations for the `Foo` and `Bar` namespace,
# then defines those namespaces, then establishes the sqlite3 connection for the namespaces
# to simulate an application with multiple database connections.
db_directory = "#{Rails.root}/db"
# setup alternate databases
if RUBY_VERSION.to_f >= 1.9
FileUtils.cp "#{db_directory}/test.sqlite3", "#{db_directory}/test-foo.sqlite3"
FileUtils.cp "#{db_directory}/test.sqlite3", "#{db_directory}/test-bar.sqlite3"
else
File.cp "#{db_directory}/test.sqlite3", "#{db_directory}/test-foo.sqlite3"
File.cp "#{db_directory}/test.sqlite3", "#{db_directory}/test-bar.sqlite3"
end
module Foo
class Base < ActiveRecord::Base
self.abstract_class = true
end
class Version < Base
include PaperTrail::VersionConcern
end
class Document < Base
has_paper_trail :class_name => 'Foo::Version'
end
end
Foo::Base.establish_connection(:adapter => 'sqlite3', :database => "#{db_directory}/test-foo.sqlite3")
module Bar
class Base < ActiveRecord::Base
self.abstract_class = true
end
class Version < Base
include PaperTrail::VersionConcern
end
class Document < Base
has_paper_trail :class_name => 'Bar::Version'
end
end
Bar::Base.establish_connection(:adapter => 'sqlite3', :database => "#{db_directory}/test-bar.sqlite3")