Skip to content

Commit fc4478f

Browse files
authored
Merge pull request #76 from jamis/mberlanda/fix-ci-mysql-tests
test: stub connection in unit test to emulate multi adapter support within the same suite
2 parents 171abf5 + a9d6195 commit fc4478f

File tree

2 files changed

+111
-38
lines changed

2 files changed

+111
-38
lines changed

.travis.yml

+20
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,34 @@ matrix:
2626
include:
2727
- rvm: 2.2
2828
gemfile: gemfiles/rails_3.gemfile
29+
env: DB_ADAPTER=sqlite
30+
- rvm: 2.2
31+
gemfile: gemfiles/rails_3.gemfile
32+
env: DB_ADAPTER=mysql
2933
- rvm: 2.2
3034
gemfile: gemfiles/rails_4.gemfile
35+
env: DB_ADAPTER=sqlite
36+
- rvm: 2.2
37+
gemfile: gemfiles/rails_4.gemfile
38+
env: DB_ADAPTER=mysql
39+
- rvm: 2.3
40+
gemfile: gemfiles/rails_3.gemfile
41+
env: DB_ADAPTER=sqlite
3142
- rvm: 2.3
3243
gemfile: gemfiles/rails_3.gemfile
44+
env: DB_ADAPTER=mysql
45+
- rvm: 2.3
46+
gemfile: gemfiles/rails_4.gemfile
47+
env: DB_ADAPTER=sqlite
3348
- rvm: 2.3
3449
gemfile: gemfiles/rails_4.gemfile
50+
env: DB_ADAPTER=mysql
51+
- rvm: 2.4
52+
gemfile: gemfiles/rails_4.gemfile
53+
env: DB_ADAPTER=sqlite
3554
- rvm: 2.4
3655
gemfile: gemfiles/rails_4.gemfile
56+
env: DB_ADAPTER=mysql
3757
exclude:
3858
- rvm: 2.3
3959
gemfile: gemfiles/rails_6.gemfile

test/bulk_insert/worker_test.rb

+91-38
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,9 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
270270
assert_equal @insert.compose_insert_query, "INSERT INTO \"testings\" (\"greeting\",\"age\",\"happy\",\"created_at\",\"updated_at\",\"color\") VALUES ('Yo',15,0,NULL,NULL,'chartreuse')"
271271
end
272272

273-
test "adapter dependent mysql methods" do
273+
test "adapter dependent MySQL methods" do
274274
connection = Testing.connection
275-
connection.stub :adapter_name, 'MySQL' do
275+
stub_connection_if_needed(connection, 'mysql') do
276276
mysql_worker = BulkInsert::Worker.new(
277277
connection,
278278
Testing.table_name,
@@ -282,20 +282,21 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
282282
true # ignore
283283
)
284284

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
287287
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
289289

290290
mysql_worker.add ["Yo", 15, false, nil, nil]
291291

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')"
293294
end
294295
end
295296

296297
test "adapter dependent mysql methods work for mysql2" do
297298
connection = Testing.connection
298-
connection.stub :adapter_name, 'Mysql2' do
299+
stub_connection_if_needed(connection, 'mysql2') do
299300
mysql_worker = BulkInsert::Worker.new(
300301
connection,
301302
Testing.table_name,
@@ -305,18 +306,19 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
305306
true, # ignore
306307
true) # update_duplicates
307308

308-
assert_equal mysql_worker.adapter_name, 'Mysql2'
309+
assert_equal mysql_worker.adapter_name, 'mysql2'
309310
assert mysql_worker.ignore
310311

311312
mysql_worker.add ["Yo", 15, false, nil, nil]
312313

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`)"
314316
end
315317
end
316318

317319
test "adapter dependent Mysql2Spatial methods" do
318320
connection = Testing.connection
319-
connection.stub :adapter_name, 'Mysql2Spatial' do
321+
stub_connection_if_needed(connection, 'mysql2spatial') do
320322
mysql_worker = BulkInsert::Worker.new(
321323
connection,
322324
Testing.table_name,
@@ -325,17 +327,18 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
325327
500, # batch size
326328
true) # ignore
327329

328-
assert_equal mysql_worker.adapter_name, 'Mysql2Spatial'
330+
assert_equal mysql_worker.adapter_name, 'mysql2spatial'
329331

330332
mysql_worker.add ["Yo", 15, false, nil, nil]
331333

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')"
333336
end
334337
end
335338

336339
test "adapter dependent postgresql methods" do
337340
connection = Testing.connection
338-
connection.stub :adapter_name, 'PostgreSQL' do
341+
stub_connection_if_needed(connection, 'PostgreSQL') do
339342
pgsql_worker = BulkInsert::Worker.new(
340343
connection,
341344
Testing.table_name,
@@ -349,13 +352,14 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
349352

350353
pgsql_worker.add ["Yo", 15, false, nil, nil]
351354

355+
assert_statement_adapter pgsql_worker, 'BulkInsert::StatementAdapters::PostgreSQLAdapter'
352356
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"
353357
end
354358
end
355359

356360
test "adapter dependent postgresql methods (no ignore, no update_duplicates)" do
357361
connection = Testing.connection
358-
connection.stub :adapter_name, 'PostgreSQL' do
362+
stub_connection_if_needed(connection, 'PostgreSQL') do
359363
pgsql_worker = BulkInsert::Worker.new(
360364
connection,
361365
Testing.table_name,
@@ -369,13 +373,14 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
369373

370374
pgsql_worker.add ["Yo", 15, false, nil, nil]
371375

376+
assert_statement_adapter pgsql_worker, 'BulkInsert::StatementAdapters::PostgreSQLAdapter'
372377
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"
373378
end
374379
end
375380

376381
test "adapter dependent postgresql methods (with update_duplicates)" do
377382
connection = Testing.connection
378-
connection.stub :adapter_name, 'PostgreSQL' do
383+
stub_connection_if_needed(connection, 'PostgreSQL') do
379384
pgsql_worker = BulkInsert::Worker.new(
380385
connection,
381386
Testing.table_name,
@@ -388,13 +393,14 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
388393
)
389394
pgsql_worker.add ["Yo", 15, false, nil, nil]
390395

396+
assert_statement_adapter pgsql_worker, 'BulkInsert::StatementAdapters::PostgreSQLAdapter'
391397
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"
392398
end
393399
end
394400

395401
test "adapter dependent PostGIS methods" do
396402
connection = Testing.connection
397-
connection.stub :adapter_name, 'PostGIS' do
403+
stub_connection_if_needed(connection, 'postgis') do
398404
pgsql_worker = BulkInsert::Worker.new(
399405
connection,
400406
Testing.table_name,
@@ -407,41 +413,50 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
407413
)
408414
pgsql_worker.add ["Yo", 15, false, nil, nil]
409415

416+
assert_statement_adapter pgsql_worker, 'BulkInsert::StatementAdapters::PostgreSQLAdapter'
410417
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"
411418
end
412419
end
413420

414421
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]
424433

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
426437
end
427438

428439
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]
438451

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
440455
end
441456

442457
test "mysql adapter can update duplicates" do
443458
connection = Testing.connection
444-
connection.stub :adapter_name, 'MySQL' do
459+
stub_connection_if_needed(connection, 'mysql') do
445460
mysql_worker = BulkInsert::Worker.new(
446461
connection,
447462
Testing.table_name,
@@ -453,7 +468,45 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
453468
)
454469
mysql_worker.add ["Yo", 15, false, nil, nil]
455470

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
457510
end
458511
end
459512
end

0 commit comments

Comments
 (0)