prevent specs from leaking global state modifications.

This commit is contained in:
Micah Geisel 2018-04-26 09:59:07 -07:00
parent 467ab08e35
commit ae234c4e1a
15 changed files with 27 additions and 65 deletions

View file

@ -79,7 +79,7 @@ module DatabaseCleaner
if ::ActiveRecord::Base.respond_to?(:descendants) if ::ActiveRecord::Base.respond_to?(:descendants)
database_name = connection_hash["database"] || connection_hash[:database] database_name = connection_hash["database"] || connection_hash[:database]
models = ::ActiveRecord::Base.descendants models = ::ActiveRecord::Base.descendants
models.detect { |m| m.connection_pool.spec.config[:database] == database_name } models.select(&:connection_pool).detect { |m| m.connection_pool.spec.config[:database] == database_name }
end end
end end

View file

@ -2,6 +2,7 @@ require 'spec_helper'
require 'active_record' require 'active_record'
require 'database_cleaner/active_record/base' require 'database_cleaner/active_record/base'
require 'database_cleaner/shared_strategy' require 'database_cleaner/shared_strategy'
require 'support/active_record/schema_setup'
class FakeModel class FakeModel
def self.connection def self.connection

View file

@ -21,23 +21,19 @@ module DatabaseCleaner
before do before do
connection.stub(:transaction) connection.stub(:transaction)
connection.stub(begin_transaction_method) connection.stub(begin_transaction_method)
connection.stub(:respond_to?).with(:begin_transaction).and_return(:begin_transaction == begin_transaction_method)
end end
it "should increment open transactions if possible" do it "should increment open transactions if possible" do
connection.stub(:respond_to?).with(:increment_open_transactions).and_return(true)
connection.should_receive(:increment_open_transactions) connection.should_receive(:increment_open_transactions)
Transaction.new.start Transaction.new.start
end end
it "should tell ActiveRecord to increment connection if its not possible to increment current connection" do it "should tell ActiveRecord to increment connection if its not possible to increment current connection" do
connection.stub(:respond_to?).with(:increment_open_transactions).and_return(false)
::ActiveRecord::Base.should_receive(:increment_open_transactions) ::ActiveRecord::Base.should_receive(:increment_open_transactions)
Transaction.new.start Transaction.new.start
end end
it "should start a transaction" do it "should start a transaction" do
connection.stub(:respond_to?).with(:increment_open_transactions).and_return(true)
connection.stub(:increment_open_transactions) connection.stub(:increment_open_transactions)
connection.should_receive(begin_transaction_method) connection.should_receive(begin_transaction_method)
connection.should_receive(:transaction) connection.should_receive(:transaction)
@ -61,9 +57,6 @@ module DatabaseCleaner
it "should decrement open transactions if possible" do it "should decrement open transactions if possible" do
connection.should_receive(:open_transactions).and_return(1) connection.should_receive(:open_transactions).and_return(1)
connection.stub(:respond_to?).with(:decrement_open_transactions).and_return(true)
connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false)
connection.stub(:respond_to?).with(:rollback_transaction).and_return(false)
connection.stub(:rollback_db_transaction) connection.stub(:rollback_db_transaction)
connection.should_receive(:decrement_open_transactions) connection.should_receive(:decrement_open_transactions)
@ -78,9 +71,6 @@ module DatabaseCleaner
it "should decrement connection via ActiveRecord::Base if connection won't" do it "should decrement connection via ActiveRecord::Base if connection won't" do
connection.should_receive(:open_transactions).and_return(1) connection.should_receive(:open_transactions).and_return(1)
connection.stub(:respond_to?).with(:decrement_open_transactions).and_return(false)
connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false)
connection.stub(:respond_to?).with(:rollback_transaction).and_return(false)
connection.stub(:rollback_db_transaction) connection.stub(:rollback_db_transaction)
::ActiveRecord::Base.should_receive(:decrement_open_transactions) ::ActiveRecord::Base.should_receive(:decrement_open_transactions)
@ -91,15 +81,9 @@ module DatabaseCleaner
connection_pool.stub(:connections).and_return([connection, connection_2]) connection_pool.stub(:connections).and_return([connection, connection_2])
connection.should_receive(:open_transactions).and_return(1) connection.should_receive(:open_transactions).and_return(1)
connection.stub(:respond_to?).with(:decrement_open_transactions).and_return(false)
connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false)
connection.stub(:respond_to?).with(:rollback_transaction).and_return(false)
connection.stub(:rollback_db_transaction) connection.stub(:rollback_db_transaction)
connection_2.should_receive(:open_transactions).and_return(1) connection_2.should_receive(:open_transactions).and_return(1)
connection_2.stub(:respond_to?).with(:decrement_open_transactions).and_return(false)
connection_2.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false)
connection_2.stub(:respond_to?).with(:rollback_transaction).and_return(false)
connection_2.stub(:rollback_db_transaction) connection_2.stub(:rollback_db_transaction)
::ActiveRecord::Base.should_receive(:decrement_open_transactions).twice ::ActiveRecord::Base.should_receive(:decrement_open_transactions).twice
@ -110,9 +94,6 @@ module DatabaseCleaner
connection_pool.stub(:connections).and_return([connection, connection_2]) connection_pool.stub(:connections).and_return([connection, connection_2])
connection.should_receive(:open_transactions).and_return(1) connection.should_receive(:open_transactions).and_return(1)
connection.stub(:respond_to?).with(:decrement_open_transactions).and_return(false)
connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false)
connection.stub(:respond_to?).with(:rollback_transaction).and_return(false)
connection.stub(:rollback_db_transaction) connection.stub(:rollback_db_transaction)
connection_2.should_receive(:open_transactions).and_return(0) connection_2.should_receive(:open_transactions).and_return(0)
@ -150,8 +131,6 @@ module DatabaseCleaner
it "should decrement connection via ActiveRecord::Base if connection won't" do it "should decrement connection via ActiveRecord::Base if connection won't" do
connection.should_receive(:open_transactions).and_return(1) connection.should_receive(:open_transactions).and_return(1)
connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false)
connection.stub(:respond_to?).with(:rollback_transaction).and_return(true)
connection.stub(:rollback_transaction) connection.stub(:rollback_transaction)
::ActiveRecord::Base.should_not_receive(:decrement_open_transactions) ::ActiveRecord::Base.should_not_receive(:decrement_open_transactions)

