extract BaseHelper from ActiveRecord spec helpers.

This commit is contained in:
Micah Geisel 2018-05-05 11:10:34 -07:00
parent 5afe967723
commit 911f99d197
5 changed files with 42 additions and 79 deletions

View 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

View file

@ -1,25 +1,11 @@
require 'support/active_record/database_setup'
require 'support/active_record/schema_setup'
require 'support/active_record/base_helper'
class MySQL2Helper
class MySQL2Helper < BaseHelper
puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql2"
# require 'logger'
# ActiveRecord::Base.logger = Logger.new(STDERR)
def setup
patch_mysql2_adapter
create_db
establish_connection
active_record_load_schema
end
def connection
ActiveRecord::Base.connection
end
def teardown
ActiveRecord::Base.connection.drop_database default_config['database']
super
end
private
@ -35,10 +21,6 @@ class MySQL2Helper
ActiveRecord::Base.connection.create_database default_config['database']
end
def establish_connection(config = default_config)
ActiveRecord::Base.establish_connection config
end
def patch_mysql2_adapter
# 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"

View file

@ -1,24 +1,11 @@
require 'support/active_record/database_setup'
require 'support/active_record/schema_setup'
require 'support/active_record/base_helper'
class MySQLHelper
class MySQLHelper < BaseHelper
puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql"
# require 'logger'
# ActiveRecord::Base.logger = Logger.new(STDERR)
def setup
patch_mysql_adapter
create_db
active_record_load_schema
end
def connection
ActiveRecord::Base.connection
end
def teardown
ActiveRecord::Base.connection.drop_database default_config['database']
super
end
private

View file

@ -1,25 +1,8 @@
require 'support/active_record/database_setup'
require 'support/active_record/schema_setup'
require 'support/active_record/base_helper'
class PostgreSQLHelper
class PostgreSQLHelper < BaseHelper
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
ActiveRecord::Base.connection.execute "DROP TABLE users, agents;"
rescue ActiveRecord::StatementInvalid
@ -37,9 +20,5 @@ class PostgreSQLHelper
ActiveRecord::Base.connection.create_database(default_config['database'], default_config.merge('encoding' => @encoding))
rescue ActiveRecord::StatementInvalid
end
def establish_connection(config = default_config)
ActiveRecord::Base.establish_connection(config)
end
end

View file

@ -1,21 +1,8 @@
require 'support/active_record/database_setup'
require 'support/active_record/schema_setup'
require 'support/active_record/base_helper'
class SQLite3Helper
class SQLite3Helper < BaseHelper
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
ActiveRecord::Base.connection.truncate_table('users')
ActiveRecord::Base.connection.truncate_table('agents')
@ -31,9 +18,5 @@ class SQLite3Helper
@encoding = default_config['encoding'] || ENV['CHARSET'] || 'utf8'
establish_connection(default_config.merge('database' => 'sqlite3', 'schema_search_path' => 'public'))
end
def establish_connection(config = default_config)
ActiveRecord::Base.establish_connection(config)
end
end