7
7
require "models/subscriber"
8
8
require "models/minimalistic"
9
9
require "models/college"
10
+ require "models/dog"
11
+ require "models/other_dog"
10
12
11
13
class AdapterTestSQLServer < ActiveRecord ::TestCase
12
14
fixtures :tasks
13
15
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
+
14
21
let ( :basic_insert_sql ) { "INSERT INTO [funny_jokes] ([name]) VALUES('Knock knock')" }
15
22
let ( :basic_update_sql ) { "UPDATE [customers] SET [address_street] = NULL WHERE [id] = 2" }
16
23
let ( :basic_select_sql ) { "SELECT * FROM [customers] WHERE ([customers].[id] = 1)" }
@@ -50,21 +57,14 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
50
57
assert Topic . table_exists? , "Topics table name of 'dbo.topics' should return true for exists."
51
58
52
59
# 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"
55
61
assert Topic . table_exists? , "Topics table name of '[DATABASE].dbo.topics' should return true for exists."
56
62
ensure
57
63
Topic . table_name = "topics"
58
64
end
59
65
end
60
66
61
67
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
-
68
68
# Assert that connections use different default databases schemas.
69
69
assert_not_equal arunit_database , arunit2_database
70
70
@@ -200,6 +200,8 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
200
200
@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'"
201
201
@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'"
202
202
@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"
203
205
end
204
206
205
207
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
216
218
assert_equal "[test].[aliens]" , connection . send ( :query_requires_identity_insert? , @identity_insert_sql_non_dbo_sp )
217
219
assert_equal "[test].[aliens]" , connection . send ( :query_requires_identity_insert? , @identity_insert_sql_non_dbo_unquoted_sp )
218
220
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 )
219
223
end
220
224
221
225
it "return false to #query_requires_identity_insert? for normal SQL" do
@@ -224,40 +228,26 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
224
228
end
225
229
end
226
230
227
- it "find identity column using #identity_columns " do
231
+ it "find identity column" do
228
232
task_id_column = Task . columns_hash [ "id" ]
229
233
assert_equal task_id_column . name , connection . send ( :identity_columns , Task . table_name ) . first . name
230
234
assert_equal task_id_column . sql_type , connection . send ( :identity_columns , Task . table_name ) . first . sql_type
231
235
end
232
236
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
243
241
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
247
246
248
- OtherDog . destroy_all
249
247
250
- #
251
- # assert Dog.count, 1
252
- # assert OtherDog.count, 0
253
248
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 [ ]
261
251
end
262
252
end
263
253
0 commit comments