test_statement.rb 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. require 'helper'
  2. module SQLite3
  3. class TestStatement < SQLite3::TestCase
  4. def setup
  5. @db = SQLite3::Database.new(':memory:')
  6. @stmt = SQLite3::Statement.new(@db, "select 'foo'")
  7. end
  8. def test_raises_type_error
  9. assert_raises(TypeError) do
  10. SQLite3::Statement.new( @db, nil )
  11. end
  12. end
  13. ###
  14. # This method may not exist depending on how sqlite3 was compiled
  15. def test_database_name
  16. @db.execute('create table foo(text BLOB)')
  17. @db.execute('insert into foo(text) values (?)',SQLite3::Blob.new('hello'))
  18. stmt = @db.prepare('select text from foo')
  19. if stmt.respond_to?(:database_name)
  20. assert_equal 'main', stmt.database_name(0)
  21. end
  22. end
  23. def test_prepare_blob
  24. @db.execute('create table foo(text BLOB)')
  25. stmt = @db.prepare('insert into foo(text) values (?)')
  26. stmt.bind_param(1, SQLite3::Blob.new('hello'))
  27. stmt.step
  28. stmt.close
  29. end
  30. def test_select_blob
  31. @db.execute('create table foo(text BLOB)')
  32. @db.execute('insert into foo(text) values (?)',SQLite3::Blob.new('hello'))
  33. assert_equal 'hello', @db.execute('select * from foo').first.first
  34. end
  35. def test_new
  36. assert @stmt
  37. end
  38. def test_new_closed_handle
  39. @db = SQLite3::Database.new(':memory:')
  40. @db.close
  41. assert_raises(ArgumentError) do
  42. SQLite3::Statement.new(@db, 'select "foo"')
  43. end
  44. end
  45. def test_new_with_remainder
  46. stmt = SQLite3::Statement.new(@db, "select 'foo';bar")
  47. assert_equal 'bar', stmt.remainder
  48. end
  49. def test_empty_remainder
  50. assert_equal '', @stmt.remainder
  51. end
  52. def test_close
  53. @stmt.close
  54. assert @stmt.closed?
  55. end
  56. def test_double_close
  57. @stmt.close
  58. assert_raises(SQLite3::Exception) do
  59. @stmt.close
  60. end
  61. end
  62. def test_bind_param_string
  63. stmt = SQLite3::Statement.new(@db, "select ?")
  64. stmt.bind_param(1, "hello")
  65. result = nil
  66. stmt.each { |x| result = x }
  67. assert_equal ['hello'], result
  68. end
  69. def test_bind_param_int
  70. stmt = SQLite3::Statement.new(@db, "select ?")
  71. stmt.bind_param(1, 10)
  72. result = nil
  73. stmt.each { |x| result = x }
  74. assert_equal [10], result
  75. end
  76. def test_bind_nil
  77. stmt = SQLite3::Statement.new(@db, "select ?")
  78. stmt.bind_param(1, nil)
  79. result = nil
  80. stmt.each { |x| result = x }
  81. assert_equal [nil], result
  82. end
  83. def test_bind_blobs
  84. end
  85. def test_bind_64
  86. stmt = SQLite3::Statement.new(@db, "select ?")
  87. stmt.bind_param(1, 2 ** 31)
  88. result = nil
  89. stmt.each { |x| result = x }
  90. assert_equal [2 ** 31], result
  91. end
  92. def test_bind_double
  93. stmt = SQLite3::Statement.new(@db, "select ?")
  94. stmt.bind_param(1, 2.2)
  95. result = nil
  96. stmt.each { |x| result = x }
  97. assert_equal [2.2], result
  98. end
  99. def test_named_bind
  100. stmt = SQLite3::Statement.new(@db, "select :foo")
  101. stmt.bind_param(':foo', 'hello')
  102. result = nil
  103. stmt.each { |x| result = x }
  104. assert_equal ['hello'], result
  105. end
  106. def test_named_bind_no_colon
  107. stmt = SQLite3::Statement.new(@db, "select :foo")
  108. stmt.bind_param('foo', 'hello')
  109. result = nil
  110. stmt.each { |x| result = x }
  111. assert_equal ['hello'], result
  112. end
  113. def test_named_bind_symbol
  114. stmt = SQLite3::Statement.new(@db, "select :foo")
  115. stmt.bind_param(:foo, 'hello')
  116. result = nil
  117. stmt.each { |x| result = x }
  118. assert_equal ['hello'], result
  119. end
  120. def test_named_bind_not_found
  121. stmt = SQLite3::Statement.new(@db, "select :foo")
  122. assert_raises(SQLite3::Exception) do
  123. stmt.bind_param('bar', 'hello')
  124. end
  125. end
  126. def test_each
  127. r = nil
  128. @stmt.each do |row|
  129. r = row
  130. end
  131. assert_equal(['foo'], r)
  132. end
  133. def test_reset!
  134. r = []
  135. @stmt.each { |row| r << row }
  136. @stmt.reset!
  137. @stmt.each { |row| r << row }
  138. assert_equal [['foo'], ['foo']], r
  139. end
  140. def test_step
  141. r = @stmt.step
  142. assert_equal ['foo'], r
  143. end
  144. def test_tainted
  145. r = @stmt.step
  146. assert r.first.tainted?
  147. end
  148. def test_step_twice
  149. assert_not_nil @stmt.step
  150. assert !@stmt.done?
  151. assert_nil @stmt.step
  152. assert @stmt.done?
  153. @stmt.reset!
  154. assert !@stmt.done?
  155. end
  156. def test_step_never_moves_past_done
  157. 10.times { @stmt.step }
  158. @stmt.done?
  159. end
  160. def test_column_count
  161. assert_equal 1, @stmt.column_count
  162. end
  163. def test_column_name
  164. assert_equal "'foo'", @stmt.column_name(0)
  165. assert_equal nil, @stmt.column_name(10)
  166. end
  167. def test_bind_parameter_count
  168. stmt = SQLite3::Statement.new(@db, "select ?, ?, ?")
  169. assert_equal 3, stmt.bind_parameter_count
  170. end
  171. def test_execute_with_varargs
  172. stmt = @db.prepare('select ?, ?')
  173. assert_equal [[nil, nil]], stmt.execute(nil, nil).to_a
  174. end
  175. def test_execute_with_hash
  176. stmt = @db.prepare('select :n, :h')
  177. assert_equal [[10, nil]], stmt.execute('n' => 10, 'h' => nil).to_a
  178. end
  179. def test_with_error
  180. @db.execute('CREATE TABLE "employees" ("name" varchar(20) NOT NULL CONSTRAINT "index_employees_on_name" UNIQUE)')
  181. stmt = @db.prepare("INSERT INTO Employees(name) VALUES(?)")
  182. stmt.execute('employee-1')
  183. stmt.execute('employee-1') rescue SQLite3::ConstraintException
  184. stmt.reset!
  185. assert_nothing_raised(SQLite3::ConstraintException) {
  186. stmt.execute('employee-2')
  187. }
  188. end
  189. def test_clear_bindings
  190. stmt = @db.prepare('select ?, ?')
  191. stmt.bind_param 1, "foo"
  192. stmt.bind_param 2, "bar"
  193. # We can't fetch bound parameters back out of sqlite3, so just call
  194. # the clear_bindings! method and assert that nil is returned
  195. stmt.clear_bindings!
  196. while x = stmt.step
  197. assert_equal [nil, nil], x
  198. end
  199. end
  200. end
  201. end