View file

@ -7,9 +7,9 @@ require 'database_cleaner/active_record/truncation/shared_fast_truncation'
module ActiveRecord module ActiveRecord
module ConnectionAdapters module ConnectionAdapters
describe do describe do
before(:all) { active_record_mysql2_setup } before(:all) { MySQL2Helper.active_record_mysql2_setup }
let(:connection) { active_record_mysql2_connection } let(:connection) { MySQL2Helper.active_record_mysql2_connection }
describe "#truncate_table" do describe "#truncate_table" do
it "should truncate the table" do it "should truncate the table" do
@ -30,7 +30,7 @@ module ActiveRecord
end end
it_behaves_like "an adapter with pre-count truncation" do it_behaves_like "an adapter with pre-count truncation" do
let(:connection) { active_record_mysql2_connection } let(:connection) { MySQL2Helper.active_record_mysql2_connection }
end end
end end
end end

View file

@ -7,9 +7,9 @@ require 'database_cleaner/active_record/truncation/shared_fast_truncation'
module ActiveRecord module ActiveRecord
module ConnectionAdapters module ConnectionAdapters
describe do describe do
before(:all) { active_record_mysql_setup } before(:all) { MySQLHelper.active_record_mysql_setup }
let(:connection) { active_record_mysql_connection } let(:connection) { MySQLHelper.active_record_mysql_connection }
describe "#truncate_table" do describe "#truncate_table" do
it "should truncate the table" do it "should truncate the table" do
@ -30,7 +30,7 @@ module ActiveRecord
end end
it_behaves_like "an adapter with pre-count truncation" do it_behaves_like "an adapter with pre-count truncation" do
let(:connection) { active_record_mysql_connection } let(:connection) { MySQLHelper.active_record_mysql_connection }
end end
end end
end end

