Browse files

Add test within VersionConcern spec with persistence to simulate func…

…tionality in an app with multiple DB's
  • Loading branch information...
1 parent 422ca91 commit 1e5b75d26d453543eb9b2e5a6662739ba722788a @batter batter committed Dec 3, 2013
Showing with 61 additions and 36 deletions.
  1. +18 −36 spec/modules/version_concern_spec.rb
  2. +43 −0 spec/support/alt_db_init.rb
View
54 spec/modules/version_concern_spec.rb
@@ -2,49 +2,31 @@
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
43 spec/support/alt_db_init.rb
@@ -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")

0 comments on commit 1e5b75d

Please sign in to comment.