sqlite3-ruby 0.6.0 → 0.9.0

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.

Potentially problematic release.


This version of sqlite3-ruby might be problematic. Click here for more details.

@@ -84,7 +84,8 @@ class TC_Database < Test::Unit::TestCase
84
84
  end
85
85
 
86
86
  def test_quote
87
- assert_equal "''one''two''three''", @db.quote( "'one'two'three'" )
87
+ assert_equal "''one''two''three''", SQLite3::Database.quote(
88
+ "'one'two'three'" )
88
89
  end
89
90
 
90
91
  def test_complete
@@ -0,0 +1,838 @@
1
+ $:.unshift "../lib"
2
+ $:.unshift "../ext/sqlite3_api"
3
+
4
+ require 'test/unit'
5
+ require 'benchmark'
6
+ require 'sqlite3/database'
7
+
8
+ class String
9
+ def to_utf16
10
+ result = ""
11
+ self.each_byte { |b| result << b.chr << "\0" }
12
+ result
13
+ end
14
+
15
+ def from_utf16
16
+ result = ""
17
+ length.times do |i|
18
+ result << self[i,1] if i % 2 == 0 && self[i] != 0
19
+ end
20
+ result
21
+ end
22
+ end
23
+
24
+ module Integration
25
+
26
+ drivers_to_test = ( ENV["SQLITE3_DRIVERS"] || "Native" ).split(',')
27
+ drivers_to_test.each do |driver|
28
+
29
+ # == TC_OpenClose =========================================================
30
+
31
+ test_case = Class.new( Test::Unit::TestCase ) do
32
+ define_method( "test_create_close" ) do
33
+ begin
34
+ db = SQLite3::Database.new( "test-create.db",
35
+ :driver => driver )
36
+ assert File.exist?( "test-create.db" )
37
+ assert_nothing_raised { db.close }
38
+ ensure
39
+ File.delete( "test-create.db" ) rescue nil
40
+ end
41
+ end
42
+
43
+ define_method( "test_open_close" ) do
44
+ begin
45
+ File.open( "test-open.db", "w" ) { |f| }
46
+ assert File.exist?( "test-open.db" )
47
+ db = SQLite3::Database.new( "test-open.db",
48
+ :driver => driver )
49
+ assert_nothing_raised { db.close }
50
+ ensure
51
+ File.delete( "test-open.db" ) rescue nil
52
+ end
53
+ end
54
+
55
+ define_method( "test_bad_open" ) do
56
+ assert_raise( SQLite3::CantOpenException ) do
57
+ SQLite3::Database.new( ".", :driver => driver )
58
+ end
59
+ end
60
+ end
61
+ const_set( "TC_OpenClose_#{driver}", test_case )
62
+
63
+ # == TC_Database ==========================================================
64
+
65
+ test_case = Class.new( Test::Unit::TestCase ) do
66
+ define_method( "setup" ) do
67
+ @db = SQLite3::Database.new( "test.db", :driver=>driver )
68
+ @db.transaction do
69
+ @db.execute "create table foo ( a integer primary key, b text )"
70
+ @db.execute "insert into foo ( b ) values ( 'foo' )"
71
+ @db.execute "insert into foo ( b ) values ( 'bar' )"
72
+ @db.execute "insert into foo ( b ) values ( 'baz' )"
73
+ end
74
+ end
75
+
76
+ define_method( "teardown" ) do
77
+ @db.close
78
+ File.delete( "test.db" )
79
+ end
80
+
81
+ define_method( "test_complete_fail" ) do
82
+ assert !@db.complete?( "select * from foo" )
83
+ end
84
+ define_method( "test_complete_success" ) do
85
+ assert @db.complete?( "select * from foo;" )
86
+ end
87
+
88
+ define_method( "test_complete_fail_utf16" ) do
89
+ assert !@db.complete?( "select * from foo".to_utf16+"\0\0", true )
90
+ end
91
+ define_method( "test_complete_success_utf16" ) do
92
+ assert @db.complete?( "select * from foo;".to_utf16+"\0\0", true )
93
+ end
94
+
95
+ define_method( "test_errmsg" ) do
96
+ assert_equal "not an error", @db.errmsg
97
+ end
98
+
99
+ define_method( "test_errmsg_utf16" ) do
100
+ assert_equal "not an error".to_utf16, @db.errmsg(true)
101
+ end
102
+
103
+ define_method( "test_errcode" ) do
104
+ assert_equal 0, @db.errcode
105
+ end
106
+
107
+ define_method( "test_trace" ) do
108
+ result = nil
109
+ @db.trace( "data" ) { |data,sql| result = [ data, sql ]; 0 }
110
+ @db.execute "select * from foo"
111
+ assert_equal ["data","select * from foo"], result
112
+ end
113
+
114
+ define_method( "test_authorizer_okay" ) do
115
+ @db.authorizer( "data" ) { |data,type,a,b,c,d| 0 }
116
+ rows = @db.execute "select * from foo"
117
+ assert_equal 3, rows.length
118
+ end
119
+
120
+ define_method( "test_authorizer_error" ) do
121
+ @db.authorizer( "data" ) { |data,type,a,b,c,d| 1 }
122
+ assert_raise( SQLite3::AuthorizationException ) do
123
+ @db.execute "select * from foo"
124
+ end
125
+ end
126
+
127
+ define_method( "test_authorizer_silent" ) do
128
+ @db.authorizer( "data" ) { |data,type,a,b,c,d| 2 }
129
+ rows = @db.execute "select * from foo"
130
+ assert rows.empty?
131
+ end
132
+
133
+ define_method( "test_prepare_invalid_syntax" ) do
134
+ assert_raise( SQLite3::SQLException ) do
135
+ @db.prepare "select from foo"
136
+ end
137
+ end
138
+
139
+ define_method( "test_prepare_invalid_column" ) do
140
+ assert_raise( SQLite3::SQLException ) do
141
+ @db.prepare "select k from foo"
142
+ end
143
+ end
144
+
145
+ define_method( "test_prepare_invalid_table" ) do
146
+ assert_raise( SQLite3::SQLException ) do
147
+ @db.prepare "select * from barf"
148
+ end
149
+ end
150
+
151
+ define_method( "test_prepare_no_block" ) do
152
+ stmt = @db.prepare "select * from foo"
153
+ assert stmt.respond_to?(:execute)
154
+ stmt.close
155
+ end
156
+
157
+ define_method( "test_prepare_with_block" ) do
158
+ called = false
159
+ @db.prepare "select * from foo" do |stmt|
160
+ called = true
161
+ assert stmt.respond_to?(:execute)
162
+ end
163
+ assert called
164
+ end
165
+
166
+ define_method( "test_execute_no_block_no_bind_no_match" ) do
167
+ rows = @db.execute( "select * from foo where a > 100" )
168
+ assert rows.empty?
169
+ end
170
+
171
+ define_method( "test_execute_with_block_no_bind_no_match" ) do
172
+ called = false
173
+ @db.execute( "select * from foo where a > 100" ) do |row|
174
+ called = true
175
+ end
176
+ assert !called
177
+ end
178
+
179
+ define_method( "test_execute_no_block_with_bind_no_match" ) do
180
+ rows = @db.execute( "select * from foo where a > ?", 100 )
181
+ assert rows.empty?
182
+ end
183
+
184
+ define_method( "test_execute_with_block_with_bind_no_match" ) do
185
+ called = false
186
+ @db.execute( "select * from foo where a > ?", 100 ) do |row|
187
+ called = true
188
+ end
189
+ assert !called
190
+ end
191
+
192
+ define_method( "test_execute_no_block_no_bind_with_match" ) do
193
+ rows = @db.execute( "select * from foo where a = 1" )
194
+ assert_equal 1, rows.length
195
+ end
196
+
197
+ define_method( "test_execute_with_block_no_bind_with_match" ) do
198
+ called = 0
199
+ @db.execute( "select * from foo where a = 1" ) do |row|
200
+ called += 1
201
+ end
202
+ assert_equal 1, called
203
+ end
204
+
205
+ define_method( "test_execute_no_block_with_bind_with_match" ) do
206
+ rows = @db.execute( "select * from foo where a = ?", 1 )
207
+ assert_equal 1, rows.length
208
+ end
209
+
210
+ define_method( "test_execute_with_block_with_bind_with_match" ) do
211
+ called = 0
212
+ @db.execute( "select * from foo where a = ?", 1 ) do |row|
213
+ called += 1
214
+ end
215
+ assert_equal 1, called
216
+ end
217
+
218
+ define_method( "test_execute2_no_block_no_bind_no_match" ) do
219
+ columns, *rows = @db.execute2( "select * from foo where a > 100" )
220
+ assert rows.empty?
221
+ assert [ "a", "b" ], columns
222
+ end
223
+
224
+ define_method( "test_execute2_with_block_no_bind_no_match" ) do
225
+ called = 0
226
+ @db.execute2( "select * from foo where a > 100" ) do |row|
227
+ assert [ "a", "b" ], row unless called == 0
228
+ called += 1
229
+ end
230
+ assert_equal 1, called
231
+ end
232
+
233
+ define_method( "test_execute2_no_block_with_bind_no_match" ) do
234
+ columns, *rows = @db.execute2( "select * from foo where a > ?", 100 )
235
+ assert rows.empty?
236
+ assert [ "a", "b" ], columns
237
+ end
238
+
239
+ define_method( "test_execute2_with_block_with_bind_no_match" ) do
240
+ called = 0
241
+ @db.execute2( "select * from foo where a > ?", 100 ) do |row|
242
+ assert [ "a", "b" ], row unless called == 0
243
+ called += 1
244
+ end
245
+ assert_equal 1, called
246
+ end
247
+
248
+ define_method( "test_execute2_no_block_no_bind_with_match" ) do
249
+ columns, *rows = @db.execute2( "select * from foo where a = 1" )
250
+ assert_equal 1, rows.length
251
+ assert [ "a", "b" ], columns
252
+ end
253
+
254
+ define_method( "test_execute2_with_block_no_bind_with_match" ) do
255
+ called = 0
256
+ @db.execute2( "select * from foo where a = 1" ) do |row|
257
+ assert [ "a", "b" ], row unless called == 0
258
+ called += 1
259
+ end
260
+ assert_equal 2, called
261
+ end
262
+
263
+ define_method( "test_execute2_no_block_with_bind_with_match" ) do
264
+ columns, *rows = @db.execute2( "select * from foo where a = ?", 1 )
265
+ assert_equal 1, rows.length
266
+ assert [ "a", "b" ], columns
267
+ end
268
+
269
+ define_method( "test_execute2_with_block_with_bind_with_match" ) do
270
+ called = 0
271
+ @db.execute2( "select * from foo where a = ?", 1 ) do |row|
272
+ called += 1
273
+ end
274
+ assert_equal 2, called
275
+ end
276
+
277
+ define_method( "test_execute_batch_empty" ) do
278
+ assert_nothing_raised { @db.execute_batch "" }
279
+ end
280
+
281
+ define_method( "test_execute_batch_no_bind" ) do
282
+ @db.transaction do
283
+ @db.execute_batch <<-SQL
284
+ create table bar ( a, b, c );
285
+ insert into bar values ( 'one', 2, 'three' );
286
+ insert into bar values ( 'four', 5, 'six' );
287
+ insert into bar values ( 'seven', 8, 'nine' );
288
+ SQL
289
+ end
290
+ rows = @db.execute( "select * from bar" )
291
+ assert_equal 3, rows.length
292
+ end
293
+
294
+ define_method( "test_execute_batch_with_bind" ) do
295
+ @db.execute_batch( <<-SQL, 1 )
296
+ create table bar ( a, b, c );
297
+ insert into bar values ( 'one', 2, ? );
298
+ insert into bar values ( 'four', 5, ? );
299
+ insert into bar values ( 'seven', 8, ? );
300
+ SQL
301
+ rows = @db.execute( "select * from bar" ).map { |a,b,c| c }
302
+ assert_equal %w{1 1 1}, rows
303
+ end
304
+
305
+ define_method( "test_get_first_row_no_bind_no_match" ) do
306
+ result = @db.get_first_row( "select * from foo where a=100" )
307
+ assert_nil result
308
+ end
309
+
310
+ define_method( "test_get_first_row_no_bind_with_match" ) do
311
+ result = @db.get_first_row( "select * from foo where a=1" )
312
+ assert_equal [ "1", "foo" ], result
313
+ end
314
+
315
+ define_method( "test_get_first_row_with_bind_no_match" ) do
316
+ result = @db.get_first_row( "select * from foo where a=?", 100 )
317
+ assert_nil result
318
+ end
319
+
320
+ define_method( "test_get_first_row_with_bind_with_match" ) do
321
+ result = @db.get_first_row( "select * from foo where a=?", 1 )
322
+ assert_equal [ "1", "foo" ], result
323
+ end
324
+
325
+ define_method( "test_get_first_value_no_bind_no_match" ) do
326
+ result = @db.get_first_value( "select b, a from foo where a=100" )
327
+ assert_nil result
328
+ end
329
+
330
+ define_method( "test_get_first_value_no_bind_with_match" ) do
331
+ result = @db.get_first_value( "select b, a from foo where a=1" )
332
+ assert_equal "foo", result
333
+ end
334
+
335
+ define_method( "test_get_first_value_with_bind_no_match" ) do
336
+ result = @db.get_first_value( "select b, a from foo where a=?", 100 )
337
+ assert_nil result
338
+ end
339
+
340
+ define_method( "test_get_first_value_with_bind_with_match" ) do
341
+ result = @db.get_first_value( "select b, a from foo where a=?", 1 )
342
+ assert_equal "foo", result
343
+ end
344
+
345
+ define_method( "test_last_insert_row_id" ) do
346
+ @db.execute "insert into foo ( b ) values ( 'test' )"
347
+ assert_equal 4, @db.last_insert_row_id
348
+ @db.execute "insert into foo ( b ) values ( 'again' )"
349
+ assert_equal 5, @db.last_insert_row_id
350
+ end
351
+
352
+ define_method( "test_changes" ) do
353
+ @db.execute "insert into foo ( b ) values ( 'test' )"
354
+ assert_equal 1, @db.changes
355
+ @db.execute "delete from foo where 1=1"
356
+ assert_equal 4, @db.changes
357
+ end
358
+
359
+ define_method( "test_total_changes" ) do
360
+ assert_equal 3, @db.total_changes
361
+ @db.execute "insert into foo ( b ) values ( 'test' )"
362
+ @db.execute "delete from foo where 1=1"
363
+ assert_equal 8, @db.total_changes
364
+ end
365
+
366
+ define_method( "test_transaction_nest" ) do
367
+ assert_raise( SQLite3::SQLException ) do
368
+ @db.transaction do
369
+ @db.transaction do
370
+ end
371
+ end
372
+ end
373
+ end
374
+
375
+ define_method( "test_transaction_rollback" ) do
376
+ @db.transaction
377
+ @db.execute_batch <<-SQL
378
+ insert into foo (b) values ( 'test1' );
379
+ insert into foo (b) values ( 'test2' );
380
+ insert into foo (b) values ( 'test3' );
381
+ insert into foo (b) values ( 'test4' );
382
+ SQL
383
+ assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
384
+ @db.rollback
385
+ assert_equal 3, @db.get_first_value("select count(*) from foo").to_i
386
+ end
387
+
388
+ define_method( "test_transaction_commit" ) do
389
+ @db.transaction
390
+ @db.execute_batch <<-SQL
391
+ insert into foo (b) values ( 'test1' );
392
+ insert into foo (b) values ( 'test2' );
393
+ insert into foo (b) values ( 'test3' );
394
+ insert into foo (b) values ( 'test4' );
395
+ SQL
396
+ assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
397
+ @db.commit
398
+ assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
399
+ end
400
+
401
+ define_method( "test_transaction_rollback_in_block" ) do
402
+ assert_raise( SQLite3::SQLException ) do
403
+ @db.transaction do
404
+ @db.rollback
405
+ end
406
+ end
407
+ end
408
+
409
+ define_method( "test_transaction_commit_in_block" ) do
410
+ assert_raise( SQLite3::SQLException ) do
411
+ @db.transaction do
412
+ @db.commit
413
+ end
414
+ end
415
+ end
416
+
417
+ define_method( "test_transaction_active" ) do
418
+ assert !@db.transaction_active?
419
+ @db.transaction
420
+ assert @db.transaction_active?
421
+ @db.commit
422
+ assert !@db.transaction_active?
423
+ end
424
+
425
+ define_method( "no_tests_at" ) do |file,line,method|
426
+ warn "[(#{self.class}):#{file}:#{line}] no tests for #{method}"
427
+ end
428
+
429
+ define_method( "test_interrupt" ) do
430
+ @db.create_function( "abort", 1 ) do |func,x|
431
+ @db.interrupt
432
+ func.result = x
433
+ end
434
+
435
+ assert_raise( SQLite3::SQLException ) do
436
+ @db.execute "select abort(a) from foo"
437
+ end
438
+ end
439
+
440
+ define_method( "test_busy_handler_outwait" ) do
441
+ begin
442
+ db2 = SQLite3::Database.open( "test.db", :driver=>driver )
443
+ handler_call_count = 0
444
+ db2.busy_handler do |data,count|
445
+ handler_call_count += 1
446
+ sleep 0.1
447
+ 1
448
+ end
449
+
450
+ t = Thread.new do
451
+ @db.transaction( :exclusive ) do
452
+ sleep 0.1
453
+ end
454
+ end
455
+
456
+ assert_nothing_raised do
457
+ db2.execute "insert into foo (b) values ( 'from 2' )"
458
+ end
459
+
460
+ assert_equal 1, handler_call_count
461
+
462
+ t.join
463
+ ensure
464
+ db2.close if db2
465
+ end
466
+ end
467
+
468
+ define_method( "test_busy_handler_impatient" ) do
469
+ begin
470
+ db2 = SQLite3::Database.open( "test.db", :driver=>driver )
471
+ handler_call_count = 0
472
+ db2.busy_handler do |data,count|
473
+ handler_call_count += 1
474
+ sleep 0.1
475
+ 0
476
+ end
477
+
478
+ t = Thread.new do
479
+ @db.transaction( :exclusive ) do
480
+ sleep 0.2
481
+ end
482
+ end
483
+
484
+ assert_raise( SQLite3::BusyException ) do
485
+ db2.execute "insert into foo (b) values ( 'from 2' )"
486
+ end
487
+
488
+ assert_equal 1, handler_call_count
489
+
490
+ t.join
491
+ ensure
492
+ db2.close if db2
493
+ end
494
+ end
495
+
496
+ define_method( "test_busy_timeout" ) do
497
+ begin
498
+ db2 = SQLite3::Database.open( "test.db", :driver=>driver )
499
+ db2.busy_timeout 300
500
+
501
+ t = Thread.new do
502
+ @db.transaction( :exclusive ) do
503
+ sleep 0.1
504
+ end
505
+ end
506
+
507
+ time = Benchmark.measure do
508
+ assert_raise( SQLite3::BusyException ) do
509
+ db2.execute "insert into foo (b) values ( 'from 2' )"
510
+ end
511
+ end
512
+ assert time.real*1000 >= 300
513
+
514
+ t.join
515
+ ensure
516
+ db2.close if db2
517
+ end
518
+ end
519
+
520
+ define_method( "test_create_function" ) do
521
+ @db.create_function( "munge", 1 ) do |func,x|
522
+ func.result = ">>>#{x}<<<"
523
+ end
524
+
525
+ value = @db.get_first_value( "select munge(b) from foo where a=1" )
526
+ assert_match( />>>.*<<</, value )
527
+ end
528
+
529
+ define_method( "test_create_aggregate_without_block" ) do
530
+ step = proc do |ctx,a|
531
+ ctx[:sum] ||= 0
532
+ ctx[:sum] += a.to_i
533
+ end
534
+
535
+ final = proc { |ctx| ctx.result = ctx[:sum] }
536
+
537
+ @db.create_aggregate( "accumulate", 1, step, final )
538
+
539
+ value = @db.get_first_value( "select accumulate(a) from foo" )
540
+ assert_equal "6", value
541
+ end
542
+
543
+ define_method( "test_create_aggregate_with_block" ) do
544
+ @db.create_aggregate( "accumulate", 1 ) do
545
+ step do |ctx,a|
546
+ ctx[:sum] ||= 0
547
+ ctx[:sum] += a.to_i
548
+ end
549
+
550
+ finalize { |ctx| ctx.result = ctx[:sum] }
551
+ end
552
+
553
+ value = @db.get_first_value( "select accumulate(a) from foo" )
554
+ assert_equal "6", value
555
+ end
556
+
557
+ define_method( "test_create_aggregate_with_no_data" ) do
558
+ @db.create_aggregate( "accumulate", 1 ) do
559
+ step do |ctx,a|
560
+ ctx[:sum] ||= 0
561
+ ctx[:sum] += a.to_i
562
+ end
563
+
564
+ finalize { |ctx| ctx.result = ctx[:sum] || 0 }
565
+ end
566
+
567
+ value = @db.get_first_value(
568
+ "select accumulate(a) from foo where a = 100" )
569
+ assert_equal "0", value
570
+ end
571
+
572
+ define_method( "test_create_aggregate_handler" ) do
573
+ handler = Class.new do
574
+ class << self
575
+ define_method( "arity" ) { 1 }
576
+ define_method( "text_rep" ) { SQLite3::Constants::TextRep::ANY }
577
+ define_method( "name" ) { "multiply" }
578
+ end
579
+ define_method( "step" ) do |ctx,a|
580
+ ctx[:buffer] ||= 1
581
+ ctx[:buffer] *= a.to_i
582
+ end
583
+ define_method( "finalize" ) { |ctx| ctx.result = ctx[:buffer] }
584
+ end
585
+
586
+ @db.create_aggregate_handler( handler )
587
+ value = @db.get_first_value( "select multiply(a) from foo" )
588
+ assert_equal "6", value
589
+ end
590
+ end
591
+ const_set( "TC_Database_#{driver}", test_case )
592
+
593
+ # == TC_Statement =========================================================
594
+
595
+ test_case = Class.new( Test::Unit::TestCase ) do
596
+ define_method( "setup" ) do
597
+ @db = SQLite3::Database.new( "test.db", :driver=>driver )
598
+ @db.transaction do
599
+ @db.execute "create table foo ( a integer primary key, b text )"
600
+ @db.execute "insert into foo ( b ) values ( 'foo' )"
601
+ @db.execute "insert into foo ( b ) values ( 'bar' )"
602
+ @db.execute "insert into foo ( b ) values ( 'baz' )"
603
+ end
604
+ @stmt = @db.prepare( "select * from foo where a in ( ?, :named )" )
605
+ end
606
+
607
+ define_method( "teardown" ) do
608
+ @stmt.close
609
+ @db.close
610
+ File.delete( "test.db" )
611
+ end
612
+
613
+ define_method( "test_remainder_empty" ) do
614
+ assert_equal "", @stmt.remainder
615
+ end
616
+
617
+ define_method( "test_remainder_nonempty" ) do
618
+ called = false
619
+ @db.prepare( "select * from foo;\n blah" ) do |stmt|
620
+ called = true
621
+ assert_equal "\n blah", stmt.remainder
622
+ end
623
+ assert called
624
+ end
625
+
626
+ define_method( "test_bind_params_empty" ) do
627
+ assert_nothing_raised { @stmt.bind_params }
628
+ assert @stmt.execute!.empty?
629
+ end
630
+
631
+ define_method( "test_bind_params_array" ) do
632
+ @stmt.bind_params 1, 2
633
+ assert_equal 2, @stmt.execute!.length
634
+ end
635
+
636
+ define_method( "test_bind_params_hash" ) do
637
+ @stmt.bind_params ":named" => 2
638
+ assert_equal 1, @stmt.execute!.length
639
+ end
640
+
641
+ define_method( "test_bind_params_mixed" ) do
642
+ @stmt.bind_params( 1, ":named" => 2 )
643
+ assert_equal 2, @stmt.execute!.length
644
+ end
645
+
646
+ define_method( "test_bind_param_by_index" ) do
647
+ @stmt.bind_params( 1, 2 )
648
+ assert_equal 2, @stmt.execute!.length
649
+ end
650
+
651
+ define_method( "test_bind_param_by_name_bad" ) do
652
+ assert_raise( SQLite3::Exception ) { @stmt.bind_param( "named", 2 ) }
653
+ end
654
+
655
+ define_method( "test_bind_param_by_name_good" ) do
656
+ @stmt.bind_param( ":named", 2 )
657
+ assert_equal 1, @stmt.execute!.length
658
+ end
659
+
660
+ define_method( "test_execute_no_bind_no_block" ) do
661
+ assert_instance_of SQLite3::ResultSet, @stmt.execute
662
+ end
663
+
664
+ define_method( "test_execute_with_bind_no_block" ) do
665
+ assert_instance_of SQLite3::ResultSet, @stmt.execute( 1, 2 )
666
+ end
667
+
668
+ define_method( "test_execute_no_bind_with_block" ) do
669
+ called = false
670
+ @stmt.execute { |row| called = true }
671
+ assert called
672
+ end
673
+
674
+ define_method( "test_execute_with_bind_with_block" ) do
675
+ called = 0
676
+ @stmt.execute( 1, 2 ) { |row| called += 1 }
677
+ assert_equal 1, called
678
+ end
679
+
680
+ define_method( "test_reexecute" ) do
681
+ r = @stmt.execute( 1, 2 )
682
+ assert_equal 2, r.to_a.length
683
+ assert_nothing_raised { r = @stmt.execute( 1, 2 ) }
684
+ assert_equal 2, r.to_a.length
685
+ end
686
+
687
+ define_method( "test_execute_bang_no_bind_no_block" ) do
688
+ assert @stmt.execute!.empty?
689
+ end
690
+
691
+ define_method( "test_execute_bang_with_bind_no_block" ) do
692
+ assert_equal 2, @stmt.execute!( 1, 2 ).length
693
+ end
694
+
695
+ define_method( "test_execute_bang_no_bind_with_block" ) do
696
+ called = 0
697
+ @stmt.execute! { |row| called += 1 }
698
+ assert_equal 0, called
699
+ end
700
+
701
+ define_method( "test_execute_bang_with_bind_with_block" ) do
702
+ called = 0
703
+ @stmt.execute!( 1, 2 ) { |row| called += 1 }
704
+ assert_equal 2, called
705
+ end
706
+
707
+ define_method( "test_columns" ) do
708
+ c1 = @stmt.columns
709
+ c2 = @stmt.columns
710
+ assert_same c1, c2
711
+ assert_equal 2, c1.length
712
+ end
713
+
714
+ define_method( "test_columns_computed" ) do
715
+ called = false
716
+ @db.prepare( "select count(*) from foo" ) do |stmt|
717
+ called = true
718
+ assert_equal [ "count(*)" ], stmt.columns
719
+ end
720
+ assert called
721
+ end
722
+
723
+ define_method( "test_types" ) do
724
+ t1 = @stmt.types
725
+ t2 = @stmt.types
726
+ assert_same t1, t2
727
+ assert_equal 2, t1.length
728
+ end
729
+
730
+ define_method( "test_types_computed" ) do
731
+ called = false
732
+ @db.prepare( "select count(*) from foo" ) do |stmt|
733
+ called = true
734
+ assert_equal [ nil ], stmt.types
735
+ end
736
+ assert called
737
+ end
738
+ end
739
+ const_set( "TC_Statement_#{driver}", test_case )
740
+
741
+ # == TC_ResultSet =========================================================
742
+
743
+ test_case = Class.new( Test::Unit::TestCase ) do
744
+ define_method( "setup" ) do
745
+ @db = SQLite3::Database.new( "test.db", :driver=>driver )
746
+ @db.transaction do
747
+ @db.execute "create table foo ( a integer primary key, b text )"
748
+ @db.execute "insert into foo ( b ) values ( 'foo' )"
749
+ @db.execute "insert into foo ( b ) values ( 'bar' )"
750
+ @db.execute "insert into foo ( b ) values ( 'baz' )"
751
+ end
752
+ @stmt = @db.prepare( "select * from foo where a in ( ?, ? )" )
753
+ @result = @stmt.execute
754
+ end
755
+
756
+ define_method( "teardown" ) do
757
+ @stmt.close
758
+ @db.close
759
+ File.delete( "test.db" )
760
+ end
761
+
762
+ define_method( "test_reset_unused" ) do
763
+ assert_nothing_raised { @result.reset }
764
+ assert @result.to_a.empty?
765
+ end
766
+
767
+ define_method( "test_reset_used" ) do
768
+ @result.to_a
769
+ assert_nothing_raised { @result.reset }
770
+ assert @result.to_a.empty?
771
+ end
772
+
773
+ define_method( "test_reset_with_bind" ) do
774
+ @result.to_a
775
+ assert_nothing_raised { @result.reset( 1, 2 ) }
776
+ assert_equal 2, @result.to_a.length
777
+ end
778
+
779
+ define_method( "test_eof_inner" ) do
780
+ @result.reset( 1 )
781
+ assert !@result.eof?
782
+ end
783
+
784
+ define_method( "test_eof_edge" ) do
785
+ @result.reset( 1 )
786
+ @result.next # to first row
787
+ @result.next # to end of result set
788
+ assert @result.eof?
789
+ end
790
+
791
+ define_method( "test_next_eof" ) do
792
+ @result.reset( 1 )
793
+ assert_not_nil @result.next
794
+ assert_nil @result.next
795
+ end
796
+
797
+ define_method( "test_next_no_type_translation_no_hash" ) do
798
+ @result.reset( 1 )
799
+ assert_equal [ "1", "foo" ], @result.next
800
+ end
801
+
802
+ define_method( "test_next_type_translation" ) do
803
+ @db.type_translation = true
804
+ @result.reset( 1 )
805
+ assert_equal [ 1, "foo" ], @result.next
806
+ end
807
+
808
+ define_method( "test_next_results_as_hash" ) do
809
+ @db.results_as_hash = true
810
+ @result.reset( 1 )
811
+ assert_equal( { "a" => "1", "b" => "foo", 0 => "1", 1 => "foo" },
812
+ @result.next )
813
+ end
814
+
815
+ define_method( "test_each" ) do
816
+ called = 0
817
+ @result.reset( 1, 2 )
818
+ @result.each { |row| called += 1 }
819
+ assert_equal 2, called
820
+ end
821
+
822
+ define_method( "test_enumerable" ) do
823
+ @result.reset( 1, 2 )
824
+ assert_equal 2, @result.to_a.length
825
+ end
826
+
827
+ define_method( "test_types" ) do
828
+ assert_equal [ "integer", "text" ], @result.types
829
+ end
830
+
831
+ define_method( "test_columns" ) do
832
+ assert_equal [ "a", "b" ], @result.columns
833
+ end
834
+ end
835
+ const_set( "TC_ResultSet_#{driver}", test_case )
836
+ end
837
+
838
+ end