View file

@ -8,18 +8,18 @@ module ActiveRecord
module ConnectionAdapters module ConnectionAdapters
describe "schema_migrations table" do describe "schema_migrations table" do
it "is not truncated" do it "is not truncated" do
active_record_pg_migrate PostgreSQLHelper.active_record_pg_migrate
DatabaseCleaner::ActiveRecord::Truncation.new.clean DatabaseCleaner::ActiveRecord::Truncation.new.clean
result = active_record_pg_connection.execute("select count(*) from schema_migrations;") result = PostgreSQLHelper.active_record_pg_connection.execute("select count(*) from schema_migrations;")
result.values.first.should eq ["2"] result.values.first.should eq ["2"]
end end
end end
describe do describe do
before(:all) { active_record_pg_setup } before(:all) { PostgreSQLHelper.active_record_pg_setup }
let(:connection) do let(:connection) do
active_record_pg_connection PostgreSQLHelper.active_record_pg_connection
end end
before(:each) do before(:each) do
@ -67,7 +67,7 @@ module ActiveRecord
end end
it_behaves_like "an adapter with pre-count truncation" do it_behaves_like "an adapter with pre-count truncation" do
let(:connection) { active_record_pg_connection } let(:connection) { PostgreSQLHelper.active_record_pg_connection }
end end
end end

View file

@ -6,10 +6,10 @@ require 'database_cleaner/active_record/truncation'
module ActiveRecord module ActiveRecord
module ConnectionAdapters module ConnectionAdapters
describe do describe do
before(:all) { active_record_sqlite3_setup } before(:all) { SQLite3Helper.active_record_sqlite3_setup }
let(:connection) do let(:connection) do
active_record_sqlite3_connection SQLite3Helper.active_record_sqlite3_connection
end end
before(:each) do before(:each) do

View file

@ -302,14 +302,11 @@ module DatabaseCleaner
end end
it "should check that strategy supports db specification" do it "should check that strategy supports db specification" do
strategy.should_receive(:respond_to?).with(:db=).and_return(true)
strategy.stub(:db=) strategy.stub(:db=)
subject.strategy_db = :a_db subject.strategy_db = :a_db
end end
context "when strategy supports db specification" do context "when strategy supports db specification" do
before(:each) { strategy.stub(:respond_to?).with(:db=).and_return true }
it "should pass db to the strategy" do it "should pass db to the strategy" do
strategy.should_receive(:db=).with(:a_db) strategy.should_receive(:db=).with(:a_db)
subject.strategy_db = :a_db subject.strategy_db = :a_db
@ -317,8 +314,6 @@ module DatabaseCleaner
end end
context "when strategy doesn't supports db specification" do context "when strategy doesn't supports db specification" do
before(:each) { strategy.stub(:respond_to?).with(:db=).and_return false }
it "should check to see if db is :default" do it "should check to see if db is :default" do
db = double("default") db = double("default")
db.should_receive(:==).with(:default).and_return(true) db.should_receive(:==).with(:default).and_return(true)
@ -540,7 +535,6 @@ module DatabaseCleaner
end end
it "should use available_strategies (for the error message) if its available" do it "should use available_strategies (for the error message) if its available" do
strategy_class.stub(:respond_to?).with(:available_strategies).and_return(true)
strategy_class.should_receive(:available_strategies).and_return([]) strategy_class.should_receive(:available_strategies).and_return([])
subject.stub(:orm_module).and_return(strategy_class) subject.stub(:orm_module).and_return(strategy_class)

View file

