test_integration.rb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. require 'helper'
  2. class TC_Database_Integration < SQLite3::TestCase
  3. def setup
  4. @db = SQLite3::Database.new(":memory:")
  5. @db.transaction do
  6. @db.execute "create table foo ( a integer primary key, b text )"
  7. @db.execute "insert into foo ( b ) values ( 'foo' )"
  8. @db.execute "insert into foo ( b ) values ( 'bar' )"
  9. @db.execute "insert into foo ( b ) values ( 'baz' )"
  10. end
  11. end
  12. def teardown
  13. @db.close
  14. end
  15. def test_table_info_with_type_translation_active
  16. assert_nothing_raised { @db.table_info("foo") }
  17. end
  18. def test_table_info_with_defaults_for_version_3_3_8_and_higher
  19. @db.transaction do
  20. @db.execute "create table defaults_test ( a string default NULL, b string default 'Hello' )"
  21. data = @db.table_info( "defaults_test" )
  22. assert_equal({"name" => "a", "type" => "string", "dflt_value" => nil, "notnull" => 0, "cid" => 0, "pk" => 0},
  23. data[0])
  24. assert_equal({"name" => "b", "type" => "string", "dflt_value" => "Hello", "notnull" => 0, "cid" => 1, "pk" => 0},
  25. data[1])
  26. end
  27. end
  28. def test_table_info_without_defaults_for_version_3_3_8_and_higher
  29. @db.transaction do
  30. @db.execute "create table no_defaults_test ( a integer default 1, b integer )"
  31. data = @db.table_info( "no_defaults_test" )
  32. assert_equal({"name" => "a", "type" => "integer", "dflt_value" => "1", "notnull" => 0, "cid" => 0, "pk" => 0},
  33. data[0])
  34. assert_equal({"name" => "b", "type" => "integer", "dflt_value" => nil, "notnull" => 0, "cid" => 1, "pk" => 0},
  35. data[1])
  36. end
  37. end
  38. def test_complete_fail
  39. assert !@db.complete?( "select * from foo" )
  40. end
  41. def test_complete_success
  42. assert @db.complete?( "select * from foo;" )
  43. end
  44. # FIXME: do people really need UTF16 sql statements?
  45. #def test_complete_fail_utf16
  46. # assert !@db.complete?( "select * from foo".to_utf16(false), true )
  47. #end
  48. # FIXME: do people really need UTF16 sql statements?
  49. #def test_complete_success_utf16
  50. # assert @db.complete?( "select * from foo;".to_utf16(true), true )
  51. #end
  52. def test_errmsg
  53. assert_equal "not an error", @db.errmsg
  54. end
  55. # FIXME: do people really need UTF16 error messages?
  56. #def test_errmsg_utf16
  57. # msg = Iconv.conv('UTF-16', 'UTF-8', 'not an error')
  58. # assert_equal msg, @db.errmsg(true)
  59. #end
  60. def test_errcode
  61. assert_equal 0, @db.errcode
  62. end
  63. def test_trace
  64. result = nil
  65. @db.trace { |sql| result = sql }
  66. @db.execute "select * from foo"
  67. assert_equal "select * from foo", result
  68. end
  69. def test_authorizer_okay
  70. @db.authorizer { |type,a,b,c,d| 0 }
  71. rows = @db.execute "select * from foo"
  72. assert_equal 3, rows.length
  73. end
  74. def test_authorizer_error
  75. @db.authorizer { |type,a,b,c,d| 1 }
  76. assert_raise( SQLite3::AuthorizationException ) do
  77. @db.execute "select * from foo"
  78. end
  79. end
  80. def test_authorizer_silent
  81. @db.authorizer { |type,a,b,c,d| 2 }
  82. rows = @db.execute "select * from foo"
  83. assert rows.empty?
  84. end
  85. def test_prepare_invalid_syntax
  86. assert_raise( SQLite3::SQLException ) do
  87. @db.prepare "select from foo"
  88. end
  89. end
  90. def test_prepare_invalid_column
  91. assert_raise( SQLite3::SQLException ) do
  92. @db.prepare "select k from foo"
  93. end
  94. end
  95. def test_prepare_invalid_table
  96. assert_raise( SQLite3::SQLException ) do
  97. @db.prepare "select * from barf"
  98. end
  99. end
  100. def test_prepare_no_block
  101. stmt = @db.prepare "select * from foo"
  102. assert stmt.respond_to?(:execute)
  103. stmt.close
  104. end
  105. def test_prepare_with_block
  106. called = false
  107. @db.prepare "select * from foo" do |stmt|
  108. called = true
  109. assert stmt.respond_to?(:execute)
  110. end
  111. assert called
  112. end
  113. def test_execute_no_block_no_bind_no_match
  114. rows = @db.execute( "select * from foo where a > 100" )
  115. assert rows.empty?
  116. end
  117. def test_execute_with_block_no_bind_no_match
  118. called = false
  119. @db.execute( "select * from foo where a > 100" ) do |row|
  120. called = true
  121. end
  122. assert !called
  123. end
  124. def test_execute_no_block_with_bind_no_match
  125. rows = @db.execute( "select * from foo where a > ?", 100 )
  126. assert rows.empty?
  127. end
  128. def test_execute_with_block_with_bind_no_match
  129. called = false
  130. @db.execute( "select * from foo where a > ?", 100 ) do |row|
  131. called = true
  132. end
  133. assert !called
  134. end
  135. def test_execute_no_block_no_bind_with_match
  136. rows = @db.execute( "select * from foo where a = 1" )
  137. assert_equal 1, rows.length
  138. end
  139. def test_execute_with_block_no_bind_with_match
  140. called = 0
  141. @db.execute( "select * from foo where a = 1" ) do |row|
  142. called += 1
  143. end
  144. assert_equal 1, called
  145. end
  146. def test_execute_no_block_with_bind_with_match
  147. rows = @db.execute( "select * from foo where a = ?", 1 )
  148. assert_equal 1, rows.length
  149. end
  150. def test_execute_with_block_with_bind_with_match
  151. called = 0
  152. @db.execute( "select * from foo where a = ?", 1 ) do |row|
  153. called += 1
  154. end
  155. assert_equal 1, called
  156. end
  157. def test_execute2_no_block_no_bind_no_match
  158. columns, *rows = @db.execute2( "select * from foo where a > 100" )
  159. assert rows.empty?
  160. assert_equal [ "a", "b" ], columns
  161. end
  162. def test_execute2_with_block_no_bind_no_match
  163. called = 0
  164. @db.execute2( "select * from foo where a > 100" ) do |row|
  165. assert [ "a", "b" ], row unless called == 0
  166. called += 1
  167. end
  168. assert_equal 1, called
  169. end
  170. def test_execute2_no_block_with_bind_no_match
  171. columns, *rows = @db.execute2( "select * from foo where a > ?", 100 )
  172. assert rows.empty?
  173. assert_equal [ "a", "b" ], columns
  174. end
  175. def test_execute2_with_block_with_bind_no_match
  176. called = 0
  177. @db.execute2( "select * from foo where a > ?", 100 ) do |row|
  178. assert_equal [ "a", "b" ], row unless called == 0
  179. called += 1
  180. end
  181. assert_equal 1, called
  182. end
  183. def test_execute2_no_block_no_bind_with_match
  184. columns, *rows = @db.execute2( "select * from foo where a = 1" )
  185. assert_equal 1, rows.length
  186. assert_equal [ "a", "b" ], columns
  187. end
  188. def test_execute2_with_block_no_bind_with_match
  189. called = 0
  190. @db.execute2( "select * from foo where a = 1" ) do |row|
  191. assert_equal [ 1, "foo" ], row unless called == 0
  192. called += 1
  193. end
  194. assert_equal 2, called
  195. end
  196. def test_execute2_no_block_with_bind_with_match
  197. columns, *rows = @db.execute2( "select * from foo where a = ?", 1 )
  198. assert_equal 1, rows.length
  199. assert_equal [ "a", "b" ], columns
  200. end
  201. def test_execute2_with_block_with_bind_with_match
  202. called = 0
  203. @db.execute2( "select * from foo where a = ?", 1 ) do |row|
  204. called += 1
  205. end
  206. assert_equal 2, called
  207. end
  208. def test_execute_batch_empty
  209. assert_nothing_raised { @db.execute_batch "" }
  210. end
  211. def test_execute_batch_no_bind
  212. @db.transaction do
  213. @db.execute_batch <<-SQL
  214. create table bar ( a, b, c );
  215. insert into bar values ( 'one', 2, 'three' );
  216. insert into bar values ( 'four', 5, 'six' );
  217. insert into bar values ( 'seven', 8, 'nine' );
  218. SQL
  219. end
  220. rows = @db.execute( "select * from bar" )
  221. assert_equal 3, rows.length
  222. end
  223. def test_execute_batch_with_bind
  224. @db.execute_batch( <<-SQL, [1] )
  225. create table bar ( a, b, c );
  226. insert into bar values ( 'one', 2, ? );
  227. insert into bar values ( 'four', 5, ? );
  228. insert into bar values ( 'seven', 8, ? );
  229. SQL
  230. rows = @db.execute( "select * from bar" ).map { |a,b,c| c }
  231. assert_equal [1, 1, 1], rows
  232. end
  233. def test_query_no_block_no_bind_no_match
  234. result = @db.query( "select * from foo where a > 100" )
  235. assert_nil result.next
  236. result.close
  237. end
  238. def test_query_with_block_no_bind_no_match
  239. r = nil
  240. @db.query( "select * from foo where a > 100" ) do |result|
  241. assert_nil result.next
  242. r = result
  243. end
  244. assert r.closed?
  245. end
  246. def test_query_no_block_with_bind_no_match
  247. result = @db.query( "select * from foo where a > ?", 100 )
  248. assert_nil result.next
  249. result.close
  250. end
  251. def test_query_with_block_with_bind_no_match
  252. r = nil
  253. @db.query( "select * from foo where a > ?", 100 ) do |result|
  254. assert_nil result.next
  255. r = result
  256. end
  257. assert r.closed?
  258. end
  259. def test_query_no_block_no_bind_with_match
  260. result = @db.query( "select * from foo where a = 1" )
  261. assert_not_nil result.next
  262. assert_nil result.next
  263. result.close
  264. end
  265. def test_query_with_block_no_bind_with_match
  266. r = nil
  267. @db.query( "select * from foo where a = 1" ) do |result|
  268. assert_not_nil result.next
  269. assert_nil result.next
  270. r = result
  271. end
  272. assert r.closed?
  273. end
  274. def test_query_no_block_with_bind_with_match
  275. result = @db.query( "select * from foo where a = ?", 1 )
  276. assert_not_nil result.next
  277. assert_nil result.next
  278. result.close
  279. end
  280. def test_query_with_block_with_bind_with_match
  281. r = nil
  282. @db.query( "select * from foo where a = ?", 1 ) do |result|
  283. assert_not_nil result.next
  284. assert_nil result.next
  285. r = result
  286. end
  287. assert r.closed?
  288. end
  289. def test_get_first_row_no_bind_no_match
  290. result = @db.get_first_row( "select * from foo where a=100" )
  291. assert_nil result
  292. end
  293. def test_get_first_row_no_bind_with_match
  294. result = @db.get_first_row( "select * from foo where a=1" )
  295. assert_equal [ 1, "foo" ], result
  296. end
  297. def test_get_first_row_with_bind_no_match
  298. result = @db.get_first_row( "select * from foo where a=?", 100 )
  299. assert_nil result
  300. end
  301. def test_get_first_row_with_bind_with_match
  302. result = @db.get_first_row( "select * from foo where a=?", 1 )
  303. assert_equal [ 1, "foo" ], result
  304. end
  305. def test_get_first_value_no_bind_no_match
  306. result = @db.get_first_value( "select b, a from foo where a=100" )
  307. assert_nil result
  308. end
  309. def test_get_first_value_no_bind_with_match
  310. result = @db.get_first_value( "select b, a from foo where a=1" )
  311. assert_equal "foo", result
  312. end
  313. def test_get_first_value_with_bind_no_match
  314. result = @db.get_first_value( "select b, a from foo where a=?", 100 )
  315. assert_nil result
  316. end
  317. def test_get_first_value_with_bind_with_match
  318. result = @db.get_first_value( "select b, a from foo where a=?", 1 )
  319. assert_equal "foo", result
  320. end
  321. def test_last_insert_row_id
  322. @db.execute "insert into foo ( b ) values ( 'test' )"
  323. assert_equal 4, @db.last_insert_row_id
  324. @db.execute "insert into foo ( b ) values ( 'again' )"
  325. assert_equal 5, @db.last_insert_row_id
  326. end
  327. def test_changes
  328. @db.execute "insert into foo ( b ) values ( 'test' )"
  329. assert_equal 1, @db.changes
  330. @db.execute "delete from foo where 1=1"
  331. assert_equal 4, @db.changes
  332. end
  333. def test_total_changes
  334. assert_equal 3, @db.total_changes
  335. @db.execute "insert into foo ( b ) values ( 'test' )"
  336. @db.execute "delete from foo where 1=1"
  337. assert_equal 8, @db.total_changes
  338. end
  339. def test_transaction_nest
  340. assert_raise( SQLite3::SQLException ) do
  341. @db.transaction do
  342. @db.transaction do
  343. end
  344. end
  345. end
  346. end
  347. def test_transaction_rollback
  348. @db.transaction
  349. @db.execute_batch <<-SQL
  350. insert into foo (b) values ( 'test1' );
  351. insert into foo (b) values ( 'test2' );
  352. insert into foo (b) values ( 'test3' );
  353. insert into foo (b) values ( 'test4' );
  354. SQL
  355. assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
  356. @db.rollback
  357. assert_equal 3, @db.get_first_value("select count(*) from foo").to_i
  358. end
  359. def test_transaction_commit
  360. @db.transaction
  361. @db.execute_batch <<-SQL
  362. insert into foo (b) values ( 'test1' );
  363. insert into foo (b) values ( 'test2' );
  364. insert into foo (b) values ( 'test3' );
  365. insert into foo (b) values ( 'test4' );
  366. SQL
  367. assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
  368. @db.commit
  369. assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
  370. end
  371. def test_transaction_rollback_in_block
  372. assert_raise( SQLite3::SQLException ) do
  373. @db.transaction do
  374. @db.rollback
  375. end
  376. end
  377. end
  378. def test_transaction_commit_in_block
  379. assert_raise( SQLite3::SQLException ) do
  380. @db.transaction do
  381. @db.commit
  382. end
  383. end
  384. end
  385. def test_transaction_active
  386. assert !@db.transaction_active?
  387. @db.transaction
  388. assert @db.transaction_active?
  389. @db.commit
  390. assert !@db.transaction_active?
  391. end
  392. def test_transaction_implicit_rollback
  393. assert !@db.transaction_active?
  394. @db.transaction
  395. @db.execute('create table bar (x CHECK(1 = 0))')
  396. assert @db.transaction_active?
  397. assert_raises( SQLite3::ConstraintException ) do
  398. @db.execute("insert or rollback into bar (x) VALUES ('x')")
  399. end
  400. assert !@db.transaction_active?
  401. end
  402. def test_interrupt
  403. @db.create_function( "abort", 1 ) do |func,x|
  404. @db.interrupt
  405. func.result = x
  406. end
  407. assert_raise( SQLite3::InterruptException ) do
  408. @db.execute "select abort(a) from foo"
  409. end
  410. end
  411. def test_create_function
  412. @db.create_function( "munge", 1 ) do |func,x|
  413. func.result = ">>>#{x}<<<"
  414. end
  415. value = @db.get_first_value( "select munge(b) from foo where a=1" )
  416. assert_match( />>>.*<<</, value )
  417. end
  418. def test_create_aggregate_without_block
  419. step = proc do |ctx,a|
  420. ctx[:sum] ||= 0
  421. ctx[:sum] += a.to_i
  422. end
  423. final = proc { |ctx| ctx.result = ctx[:sum] }
  424. @db.create_aggregate( "accumulate", 1, step, final )
  425. value = @db.get_first_value( "select accumulate(a) from foo" )
  426. assert_equal 6, value
  427. end
  428. def test_create_aggregate_with_block
  429. @db.create_aggregate( "accumulate", 1 ) do
  430. step do |ctx,a|
  431. ctx[:sum] ||= 0
  432. ctx[:sum] += a.to_i
  433. end
  434. finalize { |ctx| ctx.result = ctx[:sum] }
  435. end
  436. value = @db.get_first_value( "select accumulate(a) from foo" )
  437. assert_equal 6, value
  438. end
  439. def test_create_aggregate_with_no_data
  440. @db.create_aggregate( "accumulate", 1 ) do
  441. step do |ctx,a|
  442. ctx[:sum] ||= 0
  443. ctx[:sum] += a.to_i
  444. end
  445. finalize { |ctx| ctx.result = ctx[:sum] || 0 }
  446. end
  447. value = @db.get_first_value(
  448. "select accumulate(a) from foo where a = 100" )
  449. assert_equal 0, value
  450. end
  451. def test_create_aggregate_handler
  452. handler = Class.new do
  453. class << self
  454. def arity; 1; end
  455. def text_rep; SQLite3::Constants::TextRep::ANY; end
  456. def name; "multiply"; end
  457. end
  458. def step(ctx, a)
  459. ctx[:buffer] ||= 1
  460. ctx[:buffer] *= a.to_i
  461. end
  462. def finalize(ctx); ctx.result = ctx[:buffer]; end
  463. end
  464. @db.create_aggregate_handler( handler )
  465. value = @db.get_first_value( "select multiply(a) from foo" )
  466. assert_equal 6, value
  467. end
  468. def test_bind_array_parameter
  469. result = @db.get_first_value( "select b from foo where a=? and b=?",
  470. [ 1, "foo" ] )
  471. assert_equal "foo", result
  472. end
  473. end