From 911f99d197aa91cd424706ada5e6a5de330505e6 Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Sat, 5 May 2018 11:10:34 -0700 Subject: [PATCH] extract BaseHelper from ActiveRecord spec helpers. --- spec/support/active_record/base_helper.rb | 32 +++++++++++++++++++ spec/support/active_record/mysql2_helper.rb | 24 ++------------ spec/support/active_record/mysql_helper.rb | 19 ++--------- .../active_record/postgresql_helper.rb | 25 ++------------- spec/support/active_record/sqlite3_helper.rb | 21 ++---------- 5 files changed, 42 insertions(+), 79 deletions(-) create mode 100644 spec/support/active_record/base_helper.rb diff --git a/spec/support/active_record/base_helper.rb b/spec/support/active_record/base_helper.rb new file mode 100644 index 0000000..e3beb94 --- /dev/null +++ b/spec/support/active_record/base_helper.rb @@ -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 + diff --git a/spec/support/active_record/mysql2_helper.rb b/spec/support/active_record/mysql2_helper.rb index cbdd754..5bbb27a 100644 --- a/spec/support/active_record/mysql2_helper.rb +++ b/spec/support/active_record/mysql2_helper.rb @@ -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" diff --git a/spec/support/active_record/mysql_helper.rb b/spec/support/active_record/mysql_helper.rb index 4d1fba7..eb9b8c1 100644 --- a/spec/support/active_record/mysql_helper.rb +++ b/spec/support/active_record/mysql_helper.rb @@ -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 diff --git a/spec/support/active_record/postgresql_helper.rb b/spec/support/active_record/postgresql_helper.rb index 8f5bbe6..6dd16cf 100644 --- a/spec/support/active_record/postgresql_helper.rb +++ b/spec/support/active_record/postgresql_helper.rb @@ -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 diff --git a/spec/support/active_record/sqlite3_helper.rb b/spec/support/active_record/sqlite3_helper.rb index fb373aa..5471e8e 100644 --- a/spec/support/active_record/sqlite3_helper.rb +++ b/spec/support/active_record/sqlite3_helper.rb @@ -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