@ -1,18 +1,18 @@
require 'spec_helper' require 'spec_helper'
require 'dm-core' require 'dm-core'
require 'dm-sqlite-adapter' require 'dm-sqlite-adapter'
require File.expand_path('../../../../support/data_mapper/sqlite3_setup', __FILE__) require 'support/data_mapper/sqlite3_setup'
require 'database_cleaner/data_mapper/truncation' require 'database_cleaner/data_mapper/truncation'
module DataMapper module DataMapper
module ConnectionAdapters module ConnectionAdapters
describe do describe do
before(:all) { data_mapper_sqlite3_setup } before(:all) { DataMapperSQLite3Helper.data_mapper_sqlite3_setup }
let(:adapter) { DataMapperSQLite3Adapter } let(:adapter) { DataMapperSQLite3Adapter }
let(:connection) do let(:connection) do
data_mapper_sqlite3_connection DataMapperSQLite3Helper.data_mapper_sqlite3_connection
end end
before(:each) do before(:each) do

View file

@ -23,16 +23,10 @@ RSpec.configure do |config|
config.run_all_when_everything_filtered = true config.run_all_when_everything_filtered = true
config.expect_with :rspec do |expectations| config.expect_with :rspec do |expectations|
# Enable only the newer, non-monkey-patching expect syntax.
# For more details, see:
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
expectations.syntax = [:should, :expect] expectations.syntax = [:should, :expect]
end end
config.mock_with :rspec do |mocks| config.mock_with :rspec do |mocks|
# Enable only the newer, non-monkey-patching expect syntax.
# For more details, see:
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
mocks.syntax = [:should, :expect] mocks.syntax = [:should, :expect]
end end
end end

View file

@ -2,6 +2,8 @@ require 'support/active_record/database_setup'
require 'support/active_record/schema_setup' require 'support/active_record/schema_setup'
module MySQL2Helper module MySQL2Helper
extend self
puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql2" puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql2"
# require 'logger' # require 'logger'
@ -39,6 +41,3 @@ module MySQL2Helper
end end
end end
RSpec.configure do |c|
c.include MySQL2Helper
end

View file

@ -2,6 +2,8 @@ require 'support/active_record/database_setup'
require 'support/active_record/schema_setup' require 'support/active_record/schema_setup'
module MySQLHelper module MySQLHelper
extend self
puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql" puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql"
# require 'logger' # require 'logger'
@ -39,6 +41,3 @@ module MySQLHelper
end end
end end
RSpec.configure do |c|
c.include MySQLHelper
end

View file

@ -2,6 +2,8 @@ require 'support/active_record/database_setup'
require 'support/active_record/schema_setup' require 'support/active_record/schema_setup'
module PostgreSQLHelper module PostgreSQLHelper
extend self
puts "Active Record #{ActiveRecord::VERSION::STRING}, pg" puts "Active Record #{ActiveRecord::VERSION::STRING}, pg"
# ActiveRecord::Base.logger = Logger.new(STDERR) # ActiveRecord::Base.logger = Logger.new(STDERR)
@ -43,6 +45,3 @@ module PostgreSQLHelper
end end
end end
RSpec.configure do |c|
c.include PostgreSQLHelper
end

View file

@ -2,6 +2,8 @@ require 'support/active_record/database_setup'
require 'support/active_record/schema_setup' require 'support/active_record/schema_setup'
module SQLite3Helper module SQLite3Helper
extend self
puts "Active Record #{ActiveRecord::VERSION::STRING}, sqlite3" puts "Active Record #{ActiveRecord::VERSION::STRING}, sqlite3"
# ActiveRecord::Base.logger = Logger.new(STDERR) # ActiveRecord::Base.logger = Logger.new(STDERR)
@ -35,6 +37,3 @@ module SQLite3Helper
end end
end end
RSpec.configure do |c|
c.include SQLite3Helper
end

View file

@ -2,6 +2,7 @@ require 'support/active_record/database_setup'
require 'support/data_mapper/schema_setup' require 'support/data_mapper/schema_setup'
module DataMapperSQLite3Helper module DataMapperSQLite3Helper
extend self
puts "DataMapper #{DataMapper::VERSION}, sqlite3" puts "DataMapper #{DataMapper::VERSION}, sqlite3"
@ -34,6 +35,3 @@ module DataMapperSQLite3Helper
end end
end end
RSpec.configure do |c|
c.include(DataMapperSQLite3Helper)
end