77require "models/subscriber"
88require "models/minimalistic"
99require "models/college"
10+ require "models/dog"
11+ require "models/other_dog"
1012
1113class AdapterTestSQLServer < ActiveRecord ::TestCase
1214 fixtures :tasks
1315
16+ let ( :arunit_connection ) { Topic . lease_connection }
17+ let ( :arunit2_connection ) { College . lease_connection }
18+ let ( :arunit_database ) { arunit_connection . pool . db_config . database }
19+ let ( :arunit2_database ) { arunit2_connection . pool . db_config . database }
20+
1421 let ( :basic_insert_sql ) { "INSERT INTO [funny_jokes] ([name]) VALUES('Knock knock')" }
1522 let ( :basic_update_sql ) { "UPDATE [customers] SET [address_street] = NULL WHERE [id] = 2" }
1623 let ( :basic_select_sql ) { "SELECT * FROM [customers] WHERE ([customers].[id] = 1)" }
@@ -50,21 +57,14 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
5057 assert Topic . table_exists? , "Topics table name of 'dbo.topics' should return true for exists."
5158
5259 # Test when database and owner included in table name.
53- db_config = ActiveRecord ::Base . configurations . configs_for ( env_name : "arunit" , name : "primary" )
54- Topic . table_name = "#{ db_config . database } .dbo.topics"
60+ Topic . table_name = "#{ arunit_database } .dbo.topics"
5561 assert Topic . table_exists? , "Topics table name of '[DATABASE].dbo.topics' should return true for exists."
5662 ensure
5763 Topic . table_name = "topics"
5864 end
5965 end
6066
6167 it "test table existence across database schemas" do
62- arunit_connection = Topic . lease_connection
63- arunit2_connection = College . lease_connection
64-
65- arunit_database = arunit_connection . pool . db_config . database
66- arunit2_database = arunit2_connection . pool . db_config . database
67-
6868 # Assert that connections use different default databases schemas.
6969 assert_not_equal arunit_database , arunit2_database
7070
@@ -200,6 +200,8 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
200200 @identity_insert_sql_non_dbo_sp = "EXEC sp_executesql N'INSERT INTO [test].[aliens] ([id],[name]) VALUES (@0, @1)', N'@0 int, @1 nvarchar(255)', @0 = 420, @1 = N'Mork'"
201201 @identity_insert_sql_non_dbo_unquoted_sp = "EXEC sp_executesql N'INSERT INTO test.aliens (id, name) VALUES (@0, @1)', N'@0 int, @1 nvarchar(255)', @0 = 420, @1 = N'Mork'"
202202 @identity_insert_sql_non_dbo_unordered_sp = "EXEC sp_executesql N'INSERT INTO [test].[aliens] ([name],[id]) VALUES (@0, @1)', N'@0 nvarchar(255), @1 int', @0 = N'Mork', @1 = 420"
203+
204+ @identity_insert_sql_cross_database = "INSERT INTO #{ arunit2_database } .dbo.dogs(id) SELECT id FROM #{ arunit_database } .dbo.dogs"
203205 end
204206
205207 it "return quoted table_name to #query_requires_identity_insert? when INSERT sql contains id column" do
@@ -216,6 +218,8 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
216218 assert_equal "[test].[aliens]" , connection . send ( :query_requires_identity_insert? , @identity_insert_sql_non_dbo_sp )
217219 assert_equal "[test].[aliens]" , connection . send ( :query_requires_identity_insert? , @identity_insert_sql_non_dbo_unquoted_sp )
218220 assert_equal "[test].[aliens]" , connection . send ( :query_requires_identity_insert? , @identity_insert_sql_non_dbo_unordered_sp )
221+
222+ assert_equal "[#{ arunit2_database } ].[dbo].[dogs]" , connection . send ( :query_requires_identity_insert? , @identity_insert_sql_cross_database )
219223 end
220224
221225 it "return false to #query_requires_identity_insert? for normal SQL" do
@@ -224,40 +228,26 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
224228 end
225229 end
226230
227- it "find identity column using #identity_columns " do
231+ it "find identity column" do
228232 task_id_column = Task . columns_hash [ "id" ]
229233 assert_equal task_id_column . name , connection . send ( :identity_columns , Task . table_name ) . first . name
230234 assert_equal task_id_column . sql_type , connection . send ( :identity_columns , Task . table_name ) . first . sql_type
231235 end
232236
233- it "return an empty array when calling #identity_columns for a table_name with no identity" do
234- _ ( connection . send ( :identity_columns , Subscriber . table_name ) ) . must_equal [ ]
235- end
236-
237- it "identity insert enabled for cross database insert" do
238- arunit_connection = Dog . lease_connection
239- arunit2_connection = OtherDog . lease_connection
240-
241- arunit_database = arunit_connection . pool . db_config . database
242- arunit2_database = arunit2_connection . pool . db_config . database
237+ it "find identity column cross database" do
238+ id_column = Dog . columns_hash [ "id" ]
239+ assert_equal id_column . name , arunit2_connection . send ( :identity_columns , Dog . table_name ) . first . name
240+ assert_equal id_column . sql_type , arunit2_connection . send ( :identity_columns , Dog . table_name ) . first . sql_type
243241
244- sql = <<~SQL
245- INSERT INTO #{ arunit2_database } .dbo.dogs(id) SELECT id FROM #{ arunit_database } .dbo.dogs
246- SQL
242+ id_column = OtherDog . columns_hash [ "id" ]
243+ assert_equal id_column . name , arunit_connection . send ( :identity_columns , OtherDog . table_name ) . first . name
244+ assert_equal id_column . sql_type , arunit_connection . send ( :identity_columns , OtherDog . table_name ) . first . sql_type
245+ end
247246
248- OtherDog . destroy_all
249247
250- #
251- # assert Dog.count, 1
252- # assert OtherDog.count, 0
253248
254- assert_nothing_raised do
255- # TODO: Check if this is causing issues with the test suite.
256- # arunit_connection.execute(sql)
257- end
258-
259- # assert Dog.count, 1
260- # assert OtherDog.count, 1
249+ it "return an empty array when calling #identity_columns for a table_name with no identity" do
250+ _ ( connection . send ( :identity_columns , Subscriber . table_name ) ) . must_equal [ ]
261251 end
262252 end
263253
0 commit comments