@@ -270,9 +270,9 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
270
270
assert_equal @insert . compose_insert_query , "INSERT INTO \" testings\" (\" greeting\" ,\" age\" ,\" happy\" ,\" created_at\" ,\" updated_at\" ,\" color\" ) VALUES ('Yo',15,0,NULL,NULL,'chartreuse')"
271
271
end
272
272
273
- test "adapter dependent mysql methods" do
273
+ test "adapter dependent MySQL methods" do
274
274
connection = Testing . connection
275
- connection . stub :adapter_name , 'MySQL' do
275
+ stub_connection_if_needed ( connection , 'mysql' ) do
276
276
mysql_worker = BulkInsert ::Worker . new (
277
277
connection ,
278
278
Testing . table_name ,
@@ -282,20 +282,21 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
282
282
true # ignore
283
283
)
284
284
285
- assert_equal mysql_worker . adapter_name , 'MySQL '
286
- assert_equal ( mysql_worker . adapter_name == 'MySQL ' ) , true
285
+ assert_equal mysql_worker . adapter_name , 'mysql '
286
+ assert_equal ( mysql_worker . adapter_name == 'mysql ' ) , true
287
287
assert_equal mysql_worker . ignore , true
288
- assert_equal ( ( mysql_worker . adapter_name == 'MySQL ' ) & mysql_worker . ignore ) , true
288
+ assert_equal ( ( mysql_worker . adapter_name == 'mysql ' ) & mysql_worker . ignore ) , true
289
289
290
290
mysql_worker . add [ "Yo" , 15 , false , nil , nil ]
291
291
292
- assert_equal mysql_worker . compose_insert_query , "INSERT IGNORE INTO \" testings\" (\" greeting\" ,\" age\" ,\" happy\" ,\" created_at\" ,\" updated_at\" ,\" color\" ) VALUES ('Yo',15,0,NULL,NULL,'chartreuse')"
292
+ assert_statement_adapter mysql_worker , 'BulkInsert::StatementAdapters::MySQLAdapter'
293
+ assert_equal mysql_worker . compose_insert_query , "INSERT IGNORE INTO `testings` (`greeting`,`age`,`happy`,`created_at`,`updated_at`,`color`) VALUES ('Yo',15,0,NULL,NULL,'chartreuse')"
293
294
end
294
295
end
295
296
296
297
test "adapter dependent mysql methods work for mysql2" do
297
298
connection = Testing . connection
298
- connection . stub :adapter_name , 'Mysql2' do
299
+ stub_connection_if_needed ( connection , 'mysql2' ) do
299
300
mysql_worker = BulkInsert ::Worker . new (
300
301
connection ,
301
302
Testing . table_name ,
@@ -305,18 +306,19 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
305
306
true , # ignore
306
307
true ) # update_duplicates
307
308
308
- assert_equal mysql_worker . adapter_name , 'Mysql2 '
309
+ assert_equal mysql_worker . adapter_name , 'mysql2 '
309
310
assert mysql_worker . ignore
310
311
311
312
mysql_worker . add [ "Yo" , 15 , false , nil , nil ]
312
313
313
- assert_equal mysql_worker . compose_insert_query , "INSERT IGNORE INTO \" testings\" (\" greeting\" ,\" age\" ,\" happy\" ,\" created_at\" ,\" updated_at\" ,\" color\" ) VALUES ('Yo',15,0,NULL,NULL,'chartreuse') ON DUPLICATE KEY UPDATE `greeting`=VALUES(`greeting`), `age`=VALUES(`age`), `happy`=VALUES(`happy`), `created_at`=VALUES(`created_at`), `updated_at`=VALUES(`updated_at`), `color`=VALUES(`color`)"
314
+ assert_statement_adapter mysql_worker , 'BulkInsert::StatementAdapters::MySQLAdapter'
315
+ assert_equal mysql_worker . compose_insert_query , "INSERT IGNORE INTO `testings` (`greeting`,`age`,`happy`,`created_at`,`updated_at`,`color`) VALUES ('Yo',15,0,NULL,NULL,'chartreuse') ON DUPLICATE KEY UPDATE `greeting`=VALUES(`greeting`), `age`=VALUES(`age`), `happy`=VALUES(`happy`), `created_at`=VALUES(`created_at`), `updated_at`=VALUES(`updated_at`), `color`=VALUES(`color`)"
314
316
end
315
317
end
316
318
317
319
test "adapter dependent Mysql2Spatial methods" do
318
320
connection = Testing . connection
319
- connection . stub :adapter_name , 'Mysql2Spatial' do
321
+ stub_connection_if_needed ( connection , 'mysql2spatial' ) do
320
322
mysql_worker = BulkInsert ::Worker . new (
321
323
connection ,
322
324
Testing . table_name ,
@@ -325,17 +327,18 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
325
327
500 , # batch size
326
328
true ) # ignore
327
329
328
- assert_equal mysql_worker . adapter_name , 'Mysql2Spatial '
330
+ assert_equal mysql_worker . adapter_name , 'mysql2spatial '
329
331
330
332
mysql_worker . add [ "Yo" , 15 , false , nil , nil ]
331
333
332
- assert_equal mysql_worker . compose_insert_query , "INSERT IGNORE INTO \" testings\" (\" greeting\" ,\" age\" ,\" happy\" ,\" created_at\" ,\" updated_at\" ,\" color\" ) VALUES ('Yo',15,0,NULL,NULL,'chartreuse')"
334
+ assert_statement_adapter mysql_worker , 'BulkInsert::StatementAdapters::MySQLAdapter'
335
+ assert_equal mysql_worker . compose_insert_query , "INSERT IGNORE INTO `testings` (`greeting`,`age`,`happy`,`created_at`,`updated_at`,`color`) VALUES ('Yo',15,0,NULL,NULL,'chartreuse')"
333
336
end
334
337
end
335
338
336
339
test "adapter dependent postgresql methods" do
337
340
connection = Testing . connection
338
- connection . stub :adapter_name , 'PostgreSQL' do
341
+ stub_connection_if_needed ( connection , 'PostgreSQL' ) do
339
342
pgsql_worker = BulkInsert ::Worker . new (
340
343
connection ,
341
344
Testing . table_name ,
@@ -349,13 +352,14 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
349
352
350
353
pgsql_worker . add [ "Yo" , 15 , false , nil , nil ]
351
354
355
+ assert_statement_adapter pgsql_worker , 'BulkInsert::StatementAdapters::PostgreSQLAdapter'
352
356
assert_equal pgsql_worker . compose_insert_query , "INSERT INTO \" testings\" (\" greeting\" ,\" age\" ,\" happy\" ,\" created_at\" ,\" updated_at\" ,\" color\" ) VALUES ('Yo',15,0,NULL,NULL,'chartreuse') ON CONFLICT DO NOTHING RETURNING id"
353
357
end
354
358
end
355
359
356
360
test "adapter dependent postgresql methods (no ignore, no update_duplicates)" do
357
361
connection = Testing . connection
358
- connection . stub :adapter_name , 'PostgreSQL' do
362
+ stub_connection_if_needed ( connection , 'PostgreSQL' ) do
359
363
pgsql_worker = BulkInsert ::Worker . new (
360
364
connection ,
361
365
Testing . table_name ,
@@ -369,13 +373,14 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
369
373
370
374
pgsql_worker . add [ "Yo" , 15 , false , nil , nil ]
371
375
376
+ assert_statement_adapter pgsql_worker , 'BulkInsert::StatementAdapters::PostgreSQLAdapter'
372
377
assert_equal pgsql_worker . compose_insert_query , "INSERT INTO \" testings\" (\" greeting\" ,\" age\" ,\" happy\" ,\" created_at\" ,\" updated_at\" ,\" color\" ) VALUES ('Yo',15,0,NULL,NULL,'chartreuse') RETURNING id"
373
378
end
374
379
end
375
380
376
381
test "adapter dependent postgresql methods (with update_duplicates)" do
377
382
connection = Testing . connection
378
- connection . stub :adapter_name , 'PostgreSQL' do
383
+ stub_connection_if_needed ( connection , 'PostgreSQL' ) do
379
384
pgsql_worker = BulkInsert ::Worker . new (
380
385
connection ,
381
386
Testing . table_name ,
@@ -388,13 +393,14 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
388
393
)
389
394
pgsql_worker . add [ "Yo" , 15 , false , nil , nil ]
390
395
396
+ assert_statement_adapter pgsql_worker , 'BulkInsert::StatementAdapters::PostgreSQLAdapter'
391
397
assert_equal pgsql_worker . compose_insert_query , "INSERT INTO \" testings\" (\" greeting\" ,\" age\" ,\" happy\" ,\" created_at\" ,\" updated_at\" ,\" color\" ) VALUES ('Yo',15,0,NULL,NULL,'chartreuse') ON CONFLICT(greeting, age, happy) DO UPDATE SET greeting=EXCLUDED.greeting, age=EXCLUDED.age, happy=EXCLUDED.happy, created_at=EXCLUDED.created_at, updated_at=EXCLUDED.updated_at, color=EXCLUDED.color RETURNING id"
392
398
end
393
399
end
394
400
395
401
test "adapter dependent PostGIS methods" do
396
402
connection = Testing . connection
397
- connection . stub :adapter_name , 'PostGIS' do
403
+ stub_connection_if_needed ( connection , 'postgis' ) do
398
404
pgsql_worker = BulkInsert ::Worker . new (
399
405
connection ,
400
406
Testing . table_name ,
@@ -407,41 +413,50 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
407
413
)
408
414
pgsql_worker . add [ "Yo" , 15 , false , nil , nil ]
409
415
416
+ assert_statement_adapter pgsql_worker , 'BulkInsert::StatementAdapters::PostgreSQLAdapter'
410
417
assert_equal pgsql_worker . compose_insert_query , "INSERT INTO \" testings\" (\" greeting\" ,\" age\" ,\" happy\" ,\" created_at\" ,\" updated_at\" ,\" color\" ) VALUES ('Yo',15,0,NULL,NULL,'chartreuse') ON CONFLICT DO NOTHING RETURNING id"
411
418
end
412
419
end
413
420
414
421
test "adapter dependent sqlite3 methods (with lowercase adapter name)" do
415
- sqlite_worker = BulkInsert ::Worker . new (
416
- Testing . connection ,
417
- Testing . table_name ,
418
- 'id' ,
419
- %w( greeting age happy created_at updated_at color ) ,
420
- 500 , # batch size
421
- true ) # ignore
422
- sqlite_worker . adapter_name = 'sqlite3'
423
- sqlite_worker . add [ "Yo" , 15 , false , nil , nil ]
422
+ connection = Testing . connection
423
+ stub_connection_if_needed ( connection , 'sqlite3' ) do
424
+ sqlite_worker = BulkInsert ::Worker . new (
425
+ Testing . connection ,
426
+ Testing . table_name ,
427
+ 'id' ,
428
+ %w( greeting age happy created_at updated_at color ) ,
429
+ 500 , # batch size
430
+ true ) # ignore
431
+ sqlite_worker . adapter_name = 'sqlite3'
432
+ sqlite_worker . add [ "Yo" , 15 , false , nil , nil ]
424
433
425
- assert_equal sqlite_worker . compose_insert_query , "INSERT OR IGNORE INTO \" testings\" (\" greeting\" ,\" age\" ,\" happy\" ,\" created_at\" ,\" updated_at\" ,\" color\" ) VALUES ('Yo',15,0,NULL,NULL,'chartreuse')"
434
+ assert_statement_adapter sqlite_worker , 'BulkInsert::StatementAdapters::SQLiteAdapter'
435
+ assert_equal sqlite_worker . compose_insert_query , "INSERT OR IGNORE INTO \" testings\" (\" greeting\" ,\" age\" ,\" happy\" ,\" created_at\" ,\" updated_at\" ,\" color\" ) VALUES ('Yo',15,0,NULL,NULL,'chartreuse')"
436
+ end
426
437
end
427
438
428
439
test "adapter dependent sqlite3 methods (with stylecase adapter name)" do
429
- sqlite_worker = BulkInsert ::Worker . new (
430
- Testing . connection ,
431
- Testing . table_name ,
432
- 'id' ,
433
- %w( greeting age happy created_at updated_at color ) ,
434
- 500 , # batch size
435
- true ) # ignore
436
- sqlite_worker . adapter_name = 'SQLite'
437
- sqlite_worker . add [ "Yo" , 15 , false , nil , nil ]
440
+ connection = Testing . connection
441
+ stub_connection_if_needed ( connection , 'SQLite' ) do
442
+ sqlite_worker = BulkInsert ::Worker . new (
443
+ connection ,
444
+ Testing . table_name ,
445
+ 'id' ,
446
+ %w( greeting age happy created_at updated_at color ) ,
447
+ 500 , # batch size
448
+ true ) # ignore
449
+ sqlite_worker . adapter_name = 'SQLite'
450
+ sqlite_worker . add [ "Yo" , 15 , false , nil , nil ]
438
451
439
- assert_equal sqlite_worker . compose_insert_query , "INSERT OR IGNORE INTO \" testings\" (\" greeting\" ,\" age\" ,\" happy\" ,\" created_at\" ,\" updated_at\" ,\" color\" ) VALUES ('Yo',15,0,NULL,NULL,'chartreuse')"
452
+ assert_statement_adapter sqlite_worker , 'BulkInsert::StatementAdapters::SQLiteAdapter'
453
+ assert_equal sqlite_worker . compose_insert_query , "INSERT OR IGNORE INTO \" testings\" (\" greeting\" ,\" age\" ,\" happy\" ,\" created_at\" ,\" updated_at\" ,\" color\" ) VALUES ('Yo',15,0,NULL,NULL,'chartreuse')"
454
+ end
440
455
end
441
456
442
457
test "mysql adapter can update duplicates" do
443
458
connection = Testing . connection
444
- connection . stub :adapter_name , 'MySQL' do
459
+ stub_connection_if_needed ( connection , 'mysql' ) do
445
460
mysql_worker = BulkInsert ::Worker . new (
446
461
connection ,
447
462
Testing . table_name ,
@@ -453,7 +468,45 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
453
468
)
454
469
mysql_worker . add [ "Yo" , 15 , false , nil , nil ]
455
470
456
- assert_equal mysql_worker . compose_insert_query , "INSERT INTO \" testings\" (\" greeting\" ,\" age\" ,\" happy\" ,\" created_at\" ,\" updated_at\" ,\" color\" ) VALUES ('Yo',15,0,NULL,NULL,'chartreuse') ON DUPLICATE KEY UPDATE `greeting`=VALUES(`greeting`), `age`=VALUES(`age`), `happy`=VALUES(`happy`), `created_at`=VALUES(`created_at`), `updated_at`=VALUES(`updated_at`), `color`=VALUES(`color`)"
471
+ assert_statement_adapter mysql_worker , 'BulkInsert::StatementAdapters::MySQLAdapter'
472
+ assert_equal mysql_worker . compose_insert_query , "INSERT INTO `testings` (`greeting`,`age`,`happy`,`created_at`,`updated_at`,`color`) VALUES ('Yo',15,0,NULL,NULL,'chartreuse') ON DUPLICATE KEY UPDATE `greeting`=VALUES(`greeting`), `age`=VALUES(`age`), `happy`=VALUES(`happy`), `created_at`=VALUES(`created_at`), `updated_at`=VALUES(`updated_at`), `color`=VALUES(`color`)"
473
+ end
474
+ end
475
+
476
+ def assert_statement_adapter ( worker , adapter_name )
477
+ assert_equal worker . instance_variable_get ( :@statement_adapter ) . class . to_s , adapter_name
478
+ end
479
+
480
+ DOUBLE_QUOTE_PROC = Proc . new do |value , *_column |
481
+ return value unless value . is_a? String
482
+ "\" #{ value } \" "
483
+ end
484
+
485
+ BACKTICK_QUOTE_PROC = Proc . new do |value , *_column |
486
+ return value unless value . is_a? String
487
+ '`' + value + '`'
488
+ end
489
+
490
+ def stub_connection_if_needed ( connection , adapter_name )
491
+ raise "You need to provide a block" unless block_given?
492
+ if connection . adapter_name == adapter_name
493
+ yield
494
+ else
495
+ connection . stub :adapter_name , adapter_name do
496
+ if adapter_name =~ /^mysql/i
497
+ connection . stub :quote_table_name , BACKTICK_QUOTE_PROC do
498
+ connection . stub :quote_column_name , BACKTICK_QUOTE_PROC do
499
+ yield
500
+ end
501
+ end
502
+ else
503
+ connection . stub :quote_table_name , DOUBLE_QUOTE_PROC do
504
+ connection . stub :quote_column_name , DOUBLE_QUOTE_PROC do
505
+ yield
506
+ end
507
+ end
508
+ end
509
+ end
457
510
end
458
511
end
459
512
end
0 commit comments