luislavena-sqlite3-ruby 1.2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,542 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TC_Database_Integration < Test::Unit::TestCase
4
+ def setup
5
+ @db = SQLite3::Database.new( "test.db" )
6
+ @db.transaction do
7
+ @db.execute "create table foo ( a integer primary key, b text )"
8
+ @db.execute "insert into foo ( b ) values ( 'foo' )"
9
+ @db.execute "insert into foo ( b ) values ( 'bar' )"
10
+ @db.execute "insert into foo ( b ) values ( 'baz' )"
11
+ end
12
+ end
13
+
14
+ def teardown
15
+ @db.close
16
+ File.delete( "test.db" )
17
+ end
18
+
19
+ def test_table_info_with_type_translation_active
20
+ @db.type_translation = true
21
+ assert_nothing_raised { @db.table_info("foo") }
22
+ end
23
+
24
+ def test_table_info_with_defaults_for_version_3_3_8_and_higher
25
+ @db.transaction do
26
+ @db.execute "create table defaults_test ( a string default NULL, b string default 'Hello' )"
27
+ data = @db.table_info( "defaults_test" )
28
+ assert_equal({"name" => "a", "type" => "string", "dflt_value" => nil, "notnull" => "0", "cid" => "0", "pk" => "0"},
29
+ data[0])
30
+ assert_equal({"name" => "b", "type" => "string", "dflt_value" => "Hello", "notnull" => "0", "cid" => "1", "pk" => "0"},
31
+ data[1])
32
+ end
33
+ end
34
+
35
+ def test_table_info_without_defaults_for_version_3_3_8_and_higher
36
+ @db.transaction do
37
+ @db.execute "create table no_defaults_test ( a integer default 1, b integer )"
38
+ data = @db.table_info( "no_defaults_test" )
39
+ assert_equal({"name" => "a", "type" => "integer", "dflt_value" => "1", "notnull" => "0", "cid" => "0", "pk" => "0"},
40
+ data[0])
41
+ assert_equal({"name" => "b", "type" => "integer", "dflt_value" => nil, "notnull" => "0", "cid" => "1", "pk" => "0"},
42
+ data[1])
43
+ end
44
+ end
45
+
46
+ def test_complete_fail
47
+ assert !@db.complete?( "select * from foo" )
48
+ end
49
+ def test_complete_success
50
+ assert @db.complete?( "select * from foo;" )
51
+ end
52
+
53
+ def test_complete_fail_utf16
54
+ assert !@db.complete?( "select * from foo".to_utf16(false), true )
55
+ end
56
+
57
+ def test_complete_success_utf16
58
+ assert @db.complete?( "select * from foo;".to_utf16(true), true )
59
+ end
60
+
61
+ def test_errmsg
62
+ assert_equal "not an error", @db.errmsg
63
+ end
64
+
65
+ def test_errmsg_utf16
66
+ assert_equal "not an error".to_utf16, @db.errmsg(true)
67
+ end
68
+
69
+ def test_errcode
70
+ assert_equal 0, @db.errcode
71
+ end
72
+
73
+ def test_trace
74
+ result = nil
75
+ @db.trace( "data" ) { |data,sql| result = [ data, sql ]; 0 }
76
+ @db.execute "select * from foo"
77
+ assert_equal ["data","select * from foo"], result
78
+ end
79
+
80
+ def test_authorizer_okay
81
+ @db.authorizer( "data" ) { |data,type,a,b,c,d| 0 }
82
+ rows = @db.execute "select * from foo"
83
+ assert_equal 3, rows.length
84
+ end
85
+
86
+ def test_authorizer_error
87
+ @db.authorizer( "data" ) { |data,type,a,b,c,d| 1 }
88
+ assert_raise( SQLite3::AuthorizationException ) do
89
+ @db.execute "select * from foo"
90
+ end
91
+ end
92
+
93
+ def test_authorizer_silent
94
+ @db.authorizer( "data" ) { |data,type,a,b,c,d| 2 }
95
+ rows = @db.execute "select * from foo"
96
+ assert rows.empty?
97
+ end
98
+
99
+ def test_prepare_invalid_syntax
100
+ assert_raise( SQLite3::SQLException ) do
101
+ @db.prepare "select from foo"
102
+ end
103
+ end
104
+
105
+ def test_prepare_invalid_column
106
+ assert_raise( SQLite3::SQLException ) do
107
+ @db.prepare "select k from foo"
108
+ end
109
+ end
110
+
111
+ def test_prepare_invalid_table
112
+ assert_raise( SQLite3::SQLException ) do
113
+ @db.prepare "select * from barf"
114
+ end
115
+ end
116
+
117
+ def test_prepare_no_block
118
+ stmt = @db.prepare "select * from foo"
119
+ assert stmt.respond_to?(:execute)
120
+ stmt.close
121
+ end
122
+
123
+ def test_prepare_with_block
124
+ called = false
125
+ @db.prepare "select * from foo" do |stmt|
126
+ called = true
127
+ assert stmt.respond_to?(:execute)
128
+ end
129
+ assert called
130
+ end
131
+
132
+ def test_execute_no_block_no_bind_no_match
133
+ rows = @db.execute( "select * from foo where a > 100" )
134
+ assert rows.empty?
135
+ end
136
+
137
+ def test_execute_with_block_no_bind_no_match
138
+ called = false
139
+ @db.execute( "select * from foo where a > 100" ) do |row|
140
+ called = true
141
+ end
142
+ assert !called
143
+ end
144
+
145
+ def test_execute_no_block_with_bind_no_match
146
+ rows = @db.execute( "select * from foo where a > ?", 100 )
147
+ assert rows.empty?
148
+ end
149
+
150
+ def test_execute_with_block_with_bind_no_match
151
+ called = false
152
+ @db.execute( "select * from foo where a > ?", 100 ) do |row|
153
+ called = true
154
+ end
155
+ assert !called
156
+ end
157
+
158
+ def test_execute_no_block_no_bind_with_match
159
+ rows = @db.execute( "select * from foo where a = 1" )
160
+ assert_equal 1, rows.length
161
+ end
162
+
163
+ def test_execute_with_block_no_bind_with_match
164
+ called = 0
165
+ @db.execute( "select * from foo where a = 1" ) do |row|
166
+ called += 1
167
+ end
168
+ assert_equal 1, called
169
+ end
170
+
171
+ def test_execute_no_block_with_bind_with_match
172
+ rows = @db.execute( "select * from foo where a = ?", 1 )
173
+ assert_equal 1, rows.length
174
+ end
175
+
176
+ def test_execute_with_block_with_bind_with_match
177
+ called = 0
178
+ @db.execute( "select * from foo where a = ?", 1 ) do |row|
179
+ called += 1
180
+ end
181
+ assert_equal 1, called
182
+ end
183
+
184
+ def test_execute2_no_block_no_bind_no_match
185
+ columns, *rows = @db.execute2( "select * from foo where a > 100" )
186
+ assert rows.empty?
187
+ assert [ "a", "b" ], columns
188
+ end
189
+
190
+ def test_execute2_with_block_no_bind_no_match
191
+ called = 0
192
+ @db.execute2( "select * from foo where a > 100" ) do |row|
193
+ assert [ "a", "b" ], row unless called == 0
194
+ called += 1
195
+ end
196
+ assert_equal 1, called
197
+ end
198
+
199
+ def test_execute2_no_block_with_bind_no_match
200
+ columns, *rows = @db.execute2( "select * from foo where a > ?", 100 )
201
+ assert rows.empty?
202
+ assert [ "a", "b" ], columns
203
+ end
204
+
205
+ def test_execute2_with_block_with_bind_no_match
206
+ called = 0
207
+ @db.execute2( "select * from foo where a > ?", 100 ) do |row|
208
+ assert [ "a", "b" ], row unless called == 0
209
+ called += 1
210
+ end
211
+ assert_equal 1, called
212
+ end
213
+
214
+ def test_execute2_no_block_no_bind_with_match
215
+ columns, *rows = @db.execute2( "select * from foo where a = 1" )
216
+ assert_equal 1, rows.length
217
+ assert [ "a", "b" ], columns
218
+ end
219
+
220
+ def test_execute2_with_block_no_bind_with_match
221
+ called = 0
222
+ @db.execute2( "select * from foo where a = 1" ) do |row|
223
+ assert [ "a", "b" ], row unless called == 0
224
+ called += 1
225
+ end
226
+ assert_equal 2, called
227
+ end
228
+
229
+ def test_execute2_no_block_with_bind_with_match
230
+ columns, *rows = @db.execute2( "select * from foo where a = ?", 1 )
231
+ assert_equal 1, rows.length
232
+ assert [ "a", "b" ], columns
233
+ end
234
+
235
+ def test_execute2_with_block_with_bind_with_match
236
+ called = 0
237
+ @db.execute2( "select * from foo where a = ?", 1 ) do |row|
238
+ called += 1
239
+ end
240
+ assert_equal 2, called
241
+ end
242
+
243
+ def test_execute_batch_empty
244
+ assert_nothing_raised { @db.execute_batch "" }
245
+ end
246
+
247
+ def test_execute_batch_no_bind
248
+ @db.transaction do
249
+ @db.execute_batch <<-SQL
250
+ create table bar ( a, b, c );
251
+ insert into bar values ( 'one', 2, 'three' );
252
+ insert into bar values ( 'four', 5, 'six' );
253
+ insert into bar values ( 'seven', 8, 'nine' );
254
+ SQL
255
+ end
256
+ rows = @db.execute( "select * from bar" )
257
+ assert_equal 3, rows.length
258
+ end
259
+
260
+ def test_execute_batch_with_bind
261
+ @db.execute_batch( <<-SQL, 1 )
262
+ create table bar ( a, b, c );
263
+ insert into bar values ( 'one', 2, ? );
264
+ insert into bar values ( 'four', 5, ? );
265
+ insert into bar values ( 'seven', 8, ? );
266
+ SQL
267
+ rows = @db.execute( "select * from bar" ).map { |a,b,c| c }
268
+ assert_equal %w{1 1 1}, rows
269
+ end
270
+
271
+ def test_query_no_block_no_bind_no_match
272
+ result = @db.query( "select * from foo where a > 100" )
273
+ assert_nil result.next
274
+ result.close
275
+ end
276
+
277
+ def test_query_with_block_no_bind_no_match
278
+ r = nil
279
+ @db.query( "select * from foo where a > 100" ) do |result|
280
+ assert_nil result.next
281
+ r = result
282
+ end
283
+ assert r.closed?
284
+ end
285
+
286
+ def test_query_no_block_with_bind_no_match
287
+ result = @db.query( "select * from foo where a > ?", 100 )
288
+ assert_nil result.next
289
+ result.close
290
+ end
291
+
292
+ def test_query_with_block_with_bind_no_match
293
+ r = nil
294
+ @db.query( "select * from foo where a > ?", 100 ) do |result|
295
+ assert_nil result.next
296
+ r = result
297
+ end
298
+ assert r.closed?
299
+ end
300
+
301
+ def test_query_no_block_no_bind_with_match
302
+ result = @db.query( "select * from foo where a = 1" )
303
+ assert_not_nil result.next
304
+ assert_nil result.next
305
+ result.close
306
+ end
307
+
308
+ def test_query_with_block_no_bind_with_match
309
+ r = nil
310
+ @db.query( "select * from foo where a = 1" ) do |result|
311
+ assert_not_nil result.next
312
+ assert_nil result.next
313
+ r = result
314
+ end
315
+ assert r.closed?
316
+ end
317
+
318
+ def test_query_no_block_with_bind_with_match
319
+ result = @db.query( "select * from foo where a = ?", 1 )
320
+ assert_not_nil result.next
321
+ assert_nil result.next
322
+ result.close
323
+ end
324
+
325
+ def test_query_with_block_with_bind_with_match
326
+ r = nil
327
+ @db.query( "select * from foo where a = ?", 1 ) do |result|
328
+ assert_not_nil result.next
329
+ assert_nil result.next
330
+ r = result
331
+ end
332
+ assert r.closed?
333
+ end
334
+
335
+ def test_get_first_row_no_bind_no_match
336
+ result = @db.get_first_row( "select * from foo where a=100" )
337
+ assert_nil result
338
+ end
339
+
340
+ def test_get_first_row_no_bind_with_match
341
+ result = @db.get_first_row( "select * from foo where a=1" )
342
+ assert_equal [ "1", "foo" ], result
343
+ end
344
+
345
+ def test_get_first_row_with_bind_no_match
346
+ result = @db.get_first_row( "select * from foo where a=?", 100 )
347
+ assert_nil result
348
+ end
349
+
350
+ def test_get_first_row_with_bind_with_match
351
+ result = @db.get_first_row( "select * from foo where a=?", 1 )
352
+ assert_equal [ "1", "foo" ], result
353
+ end
354
+
355
+ def test_get_first_value_no_bind_no_match
356
+ result = @db.get_first_value( "select b, a from foo where a=100" )
357
+ assert_nil result
358
+ end
359
+
360
+ def test_get_first_value_no_bind_with_match
361
+ result = @db.get_first_value( "select b, a from foo where a=1" )
362
+ assert_equal "foo", result
363
+ end
364
+
365
+ def test_get_first_value_with_bind_no_match
366
+ result = @db.get_first_value( "select b, a from foo where a=?", 100 )
367
+ assert_nil result
368
+ end
369
+
370
+ def test_get_first_value_with_bind_with_match
371
+ result = @db.get_first_value( "select b, a from foo where a=?", 1 )
372
+ assert_equal "foo", result
373
+ end
374
+
375
+ def test_last_insert_row_id
376
+ @db.execute "insert into foo ( b ) values ( 'test' )"
377
+ assert_equal 4, @db.last_insert_row_id
378
+ @db.execute "insert into foo ( b ) values ( 'again' )"
379
+ assert_equal 5, @db.last_insert_row_id
380
+ end
381
+
382
+ def test_changes
383
+ @db.execute "insert into foo ( b ) values ( 'test' )"
384
+ assert_equal 1, @db.changes
385
+ @db.execute "delete from foo where 1=1"
386
+ assert_equal 4, @db.changes
387
+ end
388
+
389
+ def test_total_changes
390
+ assert_equal 3, @db.total_changes
391
+ @db.execute "insert into foo ( b ) values ( 'test' )"
392
+ @db.execute "delete from foo where 1=1"
393
+ assert_equal 8, @db.total_changes
394
+ end
395
+
396
+ def test_transaction_nest
397
+ assert_raise( SQLite3::SQLException ) do
398
+ @db.transaction do
399
+ @db.transaction do
400
+ end
401
+ end
402
+ end
403
+ end
404
+
405
+ def test_transaction_rollback
406
+ @db.transaction
407
+ @db.execute_batch <<-SQL
408
+ insert into foo (b) values ( 'test1' );
409
+ insert into foo (b) values ( 'test2' );
410
+ insert into foo (b) values ( 'test3' );
411
+ insert into foo (b) values ( 'test4' );
412
+ SQL
413
+ assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
414
+ @db.rollback
415
+ assert_equal 3, @db.get_first_value("select count(*) from foo").to_i
416
+ end
417
+
418
+ def test_transaction_commit
419
+ @db.transaction
420
+ @db.execute_batch <<-SQL
421
+ insert into foo (b) values ( 'test1' );
422
+ insert into foo (b) values ( 'test2' );
423
+ insert into foo (b) values ( 'test3' );
424
+ insert into foo (b) values ( 'test4' );
425
+ SQL
426
+ assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
427
+ @db.commit
428
+ assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
429
+ end
430
+
431
+ def test_transaction_rollback_in_block
432
+ assert_raise( SQLite3::SQLException ) do
433
+ @db.transaction do
434
+ @db.rollback
435
+ end
436
+ end
437
+ end
438
+
439
+ def test_transaction_commit_in_block
440
+ assert_raise( SQLite3::SQLException ) do
441
+ @db.transaction do
442
+ @db.commit
443
+ end
444
+ end
445
+ end
446
+
447
+ def test_transaction_active
448
+ assert !@db.transaction_active?
449
+ @db.transaction
450
+ assert @db.transaction_active?
451
+ @db.commit
452
+ assert !@db.transaction_active?
453
+ end
454
+
455
+ def test_interrupt
456
+ @db.create_function( "abort", 1 ) do |func,x|
457
+ @db.interrupt
458
+ func.result = x
459
+ end
460
+
461
+ assert_raise( SQLite3::SQLException ) do
462
+ @db.execute "select abort(a) from foo"
463
+ end
464
+ end
465
+
466
+ def test_create_function
467
+ @db.create_function( "munge", 1 ) do |func,x|
468
+ func.result = ">>>#{x}<<<"
469
+ end
470
+
471
+ value = @db.get_first_value( "select munge(b) from foo where a=1" )
472
+ assert_match( />>>.*<<</, value )
473
+ end
474
+
475
+ def test_create_aggregate_without_block
476
+ step = proc do |ctx,a|
477
+ ctx[:sum] ||= 0
478
+ ctx[:sum] += a.to_i
479
+ end
480
+
481
+ final = proc { |ctx| ctx.result = ctx[:sum] }
482
+
483
+ @db.create_aggregate( "accumulate", 1, step, final )
484
+
485
+ value = @db.get_first_value( "select accumulate(a) from foo" )
486
+ assert_equal "6", value
487
+ end
488
+
489
+ def test_create_aggregate_with_block
490
+ @db.create_aggregate( "accumulate", 1 ) do
491
+ step do |ctx,a|
492
+ ctx[:sum] ||= 0
493
+ ctx[:sum] += a.to_i
494
+ end
495
+
496
+ finalize { |ctx| ctx.result = ctx[:sum] }
497
+ end
498
+
499
+ value = @db.get_first_value( "select accumulate(a) from foo" )
500
+ assert_equal "6", value
501
+ end
502
+
503
+ def test_create_aggregate_with_no_data
504
+ @db.create_aggregate( "accumulate", 1 ) do
505
+ step do |ctx,a|
506
+ ctx[:sum] ||= 0
507
+ ctx[:sum] += a.to_i
508
+ end
509
+
510
+ finalize { |ctx| ctx.result = ctx[:sum] || 0 }
511
+ end
512
+
513
+ value = @db.get_first_value(
514
+ "select accumulate(a) from foo where a = 100" )
515
+ assert_equal "0", value
516
+ end
517
+
518
+ def test_create_aggregate_handler
519
+ handler = Class.new do
520
+ class << self
521
+ def arity; 1; end
522
+ def text_rep; SQLite3::Constants::TextRep::ANY; end
523
+ def name; "multiply"; end
524
+ end
525
+ def step(ctx, a)
526
+ ctx[:buffer] ||= 1
527
+ ctx[:buffer] *= a.to_i
528
+ end
529
+ def finalize(ctx); ctx.result = ctx[:buffer]; end
530
+ end
531
+
532
+ @db.create_aggregate_handler( handler )
533
+ value = @db.get_first_value( "select multiply(a) from foo" )
534
+ assert_equal "6", value
535
+ end
536
+
537
+ def test_bind_array_parameter
538
+ result = @db.get_first_value( "select b from foo where a=? and b=?",
539
+ [ 1, "foo" ] )
540
+ assert_equal "foo", result
541
+ end
542
+ end
@@ -0,0 +1,30 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TC_OpenClose < Test::Unit::TestCase
4
+ def test_create_close
5
+ begin
6
+ db = SQLite3::Database.new( "test-create.db" )
7
+ assert File.exist?( "test-create.db" )
8
+ assert_nothing_raised { db.close }
9
+ ensure
10
+ File.delete( "test-create.db" ) rescue nil
11
+ end
12
+ end
13
+
14
+ def test_open_close
15
+ begin
16
+ File.open( "test-open.db", "w" ) { |f| }
17
+ assert File.exist?( "test-open.db" )
18
+ db = SQLite3::Database.new( "test-open.db" )
19
+ assert_nothing_raised { db.close }
20
+ ensure
21
+ File.delete( "test-open.db" ) rescue nil
22
+ end
23
+ end
24
+
25
+ def test_bad_open
26
+ assert_raise( SQLite3::CantOpenException ) do
27
+ SQLite3::Database.new( "." )
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,111 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ require 'thread'
4
+ require 'benchmark'
5
+
6
+ class TC_Integration_Pending < Test::Unit::TestCase
7
+ def setup
8
+ @db = SQLite3::Database.new( "test.db" )
9
+ @db.transaction do
10
+ @db.execute "create table foo ( a integer primary key, b text )"
11
+ @db.execute "insert into foo ( b ) values ( 'foo' )"
12
+ @db.execute "insert into foo ( b ) values ( 'bar' )"
13
+ @db.execute "insert into foo ( b ) values ( 'baz' )"
14
+ end
15
+ end
16
+
17
+ def teardown
18
+ @db.close
19
+ File.delete( "test.db" )
20
+ end
21
+
22
+ def test_busy_handler_outwait
23
+ busy = Mutex.new
24
+ busy.lock
25
+ handler_call_count = 0
26
+
27
+ t = Thread.new(busy) do |locker|
28
+ begin
29
+ db2 = SQLite3::Database.open( "test.db" )
30
+ db2.transaction( :exclusive ) do
31
+ locker.lock
32
+ end
33
+ ensure
34
+ db2.close if db2
35
+ end
36
+ end
37
+
38
+ @db.busy_handler do |data,count|
39
+ handler_call_count += 1
40
+ busy.unlock
41
+ true
42
+ end
43
+
44
+ assert_nothing_raised do
45
+ @db.execute "insert into foo (b) values ( 'from 2' )"
46
+ end
47
+
48
+ t.join
49
+
50
+ assert_equal 1, handler_call_count
51
+ end
52
+
53
+ def test_busy_handler_impatient
54
+ busy = Mutex.new
55
+ busy.lock
56
+ handler_call_count = 0
57
+
58
+ t = Thread.new do
59
+ begin
60
+ db2 = SQLite3::Database.open( "test.db" )
61
+ db2.transaction( :exclusive ) do
62
+ busy.lock
63
+ end
64
+ ensure
65
+ db2.close if db2
66
+ end
67
+ end
68
+
69
+ @db.busy_handler do |data, count|
70
+ handler_call_count += 1
71
+ false
72
+ end
73
+
74
+ assert_raise( SQLite3::BusyException ) do
75
+ @db.execute "insert into foo (b) values ( 'from 2' )"
76
+ end
77
+
78
+ busy.unlock
79
+ t.join
80
+
81
+ assert_equal 1, handler_call_count
82
+ end
83
+
84
+ def test_busy_timeout
85
+ @db.busy_timeout 1000
86
+ busy = Mutex.new
87
+ busy.lock
88
+
89
+ t = Thread.new do
90
+ begin
91
+ db2 = SQLite3::Database.open( "test.db" )
92
+ db2.transaction( :exclusive ) do
93
+ busy.lock
94
+ end
95
+ ensure
96
+ db2.close if db2
97
+ end
98
+ end
99
+
100
+ time = Benchmark.measure do
101
+ assert_raise( SQLite3::BusyException ) do
102
+ @db.execute "insert into foo (b) values ( 'from 2' )"
103
+ end
104
+ end
105
+
106
+ busy.unlock
107
+ t.join
108
+
109
+ assert time.real*1000 >= 1000
110
+ end
111
+ end