Skip to content
Browse files

Add ConnectionHelper to refactor tests.

  • Loading branch information...
1 parent 79f06a9 commit f1169d9d2509189a16f7b03fee5c76b9d023a347 @tgxworld tgxworld committed Apr 1, 2014
View
9 activerecord/test/cases/adapters/mysql/active_schema_test.rb
@@ -1,19 +1,18 @@
require "cases/helper"
+require 'support/connection_helper'
class ActiveSchemaTest < ActiveRecord::TestCase
- def setup
- @connection = ActiveRecord::Base.remove_connection
- ActiveRecord::Base.establish_connection(@connection)
+ include ConnectionHelper
+ def setup
ActiveRecord::Base.connection.singleton_class.class_eval do
alias_method :execute_without_stub, :execute
def execute(sql, name = nil) return sql end
end
end
teardown do
- ActiveRecord::Base.remove_connection
- ActiveRecord::Base.establish_connection(@connection)
+ reset_connection
end
def test_add_index
View
11 activerecord/test/cases/adapters/mysql/connection_test.rb
@@ -1,7 +1,9 @@
require "cases/helper"
+require 'support/connection_helper'
require 'support/ddl_helper'
class MysqlConnectionTest < ActiveRecord::TestCase
+ include ConnectionHelper
include DdlHelper
class Klass < ActiveRecord::Base
@@ -160,15 +162,6 @@ def test_mysql_set_session_variable_to_default
private
- def run_without_connection
- original_connection = ActiveRecord::Base.remove_connection
- begin
- yield original_connection
- ensure
- ActiveRecord::Base.establish_connection(original_connection)
- end
- end
-
def with_example_table(&block)
definition ||= <<-SQL
`id` int(11) auto_increment PRIMARY KEY,
View
9 activerecord/test/cases/adapters/mysql2/active_schema_test.rb
@@ -1,19 +1,18 @@
require "cases/helper"
+require 'support/connection_helper'
class ActiveSchemaTest < ActiveRecord::TestCase
- def setup
- @connection = ActiveRecord::Base.remove_connection
- ActiveRecord::Base.establish_connection(@connection)
+ include ConnectionHelper
+ def setup
ActiveRecord::Base.connection.singleton_class.class_eval do
alias_method :execute_without_stub, :execute
def execute(sql, name = nil) return sql end
end
end
teardown do
- ActiveRecord::Base.remove_connection
- ActiveRecord::Base.establish_connection(@connection)
+ reset_connection
end
def test_add_index
View
14 activerecord/test/cases/adapters/mysql2/connection_test.rb
@@ -1,6 +1,9 @@
require "cases/helper"
+require 'support/connection_helper'
class MysqlConnectionTest < ActiveRecord::TestCase
+ include ConnectionHelper
+
def setup
super
@subscriber = SQLSubscriber.new
@@ -103,15 +106,4 @@ def test_quote_time_usec
assert_equal "'1970-01-01 00:00:00.000000'", @connection.quote(Time.at(0).to_datetime)
end
end
-
- private
-
- def run_without_connection
- original_connection = ActiveRecord::Base.remove_connection
- begin
- yield original_connection
- ensure
- ActiveRecord::Base.establish_connection(original_connection)
- end
- end
end
View
15 activerecord/test/cases/adapters/postgresql/connection_test.rb
@@ -1,7 +1,10 @@
require "cases/helper"
+require 'support/connection_helper'
module ActiveRecord
class PostgresqlConnectionTest < ActiveRecord::TestCase
+ include ConnectionHelper
+
class NonExistentTable < ActiveRecord::Base
end
@@ -198,17 +201,5 @@ def test_set_session_variable_default
ActiveRecord::Base.establish_connection(orig_connection.deep_merge({:variables => {:debug_print_plan => :default}}))
end
end
-
- private
-
- def run_without_connection
- original_connection = ActiveRecord::Base.remove_connection
- begin
- yield original_connection
- ensure
- ActiveRecord::Base.establish_connection(original_connection)
- end
- end
-
end
end
View
6 activerecord/test/cases/adapters/postgresql/domain_test.rb
@@ -1,11 +1,11 @@
# -*- coding: utf-8 -*-
require "cases/helper"
-require 'support/postgresql_helper'
+require 'support/connection_helper'
require 'active_record/base'
require 'active_record/connection_adapters/postgresql_adapter'
class PostgresqlDomainTest < ActiveRecord::TestCase
- include PostgresqlHelper
+ include ConnectionHelper
class PostgresqlDomain < ActiveRecord::Base
self.table_name = "postgresql_domains"
@@ -27,7 +27,7 @@ def setup
teardown do
@connection.execute 'DROP TABLE IF EXISTS postgresql_domains'
@connection.execute 'DROP DOMAIN IF EXISTS custom_money'
- reset_pg_session
+ reset_connection
end
def test_column
View
6 activerecord/test/cases/adapters/postgresql/enum_test.rb
@@ -1,11 +1,11 @@
# -*- coding: utf-8 -*-
require "cases/helper"
-require 'support/postgresql_helper'
+require 'support/connection_helper'
require 'active_record/base'
require 'active_record/connection_adapters/postgresql_adapter'
class PostgresqlEnumTest < ActiveRecord::TestCase
- include PostgresqlHelper
+ include ConnectionHelper
class PostgresqlEnum < ActiveRecord::Base
self.table_name = "postgresql_enums"
@@ -28,7 +28,7 @@ def setup
teardown do
@connection.execute 'DROP TABLE IF EXISTS postgresql_enums'
@connection.execute 'DROP TYPE IF EXISTS mood'
- reset_pg_session
+ reset_connection
end
def test_column
View
16 activerecord/test/cases/adapters/postgresql/range_test.rb
@@ -1,5 +1,5 @@
require "cases/helper"
-require 'support/postgresql_helper'
+require 'support/connection_helper'
require 'active_record/base'
require 'active_record/connection_adapters/postgresql_adapter'
@@ -10,13 +10,7 @@ class PostgresqlRange < ActiveRecord::Base
class PostgresqlRangeTest < ActiveRecord::TestCase
self.use_transactional_fixtures = false
- include PostgresqlHelper
-
- teardown do
- @connection.execute 'DROP TABLE IF EXISTS postgresql_ranges'
- @connection.execute 'DROP TYPE IF EXISTS floatrange'
- reset_pg_session
- end
+ include ConnectionHelper
def setup
@connection = PostgresqlRange.connection
@@ -99,6 +93,12 @@ def setup
@empty_range = PostgresqlRange.find(105)
end
+ teardown do
+ @connection.execute 'DROP TABLE IF EXISTS postgresql_ranges'
+ @connection.execute 'DROP TYPE IF EXISTS floatrange'
+ reset_connection
+ end
+
def test_data_type_of_range_types
assert_equal :daterange, @first_range.column_for_attribute(:date_range).type
assert_equal :numrange, @first_range.column_for_attribute(:num_range).type
View
14 activerecord/test/support/connection_helper.rb
@@ -0,0 +1,14 @@
+module ConnectionHelper
+ def run_without_connection
+ original_connection = ActiveRecord::Base.remove_connection
+ yield original_connection
+ ensure
+ ActiveRecord::Base.establish_connection(original_connection)
+ end
+
+ # Used to drop all cache query plans in tests.
+ def reset_connection
+ original_connection = ActiveRecord::Base.remove_connection
+ ActiveRecord::Base.establish_connection(original_connection)
+ end
+end
View
8 activerecord/test/support/postgresql_helper.rb
@@ -1,8 +0,0 @@
-module PostgresqlHelper
- # Make sure to drop all cached query plans to prevent invalid reference errors like:
- # cache lookup failed for type XYZ
- def reset_pg_session
- original_connection = ActiveRecord::Base.remove_connection
- ActiveRecord::Base.establish_connection(original_connection)
- end
-end

0 comments on commit f1169d9

Please sign in to comment.
Something went wrong with that request. Please try again.