sqlite3-ruby 0.9.0-mswin32

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


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

data/test/mocks.rb ADDED
@@ -0,0 +1,99 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are met:
8
+ #
9
+ # * Redistributions of source code must retain the above copyright notice,
10
+ # this list of conditions and the following disclaimer.
11
+ #
12
+ # * Redistributions in binary form must reproduce the above copyright
13
+ # notice, this list of conditions and the following disclaimer in the
14
+ # documentation and/or other materials provided with the distribution.
15
+ #
16
+ # * The names of its contributors may not be used to endorse or promote
17
+ # products derived from this software without specific prior written
18
+ # permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
24
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ # =============================================================================
31
+ #++
32
+
33
+ require 'flexmock'
34
+
35
+ class FlexMockWithArgs < FlexMock
36
+ attr_reader :mock_args
37
+ attr_reader :mock_blocks
38
+
39
+ def initialize
40
+ super
41
+ @mock_args = Hash.new { |h,k| h[k] = [] }
42
+ @mock_blocks = Hash.new { |h,k| h[k] = [] }
43
+ end
44
+
45
+ def method_missing( sym, *args, &block )
46
+ @mock_args[sym] << args
47
+ @mock_blocks[sym] << block
48
+ super
49
+ end
50
+ end
51
+
52
+ class Driver < FlexMockWithArgs
53
+ def self.instance
54
+ @@instance
55
+ end
56
+
57
+ def initialize
58
+ super
59
+ @@instance = self
60
+ mock_handle( :open ) { [0,"cookie"] }
61
+ mock_handle( :close ) { 0 }
62
+ mock_handle( :complete? ) { 0 }
63
+ mock_handle( :errmsg ) { "" }
64
+ mock_handle( :errcode ) { 0 }
65
+ mock_handle( :trace ) { nil }
66
+ mock_handle( :set_authorizer ) { 0 }
67
+ mock_handle( :prepare ) { [0,"stmt", "remainder"] }
68
+ mock_handle( :finalize ) { 0 }
69
+ mock_handle( :changes ) { 14 }
70
+ mock_handle( :total_changes ) { 28 }
71
+ mock_handle( :interrupt ) { 0 }
72
+ end
73
+ end
74
+
75
+ class Statement < FlexMockWithArgs
76
+ def self.instance
77
+ @@instance
78
+ end
79
+
80
+ attr_reader :handle
81
+ attr_reader :sql
82
+ attr_reader :last_result
83
+
84
+ def initialize( handle, sql )
85
+ super()
86
+ @@instance = self
87
+ @handle = handle
88
+ @sql = sql
89
+ mock_handle( :close ) { 0 }
90
+ mock_handle( :remainder ) { "" }
91
+ mock_handle( :execute ) do
92
+ @last_result = FlexMockWithArgs.new
93
+ @last_result.mock_handle( :each ) { @last_result.mock_blocks[:each].last.call ["foo"] }
94
+ @last_result.mock_handle( :inject ) { |a,| @last_result.mock_blocks[:inject].last.call a, ["foo"] }
95
+ @last_result.mock_handle( :columns ) { ["name"] }
96
+ @last_result
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,126 @@
1
+ $:.unshift "../lib", "../ext/sqlite3_api"
2
+
3
+ require 'sqlite3'
4
+
5
+ require 'benchmark'
6
+
7
+ N = 1000
8
+
9
+ $VERBOSE=nil
10
+
11
+ puts "database creation..."
12
+ Benchmark.bm( 7 ) do |x|
13
+ x.report('dl') do
14
+ N.times do
15
+ File.delete "test.db" rescue nil
16
+ SQLite3::Database.open( "test.db", :driver => "DL" ).close
17
+ end
18
+ end
19
+ x.report('native') do
20
+ N.times do
21
+ File.delete "test.db" rescue nil
22
+ SQLite3::Database.open( "test.db", :driver => "Native" ).close
23
+ end
24
+ end
25
+ end
26
+ File.delete "test.db" rescue nil
27
+
28
+ SQLite3::Database.open( "test.db" ).close
29
+
30
+ puts
31
+ puts "database open..."
32
+ Benchmark.bm( 7 ) do |x|
33
+ x.report('dl') do
34
+ N.times do
35
+ SQLite3::Database.open( "test.db", :driver => "DL" ).close
36
+ end
37
+ end
38
+ x.report('native') do
39
+ N.times do
40
+ SQLite3::Database.open( "test.db", :driver => "Native" ).close
41
+ end
42
+ end
43
+ end
44
+ File.delete "test.db" rescue nil
45
+
46
+ dl = SQLite3::Database.open( "test-dl.db", :driver => "DL" )
47
+ native = SQLite3::Database.open( "test-native.db", :driver => "Native" )
48
+
49
+ dl.execute "create table foo (a,b)"
50
+ native.execute "create table foo (a,b)"
51
+
52
+ puts
53
+ puts "insertions"
54
+ Benchmark.bm( 7 ) do |x|
55
+ x.report('dl') do
56
+ dl.transaction do
57
+ N.times do |i|
58
+ dl.execute "insert into foo values (#{i}, #{i+1})"
59
+ end
60
+ end
61
+ end
62
+ x.report('native') do
63
+ native.transaction do
64
+ N.times do |i|
65
+ native.execute "insert into foo values (#{i}, #{i+1})"
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ puts
72
+ puts "insertions using prepared statement"
73
+ Benchmark.bm( 7 ) do |x|
74
+ x.report('dl') do
75
+ dl.transaction do
76
+ dl.prepare "insert into foo values (?,?)" do |stmt|
77
+ N.times { |i| stmt.execute i, i+1 }
78
+ end
79
+ end
80
+ end
81
+ x.report('native') do
82
+ native.transaction do
83
+ native.prepare( "insert into foo values (?,?)" ) do |stmt|
84
+ N.times { |i| stmt.execute i, i+1 }
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ dl.close
91
+ native.close
92
+ File.delete "test-dl.db" rescue nil
93
+ File.delete "test-native.db" rescue nil
94
+
95
+ dl = SQLite3::Database.open( "test-dl.db", :driver => "DL" )
96
+ native = SQLite3::Database.open( "test-native.db", :driver => "Native" )
97
+
98
+ dl.execute "create table foo (a,b)"
99
+ dl.execute "insert into foo values (1,2)"
100
+ dl.execute "insert into foo values (3,4)"
101
+ dl.execute "insert into foo values (5,6)"
102
+
103
+ native.execute "create table foo (a,b)"
104
+ native.execute "insert into foo values (1,2)"
105
+ native.execute "insert into foo values (3,4)"
106
+ native.execute "insert into foo values (5,6)"
107
+
108
+ puts
109
+ puts "queries"
110
+ Benchmark.bm( 7 ) do |x|
111
+ x.report('dl') do
112
+ N.times do
113
+ dl.execute "select * from foo"
114
+ end
115
+ end
116
+ x.report('native') do
117
+ N.times do
118
+ native.execute "select * from foo"
119
+ end
120
+ end
121
+ end
122
+
123
+ dl.close
124
+ native.close
125
+ File.delete "test-dl.db" rescue nil
126
+ File.delete "test-native.db" rescue nil
@@ -0,0 +1,216 @@
1
+ #--
2
+ # =============================================================================
3
+ # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are met:
8
+ #
9
+ # * Redistributions of source code must retain the above copyright notice,
10
+ # this list of conditions and the following disclaimer.
11
+ #
12
+ # * Redistributions in binary form must reproduce the above copyright
13
+ # notice, this list of conditions and the following disclaimer in the
14
+ # documentation and/or other materials provided with the distribution.
15
+ #
16
+ # * The names of its contributors may not be used to endorse or promote
17
+ # products derived from this software without specific prior written
18
+ # permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
24
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ # =============================================================================
31
+ #++
32
+
33
+ $:.unshift "../lib"
34
+
35
+ require 'sqlite3/database'
36
+ require 'test/unit'
37
+
38
+ require 'mocks'
39
+
40
+ class TC_Database_Init < Test::Unit::TestCase
41
+ def test_new
42
+ db = SQLite3::Database.new( "foo.db", :driver => Driver )
43
+ assert_equal 1, Driver.instance.mock_count(:open)
44
+ assert !db.closed?
45
+ assert_equal [["foo.db",false]], Driver.instance.mock_args[:open]
46
+ assert !db.results_as_hash
47
+ assert !db.type_translation
48
+ end
49
+
50
+ def test_open
51
+ db = SQLite3::Database.open( "foo.db", :driver => Driver )
52
+ assert_equal 1, Driver.instance.mock_count(:open)
53
+ assert !db.closed?
54
+ assert_equal [["foo.db",false]], Driver.instance.mock_args[:open]
55
+ assert !db.results_as_hash
56
+ assert !db.type_translation
57
+ end
58
+
59
+ def test_with_type_translation
60
+ db = SQLite3::Database.open( "foo.db", :driver => Driver,
61
+ :type_translation => true )
62
+ assert db.type_translation
63
+ end
64
+
65
+ def test_with_results_as_hash
66
+ db = SQLite3::Database.open( "foo.db", :driver => Driver,
67
+ :results_as_hash => true )
68
+ assert db.results_as_hash
69
+ end
70
+
71
+ def test_with_type_translation_and_results_as_hash
72
+ db = SQLite3::Database.open( "foo.db", :driver => Driver,
73
+ :results_as_hash => true,
74
+ :type_translation => true )
75
+ assert db.results_as_hash
76
+ assert db.type_translation
77
+ end
78
+ end
79
+
80
+ class TC_Database < Test::Unit::TestCase
81
+ def setup
82
+ @db = SQLite3::Database.open( "foo.db",
83
+ :driver => Driver, :statement_factory => Statement )
84
+ end
85
+
86
+ def test_quote
87
+ assert_equal "''one''two''three''", SQLite3::Database.quote(
88
+ "'one'two'three'" )
89
+ end
90
+
91
+ def test_complete
92
+ @db.complete? "foo"
93
+ assert_equal 1, Driver.instance.mock_count( :complete? )
94
+ end
95
+
96
+ def test_errmsg
97
+ @db.errmsg
98
+ assert_equal 1, Driver.instance.mock_count( :errmsg )
99
+ end
100
+
101
+ def test_errcode
102
+ @db.errcode
103
+ assert_equal 1, Driver.instance.mock_count( :errcode )
104
+ end
105
+
106
+ def test_translator
107
+ translator = @db.translator
108
+ assert_instance_of SQLite3::Translator, translator
109
+ end
110
+
111
+ def test_close
112
+ @db.close
113
+ assert_equal 1, Driver.instance.mock_count( :close )
114
+ assert @db.closed?
115
+ @db.close
116
+ assert_equal 1, Driver.instance.mock_count( :close )
117
+ end
118
+
119
+ def test_trace
120
+ @db.trace( 15 ) { "foo" }
121
+ driver = Driver.instance
122
+ assert_equal 1, driver.mock_count( :trace )
123
+ assert_equal [[ "cookie", 15 ]], driver.mock_args[:trace]
124
+ assert_equal 1, driver.mock_blocks[:trace].length
125
+ end
126
+
127
+ def test_authorizer
128
+ @db.authorizer( 15 ) { "foo" }
129
+ driver = Driver.instance
130
+ assert_equal 1, driver.mock_count( :set_authorizer )
131
+ assert_equal [[ "cookie", 15 ]], driver.mock_args[:set_authorizer]
132
+ assert_equal 1, driver.mock_blocks[:set_authorizer].length
133
+ end
134
+
135
+ def test_prepare_no_block
136
+ assert_nothing_raised { @db.prepare( "foo" ) }
137
+ assert_equal 0, Statement.instance.mock_count( :close )
138
+ end
139
+
140
+ def test_prepare_with_block
141
+ called = false
142
+ @db.prepare( "foo" ) { |stmt| called = true }
143
+ assert called
144
+ assert_equal 1, Statement.instance.mock_count( :close )
145
+ end
146
+
147
+ def test_execute_no_block
148
+ result = @db.execute( "foo", "bar", "baz" )
149
+ stmt = Statement.instance
150
+ assert_equal [["foo"]], result
151
+ assert_equal [["bar", "baz"]], stmt.mock_args[:execute]
152
+ end
153
+
154
+ def test_execute_with_block
155
+ called = false
156
+ @db.execute( "foo", "bar", "baz" ) do |row|
157
+ called = true
158
+ assert_equal ["foo"], row
159
+ end
160
+
161
+ stmt = Statement.instance
162
+ assert called
163
+ assert_equal [["bar", "baz"]], stmt.mock_args[:execute]
164
+ end
165
+
166
+ def test_execute2_no_block
167
+ result = @db.execute2( "foo", "bar", "baz" )
168
+ stmt = Statement.instance
169
+ assert_equal [["name"],["foo"]], result
170
+ assert_equal [["bar", "baz"]], stmt.mock_args[:execute]
171
+ end
172
+
173
+ def test_execute2_with_block
174
+ called = false
175
+ parts = [ ["name"],["foo"] ]
176
+ @db.execute2( "foo", "bar", "baz" ) do |row|
177
+ called = true
178
+ assert_equal parts.shift, row
179
+ end
180
+
181
+ stmt = Statement.instance
182
+ assert called
183
+ assert_equal [["bar", "baz"]], stmt.mock_args[:execute]
184
+ end
185
+
186
+ def test_execute_batch
187
+ @db.execute_batch( "foo", "bar", "baz" )
188
+ stmt = Statement.instance
189
+ assert_equal [["bar", "baz"]], stmt.mock_args[:execute]
190
+ end
191
+
192
+ def test_get_first_row
193
+ result = @db.get_first_row( "foo", "bar", "baz" )
194
+ assert_equal ["foo"], result
195
+ end
196
+
197
+ def test_get_first_value
198
+ result = @db.get_first_value( "foo", "bar", "baz" )
199
+ assert_equal "foo", result
200
+ end
201
+
202
+ def test_changes
203
+ assert_equal 14, @db.changes
204
+ assert_equal 1, Driver.instance.mock_count(:changes)
205
+ end
206
+
207
+ def test_total_changes
208
+ assert_equal 28, @db.total_changes
209
+ assert_equal 1, Driver.instance.mock_count(:total_changes)
210
+ end
211
+
212
+ def test_interrupt
213
+ @db.interrupt
214
+ assert_equal 1, Driver.instance.mock_count(:interrupt)
215
+ end
216
+ end
@@ -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