-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathworker_test.rb
517 lines (423 loc) · 17.7 KB
/
worker_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
require 'minitest/mock'
require 'test_helper'
require 'connection_mocks'
class BulkInsertWorkerTest < ActiveSupport::TestCase
include ConnectionMocks
setup do
@insert = BulkInsert::Worker.new(
Testing.connection,
Testing.table_name,
'id',
%w(greeting age happy created_at updated_at color))
@now = Time.now.utc
end
test "empty insert is not pending" do
assert_equal false, @insert.pending?
end
test "pending_count should describe size of pending set" do
assert_equal 0, @insert.pending_count
@insert.add ["Hello", 15, true, @now, @now]
assert_equal 1, @insert.pending_count
end
test "default set size" do
assert_equal 500, @insert.set_size
end
test "adding row to insert makes insert pending" do
@insert.add ["Hello", 15, true, @now, @now]
assert_equal true, @insert.pending?
end
test "add should default timestamp columns to current time" do
now = Time.now
@insert.add ["Hello", 15, true]
@insert.save!
record = Testing.first
assert_operator record.created_at.to_i, :>=, now.to_i
assert_operator record.updated_at.to_i, :>=, now.to_i
end
test "default timestamp columns should be equivalent for the entire batch" do
@insert.add ["Hello", 15, true]
@insert.add ["Howdy", 20, false]
@insert.save!
first, second = Testing.all
assert_equal first.created_at.to_f, second.created_at.to_f
assert_equal first.created_at.to_f, first.updated_at.to_f
end
test "add should use database default values when present" do
@insert.add greeting: "Hello", age: 20, happy: false
@insert.save!
record = Testing.first
assert_equal record.color, "chartreuse"
end
test "explicit nil should override defaults" do
@insert.add greeting: "Hello", age: 20, happy: false, color: nil
@insert.save!
record = Testing.first
assert_nil record.color
end
test "add should allow values given as Hash" do
@insert.add greeting: "Yo", age: 20, happy: false, created_at: @now, updated_at: @now
@insert.save!
record = Testing.first
assert_not_nil record
assert_equal "Yo", record.greeting
assert_equal 20, record.age
assert_equal false, record.happy?
end
test "add should save automatically when overflowing set size" do
@insert.set_size = 1
@insert.add ["Hello", 15, true, @now, @now]
@insert.add ["Yo", 20, false, @now, @now]
assert_equal 1, Testing.count
assert_equal "Hello", Testing.first.greeting
end
test "add_all should append all items to the set" do
@insert.add_all [
[ "Hello", 15, true ],
{ greeting: "Hi", age: 55, happy: true }
]
assert_equal 2, @insert.pending_count
end
test "save! makes insert not pending" do
@insert.add ["Hello", 15, true, @now, @now]
@insert.save!
assert_equal false, @insert.pending?
end
test "save! when not pending should do nothing" do
assert_no_difference 'Testing.count' do
@insert.save!
end
end
test "save! inserts pending records" do
@insert.add ["Yo", 15, false, @now, @now]
@insert.add ["Hello", 25, true, @now, @now]
@insert.save!
yo = Testing.where(greeting: 'Yo').first
hello = Testing.where(greeting: 'Hello').first
assert_not_nil yo
assert_equal 15, yo.age
assert_equal false, yo.happy?
assert_not_nil hello
assert_equal 25, hello.age
assert_equal true, hello.happy?
end
test "save! does not add to result sets when not returning primary keys" do
@insert.add greeting: "first"
@insert.add greeting: "second"
@insert.save!
assert_equal 0, @insert.result_sets.count
end
test "save! adds to result sets when returning primary keys" do
worker = BulkInsert::Worker.new(
Testing.connection,
Testing.table_name,
'id',
%w(greeting age happy created_at updated_at color),
500,
false,
false,
true
)
# return_primary_keys is not supported for mysql and rails < 5
# skip is not supported in the minitest version used for testing rails 3
return if ActiveRecord::VERSION::STRING < "5.0.0" && worker.adapter_name =~ /^mysql/i
assert_no_difference -> { worker.result_sets.count } do
worker.save!
end
worker.add greeting: "first"
worker.add greeting: "second"
worker.save!
assert_equal 1, worker.result_sets.count
worker.add greeting: "third"
worker.add greeting: "fourth"
worker.save!
assert_equal 2, worker.result_sets.count
end
test "initialized with empty result sets array" do
new_worker = BulkInsert::Worker.new(
Testing.connection,
Testing.table_name,
'id',
%w(greeting age happy created_at updated_at color)
)
assert_instance_of(Array, new_worker.result_sets)
assert_empty new_worker.result_sets
end
test "save! calls the after_save handler" do
x = 41
@insert.after_save do
x += 1
end
@insert.add ["Yo", 15, false, @now, @now]
@insert.add ["Hello", 25, true, @now, @now]
@insert.save!
assert_equal 42, x
end
test "after_save stores a block as a proc" do
@insert.after_save do
"hello"
end
assert_equal "hello", @insert.after_save_callback.()
end
test "after_save_callback can be set as a proc" do
@insert.after_save_callback = -> do
"hello"
end
assert_equal "hello", @insert.after_save_callback.()
end
test "save! calls the before_save handler" do
x = 41
@insert.before_save do
x += 1
end
@insert.add ["Yo", 15, false, @now, @now]
@insert.add ["Hello", 25, true, @now, @now]
@insert.save!
assert_equal 42, x
end
test "before_save stores a block as a proc" do
@insert.before_save do
"hello"
end
assert_equal "hello", @insert.before_save_callback.()
end
test "before_save_callback can be set as a proc" do
@insert.before_save_callback = -> do
"hello"
end
assert_equal "hello", @insert.before_save_callback.()
end
test "before_save can manipulate the set" do
@insert.before_save do |set|
set.reject!{|row| row[0] == "Yo"}
end
@insert.add ["Yo", 15, false, @now, @now]
@insert.add ["Hello", 25, true, @now, @now]
@insert.save!
yo = Testing.where(greeting: 'Yo').first
hello = Testing.where(greeting: 'Hello').first
assert_nil yo
assert_not_nil hello
end
test "save! doesn't blow up if before_save emptying the set" do
@insert.before_save do |set|
set.clear
end
@insert.add ["Yo", 15, false, @now, @now]
@insert.add ["Hello", 25, true, @now, @now]
@insert.save!
yo = Testing.where(greeting: 'Yo').first
hello = Testing.where(greeting: 'Hello').first
assert_nil yo
assert_nil hello
end
test "adapter dependent SQLite methods" do
connection = Testing.connection
stub_connection_if_needed(connection, 'SQLite') do
sqlite_worker = BulkInsert::Worker.new(
connection,
Testing.table_name,
'id',
%w(greeting age happy created_at updated_at color),
500 # batch size
)
assert_equal sqlite_worker.adapter_name, 'SQLite'
assert_equal sqlite_worker.insert_sql_statement, "INSERT INTO \"testings\" (\"greeting\",\"age\",\"happy\",\"created_at\",\"updated_at\",\"color\") VALUES "
sqlite_worker.add ["Yo", 15, false, nil, nil]
assert_equal sqlite_worker.compose_insert_query, "INSERT INTO \"testings\" (\"greeting\",\"age\",\"happy\",\"created_at\",\"updated_at\",\"color\") VALUES ('Yo',15,0,NULL,NULL,'chartreuse')"
end
end
test "adapter dependent MySQL methods" do
connection = Testing.connection
stub_connection_if_needed(connection, 'mysql') do
mysql_worker = BulkInsert::Worker.new(
connection,
Testing.table_name,
'id',
%w(greeting age happy created_at updated_at color),
500, # batch size
true # ignore
)
assert_equal mysql_worker.adapter_name, 'mysql'
assert_equal (mysql_worker.adapter_name == 'mysql'), true
assert_equal mysql_worker.ignore, true
assert_equal ((mysql_worker.adapter_name == 'mysql') & mysql_worker.ignore), true
mysql_worker.add ["Yo", 15, false, nil, nil]
assert_statement_adapter mysql_worker, 'BulkInsert::StatementAdapters::MySQLAdapter'
assert_equal mysql_worker.compose_insert_query, "INSERT IGNORE INTO `testings` (`greeting`,`age`,`happy`,`created_at`,`updated_at`,`color`) VALUES ('Yo',15,FALSE,NULL,NULL,'chartreuse')"
end
end
test "adapter dependent mysql methods work for mysql2" do
connection = Testing.connection
stub_connection_if_needed(connection, 'mysql2') do
mysql_worker = BulkInsert::Worker.new(
connection,
Testing.table_name,
'id',
%w(greeting age happy created_at updated_at color),
500, # batch size
true, # ignore
true) # update_duplicates
assert_equal mysql_worker.adapter_name, 'mysql2'
assert mysql_worker.ignore
mysql_worker.add ["Yo", 15, false, nil, nil]
assert_statement_adapter mysql_worker, 'BulkInsert::StatementAdapters::MySQLAdapter'
assert_equal mysql_worker.compose_insert_query, "INSERT IGNORE INTO `testings` (`greeting`,`age`,`happy`,`created_at`,`updated_at`,`color`) VALUES ('Yo',15,FALSE,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`)"
end
end
test "adapter dependent Mysql2Spatial methods" do
connection = Testing.connection
stub_connection_if_needed(connection, 'mysql2spatial') do
mysql_worker = BulkInsert::Worker.new(
connection,
Testing.table_name,
'id',
%w(greeting age happy created_at updated_at color),
500, # batch size
true) # ignore
assert_equal mysql_worker.adapter_name, 'mysql2spatial'
mysql_worker.add ["Yo", 15, false, nil, nil]
assert_statement_adapter mysql_worker, 'BulkInsert::StatementAdapters::MySQLAdapter'
assert_equal mysql_worker.compose_insert_query, "INSERT IGNORE INTO `testings` (`greeting`,`age`,`happy`,`created_at`,`updated_at`,`color`) VALUES ('Yo',15,FALSE,NULL,NULL,'chartreuse')"
end
end
test "adapter dependent postgresql methods" do
connection = Testing.connection
stub_connection_if_needed(connection, 'PostgreSQL') do
pgsql_worker = BulkInsert::Worker.new(
connection,
Testing.table_name,
'id',
%w(greeting age happy created_at updated_at color),
500, # batch size
true, # ignore
false, # update duplicates
true # return primary keys
)
pgsql_worker.add ["Yo", 15, false, nil, nil]
assert_statement_adapter pgsql_worker, 'BulkInsert::StatementAdapters::PostgreSQLAdapter'
if ActiveRecord::VERSION::STRING >= "5.0.0"
assert_equal pgsql_worker.compose_insert_query, "INSERT INTO \"testings\" (\"greeting\",\"age\",\"happy\",\"created_at\",\"updated_at\",\"color\") VALUES ('Yo',15,FALSE,NULL,NULL,'chartreuse') ON CONFLICT DO NOTHING RETURNING id"
else
assert_equal pgsql_worker.compose_insert_query, "INSERT INTO \"testings\" (\"greeting\",\"age\",\"happy\",\"created_at\",\"updated_at\",\"color\") VALUES ('Yo',15,'f',NULL,NULL,'chartreuse') ON CONFLICT DO NOTHING RETURNING id"
end
end
end
test "adapter dependent postgresql methods (no ignore, no update_duplicates)" do
connection = Testing.connection
stub_connection_if_needed(connection, 'PostgreSQL') do
pgsql_worker = BulkInsert::Worker.new(
connection,
Testing.table_name,
'id',
%w(greeting age happy created_at updated_at color),
500, # batch size
false, # ignore
false, # update duplicates
true # return primary keys
)
pgsql_worker.add ["Yo", 15, false, nil, nil]
assert_statement_adapter pgsql_worker, 'BulkInsert::StatementAdapters::PostgreSQLAdapter'
if ActiveRecord::VERSION::STRING >= "5.0.0"
assert_equal pgsql_worker.compose_insert_query, "INSERT INTO \"testings\" (\"greeting\",\"age\",\"happy\",\"created_at\",\"updated_at\",\"color\") VALUES ('Yo',15,FALSE,NULL,NULL,'chartreuse') RETURNING id"
else
assert_equal pgsql_worker.compose_insert_query, "INSERT INTO \"testings\" (\"greeting\",\"age\",\"happy\",\"created_at\",\"updated_at\",\"color\") VALUES ('Yo',15,'f',NULL,NULL,'chartreuse') RETURNING id"
end
end
end
test "adapter dependent postgresql methods (with update_duplicates)" do
connection = Testing.connection
stub_connection_if_needed(connection, 'PostgreSQL') do
pgsql_worker = BulkInsert::Worker.new(
connection,
Testing.table_name,
'id',
%w(greeting age happy created_at updated_at color),
500, # batch size
false, # ignore
%w(greeting age happy), # update duplicates
true # return primary keys
)
pgsql_worker.add ["Yo", 15, false, nil, nil]
assert_statement_adapter pgsql_worker, 'BulkInsert::StatementAdapters::PostgreSQLAdapter'
if ActiveRecord::VERSION::STRING >= "5.0.0"
assert_equal pgsql_worker.compose_insert_query, "INSERT INTO \"testings\" (\"greeting\",\"age\",\"happy\",\"created_at\",\"updated_at\",\"color\") VALUES ('Yo',15,FALSE,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"
else
assert_equal pgsql_worker.compose_insert_query, "INSERT INTO \"testings\" (\"greeting\",\"age\",\"happy\",\"created_at\",\"updated_at\",\"color\") VALUES ('Yo',15,'f',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"
end
end
end
test "adapter dependent PostGIS methods" do
connection = Testing.connection
stub_connection_if_needed(connection, 'postgis') do
pgsql_worker = BulkInsert::Worker.new(
connection,
Testing.table_name,
'id',
%w(greeting age happy created_at updated_at color),
500, # batch size
true, # ignore
false, # update duplicates
true # return primary keys
)
pgsql_worker.add ["Yo", 15, false, nil, nil]
assert_statement_adapter pgsql_worker, 'BulkInsert::StatementAdapters::PostgreSQLAdapter'
if ActiveRecord::VERSION::STRING >= "5.0.0"
assert_equal pgsql_worker.compose_insert_query, "INSERT INTO \"testings\" (\"greeting\",\"age\",\"happy\",\"created_at\",\"updated_at\",\"color\") VALUES ('Yo',15,FALSE,NULL,NULL,'chartreuse') ON CONFLICT DO NOTHING RETURNING id"
else
assert_equal pgsql_worker.compose_insert_query, "INSERT INTO \"testings\" (\"greeting\",\"age\",\"happy\",\"created_at\",\"updated_at\",\"color\") VALUES ('Yo',15,'f',NULL,NULL,'chartreuse') ON CONFLICT DO NOTHING RETURNING id"
end
end
end
test "adapter dependent sqlite3 methods (with lowercase adapter name)" do
connection = Testing.connection
stub_connection_if_needed(connection, 'sqlite3') do
sqlite_worker = BulkInsert::Worker.new(
Testing.connection,
Testing.table_name,
'id',
%w(greeting age happy created_at updated_at color),
500, # batch size
true) # ignore
sqlite_worker.adapter_name = 'sqlite3'
sqlite_worker.add ["Yo", 15, false, nil, nil]
assert_statement_adapter sqlite_worker, 'BulkInsert::StatementAdapters::SQLiteAdapter'
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')"
end
end
test "adapter dependent sqlite3 methods (with stylecase adapter name)" do
connection = Testing.connection
stub_connection_if_needed(connection, 'SQLite') do
sqlite_worker = BulkInsert::Worker.new(
connection,
Testing.table_name,
'id',
%w(greeting age happy created_at updated_at color),
500, # batch size
true) # ignore
sqlite_worker.adapter_name = 'SQLite'
sqlite_worker.add ["Yo", 15, false, nil, nil]
assert_statement_adapter sqlite_worker, 'BulkInsert::StatementAdapters::SQLiteAdapter'
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')"
end
end
test "mysql adapter can update duplicates" do
connection = Testing.connection
stub_connection_if_needed(connection, 'mysql') do
mysql_worker = BulkInsert::Worker.new(
connection,
Testing.table_name,
'id',
%w(greeting age happy created_at updated_at color),
500, # batch size
false, # ignore
true # update_duplicates
)
mysql_worker.add ["Yo", 15, false, nil, nil]
assert_statement_adapter mysql_worker, 'BulkInsert::StatementAdapters::MySQLAdapter'
assert_equal mysql_worker.compose_insert_query, "INSERT INTO `testings` (`greeting`,`age`,`happy`,`created_at`,`updated_at`,`color`) VALUES ('Yo',15,FALSE,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`)"
end
end
def assert_statement_adapter(worker, adapter_name)
assert_equal worker.instance_variable_get(:@statement_adapter).class.to_s, adapter_name
end
end