PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/gems/mysql-2.7/test.rb

https://github.com/tonibergholm/Webiva
Ruby | 1359 lines | 1349 code | 8 blank | 2 comment | 2 complexity | d8d19a57801ddb559aed1b0d8752d345 MD5 | raw file
Possible License(s): AGPL-3.0, GPL-2.0, MIT
  1. #!/usr/local/bin/ruby
  2. # $Id: test.rb,v 1.16 2005/08/21 15:12:34 tommy Exp $
  3. require "test/unit"
  4. require "./mysql.o"
  5. class TC_Mysql < Test::Unit::TestCase
  6. def setup()
  7. @host, @user, @pass, db, port, sock, flag = ARGV
  8. @db = db || "test"
  9. @port = port.to_i
  10. @sock = sock.nil? || sock.empty? ? nil : sock
  11. @flag = flag.to_i
  12. end
  13. def teardown()
  14. end
  15. def test_version()
  16. assert_equal(20700, Mysql::VERSION)
  17. end
  18. def test_init()
  19. assert_nothing_raised{@m = Mysql.init}
  20. assert_nothing_raised{@m.close}
  21. end
  22. def test_real_connect()
  23. assert_nothing_raised{@m = Mysql.real_connect(@host, @user, @pass, @db, @port, @sock, @flag)}
  24. assert_nothing_raised{@m.close}
  25. end
  26. def test_connect()
  27. assert_nothing_raised{@m = Mysql.connect(@host, @user, @pass, @db, @port, @sock, @flag)}
  28. assert_nothing_raised{@m.close}
  29. end
  30. def test_new()
  31. assert_nothing_raised{@m = Mysql.new(@host, @user, @pass, @db, @port, @sock, @flag)}
  32. assert_nothing_raised{@m.close}
  33. end
  34. def test_escape_string()
  35. assert_equal("abc\\'def\\\"ghi\\0jkl%mno", Mysql.escape_string("abc'def\"ghi\0jkl%mno"))
  36. end
  37. def test_quote()
  38. assert_equal("abc\\'def\\\"ghi\\0jkl%mno", Mysql.quote("abc'def\"ghi\0jkl%mno"))
  39. end
  40. def test_get_client_info()
  41. assert_match(/^\d.\d+.\d+(-.*)?$/, Mysql.get_client_info())
  42. end
  43. def test_client_info()
  44. assert_match(/^\d.\d+.\d+(-.*)?$/, Mysql.client_info())
  45. end
  46. def test_options()
  47. @m = Mysql.init
  48. assert_equal(@m, @m.options(Mysql::INIT_COMMAND, "SET AUTOCOMMIT=0"))
  49. assert_equal(@m, @m.options(Mysql::OPT_COMPRESS))
  50. assert_equal(@m, @m.options(Mysql::OPT_CONNECT_TIMEOUT, 10))
  51. assert_equal(@m, @m.options(Mysql::GUESS_CONNECTION)) if defined? Mysql::GUESS_CONNECTION
  52. assert_equal(@m, @m.options(Mysql::OPT_LOCAL_INFILE, true))
  53. # assert_equal(@m, @m.options(Mysql::OPT_NAMED_PIPE))
  54. # assert_equal(@m, @m.options(Mysql::OPT_PROTOCOL, 1))
  55. assert_equal(@m, @m.options(Mysql::OPT_READ_TIMEOUT, 10)) if defined? Mysql::OPT_READ_TIMEOUT
  56. assert_equal(@m, @m.options(Mysql::OPT_USE_EMBEDDED_CONNECTION)) if defined? Mysql::OPT_USE_EMBEDDED_CONNECTION
  57. assert_equal(@m, @m.options(Mysql::OPT_USE_REMOTE_CONNECTION)) if defined? Mysql::OPT_USE_REMOTE_CONNECTION
  58. assert_equal(@m, @m.options(Mysql::OPT_WRITE_TIMEOUT, 10)) if defined? Mysql::OPT_WRITE_TIMEOUT
  59. # assert_equal(@m, @m.options(Mysql::READ_DEFAULT_FILE, "/tmp/hoge"))
  60. assert_equal(@m, @m.options(Mysql::READ_DEFAULT_GROUP, "test"))
  61. assert_equal(@m, @m.options(Mysql::SECURE_AUTH, true)) if defined? Mysql::SECURE_AUTH
  62. # assert_equal(@m, @m.options(Mysql::SET_CHARSET_DIR, "??"))
  63. assert_equal(@m, @m.options(Mysql::SET_CHARSET_NAME, "latin1"))
  64. assert_equal(@m, @m.options(Mysql::SET_CLIENT_IP, "127.0.0.1")) if defined? Mysql::SET_CLIENT_IP
  65. # assert_equal(@m, @m.options(Mysql::SHARED_MEMORY_BASE_NAME, "xxx"))
  66. assert_equal(@m, @m.connect(@host, @user, @pass, @db, @port, @sock, @flag))
  67. @m.close
  68. end
  69. def test_real_connect2()
  70. @m = Mysql.init
  71. assert_equal(@m, @m.real_connect(@host, @user, @pass, @db, @port, @sock, @flag))
  72. @m.close
  73. end
  74. def test_connect2()
  75. @m = Mysql.init
  76. assert_equal(@m, @m.connect(@host, @user, @pass, @db, @port, @sock, @flag))
  77. @m.close
  78. end
  79. end
  80. class TC_Mysql2 < Test::Unit::TestCase
  81. def setup()
  82. @host, @user, @pass, db, port, sock, flag = ARGV
  83. @db = db || "test"
  84. @port = port.to_i
  85. @sock = sock.nil? || sock.empty? ? nil : sock
  86. @flag = flag.to_i
  87. @m = Mysql.new(@host, @user, @pass, @db, @port, @sock, @flag)
  88. end
  89. def teardown()
  90. @m.close
  91. end
  92. def test_affected_rows()
  93. @m.query("create temporary table t (id int)")
  94. @m.query("insert into t values (1)")
  95. assert_equal(1, @m.affected_rows)
  96. end
  97. def test_autocommit()
  98. if @m.methods.include? "autocommit" then
  99. assert_equal(@m, @m.autocommit(true))
  100. assert_equal(@m, @m.autocommit(false))
  101. end
  102. end
  103. # def test_ssl_set()
  104. # end
  105. def test_more_results_next_result()
  106. if @m.server_version >= 40100 then
  107. @m.query_with_result = false
  108. @m.set_server_option(Mysql::OPTION_MULTI_STATEMENTS_ON) if defined? Mysql::OPTION_MULTI_STATEMENTS_ON
  109. @m.query("select 1,2,3; select 4,5,6")
  110. res = @m.store_result
  111. assert_equal(["1","2","3"], res.fetch_row)
  112. assert_equal(nil, res.fetch_row)
  113. assert_equal(true, @m.more_results)
  114. assert_equal(true, @m.more_results?)
  115. assert_equal(true, @m.next_result)
  116. res = @m.store_result
  117. assert_equal(["4","5","6"], res.fetch_row)
  118. assert_equal(nil, res.fetch_row)
  119. assert_equal(false, @m.more_results)
  120. assert_equal(false, @m.more_results?)
  121. assert_equal(false, @m.next_result)
  122. end
  123. end if Mysql.client_version >= 40100
  124. def test_set_server_option()
  125. if @m.server_version >= 40101 then
  126. assert_equal(@m, @m.set_server_option(Mysql::OPTION_MULTI_STATEMENTS_ON))
  127. assert_equal(@m, @m.set_server_option(Mysql::OPTION_MULTI_STATEMENTS_OFF))
  128. end
  129. end if Mysql.client_version >= 40101
  130. def test_sqlstate()
  131. if @m.server_version >= 40100 then
  132. assert_equal("00000", @m.sqlstate)
  133. assert_raises(Mysql::Error){@m.query("hogehoge")}
  134. assert_equal("42000", @m.sqlstate)
  135. end
  136. end if Mysql.client_version >= 40100
  137. def test_query_with_result()
  138. assert_equal(true, @m.query_with_result)
  139. assert_equal(false, @m.query_with_result = false)
  140. assert_equal(false, @m.query_with_result)
  141. assert_equal(true, @m.query_with_result = true)
  142. assert_equal(true, @m.query_with_result)
  143. end
  144. def test_reconnect()
  145. assert_equal(false, @m.reconnect)
  146. assert_equal(true, @m.reconnect = true)
  147. assert_equal(true, @m.reconnect)
  148. assert_equal(false, @m.reconnect = false)
  149. assert_equal(false, @m.reconnect)
  150. end
  151. end
  152. class TC_MysqlRes < Test::Unit::TestCase
  153. def setup()
  154. @host, @user, @pass, db, port, sock, flag = ARGV
  155. @db = db || "test"
  156. @port = port.to_i
  157. @sock = sock.nil? || sock.empty? ? nil : sock
  158. @flag = flag.to_i
  159. @m = Mysql.new(@host, @user, @pass, @db, @port, @sock, @flag)
  160. @m.query("create temporary table t (id int, str char(10), primary key (id))")
  161. @m.query("insert into t values (1, 'abc'), (2, 'defg'), (3, 'hi'), (4, null)")
  162. @res = @m.query("select * from t")
  163. end
  164. def teardown()
  165. @res.free
  166. @m.close
  167. end
  168. def test_num_fields()
  169. assert_equal(2, @res.num_fields)
  170. end
  171. def test_num_rows()
  172. assert_equal(4, @res.num_rows)
  173. end
  174. def test_fetch_row()
  175. assert_equal(["1","abc"], @res.fetch_row)
  176. assert_equal(["2","defg"], @res.fetch_row)
  177. assert_equal(["3","hi"], @res.fetch_row)
  178. assert_equal(["4",nil], @res.fetch_row)
  179. assert_equal(nil, @res.fetch_row)
  180. end
  181. def test_fetch_hash()
  182. assert_equal({"id"=>"1", "str"=>"abc"}, @res.fetch_hash)
  183. assert_equal({"id"=>"2", "str"=>"defg"}, @res.fetch_hash)
  184. assert_equal({"id"=>"3", "str"=>"hi"}, @res.fetch_hash)
  185. assert_equal({"id"=>"4", "str"=>nil}, @res.fetch_hash)
  186. assert_equal(nil, @res.fetch_hash)
  187. end
  188. def test_fetch_hash2()
  189. assert_equal({"t.id"=>"1", "t.str"=>"abc"}, @res.fetch_hash(true))
  190. assert_equal({"t.id"=>"2", "t.str"=>"defg"}, @res.fetch_hash(true))
  191. assert_equal({"t.id"=>"3", "t.str"=>"hi"}, @res.fetch_hash(true))
  192. assert_equal({"t.id"=>"4", "t.str"=>nil}, @res.fetch_hash(true))
  193. assert_equal(nil, @res.fetch_hash)
  194. end
  195. def test_each()
  196. ary = [["1","abc"], ["2","defg"], ["3","hi"], ["4",nil]]
  197. @res.each do |a|
  198. assert_equal(ary.shift, a)
  199. end
  200. end
  201. def test_each_hash()
  202. hash = [{"id"=>"1","str"=>"abc"}, {"id"=>"2","str"=>"defg"}, {"id"=>"3","str"=>"hi"}, {"id"=>"4","str"=>nil}]
  203. @res.each_hash do |h|
  204. assert_equal(hash.shift, h)
  205. end
  206. end
  207. def test_data_seek()
  208. assert_equal(["1","abc"], @res.fetch_row)
  209. assert_equal(["2","defg"], @res.fetch_row)
  210. assert_equal(["3","hi"], @res.fetch_row)
  211. @res.data_seek(1)
  212. assert_equal(["2","defg"], @res.fetch_row)
  213. end
  214. def test_row_seek()
  215. assert_equal(["1","abc"], @res.fetch_row)
  216. pos = @res.row_tell
  217. assert_equal(["2","defg"], @res.fetch_row)
  218. assert_equal(["3","hi"], @res.fetch_row)
  219. @res.row_seek(pos)
  220. assert_equal(["2","defg"], @res.fetch_row)
  221. end
  222. def test_field_seek()
  223. assert_equal(0, @res.field_tell)
  224. @res.fetch_field
  225. assert_equal(1, @res.field_tell)
  226. @res.fetch_field
  227. assert_equal(2, @res.field_tell)
  228. @res.field_seek(1)
  229. assert_equal(1, @res.field_tell)
  230. end
  231. def test_fetch_field()
  232. f = @res.fetch_field
  233. assert_equal("id", f.name)
  234. assert_equal("t", f.table)
  235. assert_equal(nil, f.def)
  236. assert_equal(Mysql::Field::TYPE_LONG, f.type)
  237. assert_equal(11, f.length)
  238. assert_equal(1, f.max_length)
  239. assert_equal(Mysql::Field::NUM_FLAG|Mysql::Field::PRI_KEY_FLAG|Mysql::Field::PART_KEY_FLAG|Mysql::Field::NOT_NULL_FLAG, f.flags)
  240. assert_equal(0, f.decimals)
  241. f = @res.fetch_field
  242. assert_equal("str", f.name)
  243. assert_equal("t", f.table)
  244. assert_equal(nil, f.def)
  245. assert_equal(Mysql::Field::TYPE_STRING, f.type)
  246. assert_equal(10, f.length)
  247. assert_equal(4, f.max_length)
  248. assert_equal(0, f.flags)
  249. assert_equal(0, f.decimals)
  250. f = @res.fetch_field
  251. assert_equal(nil, f)
  252. end
  253. def test_fetch_fields()
  254. a = @res.fetch_fields
  255. assert_equal(2, a.size)
  256. assert_equal("id", a[0].name)
  257. assert_equal("str", a[1].name)
  258. end
  259. def test_fetch_field_direct()
  260. f = @res.fetch_field_direct(0)
  261. assert_equal("id", f.name)
  262. f = @res.fetch_field_direct(1)
  263. assert_equal("str", f.name)
  264. assert_raises(Mysql::Error){@res.fetch_field_direct(-1)}
  265. assert_raises(Mysql::Error){@res.fetch_field_direct(2)}
  266. end
  267. def test_fetch_lengths()
  268. assert_equal(nil, @res.fetch_lengths())
  269. @res.fetch_row
  270. assert_equal([1, 3], @res.fetch_lengths())
  271. @res.fetch_row
  272. assert_equal([1, 4], @res.fetch_lengths())
  273. @res.fetch_row
  274. assert_equal([1, 2], @res.fetch_lengths())
  275. @res.fetch_row
  276. assert_equal([1, 0], @res.fetch_lengths())
  277. @res.fetch_row
  278. assert_equal(nil, @res.fetch_lengths())
  279. end
  280. def test_field_hash()
  281. f = @res.fetch_field
  282. h = {
  283. "name" => "id",
  284. "table" => "t",
  285. "def" => nil,
  286. "type" => Mysql::Field::TYPE_LONG,
  287. "length" => 11,
  288. "max_length" => 1,
  289. "flags" => Mysql::Field::NUM_FLAG|Mysql::Field::PRI_KEY_FLAG|Mysql::Field::PART_KEY_FLAG|Mysql::Field::NOT_NULL_FLAG,
  290. "decimals" => 0,
  291. }
  292. assert_equal(h, f.hash)
  293. f = @res.fetch_field
  294. h = {
  295. "name" => "str",
  296. "table" => "t",
  297. "def" => nil,
  298. "type" => Mysql::Field::TYPE_STRING,
  299. "length" => 10,
  300. "max_length" => 4,
  301. "flags" => 0,
  302. "decimals" => 0,
  303. }
  304. assert_equal(h, f.hash)
  305. end
  306. def test_field_inspect()
  307. f = @res.fetch_field
  308. assert_equal("#<Mysql::Field:id>", f.inspect)
  309. f = @res.fetch_field
  310. assert_equal("#<Mysql::Field:str>", f.inspect)
  311. end
  312. def test_is_num()
  313. f = @res.fetch_field
  314. assert_equal(true, f.is_num?)
  315. f = @res.fetch_field
  316. assert_equal(false, f.is_num?)
  317. end
  318. def test_is_not_null()
  319. f = @res.fetch_field
  320. assert_equal(true, f.is_not_null?)
  321. f = @res.fetch_field
  322. assert_equal(false, f.is_not_null?)
  323. end
  324. def test_is_pri_key()
  325. f = @res.fetch_field
  326. assert_equal(true, f.is_pri_key?)
  327. f = @res.fetch_field
  328. assert_equal(false, f.is_pri_key?)
  329. end
  330. end
  331. class TC_MysqlStmt < Test::Unit::TestCase
  332. def setup()
  333. @host, @user, @pass, db, port, sock, flag = ARGV
  334. @db = db || "test"
  335. @port = port.to_i
  336. @sock = sock.nil? || sock.empty? ? nil : sock
  337. @flag = flag.to_i
  338. @m = Mysql.new(@host, @user, @pass, @db, @port, @sock, @flag)
  339. end
  340. def teardown()
  341. end
  342. def test_init()
  343. if @m.server_version >= 40100 then
  344. s = @m.stmt_init()
  345. assert_equal(Mysql::Stmt, s.class)
  346. s.close
  347. end
  348. end
  349. def test_prepare()
  350. if @m.server_version >= 40100 then
  351. s = @m.prepare("select 1")
  352. assert_equal(Mysql::Stmt, s.class)
  353. s.close
  354. end
  355. end
  356. end if Mysql.client_version >= 40100
  357. class TC_MysqlStmt2 < Test::Unit::TestCase
  358. def setup()
  359. @host, @user, @pass, db, port, sock, flag = ARGV
  360. @db = db || "test"
  361. @port = port.to_i
  362. @sock = sock.nil? || sock.empty? ? nil : sock
  363. @flag = flag.to_i
  364. @m = Mysql.new(@host, @user, @pass, @db, @port, @sock, @flag)
  365. @s = @m.stmt_init()
  366. end
  367. def teardown()
  368. @s.close
  369. @m.close
  370. end
  371. def test_affected_rows()
  372. if @m.server_version >= 40100 then
  373. @m.query("create temporary table t (i int, c char(10))")
  374. @s.prepare("insert into t values (?,?)")
  375. @s.execute(1, "hoge")
  376. assert_equal(1, @s.affected_rows())
  377. @s.execute(2, "hoge")
  378. @s.execute(3, "hoge")
  379. @s.prepare("update t set c=?")
  380. @s.execute("fuga")
  381. assert_equal(3, @s.affected_rows())
  382. end
  383. end
  384. =begin
  385. def test_attr_get()
  386. assert_equal(false, @s.attr_get(Mysql::Stmt::ATTR_UPDATE_MAX_LENGTH))
  387. assert_raises(Mysql::Error){@s.attr_get(999)}
  388. end
  389. def test_attr_set()
  390. @s.attr_set(Mysql::Stmt::ATTR_UPDATE_MAX_LENGTH, true)
  391. assert_equal(true, @s.attr_get(Mysql::Stmt::ATTR_UPDATE_MAX_LENGTH))
  392. @s.attr_set(Mysql::Stmt::ATTR_UPDATE_MAX_LENGTH, false)
  393. assert_equal(false, @s.attr_get(Mysql::Stmt::ATTR_UPDATE_MAX_LENGTH))
  394. assert_raises(Mysql::Error){@s.attr_set(999, true)}
  395. end
  396. def test_bind_param()
  397. @s.prepare("insert into t values (?,?)")
  398. @s.bind_param(123, "abc")
  399. @s.bind_param(Time.now, nil)
  400. assert_raises(Mysql::Error){@s.bind_param(1, 2, 3)}
  401. b = @s.bind_param(Bind.new(Mysql::TYPE_TINY, 99, false))
  402. @s.bind_param(98.765, b)
  403. end
  404. =end
  405. def test_bind_result_nil()
  406. if @m.server_version >= 40100 then
  407. @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
  408. @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
  409. @s.prepare("select * from t")
  410. @s.bind_result(nil,nil,nil,nil)
  411. @s.execute
  412. a = @s.fetch
  413. assert_equal([123, "9abcdefg", 1.2345, Mysql::Time.new(2005,8,2,23,50,11)], a)
  414. end
  415. end
  416. def test_bind_result_numeric()
  417. if @m.server_version >= 40100 then
  418. @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
  419. @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
  420. @s.prepare("select * from t")
  421. @s.bind_result(Numeric, Numeric, Numeric, Numeric)
  422. @s.execute
  423. a = @s.fetch
  424. if Mysql.client_version < 50000 then
  425. assert_equal([123, 9, 1, 2005], a)
  426. else
  427. assert_equal([123, 9, 1, 20050802235011], a)
  428. end
  429. end
  430. end
  431. def test_bind_result_integer()
  432. if @m.server_version >= 40100 then
  433. @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
  434. @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
  435. @s.prepare("select * from t")
  436. @s.bind_result(Integer, Integer, Integer, Integer)
  437. @s.execute
  438. a = @s.fetch
  439. if Mysql.client_version < 50000 then
  440. assert_equal([123, 9, 1, 2005], a)
  441. else
  442. assert_equal([123, 9, 1, 20050802235011], a)
  443. end
  444. end
  445. end
  446. def test_bind_result_fixnum()
  447. if @m.server_version >= 40100 then
  448. @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
  449. @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
  450. @s.prepare("select * from t")
  451. @s.bind_result(Fixnum, Fixnum, Fixnum, Fixnum)
  452. @s.execute
  453. a = @s.fetch
  454. if Mysql.client_version < 50000 then
  455. assert_equal([123, 9, 1, 2005], a)
  456. else
  457. assert_equal([123, 9, 1, 20050802235011.0], a)
  458. end
  459. end
  460. end
  461. def test_bind_result_string()
  462. if @m.server_version >= 40100 then
  463. @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
  464. @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
  465. @s.prepare("select * from t")
  466. @s.bind_result(String, String, String, String)
  467. @s.execute
  468. a = @s.fetch
  469. assert_equal(["123", "9abcdefg", "1.2345", "2005-08-02 23:50:11"], a)
  470. end
  471. end
  472. def test_bind_result_float()
  473. if @m.server_version >= 40100 then
  474. @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
  475. @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
  476. @s.prepare("select * from t")
  477. @s.bind_result(Float, Float, Float, Float)
  478. @s.execute
  479. a = @s.fetch
  480. if Mysql.client_version < 50000 then
  481. assert_equal([123.0, 9.0, 1.2345, 2005.0], a)
  482. else
  483. assert_equal([123.0, 9.0, 1.2345, 20050802235011.0], a)
  484. end
  485. end
  486. end
  487. def test_bind_result_mysqltime()
  488. if @m.server_version >= 40100 then
  489. @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
  490. @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
  491. @s.prepare("select * from t")
  492. @s.bind_result(Mysql::Time, Mysql::Time, Mysql::Time, Mysql::Time)
  493. @s.execute
  494. a = @s.fetch
  495. if Mysql.client_version < 50000 then
  496. assert_equal([Mysql::Time.new, Mysql::Time.new, Mysql::Time.new, Mysql::Time.new(2005,8,2,23,50,11)], a)
  497. else
  498. assert_equal([Mysql::Time.new(2000,1,23), Mysql::Time.new, Mysql::Time.new, Mysql::Time.new(2005,8,2,23,50,11)], a)
  499. end
  500. end
  501. end
  502. def test_bind_result_unknown()
  503. if @m.server_version >= 40100 then
  504. @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
  505. @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
  506. @s.prepare("select * from t")
  507. assert_raises(TypeError){@s.bind_result(Time, nil, nil, nil)}
  508. end
  509. end
  510. def test_bind_result_unmatch_count()
  511. if @m.server_version >= 40100 then
  512. @m.query("create temporary table t (i int, c char(10), d double, t datetime)")
  513. @m.query("insert into t values (123, '9abcdefg', 1.2345, 20050802235011)")
  514. @s.prepare("select * from t")
  515. assert_raises(Mysql::Error){@s.bind_result(nil, nil)}
  516. end
  517. end
  518. def test_data_seek()
  519. if @m.server_version >= 40100 then
  520. @m.query("create temporary table t (i int)")
  521. @m.query("insert into t values (0),(1),(2),(3),(4),(5)")
  522. @s.prepare("select i from t")
  523. @s.execute
  524. assert_equal([0], @s.fetch)
  525. assert_equal([1], @s.fetch)
  526. assert_equal([2], @s.fetch)
  527. @s.data_seek(5)
  528. assert_equal([5], @s.fetch)
  529. @s.data_seek(1)
  530. assert_equal([1], @s.fetch)
  531. end
  532. end
  533. =begin
  534. def test_errno()
  535. @s.errno()
  536. end
  537. def test_error()
  538. @s.error()
  539. end
  540. =end
  541. def test_execute()
  542. if @m.server_version >= 40100 then
  543. @m.query("create temporary table t (i int)")
  544. @s.prepare("insert into t values (123)")
  545. @s.execute()
  546. assert_equal(1, @s.affected_rows)
  547. @s.execute()
  548. assert_equal(1, @s.affected_rows)
  549. assert_equal(2, @m.query("select count(*) from t").fetch_row[0].to_i)
  550. end
  551. end
  552. def test_execute2()
  553. if @m.server_version >= 40100 then
  554. @m.query("create temporary table t (i int)")
  555. @s.prepare("insert into t values (?)")
  556. @s.execute(123)
  557. @s.execute("456")
  558. @s.prepare("select * from t")
  559. @s.execute
  560. assert_equal([123], @s.fetch)
  561. assert_equal([456], @s.fetch)
  562. end
  563. end
  564. def test_execute3()
  565. if @m.server_version >= 40100 then
  566. @m.query("create temporary table t (i int, c char(255), t timestamp)")
  567. @s.prepare("insert into t values (?,?,?)")
  568. @s.execute(123, "hoge", Time.local(2005,7,19,23,53,0));
  569. assert_raises(Mysql::Error){@s.execute(123, "hoge")}
  570. assert_raises(Mysql::Error){@s.execute(123, "hoge", 0, "fuga")}
  571. @s.prepare("select * from t")
  572. @s.execute
  573. assert_equal([123, "hoge", Mysql::Time.new(2005,7,19,23,53,0)], @s.fetch)
  574. end
  575. end
  576. def test_execute4()
  577. if @m.server_version >= 40100 then
  578. @m.query("create temporary table t (i int, c char(255), t timestamp)")
  579. @s.prepare("insert into t values (?,?,?)")
  580. @s.execute(nil, "hoge", Mysql::Time.new(2005,7,19,23,53,0));
  581. @s.prepare("select * from t")
  582. @s.execute
  583. assert_equal([nil, "hoge", Mysql::Time.new(2005,7,19,23,53,0)], @s.fetch)
  584. end
  585. end
  586. def test_fetch()
  587. if @m.server_version >= 40100 then
  588. @s.prepare("select 123, 'abc', null")
  589. @s.execute()
  590. assert_equal([123, "abc", nil], @s.fetch())
  591. end
  592. end
  593. def test_fetch_tinyint()
  594. if @m.server_version >= 40100 then
  595. @m.query("create temporary table t (i tinyint)")
  596. @m.query("insert into t values (0),(-1),(127),(-128),(255),(-255)")
  597. @s.prepare("select i from t")
  598. @s.execute
  599. assert_equal([0], @s.fetch)
  600. assert_equal([-1], @s.fetch)
  601. assert_equal([127], @s.fetch)
  602. assert_equal([-128], @s.fetch)
  603. assert_equal([127], @s.fetch)
  604. assert_equal([-128], @s.fetch)
  605. end
  606. end
  607. def test_fetch_tinyint_unsigned()
  608. if @m.server_version >= 40100 then
  609. @m.query("create temporary table t (i tinyint unsigned)")
  610. @m.query("insert into t values (0),(-1),(127),(-128),(255),(-255),(256)")
  611. @s.prepare("select i from t")
  612. @s.execute
  613. assert_equal([0], @s.fetch)
  614. assert_equal([0], @s.fetch)
  615. assert_equal([127], @s.fetch)
  616. assert_equal([0], @s.fetch)
  617. assert_equal([255], @s.fetch)
  618. assert_equal([0], @s.fetch)
  619. assert_equal([255], @s.fetch)
  620. end
  621. end
  622. def test_fetch_smallint()
  623. if @m.server_version >= 40100 then
  624. @m.query("create temporary table t (i smallint)")
  625. @m.query("insert into t values (0),(-1),(32767),(-32768),(65535),(-65535),(65536)")
  626. @s.prepare("select i from t")
  627. @s.execute
  628. assert_equal([0], @s.fetch)
  629. assert_equal([-1], @s.fetch)
  630. assert_equal([32767], @s.fetch)
  631. assert_equal([-32768], @s.fetch)
  632. assert_equal([32767], @s.fetch)
  633. assert_equal([-32768], @s.fetch)
  634. end
  635. end
  636. def test_fetch_smallint_unsigned()
  637. if @m.server_version >= 40100 then
  638. @m.query("create temporary table t (i smallint unsigned)")
  639. @m.query("insert into t values (0),(-1),(32767),(-32768),(65535),(-65535),(65536)")
  640. @s.prepare("select i from t")
  641. @s.execute
  642. assert_equal([0], @s.fetch)
  643. assert_equal([0], @s.fetch)
  644. assert_equal([32767], @s.fetch)
  645. assert_equal([0], @s.fetch)
  646. assert_equal([65535], @s.fetch)
  647. assert_equal([0], @s.fetch)
  648. assert_equal([65535], @s.fetch)
  649. end
  650. end
  651. def test_fetch_mediumint()
  652. if @m.server_version >= 40100 then
  653. @m.query("create temporary table t (i mediumint)")
  654. @m.query("insert into t values (0),(-1),(8388607),(-8388608),(16777215),(-16777215),(16777216)")
  655. @s.prepare("select i from t")
  656. @s.execute
  657. assert_equal([0], @s.fetch)
  658. assert_equal([-1], @s.fetch)
  659. assert_equal([8388607], @s.fetch)
  660. assert_equal([-8388608], @s.fetch)
  661. assert_equal([8388607], @s.fetch)
  662. assert_equal([-8388608], @s.fetch)
  663. end
  664. end
  665. def test_fetch_mediumint_unsigned()
  666. if @m.server_version >= 40100 then
  667. @m.query("create temporary table t (i mediumint unsigned)")
  668. @m.query("insert into t values (0),(-1),(8388607),(-8388608),(16777215),(-16777215),(16777216)")
  669. @s.prepare("select i from t")
  670. @s.execute
  671. assert_equal([0], @s.fetch)
  672. assert_equal([0], @s.fetch)
  673. assert_equal([8388607], @s.fetch)
  674. assert_equal([0], @s.fetch)
  675. assert_equal([16777215], @s.fetch)
  676. assert_equal([0], @s.fetch)
  677. assert_equal([16777215], @s.fetch)
  678. end
  679. end
  680. def test_fetch_int()
  681. if @m.server_version >= 40100 then
  682. @m.query("create temporary table t (i int)")
  683. @m.query("insert into t values (0),(-1),(2147483647),(-2147483648),(4294967295),(-4294967295),(4294967296)")
  684. @s.prepare("select i from t")
  685. @s.execute
  686. assert_equal([0], @s.fetch)
  687. assert_equal([-1], @s.fetch)
  688. assert_equal([2147483647], @s.fetch)
  689. assert_equal([-2147483648], @s.fetch)
  690. assert_equal([2147483647], @s.fetch)
  691. assert_equal([-2147483648], @s.fetch)
  692. end
  693. end
  694. def test_fetch_int_unsigned()
  695. if @m.server_version >= 40100 then
  696. @m.query("create temporary table t (i int unsigned)")
  697. @m.query("insert into t values (0),(-1),(2147483647),(-2147483648),(4294967295),(-4294967295),(4294967296)")
  698. @s.prepare("select i from t")
  699. @s.execute
  700. assert_equal([0], @s.fetch)
  701. assert_equal([0], @s.fetch)
  702. assert_equal([2147483647], @s.fetch)
  703. assert_equal([0], @s.fetch)
  704. assert_equal([4294967295], @s.fetch)
  705. assert_equal([0], @s.fetch)
  706. assert_equal([4294967295], @s.fetch)
  707. end
  708. end
  709. def test_fetch_bigint()
  710. if @m.server_version >= 40100 then
  711. @m.query("create temporary table t (i bigint)")
  712. @m.query("insert into t values (0),(-1),(9223372036854775807),(-9223372036854775808),(18446744073709551615),(-18446744073709551615),(18446744073709551616)")
  713. @s.prepare("select i from t")
  714. @s.execute
  715. assert_equal([0], @s.fetch)
  716. assert_equal([-1], @s.fetch)
  717. assert_equal([9223372036854775807], @s.fetch)
  718. assert_equal([-9223372036854775808], @s.fetch)
  719. assert_equal([-1], @s.fetch) # MySQL problem
  720. assert_equal([-9223372036854775808], @s.fetch)
  721. assert_equal([9223372036854775807], @s.fetch)
  722. end
  723. end
  724. def test_fetch_bigint_unsigned()
  725. if @m.server_version >= 40100 then
  726. @m.query("create temporary table t (i bigint unsigned)")
  727. @m.query("insert into t values (0),(-1),(9223372036854775807),(-9223372036854775808),(18446744073709551615),(-18446744073709551615),(18446744073709551616)")
  728. @s.prepare("select i from t")
  729. @s.execute
  730. assert_equal([0], @s.fetch)
  731. assert_equal([-1], @s.fetch) # MySQL & MySQL/Ruby problem
  732. assert_equal([9223372036854775807], @s.fetch)
  733. if @m.server_version < 50000 then
  734. assert_equal([-9223372036854775808], @s.fetch) # MySQL problem
  735. else
  736. assert_equal([0], @s.fetch)
  737. end
  738. assert_equal([-1], @s.fetch) # MySQL/Ruby problem
  739. assert_equal([0], @s.fetch)
  740. assert_equal([-1], @s.fetch) # MySQL/Ruby problem
  741. end
  742. end
  743. def test_fetch_float()
  744. if @m.server_version >= 40100 then
  745. @m.query("create temporary table t (i float)")
  746. @m.query("insert into t values (0),(-3.402823466E+38),(-1.175494351E-38),(1.175494351E-38),(3.402823466E+38)")
  747. @s.prepare("select i from t")
  748. @s.execute
  749. assert_equal([0], @s.fetch)
  750. assert_in_delta(-3.402823466E+38, @s.fetch[0], 0.000000001E+38)
  751. assert_in_delta(-1.175494351E-38, @s.fetch[0], 0.000000001E-38)
  752. assert_in_delta(1.175494351E-38, @s.fetch[0], 0.000000001E-38)
  753. assert_in_delta(3.402823466E+38, @s.fetch[0], 0.000000001E+38)
  754. end
  755. end
  756. def test_fetch_float_unsigned()
  757. if @m.server_version >= 40100 then
  758. @m.query("create temporary table t (i float unsigned)")
  759. @m.query("insert into t values (0),(-3.402823466E+38),(-1.175494351E-38),(1.175494351E-38),(3.402823466E+38)")
  760. @s.prepare("select i from t")
  761. @s.execute
  762. assert_equal([0], @s.fetch)
  763. assert_equal([0], @s.fetch)
  764. assert_equal([0], @s.fetch)
  765. assert_in_delta(1.175494351E-38, @s.fetch[0], 0.000000001E-38)
  766. assert_in_delta(3.402823466E+38, @s.fetch[0], 0.000000001E+38)
  767. end
  768. end
  769. def test_fetch_double()
  770. if @m.server_version >= 40100 then
  771. @m.query("create temporary table t (i double)")
  772. @m.query("insert into t values (0),(-1.7976931348623157E+308),(-2.2250738585072014E-308),(2.2250738585072014E-308),(1.7976931348623157E+308)")
  773. @s.prepare("select i from t")
  774. @s.execute
  775. assert_equal([0], @s.fetch)
  776. assert_equal(-Float::MAX, @s.fetch[0])
  777. if Mysql.client_version <= 40109 then # higher version has bug
  778. assert_equal(-Float::MIN, @s.fetch[0])
  779. assert_equal(Float::MIN, @s.fetch[0])
  780. assert_equal(Float::MAX, @s.fetch[0])
  781. end
  782. end
  783. end
  784. def test_fetch_double_unsigned()
  785. if @m.server_version >= 40100 then
  786. @m.query("create temporary table t (i double unsigned)")
  787. @m.query("insert into t values (0),(-1.7976931348623157E+308),(-2.2250738585072014E-308),(2.2250738585072014E-308),(1.7976931348623157E+308)")
  788. @s.prepare("select i from t")
  789. @s.execute
  790. assert_equal([0], @s.fetch)
  791. assert_equal([0], @s.fetch)
  792. if Mysql.client_version <= 40109 then # higher version has bug
  793. assert_equal([0], @s.fetch)
  794. assert_equal(Float::MIN, @s.fetch[0])
  795. assert_equal(Float::MAX, @s.fetch[0])
  796. end
  797. end
  798. end
  799. def test_fetch_decimal()
  800. if (@m.server_version >= 50000 and Mysql.client_version >= 50000) or (@m.server_version >= 40100 and @m.server_version < 50000) then
  801. @m.query("create temporary table t (i decimal)")
  802. @m.query("insert into t values (0),(9999999999),(-9999999999),(10000000000),(-10000000000)")
  803. @s.prepare("select i from t")
  804. @s.execute
  805. assert_equal(["0"], @s.fetch)
  806. assert_equal(["9999999999"], @s.fetch)
  807. assert_equal(["-9999999999"], @s.fetch)
  808. if @m.server_version < 50000 then
  809. assert_equal(["10000000000"], @s.fetch) # MySQL problem
  810. else
  811. assert_equal(["9999999999"], @s.fetch)
  812. end
  813. assert_equal(["-9999999999"], @s.fetch)
  814. end
  815. end
  816. def test_fetch_decimal_unsigned()
  817. if (@m.server_version >= 50000 and Mysql.client_version >= 50000) or (@m.server_version >= 40100 and @m.server_version < 50000) then
  818. @m.query("create temporary table t (i decimal unsigned)")
  819. @m.query("insert into t values (0),(9999999998),(9999999999),(-9999999998),(-9999999999),(10000000000),(-10000000000)")
  820. @s.prepare("select i from t")
  821. @s.execute
  822. assert_equal(["0"], @s.fetch)
  823. assert_equal(["9999999998"], @s.fetch)
  824. assert_equal(["9999999999"], @s.fetch)
  825. assert_equal(["0"], @s.fetch)
  826. assert_equal(["0"], @s.fetch)
  827. assert_equal(["9999999999"], @s.fetch)
  828. assert_equal(["0"], @s.fetch)
  829. end
  830. end
  831. def test_fetch_date()
  832. if @m.server_version >= 40100 then
  833. @m.query("create temporary table t (i date)")
  834. @m.query("insert into t values ('0000-00-00'),('1000-01-01'),('9999-12-31')")
  835. @s.prepare("select i from t")
  836. @s.execute
  837. assert_equal([Mysql::Time.new(0,0,0)], @s.fetch)
  838. assert_equal([Mysql::Time.new(1000,1,1)], @s.fetch)
  839. assert_equal([Mysql::Time.new(9999,12,31)], @s.fetch)
  840. end
  841. end
  842. def test_fetch_datetime()
  843. if @m.server_version >= 40100 then
  844. @m.query("create temporary table t (i datetime)")
  845. @m.query("insert into t values ('0000-00-00 00:00:00'),('1000-01-01 00:00:00'),('9999-12-31 23:59:59')")
  846. @s.prepare("select i from t")
  847. @s.execute
  848. assert_equal([Mysql::Time.new(0,0,0,0,0,0)], @s.fetch)
  849. assert_equal([Mysql::Time.new(1000,1,1,0,0,0)], @s.fetch)
  850. assert_equal([Mysql::Time.new(9999,12,31,23,59,59)], @s.fetch)
  851. end
  852. end
  853. def test_fetch_timestamp()
  854. if @m.server_version >= 40100 then
  855. @m.query("create temporary table t (i timestamp)")
  856. @m.query("insert into t values ('1970-01-01 12:00:00'),('2037-12-31 23:59:59')")
  857. @s.prepare("select i from t")
  858. @s.execute
  859. assert_equal([Mysql::Time.new(1970,1,1,12,0,0)], @s.fetch)
  860. assert_equal([Mysql::Time.new(2037,12,31,23,59,59)], @s.fetch)
  861. end
  862. end
  863. def test_fetch_time()
  864. if @m.server_version >= 40100 then
  865. @m.query("create temporary table t (i time)")
  866. @m.query("insert into t values ('-838:59:59'),(0),('838:59:59')")
  867. @s.prepare("select i from t")
  868. @s.execute
  869. assert_equal([Mysql::Time.new(0,0,0,838,59,59,true)], @s.fetch)
  870. assert_equal([Mysql::Time.new(0,0,0,0,0,0,false)], @s.fetch)
  871. assert_equal([Mysql::Time.new(0,0,0,838,59,59,false)], @s.fetch)
  872. end
  873. end
  874. def test_fetch_year()
  875. if @m.server_version >= 40100 then
  876. @m.query("create temporary table t (i year)")
  877. @m.query("insert into t values (0),(70),(69),(1901),(2155)")
  878. @s.prepare("select i from t")
  879. @s.execute
  880. assert_equal([0], @s.fetch)
  881. assert_equal([1970], @s.fetch)
  882. assert_equal([2069], @s.fetch)
  883. assert_equal([1901], @s.fetch)
  884. assert_equal([2155], @s.fetch)
  885. end
  886. end
  887. def test_fetch_char()
  888. if @m.server_version >= 40100 then
  889. @m.query("create temporary table t (i char(10))")
  890. @m.query("insert into t values (null),('abc')")
  891. @s.prepare("select i from t")
  892. @s.execute
  893. assert_equal([nil], @s.fetch)
  894. assert_equal(["abc"], @s.fetch)
  895. end
  896. end
  897. def test_fetch_varchar()
  898. if @m.server_version >= 40100 then
  899. @m.query("create temporary table t (i varchar(10))")
  900. @m.query("insert into t values (null),('abc')")
  901. @s.prepare("select i from t")
  902. @s.execute
  903. assert_equal([nil], @s.fetch)
  904. assert_equal(["abc"], @s.fetch)
  905. end
  906. end
  907. def test_fetch_binary()
  908. if @m.server_version >= 40100 then
  909. @m.query("create temporary table t (i binary(10))")
  910. @m.query("insert into t values (null),('abc')")
  911. @s.prepare("select i from t")
  912. @s.execute
  913. assert_equal([nil], @s.fetch)
  914. assert_equal(["abc"], @s.fetch)
  915. end
  916. end
  917. def test_fetch_varbinary()
  918. if @m.server_version >= 40100 then
  919. @m.query("create temporary table t (i varbinary(10))")
  920. @m.query("insert into t values (null),('abc')")
  921. @s.prepare("select i from t")
  922. @s.execute
  923. assert_equal([nil], @s.fetch)
  924. assert_equal(["abc"], @s.fetch)
  925. end
  926. end
  927. def test_fetch_tinyblob()
  928. if @m.server_version >= 40100 then
  929. @m.query("create temporary table t (i tinyblob)")
  930. @m.query("insert into t values (null),('abc')")
  931. @s.prepare("select i from t")
  932. @s.execute
  933. assert_equal([nil], @s.fetch)
  934. assert_equal(["abc"], @s.fetch)
  935. end
  936. end
  937. def test_fetch_tinytext()
  938. if @m.server_version >= 40100 then
  939. @m.query("create temporary table t (i tinytext)")
  940. @m.query("insert into t values (null),('abc')")
  941. @s.prepare("select i from t")
  942. @s.execute
  943. assert_equal([nil], @s.fetch)
  944. assert_equal(["abc"], @s.fetch)
  945. end
  946. end
  947. def test_fetch_blob()
  948. if @m.server_version >= 40100 then
  949. @m.query("create temporary table t (i blob)")
  950. @m.query("insert into t values (null),('abc')")
  951. @s.prepare("select i from t")
  952. @s.execute
  953. assert_equal([nil], @s.fetch)
  954. assert_equal(["abc"], @s.fetch)
  955. end
  956. end
  957. def test_fetch_text()
  958. if @m.server_version >= 40100 then
  959. @m.query("create temporary table t (i text)")
  960. @m.query("insert into t values (null),('abc')")
  961. @s.prepare("select i from t")
  962. @s.execute
  963. assert_equal([nil], @s.fetch)
  964. assert_equal(["abc"], @s.fetch)
  965. end
  966. end
  967. def test_fetch_mediumblob()
  968. if @m.server_version >= 40100 then
  969. @m.query("create temporary table t (i mediumblob)")
  970. @m.query("insert into t values (null),('abc')")
  971. @s.prepare("select i from t")
  972. @s.execute
  973. assert_equal([nil], @s.fetch)
  974. assert_equal(["abc"], @s.fetch)
  975. end
  976. end
  977. def test_fetch_mediumtext()
  978. if @m.server_version >= 40100 then
  979. @m.query("create temporary table t (i mediumtext)")
  980. @m.query("insert into t values (null),('abc')")
  981. @s.prepare("select i from t")
  982. @s.execute
  983. assert_equal([nil], @s.fetch)
  984. assert_equal(["abc"], @s.fetch)
  985. end
  986. end
  987. def test_fetch_longblob()
  988. if @m.server_version >= 40100 then
  989. @m.query("create temporary table t (i longblob)")
  990. @m.query("insert into t values (null),('abc')")
  991. @s.prepare("select i from t")
  992. @s.execute
  993. assert_equal([nil], @s.fetch)
  994. assert_equal(["abc"], @s.fetch)
  995. end
  996. end
  997. def test_fetch_longtext()
  998. if @m.server_version >= 40100 then
  999. @m.query("create temporary table t (i longtext)")
  1000. @m.query("insert into t values (null),('abc')")
  1001. @s.prepare("select i from t")
  1002. @s.execute
  1003. assert_equal([nil], @s.fetch)
  1004. assert_equal(["abc"], @s.fetch)
  1005. end
  1006. end
  1007. def test_fetch_enum()
  1008. if @m.server_version >= 40100 then
  1009. @m.query("create temporary table t (i enum('abc','def'))")
  1010. @m.query("insert into t values (null),(0),(1),(2),('abc'),('def'),('ghi')")
  1011. @s.prepare("select i from t")
  1012. @s.execute
  1013. assert_equal([nil], @s.fetch)
  1014. assert_equal([""], @s.fetch)
  1015. assert_equal(["abc"], @s.fetch)
  1016. assert_equal(["def"], @s.fetch)
  1017. assert_equal(["abc"], @s.fetch)
  1018. assert_equal(["def"], @s.fetch)
  1019. assert_equal([""], @s.fetch)
  1020. end
  1021. end
  1022. def test_fetch_set()
  1023. if @m.server_version >= 40100 then
  1024. @m.query("create temporary table t (i set('abc','def'))")
  1025. @m.query("insert into t values (null),(0),(1),(2),(3),('abc'),('def'),('abc,def'),('ghi')")
  1026. @s.prepare("select i from t")
  1027. @s.execute
  1028. assert_equal([nil], @s.fetch)
  1029. assert_equal([""], @s.fetch)
  1030. assert_equal(["abc"], @s.fetch)
  1031. assert_equal(["def"], @s.fetch)
  1032. assert_equal(["abc,def"], @s.fetch)
  1033. assert_equal(["abc"], @s.fetch)
  1034. assert_equal(["def"], @s.fetch)
  1035. assert_equal(["abc,def"], @s.fetch)
  1036. assert_equal([""], @s.fetch)
  1037. end
  1038. end
  1039. def test_each()
  1040. if @m.server_version >= 40100 then
  1041. @m.query("create temporary table t (i int, c char(255), d datetime)")
  1042. @m.query("insert into t values (1,'abc','19701224235905'),(2,'def','21120903123456'),(3,'123',null)")
  1043. @s.prepare("select * from t")
  1044. @s.execute
  1045. c = 0
  1046. @s.each do |a|
  1047. case c
  1048. when 0
  1049. assert_equal([1,"abc",Mysql::Time.new(1970,12,24,23,59,05)], a)
  1050. when 1
  1051. assert_equal([2,"def",Mysql::Time.new(2112,9,3,12,34,56)], a)
  1052. when 2
  1053. assert_equal([3,"123",nil], a)
  1054. else
  1055. raise
  1056. end
  1057. c += 1
  1058. end
  1059. end
  1060. end
  1061. def test_field_count()
  1062. if @m.server_version >= 40100 then
  1063. @s.prepare("select 1,2,3")
  1064. @s.execute()
  1065. assert_equal(3, @s.field_count())
  1066. @s.prepare("set @a=1")
  1067. @s.execute()
  1068. assert_equal(0, @s.field_count())
  1069. end
  1070. end
  1071. def test_free_result()
  1072. if @m.server_version >= 40100 then
  1073. @s.free_result()
  1074. @s.prepare("select 1,2,3")
  1075. @s.execute()
  1076. @s.free_result()
  1077. end
  1078. end
  1079. def test_insert_id()
  1080. if @m.server_version >= 40100 then
  1081. @m.query("create temporary table t (i int auto_increment, unique(i))")
  1082. @s.prepare("insert into t values (0)")
  1083. @s.execute()
  1084. assert_equal(1, @s.insert_id())
  1085. @s.execute()
  1086. assert_equal(2, @s.insert_id())
  1087. end
  1088. end
  1089. def test_num_rows()
  1090. if @m.server_version >= 40100 then
  1091. @m.query("create temporary table t (i int)")
  1092. @m.query("insert into t values (1),(2),(3),(4)")
  1093. @s.prepare("select * from t")
  1094. @s.execute
  1095. assert_equal(4, @s.num_rows())
  1096. end
  1097. end
  1098. def test_param_count()
  1099. if @m.server_version >= 40100 then
  1100. @m.query("create temporary table t (a int, b int, c int)")
  1101. @s.prepare("select * from t")
  1102. assert_equal(0, @s.param_count())
  1103. @s.prepare("insert into t values (?,?,?)")
  1104. assert_equal(3, @s.param_count())
  1105. end
  1106. end
  1107. =begin
  1108. def test_param_metadata()
  1109. @s.param_metadata()
  1110. end
  1111. =end
  1112. def test_prepare()
  1113. if @m.server_version >= 40100 then
  1114. @s.prepare("select 1")
  1115. assert_raises(Mysql::Error){@s.prepare("invalid syntax")}
  1116. end
  1117. end
  1118. =begin
  1119. def test_reset()
  1120. @s.reset()
  1121. end
  1122. =end
  1123. def test_result_metadata()
  1124. if @m.server_version >= 40100 then
  1125. @s.prepare("select 1 foo, 2 bar")
  1126. res = @s.result_metadata()
  1127. f = res.fetch_fields
  1128. assert_equal("foo", f[0].name)
  1129. assert_equal("bar", f[1].name)
  1130. end
  1131. end
  1132. def test_row_seek_tell()
  1133. if @m.server_version >= 40100 then
  1134. @m.query("create temporary table t (i int)")
  1135. @m.query("insert into t values (0),(1),(2),(3),(4)")
  1136. @s.prepare("select * from t")
  1137. @s.execute
  1138. row0 = @s.row_tell
  1139. assert_equal([0], @s.fetch)
  1140. assert_equal([1], @s.fetch)
  1141. row2 = @s.row_seek(row0)
  1142. assert_equal([0], @s.fetch)
  1143. @s.row_seek(row2)
  1144. assert_equal([2], @s.fetch)
  1145. end
  1146. end
  1147. =begin
  1148. def test_send_long_data()
  1149. @m.query("create temporary table t (i int, t text)")
  1150. @s.prepare("insert into t values (?,?)")
  1151. @s.send_long_data(1, "long long data ")
  1152. @s.send_long_data(1, "long long data2")
  1153. assert_raises(Mysql::Error){@s.send_long_data(9, "invalid param number")}
  1154. @s.execute(99, "hoge")
  1155. assert_equal("long long data long long data2", @m.query("select t from t").fetch_row[0])
  1156. end
  1157. =end
  1158. def test_sqlstate()
  1159. if @m.server_version >= 40100 then
  1160. @s.prepare("select 1")
  1161. assert_equal("", @s.sqlstate)
  1162. assert_raises(Mysql::Error){@s.prepare("hogehoge")}
  1163. assert_equal("42000", @s.sqlstate)
  1164. end
  1165. end
  1166. =begin
  1167. def test_store_result()
  1168. @s.store_result()
  1169. end
  1170. =end
  1171. end if Mysql.client_version >= 40100
  1172. class TC_MysqlTime < Test::Unit::TestCase
  1173. def setup()
  1174. end
  1175. def teardown()
  1176. end
  1177. def test_init()
  1178. t = Mysql::Time.new
  1179. assert_equal(0, t.year);
  1180. assert_equal(0, t.month);
  1181. assert_equal(0, t.day);
  1182. assert_equal(0, t.hour);
  1183. assert_equal(0, t.minute);
  1184. assert_equal(0, t.second);
  1185. assert_equal(false, t.neg);
  1186. assert_equal(0, t.second_part);
  1187. end
  1188. def test_year()
  1189. t = Mysql::Time.new
  1190. assert_equal(2005, t.year = 2005)
  1191. assert_equal(2005, t.year)
  1192. end
  1193. def test_month()
  1194. t = Mysql::Time.new
  1195. assert_equal(11, t.month = 11)
  1196. assert_equal(11, t.month)
  1197. end
  1198. def test_day()
  1199. t = Mysql::Time.new
  1200. assert_equal(23, t.day = 23)
  1201. assert_equal(23, t.day)
  1202. end
  1203. def test_hour()
  1204. t = Mysql::Time.new
  1205. assert_equal(15, t.hour = 15)
  1206. assert_equal(15, t.hour)
  1207. end
  1208. def test_minute()
  1209. t = Mysql::Time.new
  1210. assert_equal(58, t.month = 58)
  1211. assert_equal(58, t.month)
  1212. end
  1213. def test_second()
  1214. t = Mysql::Time.new
  1215. assert_equal(34, t.second = 34)
  1216. assert_equal(34, t.second)
  1217. end
  1218. def test_tos()
  1219. t = Mysql::Time.new(2005, 7, 19, 10, 15, 49)
  1220. assert_equal("2005-07-19 10:15:49", t.to_s)
  1221. end
  1222. def test_eql()
  1223. t1 = Mysql::Time.new(2005,7,19,23,56,13)
  1224. t2 = Mysql::Time.new(2005,7,19,23,56,13)
  1225. assert_equal(t1, t2)
  1226. end
  1227. end if Mysql.client_version >= 40100