luislavena-sqlite3-ruby 1.2.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,193 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TC_Database_Init < Test::Unit::TestCase
4
+ def test_new
5
+ # any_instance fails here...
6
+ driver = Driver.new
7
+ driver.expects(:open).once.with('foo.db', false).returns([0, 'cookie'])
8
+ Driver.stubs(:new).returns(driver)
9
+ db = SQLite3::Database.new( 'foo.db', :driver => Driver )
10
+ assert !db.closed?
11
+ assert !db.results_as_hash
12
+ assert !db.type_translation
13
+ end
14
+
15
+ def test_open
16
+ driver = Driver.new
17
+ driver.expects(:open).once.with('foo.db', false).returns([0, 'cookie'])
18
+ Driver.stubs(:new).returns(driver)
19
+ db = SQLite3::Database.open( "foo.db", :driver => Driver )
20
+ assert !db.closed?
21
+ assert !db.results_as_hash
22
+ assert !db.type_translation
23
+ end
24
+
25
+ def test_with_type_translation
26
+ db = SQLite3::Database.open( "foo.db", :driver => Driver,
27
+ :type_translation => true )
28
+ assert db.type_translation
29
+ end
30
+
31
+ def test_with_results_as_hash
32
+ db = SQLite3::Database.open( "foo.db", :driver => Driver,
33
+ :results_as_hash => true )
34
+ assert db.results_as_hash
35
+ end
36
+
37
+ def test_with_type_translation_and_results_as_hash
38
+ db = SQLite3::Database.open( "foo.db", :driver => Driver,
39
+ :results_as_hash => true,
40
+ :type_translation => true )
41
+ assert db.results_as_hash
42
+ assert db.type_translation
43
+ end
44
+ end
45
+
46
+ class TC_Database < Test::Unit::TestCase
47
+ def setup
48
+ @db = SQLite3::Database.open( "foo.db",
49
+ :driver => Driver, :statement_factory => Statement )
50
+ end
51
+
52
+ def test_quote
53
+ assert_equal "''one''two''three''", SQLite3::Database.quote(
54
+ "'one'two'three'" )
55
+ end
56
+
57
+ def test_complete
58
+ Driver.any_instance.expects(:complete?)
59
+ @db.complete? "foo"
60
+ end
61
+
62
+ def test_errmsg
63
+ Driver.any_instance.expects(:errmsg)
64
+ @db.errmsg
65
+ end
66
+
67
+ def test_errcode
68
+ Driver.any_instance.expects(:errcode)
69
+ @db.errcode
70
+ end
71
+
72
+ def test_translator
73
+ translator = @db.translator
74
+ assert_instance_of SQLite3::Translator, translator
75
+ end
76
+
77
+ def test_close
78
+ Driver.any_instance.expects(:close).returns(0)
79
+ @db.close
80
+ assert @db.closed?
81
+ Driver.any_instance.expects(:close).never
82
+ @db.close
83
+ end
84
+
85
+ def test_trace
86
+ Driver.any_instance.expects(:trace).with('cookie', 15)
87
+ @db.trace( 15 ) { "foo" }
88
+ # assert_equal 1, driver.mock_blocks[:trace].length
89
+ end
90
+
91
+ def test_authorizer
92
+ Driver.any_instance.expects(:set_authorizer).with('cookie', 15).returns(0)
93
+ @db.authorizer( 15 ) { "foo" }
94
+ # assert_equal 1, driver.mock_blocks[:set_authorizer].length
95
+ end
96
+
97
+ def test_prepare_no_block
98
+ Statement.any_instance.expects(:close).never
99
+ assert_nothing_raised { @db.prepare( "foo" ) }
100
+ end
101
+
102
+ def test_prepare_with_block
103
+ called = false
104
+ # any_instance fails here...
105
+ statement = Statement.new('cookie', 'foo')
106
+ statement.expects(:close).once
107
+ Statement.stubs(:new).returns(statement)
108
+ @db.prepare( "foo" ) { |stmt| called = true }
109
+ assert called
110
+ end
111
+
112
+ def test_execute_no_block
113
+ # any_instance fails here...
114
+ statement = Statement.new('cookie', 'foo')
115
+ statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
116
+ Statement.stubs(:new).returns(statement)
117
+ MockResultSet.any_instance.stubs(:inject).returns([['foo']])
118
+ result = @db.execute( "foo", "bar", "baz" )
119
+ assert_equal [["foo"]], result
120
+ end
121
+
122
+ def test_execute_with_block
123
+ called = false
124
+ # any_instance fails here...
125
+ statement = Statement.new('cookie', 'foo')
126
+ statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
127
+ Statement.stubs(:new).returns(statement)
128
+ @db.execute( "foo", "bar", "baz" ) do |row|
129
+ called = true
130
+ assert_equal ["foo"], row
131
+ end
132
+
133
+ assert called
134
+ end
135
+
136
+ def test_execute2_no_block
137
+ # any_instance fails here...
138
+ statement = Statement.new('cookie', 'foo')
139
+ statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
140
+ Statement.stubs(:new).returns(statement)
141
+ MockResultSet.any_instance.stubs(:inject).returns([['name'], ['foo']])
142
+ result = @db.execute2( "foo", "bar", "baz" )
143
+ assert_equal [["name"],["foo"]], result
144
+ end
145
+
146
+ def test_execute2_with_block
147
+ called = false
148
+ parts = [ ["name"],["foo"] ]
149
+ # any_instance fails here...
150
+ statement = Statement.new('cookie', 'foo')
151
+ statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
152
+ Statement.stubs(:new).returns(statement)
153
+ @db.execute2( "foo", "bar", "baz" ) do |row|
154
+ called = true
155
+ assert_equal parts.shift, row
156
+ end
157
+
158
+ assert called
159
+ end
160
+
161
+ def test_execute_batch
162
+ # any_instance fails here...
163
+ statement = Statement.new('cookie', 'foo')
164
+ statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
165
+ Statement.stubs(:new).returns(statement)
166
+ @db.execute_batch( "foo", "bar", "baz" )
167
+ end
168
+
169
+ def test_get_first_row
170
+ result = @db.get_first_row( "foo", "bar", "baz" )
171
+ assert_equal ["foo"], result
172
+ end
173
+
174
+ def test_get_first_value
175
+ result = @db.get_first_value( "foo", "bar", "baz" )
176
+ assert_equal "foo", result
177
+ end
178
+
179
+ def test_changes
180
+ Driver.any_instance.expects(:changes).returns(14)
181
+ assert_equal 14, @db.changes
182
+ end
183
+
184
+ def test_total_changes
185
+ Driver.any_instance.expects(:total_changes).returns(28)
186
+ assert_equal 28, @db.total_changes
187
+ end
188
+
189
+ def test_interrupt
190
+ Driver.any_instance.expects(:interrupt)
191
+ @db.interrupt
192
+ end
193
+ end
@@ -0,0 +1,17 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TC_Errors < Test::Unit::TestCase
4
+ (1..26).each do |code|
5
+ define_method( "test_error_code_%02d" % code ) do
6
+ db = stub('database', :errmsg => 'message')
7
+ begin
8
+ SQLite3::Error.check( code, db )
9
+ rescue SQLite3::Exception => e
10
+ assert_instance_of SQLite3::EXCEPTIONS[code], e
11
+ assert_equal code, e.code
12
+ assert_equal code, e.class.code
13
+ assert_equal "message", e.message
14
+ end
15
+ end
16
+ end
17
+ end