Class: SQLite3::TestStatement

Inherits:
TestCase
  • Object
show all
Defined in:
lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb,
lib/sqlite3-1.5.3-x86_64-darwin/test/test_statement.rb

Instance Method Summary collapse

Methods inherited from TestCase

#assert_nothing_raised

Instance Method Details

#setupObject



5
6
7
8
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 5

def setup
  @db   = SQLite3::Database.new(':memory:')
  @stmt = SQLite3::Statement.new(@db, "select 'foo'")
end

#test_bind_64Object



133
134
135
136
137
138
139
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 133

def test_bind_64
  stmt = SQLite3::Statement.new(@db, "select ?")
  stmt.bind_param(1, 2 ** 31)
  result = nil
  stmt.each { |x| result = x }
  assert_equal [2 ** 31], result
end

#test_bind_blobObject



122
123
124
125
126
127
128
129
130
131
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 122

def test_bind_blob
  @db.execute('create table foo(text BLOB)')
  stmt = SQLite3::Statement.new(@db, 'insert into foo(text) values (?)')
  stmt.bind_param(1, SQLite3::Blob.new('hello'))
  stmt.execute
  row = @db.execute('select * from foo')

  assert_equal ['hello'], row.first
  assert_equal ['blob'], row.first.types
end

#test_bind_doubleObject



141
142
143
144
145
146
147
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 141

def test_bind_double
  stmt = SQLite3::Statement.new(@db, "select ?")
  stmt.bind_param(1, 2.2)
  result = nil
  stmt.each { |x| result = x }
  assert_equal [2.2], result
end

#test_bind_nilObject



114
115
116
117
118
119
120
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 114

def test_bind_nil
  stmt = SQLite3::Statement.new(@db, "select ?")
  stmt.bind_param(1, nil)
  result = nil
  stmt.each { |x| result = x }
  assert_equal [nil], result
end

#test_bind_param_intObject



106
107
108
109
110
111
112
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 106

def test_bind_param_int
  stmt = SQLite3::Statement.new(@db, "select ?")
  stmt.bind_param(1, 10)
  result = nil
  stmt.each { |x| result = x }
  assert_equal [10], result
end

#test_bind_param_stringObject



98
99
100
101
102
103
104
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 98

def test_bind_param_string
  stmt = SQLite3::Statement.new(@db, "select ?")
  stmt.bind_param(1, "hello")
  result = nil
  stmt.each { |x| result = x }
  assert_equal ['hello'], result
end

#test_bind_parameter_countObject



225
226
227
228
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 225

def test_bind_parameter_count
  stmt = SQLite3::Statement.new(@db, "select ?, ?, ?")
  assert_equal 3, stmt.bind_parameter_count
end

#test_clear_bindings!Object



249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 249

def test_clear_bindings!
  stmt = @db.prepare('select ?, ?')
  stmt.bind_param 1, "foo"
  stmt.bind_param 2, "bar"

  # We can't fetch bound parameters back out of sqlite3, so just call
  # the clear_bindings! method and assert that nil is returned
  stmt.clear_bindings!

  while x = stmt.step
    assert_equal [nil, nil], x
  end
end

#test_closeObject



86
87
88
89
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 86

def test_close
  @stmt.close
  assert @stmt.closed?
end

#test_column_countObject



216
217
218
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 216

def test_column_count
  assert_equal 1, @stmt.column_count
end

#test_column_nameObject



220
221
222
223
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 220

def test_column_name
  assert_equal "'foo'", @stmt.column_name(0)
  assert_nil @stmt.column_name(10)
end

#test_database_nameObject

This method may not exist depending on how sqlite3 was compiled



42
43
44
45
46
47
48
49
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 42

def test_database_name
  @db.execute('create table foo(text BLOB)')
  @db.execute('insert into foo(text) values (?)',SQLite3::Blob.new('hello'))
  stmt = @db.prepare('select text from foo')
  if stmt.respond_to?(:database_name)
    assert_equal 'main', stmt.database_name(0)
  end
end

#test_double_closeObject



91
92
93
94
95
96
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 91

def test_double_close
  @stmt.close
  assert_raises(SQLite3::Exception) do
    @stmt.close
  end
end

#test_double_close_does_not_segvObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 10

def test_double_close_does_not_segv
  @db.execute 'CREATE TABLE "things" ("number" float NOT NULL)'

  stmt = @db.prepare 'INSERT INTO things (number) VALUES (?)'
  assert_raises(SQLite3::ConstraintException) { stmt.execute(nil) }

  stmt.close

  assert_raises(SQLite3::Exception) { stmt.close }
end

#test_eachObject



180
181
182
183
184
185
186
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 180

def test_each
  r = nil
  @stmt.each do |row|
    r = row
  end
  assert_equal(['foo'], r)
end

#test_empty_remainderObject



82
83
84
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 82

def test_empty_remainder
  assert_equal '', @stmt.remainder
end

#test_execute_with_hashObject



235
236
237
238
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 235

def test_execute_with_hash
  stmt = @db.prepare('select :n, :h')
  assert_equal [[10, nil]], stmt.execute('n' => 10, 'h' => nil).to_a
