mirror of
https://github.com/DatabaseCleaner/database_cleaner
synced 2023-03-27 23:22:03 -04:00
extract BaseHelper from ActiveRecord spec helpers.
This commit is contained in:
parent
5afe967723
commit
911f99d197
5 changed files with 42 additions and 79 deletions
32
spec/support/active_record/base_helper.rb
Normal file
32
spec/support/active_record/base_helper.rb
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
require 'support/active_record/database_setup'
|
||||||
|
require 'support/active_record/schema_setup'
|
||||||
|
|
||||||
|
class BaseHelper
|
||||||
|
# require 'logger'
|
||||||
|
# ActiveRecord::Base.logger = Logger.new(STDERR)
|
||||||
|
|
||||||
|
def setup
|
||||||
|
create_db
|
||||||
|
establish_connection
|
||||||
|
active_record_load_schema
|
||||||
|
end
|
||||||
|
|
||||||
|
def migrate
|
||||||
|
ActiveRecord::Migrator.migrate 'spec/support/active_record/migrations'
|
||||||
|
end
|
||||||
|
|
||||||
|
def connection
|
||||||
|
ActiveRecord::Base.connection
|
||||||
|
end
|
||||||
|
|
||||||
|
def teardown
|
||||||
|
ActiveRecord::Base.connection.drop_database default_config['database']
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def establish_connection(config = default_config)
|
||||||
|
ActiveRecord::Base.establish_connection(config)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
|
@ -1,25 +1,11 @@
|
||||||
require 'support/active_record/database_setup'
|
require 'support/active_record/base_helper'
|
||||||
require 'support/active_record/schema_setup'
|
|
||||||
|
|
||||||
class MySQL2Helper
|
class MySQL2Helper < BaseHelper
|
||||||
puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql2"
|
puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql2"
|
||||||
|
|
||||||
# require 'logger'
|
|
||||||
# ActiveRecord::Base.logger = Logger.new(STDERR)
|
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
patch_mysql2_adapter
|
patch_mysql2_adapter
|
||||||
create_db
|
super
|
||||||
establish_connection
|
|
||||||
active_record_load_schema
|
|
||||||
end
|
|
||||||
|
|
||||||
def connection
|
|
||||||
ActiveRecord::Base.connection
|
|
||||||
end
|
|
||||||
|
|
||||||
def teardown
|
|
||||||
ActiveRecord::Base.connection.drop_database default_config['database']
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -35,10 +21,6 @@ class MySQL2Helper
|
||||||
ActiveRecord::Base.connection.create_database default_config['database']
|
ActiveRecord::Base.connection.create_database default_config['database']
|
||||||
end
|
end
|
||||||
|
|
||||||
def establish_connection(config = default_config)
|
|
||||||
ActiveRecord::Base.establish_connection config
|
|
||||||
end
|
|
||||||
|
|
||||||
def patch_mysql2_adapter
|
def patch_mysql2_adapter
|
||||||
# remove DEFAULT NULL from column definition, which is an error on primary keys in MySQL 5.7.3+
|
# remove DEFAULT NULL from column definition, which is an error on primary keys in MySQL 5.7.3+
|
||||||
ActiveRecord::ConnectionAdapters::Mysql2Adapter::NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
|
ActiveRecord::ConnectionAdapters::Mysql2Adapter::NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
|
||||||
|
|
|
@ -1,24 +1,11 @@
|
||||||
require 'support/active_record/database_setup'
|
require 'support/active_record/base_helper'
|
||||||
require 'support/active_record/schema_setup'
|
|
||||||
|
|
||||||
class MySQLHelper
|
class MySQLHelper < BaseHelper
|
||||||
puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql"
|
puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql"
|
||||||
|
|
||||||
# require 'logger'
|
|
||||||
# ActiveRecord::Base.logger = Logger.new(STDERR)
|
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
patch_mysql_adapter
|
patch_mysql_adapter
|
||||||
create_db
|
super
|
||||||
active_record_load_schema
|
|
||||||
end
|
|
||||||
|
|
||||||
def connection
|
|
||||||
ActiveRecord::Base.connection
|
|
||||||
end
|
|
||||||
|
|
||||||
def teardown
|
|
||||||
ActiveRecord::Base.connection.drop_database default_config['database']
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -1,25 +1,8 @@
|
||||||
require 'support/active_record/database_setup'
|
require 'support/active_record/base_helper'
|
||||||
require 'support/active_record/schema_setup'
|
|
||||||
|
|
||||||
class PostgreSQLHelper
|
class PostgreSQLHelper < BaseHelper
|
||||||
puts "Active Record #{ActiveRecord::VERSION::STRING}, pg"
|
puts "Active Record #{ActiveRecord::VERSION::STRING}, pg"
|
||||||
|
|
||||||
# ActiveRecord::Base.logger = Logger.new(STDERR)
|
|
||||||
|
|
||||||
def setup
|
|
||||||
create_db
|
|
||||||
establish_connection
|
|
||||||
active_record_load_schema
|
|
||||||
end
|
|
||||||
|
|
||||||
def migrate
|
|
||||||
ActiveRecord::Migrator.migrate 'spec/support/active_record/migrations'
|
|
||||||
end
|
|
||||||
|
|
||||||
def connection
|
|
||||||
ActiveRecord::Base.connection
|
|
||||||
end
|
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
ActiveRecord::Base.connection.execute "DROP TABLE users, agents;"
|
ActiveRecord::Base.connection.execute "DROP TABLE users, agents;"
|
||||||
rescue ActiveRecord::StatementInvalid
|
rescue ActiveRecord::StatementInvalid
|
||||||
|
@ -37,9 +20,5 @@ class PostgreSQLHelper
|
||||||
ActiveRecord::Base.connection.create_database(default_config['database'], default_config.merge('encoding' => @encoding))
|
ActiveRecord::Base.connection.create_database(default_config['database'], default_config.merge('encoding' => @encoding))
|
||||||
rescue ActiveRecord::StatementInvalid
|
rescue ActiveRecord::StatementInvalid
|
||||||
end
|
end
|
||||||
|
|
||||||
def establish_connection(config = default_config)
|
|
||||||
ActiveRecord::Base.establish_connection(config)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,21 +1,8 @@
|
||||||
require 'support/active_record/database_setup'
|
require 'support/active_record/base_helper'
|
||||||
require 'support/active_record/schema_setup'
|
|
||||||
|
|
||||||
class SQLite3Helper
|
class SQLite3Helper < BaseHelper
|
||||||
puts "Active Record #{ActiveRecord::VERSION::STRING}, sqlite3"
|
puts "Active Record #{ActiveRecord::VERSION::STRING}, sqlite3"
|
||||||
|
|
||||||
# ActiveRecord::Base.logger = Logger.new(STDERR)
|
|
||||||
|
|
||||||
def setup
|
|
||||||
create_db
|
|
||||||
establish_connection
|
|
||||||
active_record_load_schema
|
|
||||||
end
|
|
||||||
|
|
||||||
def connection
|
|
||||||
ActiveRecord::Base.connection
|
|
||||||
end
|
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
ActiveRecord::Base.connection.truncate_table('users')
|
ActiveRecord::Base.connection.truncate_table('users')
|
||||||
ActiveRecord::Base.connection.truncate_table('agents')
|
ActiveRecord::Base.connection.truncate_table('agents')
|
||||||
|
@ -31,9 +18,5 @@ class SQLite3Helper
|
||||||
@encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8'
|
@encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8'
|
||||||
establish_connection(default_config.merge('database' => 'sqlite3', 'schema_search_path' => 'public'))
|
establish_connection(default_config.merge('database' => 'sqlite3', 'schema_search_path' => 'public'))
|
||||||
end
|
end
|
||||||
|
|
||||||
def establish_connection(config = default_config)
|
|
||||||
ActiveRecord::Base.establish_connection(config)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue