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,142 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TC_ResultSet < 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
+ @stmt = @db.prepare( "select * from foo where a in ( ?, ? )" )
13
+ @result = @stmt.execute
14
+ end
15
+
16
+ def teardown
17
+ @stmt.close
18
+ @db.close
19
+ File.delete( "test.db" )
20
+ end
21
+
22
+ def test_reset_unused
23
+ assert_nothing_raised { @result.reset }
24
+ assert @result.to_a.empty?
25
+ end
26
+
27
+ def test_reset_used
28
+ @result.to_a
29
+ assert_nothing_raised { @result.reset }
30
+ assert @result.to_a.empty?
31
+ end
32
+
33
+ def test_reset_with_bind
34
+ @result.to_a
35
+ assert_nothing_raised { @result.reset( 1, 2 ) }
36
+ assert_equal 2, @result.to_a.length
37
+ end
38
+
39
+ def test_eof_inner
40
+ @result.reset( 1 )
41
+ assert !@result.eof?
42
+ end
43
+
44
+ def test_eof_edge
45
+ @result.reset( 1 )
46
+ @result.next # to first row
47
+ @result.next # to end of result set
48
+ assert @result.eof?
49
+ end
50
+
51
+ def test_next_eof
52
+ @result.reset( 1 )
53
+ assert_not_nil @result.next
54
+ assert_nil @result.next
55
+ end
56
+
57
+ def test_next_no_type_translation_no_hash
58
+ @result.reset( 1 )
59
+ assert_equal [ "1", "foo" ], @result.next
60
+ end
61
+
62
+ def test_next_type_translation
63
+ @db.type_translation = true
64
+ @result.reset( 1 )
65
+ assert_equal [ 1, "foo" ], @result.next
66
+ end
67
+
68
+ def test_next_type_translation_with_untyped_column
69
+ @db.type_translation = true
70
+ @db.query( "select count(*) from foo" ) do |result|
71
+ assert_equal ["3"], result.next
72
+ end
73
+ end
74
+
75
+ def test_type_translation_with_null_column
76
+ @db.type_translation = true
77
+ @db.execute "create table bar ( a integer, b time, c string )"
78
+ @db.execute "insert into bar (a, b, c) values (NULL, '1974-07-25 14:39:00', 'hello')"
79
+ @db.execute "insert into bar (a, b, c) values (1, NULL, 'hello')"
80
+ @db.execute "insert into bar (a, b, c) values (2, '1974-07-25 14:39:00', NULL)"
81
+ @db.query( "select * from bar" ) do |result|
82
+ assert_equal [nil, Time.local(1974, 7, 25, 14, 39, 0), 'hello'], result.next
83
+ assert_equal [1, nil, 'hello'], result.next
84
+ assert_equal [2, Time.local(1974, 7, 25, 14, 39, 0), nil], result.next
85
+ end
86
+ end
87
+
88
+ def test_date_and_time_translation
89
+ @db.type_translation = true
90
+ @db.execute "create table bar ( a date, b datetime, c time, d timestamp )"
91
+ @db.execute "insert into bar (a, b, c, d) values ('1999-01-08', '1997-12-17 07:37:16', '07:37:16', '2004-10-19 10:23:54')"
92
+ @db.query( "select * from bar" ) do |result|
93
+ result = result.next
94
+ assert result[0].is_a?(Date)
95
+ assert result[1].is_a?(DateTime)
96
+ assert result[2].is_a?(Time)
97
+ assert result[3].is_a?(Time)
98
+ end
99
+ end
100
+
101
+ def test_next_results_as_hash
102
+ @db.results_as_hash = true
103
+ @result.reset( 1 )
104
+ assert_equal( { "a" => "1", "b" => "foo", 0 => "1", 1 => "foo" },
105
+ @result.next )
106
+ end
107
+
108
+ def test_each
109
+ called = 0
110
+ @result.reset( 1, 2 )
111
+ @result.each { |row| called += 1 }
112
+ assert_equal 2, called
113
+ end
114
+
115
+ def test_enumerable
116
+ @result.reset( 1, 2 )
117
+ assert_equal 2, @result.to_a.length
118
+ end
119
+
120
+ def test_types
121
+ assert_equal [ "integer", "text" ], @result.types
122
+ end
123
+
124
+ def test_columns
125
+ assert_equal [ "a", "b" ], @result.columns
126
+ end
127
+
128
+ def test_close
129
+ stmt = @db.prepare( "select * from foo" )
130
+ result = stmt.execute
131
+ assert !result.closed?
132
+ result.close
133
+ assert result.closed?
134
+ assert stmt.closed?
135
+ assert_raise( SQLite3::Exception ) { result.reset }
136
+ assert_raise( SQLite3::Exception ) { result.next }
137
+ assert_raise( SQLite3::Exception ) { result.each }
138
+ assert_raise( SQLite3::Exception ) { result.close }
139
+ assert_raise( SQLite3::Exception ) { result.types }
140
+ assert_raise( SQLite3::Exception ) { result.columns }
141
+ end
142
+ end
@@ -0,0 +1,195 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TC_Statement < 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
+ @stmt = @db.prepare( "select * from foo where a in ( ?, :named )" )
13
+ end
14
+
15
+ def teardown
16
+ @stmt.close
17
+ @db.close
18
+ File.delete( "test.db" )
19
+ end
20
+
21
+ def test_remainder_empty
22
+ assert_equal "", @stmt.remainder
23
+ end
24
+
25
+ def test_remainder_nonempty
26
+ called = false
27
+ @db.prepare( "select * from foo;\n blah" ) do |stmt|
28
+ called = true
29
+ assert_equal "\n blah", stmt.remainder
30
+ end
31
+ assert called
32
+ end
33
+
34
+ def test_bind_params_empty
35
+ assert_nothing_raised { @stmt.bind_params }
36
+ assert @stmt.execute!.empty?
37
+ end
38
+
39
+ def test_bind_params_array
40
+ @stmt.bind_params 1, 2
41
+ assert_equal 2, @stmt.execute!.length
42
+ end
43
+
44
+ def test_bind_params_hash
45
+ @stmt.bind_params ":named" => 2
46
+ assert_equal 1, @stmt.execute!.length
47
+ end
48
+
49
+ def test_bind_params_hash_without_colon
50
+ @stmt.bind_params "named" => 2
51
+ assert_equal 1, @stmt.execute!.length
52
+ end
53
+
54
+ def test_bind_params_hash_as_symbol
55
+ @stmt.bind_params :named => 2
56
+ assert_equal 1, @stmt.execute!.length
57
+ end
58
+
59
+ def test_bind_params_mixed
60
+ @stmt.bind_params( 1, ":named" => 2 )
61
+ assert_equal 2, @stmt.execute!.length
62
+ end
63
+
64
+ def test_bind_param_by_index
65
+ @stmt.bind_params( 1, 2 )
66
+ assert_equal 2, @stmt.execute!.length
67
+ end
68
+
69
+ def test_bind_param_by_name_bad
70
+ assert_raise( SQLite3::Exception ) { @stmt.bind_param( "@named", 2 ) }
71
+ end
72
+
73
+ def test_bind_param_by_name_good
74
+ @stmt.bind_param( ":named", 2 )
75
+ assert_equal 1, @stmt.execute!.length
76
+ end
77
+
78
+ def test_bind_param_with_various_types
79
+ @db.transaction do
80
+ @db.execute "create table all_types ( a integer primary key, b float, c string, d integer )"
81
+ @db.execute "insert into all_types ( b, c, d ) values ( 1.4, 'hello', 68719476735 )"
82
+ end
83
+
84
+ assert_equal 1, @db.execute( "select * from all_types where b = ?", 1.4 ).length
85
+ assert_equal 1, @db.execute( "select * from all_types where c = ?", 'hello').length
86
+ assert_equal 1, @db.execute( "select * from all_types where d = ?", 68719476735).length
87
+ end
88
+
89
+ def test_execute_no_bind_no_block
90
+ assert_instance_of SQLite3::ResultSet, @stmt.execute
91
+ end
92
+
93
+ def test_execute_with_bind_no_block
94
+ assert_instance_of SQLite3::ResultSet, @stmt.execute( 1, 2 )
95
+ end
96
+
97
+ def test_execute_no_bind_with_block
98
+ called = false
99
+ @stmt.execute { |row| called = true }
100
+ assert called
101
+ end
102
+
103
+ def test_execute_with_bind_with_block
104
+ called = 0
105
+ @stmt.execute( 1, 2 ) { |row| called += 1 }
106
+ assert_equal 1, called
107
+ end
108
+
109
+ def test_reexecute
110
+ r = @stmt.execute( 1, 2 )
111
+ assert_equal 2, r.to_a.length
112
+ assert_nothing_raised { r = @stmt.execute( 1, 2 ) }
113
+ assert_equal 2, r.to_a.length
114
+ end
115
+
116
+ def test_execute_bang_no_bind_no_block
117
+ assert @stmt.execute!.empty?
118
+ end
119
+
120
+ def test_execute_bang_with_bind_no_block
121
+ assert_equal 2, @stmt.execute!( 1, 2 ).length
122
+ end
123
+
124
+ def test_execute_bang_no_bind_with_block
125
+ called = 0
126
+ @stmt.execute! { |row| called += 1 }
127
+ assert_equal 0, called
128
+ end
129
+
130
+ def test_execute_bang_with_bind_with_block
131
+ called = 0
132
+ @stmt.execute!( 1, 2 ) { |row| called += 1 }
133
+ assert_equal 2, called
134
+ end
135
+
136
+ def test_columns
137
+ c1 = @stmt.columns
138
+ c2 = @stmt.columns
139
+ assert_same c1, c2
140
+ assert_equal 2, c1.length
141
+ end
142
+
143
+ def test_columns_computed
144
+ called = false
145
+ @db.prepare( "select count(*) from foo" ) do |stmt|
146
+ called = true
147
+ assert_equal [ "count(*)" ], stmt.columns
148
+ end
149
+ assert called
150
+ end
151
+
152
+ def test_types
153
+ t1 = @stmt.types
154
+ t2 = @stmt.types
155
+ assert_same t1, t2
156
+ assert_equal 2, t1.length
157
+ end
158
+
159
+ def test_types_computed
160
+ called = false
161
+ @db.prepare( "select count(*) from foo" ) do |stmt|
162
+ called = true
163
+ assert_equal [ nil ], stmt.types
164
+ end
165
+ assert called
166
+ end
167
+
168
+ def test_close
169
+ stmt = @db.prepare( "select * from foo" )
170
+ assert !stmt.closed?
171
+ stmt.close
172
+ assert stmt.closed?
173
+ assert_raise( SQLite3::Exception ) { stmt.execute }
174
+ assert_raise( SQLite3::Exception ) { stmt.execute! }
175
+ assert_raise( SQLite3::Exception ) { stmt.close }
176
+ assert_raise( SQLite3::Exception ) { stmt.bind_params 5 }
177
+ assert_raise( SQLite3::Exception ) { stmt.bind_param 1, 5 }
178
+ assert_raise( SQLite3::Exception ) { stmt.columns }
179
+ assert_raise( SQLite3::Exception ) { stmt.types }
180
+ end
181
+
182
+ def test_committing_tx_with_statement_active
183
+ called = false
184
+ @db.prepare( "select count(*) from foo" ) do |stmt|
185
+ called = true
186
+ count = stmt.execute!.first.first.to_i
187
+ @db.transaction do
188
+ @db.execute "insert into foo ( b ) values ( 'hello' )"
189
+ end
190
+ new_count = stmt.execute!.first.first.to_i
191
+ assert_equal new_count, count+1
192
+ end
193
+ assert called
194
+ end
195
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: luislavena-sqlite3-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.4.1
5
+ platform: ruby
6
+ authors:
7
+ - Jamis Buck
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-04 20:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: mocha
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake-compiler
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.0
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: hoe
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.3.2
44
+ version:
45
+ description: |-
46
+ This module allows Ruby programs to interface with the SQLite3
47
+ database engine (http://www.sqlite.org). You must have the
48
+ SQLite engine installed in order to build this module.
49
+
50
+ Note that this module is NOT compatible with SQLite 2.x.
51
+ email:
52
+ - jamis@37signals.com
53
+ executables: []
54
+
55
+ extensions:
56
+ - ext/sqlite3_api/extconf.rb
57
+ extra_rdoc_files:
58
+ - History.txt
59
+ - Manifest.txt
60
+ - README.txt
61
+ files:
62
+ - ChangeLog.cvs
63
+ - History.txt
64
+ - LICENSE
65
+ - Manifest.txt
66
+ - README.txt
67
+ - Rakefile
68
+ - ext/sqlite3_api/extconf.rb
69
+ - ext/sqlite3_api/sqlite3_api.i
70
+ - ext/sqlite3_api/sqlite3_api_wrap.c
71
+ - ext/sqlite3_api/win32/build.bat
72
+ - faq/faq.rb
73
+ - faq/faq.yml
74
+ - lib/sqlite3.rb
75
+ - lib/sqlite3/constants.rb
76
+ - lib/sqlite3/database.rb
77
+ - lib/sqlite3/driver/dl/api.rb
78
+ - lib/sqlite3/driver/dl/driver.rb
79
+ - lib/sqlite3/driver/native/driver.rb
80
+ - lib/sqlite3/errors.rb
81
+ - lib/sqlite3/pragmas.rb
82
+ - lib/sqlite3/resultset.rb
83
+ - lib/sqlite3/statement.rb
84
+ - lib/sqlite3/translator.rb
85
+ - lib/sqlite3/value.rb
86
+ - lib/sqlite3/version.rb
87
+ - setup.rb
88
+ - tasks/benchmark.rake
89
+ - tasks/faq.rake
90
+ - tasks/gem.rake
91
+ - tasks/native.rake
92
+ - tasks/vendor_sqlite3.rake
93
+ - test/bm.rb
94
+ - test/driver/dl/tc_driver.rb
95
+ - test/helper.rb
96
+ - test/native-vs-dl.rb
97
+ - test/test_database.rb
98
+ - test/test_errors.rb
99
+ - test/test_integration.rb
100
+ - test/test_integration_open_close.rb
101
+ - test/test_integration_pending.rb
102
+ - test/test_integration_resultset.rb
103
+ - test/test_integration_statement.rb
104
+ has_rdoc: true
105
+ homepage: http://sqlite3-ruby.rubyforge.org
106
+ post_install_message:
107
+ rdoc_options:
108
+ - --main
109
+ - README.txt
110
+ - --main=README.txt
111
+ require_paths:
112
+ - lib
113
+ - ext
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">"
117
+ - !ruby/object:Gem::Version
118
+ version: 1.8.5
119
+ version:
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: "0"
125
+ version:
126
+ requirements: []
127
+
128
+ rubyforge_project: sqlite-ruby
129
+ rubygems_version: 1.2.0
130
+ signing_key:
131
+ specification_version: 3
132
+ summary: This module allows Ruby programs to interface with the SQLite3 database engine (http://www.sqlite.org)
133
+ test_files:
134
+ - test/test_database.rb
135
+ - test/test_errors.rb
136
+ - test/test_integration.rb
137
+ - test/test_integration_open_close.rb
138
+ - test/test_integration_pending.rb
139
+ - test/test_integration_resultset.rb
140
+ - test/test_integration_statement.rb