Class: TC_Statement

Inherits:
SQLite3::TestCase show all
Defined in:
lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb,
lib/sqlite3-1.5.3-x86_64-darwin/test/test_integration_statement.rb

Instance Method Summary collapse

Methods inherited from SQLite3::TestCase

#assert_nothing_raised

Instance Method Details

#setupObject



4
5
6
7
8
9
10
11
12
13
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 4

def setup
  @db = SQLite3::Database.new(":memory:")
  @db.transaction do
    @db.execute "create table foo ( a integer primary key, b text )"
    @db.execute "insert into foo ( b ) values ( 'foo' )"
    @db.execute "insert into foo ( b ) values ( 'bar' )"
    @db.execute "insert into foo ( b ) values ( 'baz' )"
  end
  @stmt = @db.prepare( "select * from foo where a in ( ?, :named )" )
end

#teardownObject



15
16
17
18
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 15

def teardown
  @stmt.close
  @db.close
end

#test_bind_param_by_indexObject



63
64
65
66
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 63

def test_bind_param_by_index
  @stmt.bind_params( 1, 2 )
  assert_equal 2, @stmt.execute!.length
end

#test_bind_param_by_name_badObject



68
69
70
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 68

def test_bind_param_by_name_bad
  assert_raise( SQLite3::Exception ) { @stmt.bind_param( "@named", 2 ) }
end

#test_bind_param_by_name_goodObject



72
73
74
75
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 72

def test_bind_param_by_name_good
  @stmt.bind_param( ":named", 2 )
  assert_equal 1, @stmt.execute!.length
end

#test_bind_param_with_various_typesObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 77

def test_bind_param_with_various_types
  @db.transaction do
    @db.execute "create table all_types ( a integer primary key, b float, c string, d integer )"
    @db.execute "insert into all_types ( b, c, d ) values ( 1.4, 'hello', 68719476735 )"
  end

  assert_equal 1, @db.execute( "select * from all_types where b = ?", 1.4 ).length
  assert_equal 1, @db.execute( "select * from all_types where c = ?", 'hello').length
  assert_equal 1, @db.execute( "select * from all_types where d = ?", 68719476735).length
end

#test_bind_params_arrayObject



38
39
40
41
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 38

def test_bind_params_array
  @stmt.bind_params 1, 2
  assert_equal 2, @stmt.execute!.length
end

#test_bind_params_emptyObject



33
34
35
36
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 33

def test_bind_params_empty
  assert_nothing_raised { @stmt.bind_params }
  assert @stmt.execute!.empty?
end

#test_bind_params_hashObject



43
44
45
46
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 43

def test_bind_params_hash
  @stmt.bind_params ":named" => 2
  assert_equal 1, @stmt.execute!.length
end

#test_bind_params_hash_as_symbolObject



53
54
55
56
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 53

def test_bind_params_hash_as_symbol
  @stmt.bind_params :named => 2
  assert_equal 1, @stmt.execute!.length
end

#test_bind_params_hash_without_colonObject



48
49
50
51
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 48

def test_bind_params_hash_without_colon
  @stmt.bind_params "named" => 2
  assert_equal 1, @stmt.execute!.length
end

#test_bind_params_mixedObject



58
59
60
61
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 58

def test_bind_params_mixed
  @stmt.bind_params( 1, ":named" => 2 )
  assert_equal 2, @stmt.execute!.length
end

#test_closeObject



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 167

def test_close
  stmt = @db.prepare( "select * from foo" )
  assert !stmt.closed?
  stmt.close
  assert stmt.closed?
  assert_raise( SQLite3::Exception ) { stmt.execute }
  assert_raise( SQLite3::Exception ) { stmt.execute! }
  assert_raise( SQLite3::Exception ) { stmt.close }
  assert_raise( SQLite3::Exception ) { stmt.bind_params 5 }
  assert_raise( SQLite3::Exception ) { stmt.bind_param 1, 5 }
  assert_raise( SQLite3::Exception ) { stmt.columns }
  assert_raise( SQLite3::Exception ) { stmt.types }
end

#test_columnsObject



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

def test_columns
  c1 = @stmt.columns
  c2 = @stmt.columns
  assert_same c1, c2
  assert_equal 2, c1.length
end

#test_columns_computedObject



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

def test_columns_computed
  called = false
  @db.prepare( "select count(*) from foo" ) do |stmt|
    called = true
    assert_equal [ "count(*)" ], stmt.columns
  end
  assert called
end

#test_committing_tx_with_statement_activeObject



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 181

def test_committing_tx_with_statement_active
  called = false
  @db.prepare( "select count(*) from foo" ) do |stmt|
    called = true
    count = stmt.execute!.first.first.to_i
    @db.transaction do
      @db.execute "insert into foo ( b ) values ( 'hello' )"
    end
    new_count = stmt.execute!.first.first.to_i
    assert_equal new_count, count+1
  end
  assert called
end

#test_execute_bang_no_bind_no_blockObject



115
116
117
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 115

def test_execute_bang_no_bind_no_block
  assert @stmt.execute!.empty?
end

#test_execute_bang_no_bind_with_blockObject



123
124
125
126
127
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 123

def test_execute_bang_no_bind_with_block
  called = 0
  @stmt.execute! { |row| called += 1 }
  assert_equal 0, called
end

#test_execute_bang_with_bind_no_blockObject



119
120
121
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 119

def test_execute_bang_with_bind_no_block
  assert_equal 2, @stmt.execute!( 1, 2 ).length
end

#test_execute_bang_with_bind_with_blockObject



129
130
131
132
133
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 129

def test_execute_bang_with_bind_with_block
  called = 0
  @stmt.execute!( 1, 2 ) { |row| called += 1 }
  assert_equal 2, called
end

#test_execute_no_bind_no_blockObject



88
89
90
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 88

def test_execute_no_bind_no_block
  assert_instance_of SQLite3::ResultSet, @stmt.execute
end

#test_execute_no_bind_with_blockObject



96
97
98
99
100
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 96

def test_execute_no_bind_with_block
  called = false
  @stmt.execute { |row| called = true }
  assert called
end

#test_execute_with_bind_no_blockObject



92
93
94
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 92

def test_execute_with_bind_no_block
  assert_instance_of SQLite3::ResultSet, @stmt.execute( 1, 2 )
end

#test_execute_with_bind_with_blockObject



102
103
104
105
106
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 102

def test_execute_with_bind_with_block
  called = 0
  @stmt.execute( 1, 2 ) { |row| called += 1 }
  assert_equal 1, called
end

#test_reexecuteObject



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

def test_reexecute
  r = @stmt.execute( 1, 2 )
  assert_equal 2, r.to_a.length
  assert_nothing_raised { r = @stmt.execute( 1, 2 ) }
  assert_equal 2, r.to_a.length
end

#test_remainder_emptyObject



20
21
22
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 20

def test_remainder_empty
  assert_equal "", @stmt.remainder
end

#test_remainder_nonemptyObject



24
25
26
27
28
29
30
31
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_integration_statement.rb', line 24

def test_remainder_nonempty
  called = false
  @db.prepare( "select * from foo;\n blah" ) do |stmt|
    called = true
    assert_equal "\n blah", stmt.remainder
  end
  assert called
end

#test_typesObject



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

def test_types
  t1 = @stmt.types
  t2 = @stmt.types
  assert_same t1, t2
  assert_equal 2, t1.length
end

#test_types_computedObject



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

def test_types_computed
  called = false
  @db.prepare( "select count(*) from foo" ) do |stmt|
    called = true
    assert_equal [ nil ], stmt.types
  end
  assert called
end