-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathtest_integration.rb
More file actions
559 lines (479 loc) · 15 KB
/
test_integration.rb
File metadata and controls
559 lines (479 loc) · 15 KB
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
require 'helper'
class TC_Database_Integration < SQLite3::TestCase
def setup
@db = SQLite3::Database.new(":memory:")
@db.transaction do
@db.execute "create table foo ( a integer primary key, b text )"
@db.execute "insert into foo ( b ) values ( 'foo' )"
@db.execute "insert into foo ( b ) values ( 'bar' )"
@db.execute "insert into foo ( b ) values ( 'baz' )"
end
end
def teardown
@db.close
end
def test_table_info_with_type_translation_active
assert_nothing_raised { @db.table_info("foo") }
end
def test_table_info_with_defaults_for_version_3_3_8_and_higher
@db.transaction do
@db.execute "create table defaults_test ( a string default NULL, b string default 'Hello', c string default '--- []\n' )"
data = @db.table_info( "defaults_test" )
assert_equal({"name" => "a", "type" => "string", "dflt_value" => nil, "notnull" => 0, "cid" => 0, "pk" => 0},
data[0])
assert_equal({"name" => "b", "type" => "string", "dflt_value" => "Hello", "notnull" => 0, "cid" => 1, "pk" => 0},
data[1])
assert_equal({"name" => "c", "type" => "string", "dflt_value" => "--- []\n", "notnull" => 0, "cid" => 2, "pk" => 0},
data[2])
end
end
def test_table_info_without_defaults_for_version_3_3_8_and_higher
@db.transaction do
@db.execute "create table no_defaults_test ( a integer default 1, b integer )"
data = @db.table_info( "no_defaults_test" )
assert_equal({"name" => "a", "type" => "integer", "dflt_value" => "1", "notnull" => 0, "cid" => 0, "pk" => 0},
data[0])
assert_equal({"name" => "b", "type" => "integer", "dflt_value" => nil, "notnull" => 0, "cid" => 1, "pk" => 0},
data[1])
end
end
def test_complete_fail
assert !@db.complete?( "select * from foo" )
end
def test_complete_success
assert @db.complete?( "select * from foo;" )
end
# FIXME: do people really need UTF16 sql statements?
#def test_complete_fail_utf16
# assert !@db.complete?( "select * from foo".to_utf16(false), true )
#end
# FIXME: do people really need UTF16 sql statements?
#def test_complete_success_utf16
# assert @db.complete?( "select * from foo;".to_utf16(true), true )
#end
def test_errmsg
assert_equal "not an error", @db.errmsg
end
# FIXME: do people really need UTF16 error messages?
#def test_errmsg_utf16
# msg = Iconv.conv('UTF-16', 'UTF-8', 'not an error')
# assert_equal msg, @db.errmsg(true)
#end
def test_errcode
assert_equal 0, @db.errcode
end
def test_trace
result = nil
@db.trace { |sql| result = sql }
@db.execute "select * from foo"
assert_equal "select * from foo", result
end
def test_authorizer_okay
@db.authorizer { |type,a,b,c,d| 0 }
rows = @db.execute "select * from foo"
assert_equal 3, rows.length
end
def test_authorizer_error
@db.authorizer { |type,a,b,c,d| 1 }
assert_raise( SQLite3::AuthorizationException ) do
@db.execute "select * from foo"
end
end
def test_authorizer_silent
@db.authorizer { |type,a,b,c,d| 2 }
rows = @db.execute "select * from foo"
assert rows.empty?
end
def test_prepare_invalid_syntax
assert_raise( SQLite3::SQLException ) do
@db.prepare "select from foo"
end
end
def test_prepare_invalid_column
assert_raise( SQLite3::SQLException ) do
@db.prepare "select k from foo"
end
end
def test_prepare_invalid_table
assert_raise( SQLite3::SQLException ) do
@db.prepare "select * from barf"
end
end
def test_prepare_no_block
stmt = @db.prepare "select * from foo"
assert stmt.respond_to?(:execute)
stmt.close
end
def test_prepare_with_block
called = false
@db.prepare "select * from foo" do |stmt|
called = true
assert stmt.respond_to?(:execute)
end
assert called
end
def test_execute_no_block_no_bind_no_match
rows = @db.execute( "select * from foo where a > 100" )
assert rows.empty?
end
def test_execute_with_block_no_bind_no_match
called = false
@db.execute( "select * from foo where a > 100" ) do |row|
called = true
end
assert !called
end
def test_execute_no_block_with_bind_no_match
rows = @db.execute( "select * from foo where a > ?", 100 )
assert rows.empty?
end
def test_execute_with_block_with_bind_no_match
called = false
@db.execute( "select * from foo where a > ?", 100 ) do |row|
called = true
end
assert !called
end
def test_execute_no_block_no_bind_with_match
rows = @db.execute( "select * from foo where a = 1" )
assert_equal 1, rows.length
end
def test_execute_with_block_no_bind_with_match
called = 0
@db.execute( "select * from foo where a = 1" ) do |row|
called += 1
end
assert_equal 1, called
end
def test_execute_no_block_with_bind_with_match
rows = @db.execute( "select * from foo where a = ?", 1 )
assert_equal 1, rows.length
end
def test_execute_with_block_with_bind_with_match
called = 0
@db.execute( "select * from foo where a = ?", 1 ) do |row|
called += 1
end
assert_equal 1, called
end
def test_execute2_no_block_no_bind_no_match
columns, *rows = @db.execute2( "select * from foo where a > 100" )
assert rows.empty?
assert_equal [ "a", "b" ], columns
end
def test_execute2_with_block_no_bind_no_match
called = 0
@db.execute2( "select * from foo where a > 100" ) do |row|
assert [ "a", "b" ], row unless called == 0
called += 1
end
assert_equal 1, called
end
def test_execute2_no_block_with_bind_no_match
columns, *rows = @db.execute2( "select * from foo where a > ?", 100 )
assert rows.empty?
assert_equal [ "a", "b" ], columns
end
def test_execute2_with_block_with_bind_no_match
called = 0
@db.execute2( "select * from foo where a > ?", 100 ) do |row|
assert_equal [ "a", "b" ], row unless called == 0
called += 1
end
assert_equal 1, called
end
def test_execute2_no_block_no_bind_with_match
columns, *rows = @db.execute2( "select * from foo where a = 1" )
assert_equal 1, rows.length
assert_equal [ "a", "b" ], columns
end
def test_execute2_with_block_no_bind_with_match
called = 0
@db.execute2( "select * from foo where a = 1" ) do |row|
assert_equal [ 1, "foo" ], row unless called == 0
called += 1
end
assert_equal 2, called
end
def test_execute2_no_block_with_bind_with_match
columns, *rows = @db.execute2( "select * from foo where a = ?", 1 )
assert_equal 1, rows.length
assert_equal [ "a", "b" ], columns
end
def test_execute2_with_block_with_bind_with_match
called = 0
@db.execute2( "select * from foo where a = ?", 1 ) do
called += 1
end
assert_equal 2, called
end
def test_execute_batch_empty
assert_nothing_raised { @db.execute_batch "" }
end
def test_execute_batch_no_bind
@db.transaction do
@db.execute_batch <<-SQL
create table bar ( a, b, c );
insert into bar values ( 'one', 2, 'three' );
insert into bar values ( 'four', 5, 'six' );
insert into bar values ( 'seven', 8, 'nine' );
SQL
end
rows = @db.execute( "select * from bar" )
assert_equal 3, rows.length
end
def test_execute_batch_with_bind
@db.execute_batch( <<-SQL, [1] )
create table bar ( a, b, c );
insert into bar values ( 'one', 2, ? );
insert into bar values ( 'four', 5, ? );
insert into bar values ( 'seven', 8, ? );
SQL
rows = @db.execute( "select * from bar" ).map { |a,b,c| c }
assert_equal [1, 1, 1], rows
end
def test_query_no_block_no_bind_no_match
result = @db.query( "select * from foo where a > 100" )
assert_nil result.next
result.close
end
def test_query_with_block_no_bind_no_match
r = nil
@db.query( "select * from foo where a > 100" ) do |result|
assert_nil result.next
r = result
end
assert r.closed?
end
def test_query_no_block_with_bind_no_match
result = @db.query( "select * from foo where a > ?", 100 )
assert_nil result.next
result.close
end
def test_query_with_block_with_bind_no_match
r = nil
@db.query( "select * from foo where a > ?", 100 ) do |result|
assert_nil result.next
r = result
end
assert r.closed?
end
def test_query_no_block_no_bind_with_match
result = @db.query( "select * from foo where a = 1" )
assert_not_nil result.next
assert_nil result.next
result.close
end
def test_query_with_block_no_bind_with_match
r = nil
@db.query( "select * from foo where a = 1" ) do |result|
assert_not_nil result.next
assert_nil result.next
r = result
end
assert r.closed?
end
def test_query_no_block_with_bind_with_match
result = @db.query( "select * from foo where a = ?", 1 )
assert_not_nil result.next
assert_nil result.next
result.close
end
def test_query_with_block_with_bind_with_match
r = nil
@db.query( "select * from foo where a = ?", 1 ) do |result|
assert_not_nil result.next
assert_nil result.next
r = result
end
assert r.closed?
end
def test_get_first_row_no_bind_no_match
result = @db.get_first_row( "select * from foo where a=100" )
assert_nil result
end
def test_get_first_row_no_bind_with_match
result = @db.get_first_row( "select * from foo where a=1" )
assert_equal [ 1, "foo" ], result
end
def test_get_first_row_with_bind_no_match
result = @db.get_first_row( "select * from foo where a=?", 100 )
assert_nil result
end
def test_get_first_row_with_bind_with_match
result = @db.get_first_row( "select * from foo where a=?", 1 )
assert_equal [ 1, "foo" ], result
end
def test_get_first_value_no_bind_no_match
result = @db.get_first_value( "select b, a from foo where a=100" )
assert_nil result
@db.results_as_hash = true
result = @db.get_first_value( "select b, a from foo where a=100" )
assert_nil result
end
def test_get_first_value_no_bind_with_match
result = @db.get_first_value( "select b, a from foo where a=1" )
assert_equal "foo", result
@db.results_as_hash = true
result = @db.get_first_value( "select b, a from foo where a=1" )
assert_equal "foo", result
end
def test_get_first_value_with_bind_no_match
result = @db.get_first_value( "select b, a from foo where a=?", 100 )
assert_nil result
@db.results_as_hash = true
result = @db.get_first_value( "select b, a from foo where a=?", 100 )
assert_nil result
end
def test_get_first_value_with_bind_with_match
result = @db.get_first_value( "select b, a from foo where a=?", 1 )
assert_equal "foo", result
@db.results_as_hash = true
result = @db.get_first_value( "select b, a from foo where a=?", 1 )
assert_equal "foo", result
end
def test_last_insert_row_id
@db.execute "insert into foo ( b ) values ( 'test' )"
assert_equal 4, @db.last_insert_row_id
@db.execute "insert into foo ( b ) values ( 'again' )"
assert_equal 5, @db.last_insert_row_id
end
def test_changes
@db.execute "insert into foo ( b ) values ( 'test' )"
assert_equal 1, @db.changes
@db.execute "delete from foo where 1=1"
assert_equal 4, @db.changes
end
def test_total_changes
assert_equal 3, @db.total_changes
@db.execute "insert into foo ( b ) values ( 'test' )"
@db.execute "delete from foo where 1=1"
assert_equal 8, @db.total_changes
end
def test_transaction_nest
assert_raise( SQLite3::SQLException ) do
@db.transaction do
@db.transaction do
end
end
end
end
def test_transaction_rollback
@db.transaction
@db.execute_batch <<-SQL
insert into foo (b) values ( 'test1' );
insert into foo (b) values ( 'test2' );
insert into foo (b) values ( 'test3' );
insert into foo (b) values ( 'test4' );
SQL
assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
@db.rollback
assert_equal 3, @db.get_first_value("select count(*) from foo").to_i
end
def test_transaction_commit
@db.transaction
@db.execute_batch <<-SQL
insert into foo (b) values ( 'test1' );
insert into foo (b) values ( 'test2' );
insert into foo (b) values ( 'test3' );
insert into foo (b) values ( 'test4' );
SQL
assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
@db.commit
assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
end
def test_transaction_rollback_in_block
assert_raise( SQLite3::SQLException ) do
@db.transaction do
@db.rollback
end
end
end
def test_transaction_commit_in_block
assert_raise( SQLite3::SQLException ) do
@db.transaction do
@db.commit
end
end
end
def test_transaction_active
assert !@db.transaction_active?
@db.transaction
assert @db.transaction_active?
@db.commit
assert !@db.transaction_active?
end
def test_transaction_implicit_rollback
assert !@db.transaction_active?
@db.transaction
@db.execute('create table bar (x CHECK(1 = 0))')
assert @db.transaction_active?
assert_raises( SQLite3::ConstraintException ) do
@db.execute("insert or rollback into bar (x) VALUES ('x')")
end
assert !@db.transaction_active?
end
def test_interrupt
@db.create_function( "abort", 1 ) do |func,x|
@db.interrupt
func.result = x
end
assert_raise( SQLite3::InterruptException ) do
@db.execute "select abort(a) from foo"
end
end
def test_create_function
@db.create_function( "munge", 1 ) do |func,x|
func.result = ">>>#{x}<<<"
end
value = @db.get_first_value( "select munge(b) from foo where a=1" )
assert_match( />>>.*<<</, value )
end
def test_bind_array_parameter
result = @db.get_first_value( "select b from foo where a=? and b=?",
[ 1, "foo" ] )
assert_equal "foo", result
end
def test_progress_handler_used
progress_calls = []
@db.progress_handler do
progress_calls << nil
true
end
@db.execute "create table test1(a, b)"
assert_operator 1, :<, progress_calls.size
end
def test_progress_handler_opcode_arg
progress_calls = []
handler = Proc.new do
progress_calls << nil
true
end
@db.progress_handler(1, handler)
@db.execute "create table test1(a, b)"
first_count = progress_calls.size
progress_calls = []
@db.progress_handler(10, handler)
@db.execute "create table test2(a, b)"
second_count = progress_calls.size
assert_operator first_count, :>=, second_count
end
def test_progress_handler_interrupts_operation
@db.progress_handler do
false
end
assert_raises(SQLite3::InterruptException) do
@db.execute "create table test1(a, b)"
end
end
def test_clear_handler
progress_calls = []
@db.progress_handler do
progress_calls << nil
true
end
@db.progress_handler(nil)
@db.execute "create table test1(a, b)"
assert_equal 0, progress_calls.size
end
end