end

#test_execute_with_varargsObject



230
231
232
233
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 230

def test_execute_with_varargs
  stmt = @db.prepare('select ?, ?')
  assert_equal [[nil, nil]], stmt.execute(nil, nil).to_a
end

#test_insert_duplicate_recordsObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 27

def test_insert_duplicate_records
  @db.execute 'CREATE TABLE "things" ("name" varchar(20) CONSTRAINT "index_things_on_name" UNIQUE)'
  stmt = @db.prepare("INSERT INTO things(name) VALUES(?)")
  stmt.execute('ruby')

  exception = assert_raises(SQLite3::ConstraintException) { stmt.execute('ruby') }
  # SQLite 3.8.2 returns new error message:
  #   UNIQUE constraint failed: *table_name*.*column_name*
  # Older versions of SQLite return:
  #   column *column_name* is not unique
  assert_match(/(column(s)? .* (is|are) not unique|UNIQUE constraint failed: .*)/, exception.message)
end

#test_named_bindObject



149
150
151
152
153
154
155
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 149

def test_named_bind
  stmt = SQLite3::Statement.new(@db, "select :foo")
  stmt.bind_param(':foo', 'hello')
  result = nil
  stmt.each { |x| result = x }
  assert_equal ['hello'], result
end

#test_named_bind_no_colonObject



157
158
159
160
161
162
163
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 157

def test_named_bind_no_colon
  stmt = SQLite3::Statement.new(@db, "select :foo")
  stmt.bind_param('foo', 'hello')
  result = nil
  stmt.each { |x| result = x }
  assert_equal ['hello'], result
end

#test_named_bind_not_foundObject



173
174
175
176
177
178
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 173

def test_named_bind_not_found
  stmt = SQLite3::Statement.new(@db, "select :foo")
  assert_raises(SQLite3::Exception) do
    stmt.bind_param('bar', 'hello')
  end
end

#test_named_bind_symbolObject



165
166
167
168
169
170
171
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 165

def test_named_bind_symbol
  stmt = SQLite3::Statement.new(@db, "select :foo")
  stmt.bind_param(:foo, 'hello')
  result = nil
  stmt.each { |x| result = x }
  assert_equal ['hello'], result
end

#test_newObject



65
66
67
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 65

def test_new
  assert @stmt
end

#test_new_closed_handleObject



69
70
71
72
73
74
75
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 69

def test_new_closed_handle
  @db = SQLite3::Database.new(':memory:')
  @db.close
  assert_raises(ArgumentError) do
    SQLite3::Statement.new(@db, 'select "foo"')
  end
end

#test_new_with_remainderObject



77
78
79
80
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 77

def test_new_with_remainder
  stmt = SQLite3::Statement.new(@db, "select 'foo';bar")
  assert_equal 'bar', stmt.remainder
end

#test_prepare_blobObject



51
52
53
54
55
56
57
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 51

def test_prepare_blob
  @db.execute('create table foo(text BLOB)')
  stmt = @db.prepare('insert into foo(text) values (?)')
  stmt.bind_param(1, SQLite3::Blob.new('hello'))
  stmt.step
  stmt.close
end

#test_raises_type_errorObject



21
22
23
24
25
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 21

def test_raises_type_error
  assert_raises(TypeError) do
    SQLite3::Statement.new( @db, nil )
  end
end

#test_reset!Object



188
189
190
191
192
193
194
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 188

def test_reset!
  r = []
  @stmt.each { |row| r << row }
  @stmt.reset!
  @stmt.each { |row| r << row }
  assert_equal [['foo'], ['foo']], r
end

#test_select_blobObject



59
60
61
62
63
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 59

def test_select_blob
  @db.execute('create table foo(text BLOB)')
  @db.execute('insert into foo(text) values (?)',SQLite3::Blob.new('hello'))
  assert_equal 'hello', @db.execute('select * from foo').first.first
end

#test_stepObject



196
197
198
199
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 196

def test_step
  r = @stmt.step
  assert_equal ['foo'], r
end

#test_step_never_moves_past_doneObject



211
212
213
214
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 211

def test_step_never_moves_past_done
  10.times { @stmt.step }
  @stmt.done?
end

#test_step_twiceObject



201
202
203
204
205
206
207
208
209
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 201

def test_step_twice
  assert_not_nil @stmt.step
  assert !@stmt.done?
  assert_nil @stmt.step
  assert @stmt.done?

  @stmt.reset!
  assert !@stmt.done?
end

#test_with_errorObject



240
241
242
243
244
245
246
247
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_statement.rb', line 240

def test_with_error
  @db.execute('CREATE TABLE "employees" ("name" varchar(20) NOT NULL CONSTRAINT "index_employees_on_name" UNIQUE)')
  stmt = @db.prepare("INSERT INTO Employees(name) VALUES(?)")
  stmt.execute('employee-1')
  stmt.execute('employee-1') rescue SQLite3::ConstraintException
  stmt.reset!
  assert stmt.execute('employee-2')
end