Skip to content

Commit d1b7dcc

Browse files
committed
Clean up tests by adding ARTest::SQLServer::ConnectionReflection.
1 parent 4655790 commit d1b7dcc

File tree

5 files changed

+49
-22
lines changed

5 files changed

+49
-22
lines changed

test/cases/adapter_test_sqlserver.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,6 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
418418

419419
describe 'database_prefix_remote_server?' do
420420

421-
let(:connection_options) { connection.instance_variable_get(:@connection_options) }
422-
423421
after do
424422
connection_options.delete(:database_prefix)
425423
end

test/cases/connection_test_sqlserver.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ConnectionTestSQLServer < ActiveRecord::TestCase
2929
connection.use_database
3030
assert_equal 'activerecord_unittest', connection.current_database, 'Would default back to connection options'
3131
end
32-
end unless sqlserver_azure?
32+
end unless connection_sqlserver_azure?
3333

3434
describe 'ODBC connection management' do
3535

@@ -83,7 +83,7 @@ class ConnectionTestSQLServer < ActiveRecord::TestCase
8383
end
8484
end
8585

86-
end if connection_mode_odbc?
86+
end if connection_odbc?
8787

8888

8989
describe 'Connection management' do
@@ -115,7 +115,7 @@ class ConnectionTestSQLServer < ActiveRecord::TestCase
115115
private
116116

117117
def disconnect_raw_connection!
118-
case connection.instance_variable_get(:@connection_options)[:mode]
118+
case connection_options[:mode]
119119
when :dblib
120120
connection.raw_connection.close rescue nil
121121
when :odbc

test/cases/fully_qualified_identifier_test_sqlserver.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
class FullyQualifiedIdentifierTestSQLServer < ActiveRecord::TestCase
44

5-
let(:connection_options) { connection.instance_variable_get(:@connection_options) }
6-
75
describe 'local server' do
86

97
it 'should use table name in select projections' do

test/cases/helper_sqlserver.rb

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,32 @@
55
require 'support/load_schema_sqlserver'
66
require 'support/coerceable_test_sqlserver'
77
require 'support/sql_counter_sqlserver'
8+
require 'support/connection_reflection'
89
require 'mocha/mini_test'
910

1011
module ActiveRecord
1112
class TestCase < ActiveSupport::TestCase
1213

1314
SQLServer = ActiveRecord::ConnectionAdapters::SQLServer
1415

15-
include ARTest::SQLServer::CoerceableTest
16+
include ARTest::SQLServer::CoerceableTest,
17+
ARTest::SQLServer::ConnectionReflection
1618

1719
let(:logger) { ActiveRecord::Base.logger }
1820

19-
class << self
20-
def connection_mode_dblib? ; ActiveRecord::Base.connection.instance_variable_get(:@connection_options)[:mode] == :dblib ; end
21-
def connection_mode_odbc? ; ActiveRecord::Base.connection.instance_variable_get(:@connection_options)[:mode] == :odbc ; end
22-
def sqlserver_azure? ; ActiveRecord::Base.connection.sqlserver_azure? ; end
23-
def host_windows? ; RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ; end
24-
end
2521

26-
def connection_mode_dblib? ; self.class.connection_mode_dblib? ; end
27-
def connection_mode_odbc? ; self.class.connection_mode_odbc? ; end
28-
def sqlserver_azure? ; self.class.sqlserver_azure? ; end
29-
def host_windows? ; self.class.host_windows? ; end
22+
private
3023

31-
def connection
32-
ActiveRecord::Base.connection
24+
def host_windows?
25+
RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
3326
end
3427

3528
def with_use_output_inserted_disabled
36-
ActiveRecord::ConnectionAdapters::SQLServerAdapter.use_output_inserted = false
29+
klass = ActiveRecord::ConnectionAdapters::SQLServerAdapter
30+
klass.use_output_inserted = false
3731
yield
3832
ensure
39-
ActiveRecord::ConnectionAdapters::SQLServerAdapter.use_output_inserted = true
33+
klass.use_output_inserted = true
4034
end
4135

4236
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module ARTest
2+
module SQLServer
3+
module ConnectionReflection
4+
5+
extend ActiveSupport::Concern
6+
7+
included { extend ConnectionReflection }
8+
9+
def connection
10+
ActiveRecord::Base.connection
11+
end
12+
13+
def connection_options
14+
connection.instance_variable_get :@connection_options
15+
end
16+
17+
def connection_dblib?
18+
connection_options[:mode] == :dblib
19+
end
20+
21+
def connection_dblib_73?
22+
return false unless connection_dblib?
23+
rc = connection.raw_connection
24+
rc.respond_to?(:tds_73?) && rc.tds_73?
25+
end
26+
27+
def connection_odbc?
28+
connection_options[:mode] == :odbc
29+
end
30+
31+
def connection_sqlserver_azure?
32+
connection.sqlserver_azure?
33+
end
34+
35+
end
36+
end
37+
end

0 commit comments

Comments
 (0)