ruby-mysql 2.11.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test/test_mysql.rb CHANGED
@@ -20,28 +20,71 @@ MYSQL_SOCKET = ENV['MYSQL_SOCKET']
20
20
  class TestMysql < Test::Unit::TestCase
21
21
  sub_test_case 'Mysql::VERSION' do
22
22
  test 'returns client version' do
23
- assert{ Mysql::VERSION == 21101 }
23
+ assert{ Mysql::VERSION == '3.0.0' }
24
24
  end
25
25
  end
26
26
 
27
- sub_test_case 'Mysql.init' do
27
+ sub_test_case 'Mysql.new' do
28
28
  test 'returns Mysql object' do
29
- assert{ Mysql.init.kind_of? Mysql }
29
+ assert{ Mysql.new.kind_of? Mysql }
30
30
  end
31
31
  end
32
32
 
33
- sub_test_case 'Mysql.real_connect' do
34
- test 'connect to mysqld' do
35
- @m = Mysql.real_connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
36
- assert{ @m.kind_of? Mysql }
37
- end
38
-
39
- test 'flag argument affects' do
40
- @m = Mysql.real_connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET, Mysql::CLIENT_FOUND_ROWS)
41
- @m.query 'create temporary table t (c int)'
42
- @m.query 'insert into t values (123)'
43
- @m.query 'update t set c=123'
44
- assert{ @m.affected_rows == 1 }
33
+ sub_test_case 'arguments' do
34
+ test 'with fixed arguments' do
35
+ @m = Mysql.new('127.0.0.1', 'hoge', 'abc&def', 'test', 3306, '/tmp/socket', 12345)
36
+ assert{ @m.host == '127.0.0.1' }
37
+ assert{ @m.username == 'hoge' }
38
+ assert{ @m.password == 'abc&def' }
39
+ assert{ @m.database == 'test' }
40
+ assert{ @m.port == 3306 }
41
+ assert{ @m.socket == '/tmp/socket' }
42
+ assert{ @m.flags == 12345 }
43
+ end
44
+
45
+ test 'with keyword arguments' do
46
+ @m = Mysql.new(host: '127.0.0.1', username: 'hoge', password: 'abc&def', database: 'test', port: 3306, socket: '/tmp/socket', flags: 12345)
47
+ assert{ @m.host == '127.0.0.1' }
48
+ assert{ @m.username == 'hoge' }
49
+ assert{ @m.password == 'abc&def' }
50
+ assert{ @m.database == 'test' }
51
+ assert{ @m.port == 3306 }
52
+ assert{ @m.socket == '/tmp/socket' }
53
+ assert{ @m.flags == 12345 }
54
+ end
55
+
56
+ test 'with URI' do
57
+ uri = URI.parse("mysql://hoge:abc%26def@127.0.0.1:3306/test?socket=/tmp/socket&flags=12345")
58
+ @m = Mysql.new(uri)
59
+ assert{ @m.host == '127.0.0.1' }
60
+ assert{ @m.username == 'hoge' }
61
+ assert{ @m.password == 'abc&def' }
62
+ assert{ @m.database == 'test' }
63
+ assert{ @m.port == 3306 }
64
+ assert{ @m.socket == '/tmp/socket' }
65
+ assert{ @m.flags == 12345 }
66
+ end
67
+
68
+ test 'with URI string' do
69
+ @m = Mysql.new("mysql://hoge:abc%26def@127.0.0.1:3306/test?socket=/tmp/socket&flags=12345")
70
+ assert{ @m.host == '127.0.0.1' }
71
+ assert{ @m.username == 'hoge' }
72
+ assert{ @m.password == 'abc&def' }
73
+ assert{ @m.database == 'test' }
74
+ assert{ @m.port == 3306 }
75
+ assert{ @m.socket == '/tmp/socket' }
76
+ assert{ @m.flags == 12345 }
77
+ end
78
+
79
+ test 'with URI string: host is filename' do
80
+ @m = Mysql.new("mysql://hoge:abc%26def@%2Ftmp%2Fsocket:3306/test?flags=12345")
81
+ assert{ @m.host == '' }
82
+ assert{ @m.username == 'hoge' }
83
+ assert{ @m.password == 'abc&def' }
84
+ assert{ @m.database == 'test' }
85
+ assert{ @m.port == 3306 }
86
+ assert{ @m.socket == '/tmp/socket' }
87
+ assert{ @m.flags == 12345 }
45
88
  end
46
89
 
47
90
  teardown do
@@ -55,16 +98,14 @@ class TestMysql < Test::Unit::TestCase
55
98
  assert{ @m.kind_of? Mysql }
56
99
  end
57
100
 
58
- teardown do
59
- @m.close if @m
101
+ test 'flag argument affects' do
102
+ @m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET, Mysql::CLIENT_FOUND_ROWS)
103
+ @m.query 'create temporary table t (c int)'
104
+ @m.query 'insert into t values (123)'
105
+ @m.query 'update t set c=123'
106
+ assert{ @m.affected_rows == 1 }
60
107
  end
61
- end
62
108
 
63
- sub_test_case 'Mysql.new' do
64
- test 'connect to mysqld' do
65
- @m = Mysql.new(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
66
- assert{ @m.kind_of? Mysql }
67
- end
68
109
  teardown do
69
110
  @m.close if @m
70
111
  end
@@ -82,64 +123,41 @@ class TestMysql < Test::Unit::TestCase
82
123
  end
83
124
  end
84
125
 
85
- sub_test_case 'Mysql.client_info' do
86
- test 'returns client version as string' do
87
- assert{ Mysql.client_info == '5.0.0' }
88
- end
89
- end
90
-
91
- sub_test_case 'Mysql.get_client_info' do
92
- test 'returns client version as string' do
93
- assert{ Mysql.get_client_info == '5.0.0' }
126
+ sub_test_case 'Mysql#connect' do
127
+ test 'connect to mysqld' do
128
+ @m = Mysql.new(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
129
+ assert{ @m.connect == @m }
94
130
  end
95
- end
96
131
 
97
- sub_test_case 'Mysql.client_version' do
98
- test 'returns client version as Integer' do
99
- assert{ Mysql.client_version == 50000 }
132
+ test 'connect to mysqld by URI' do
133
+ @m = Mysql.new("mysql://#{MYSQL_USER}:#{MYSQL_PASSWORD}@#{MYSQL_SERVER}:#{MYSQL_PORT}/#{MYSQL_DATABASE}?socket=#{MYSQL_SOCKET}")
134
+ assert{ @m.connect == @m }
100
135
  end
101
- end
102
136
 
103
- sub_test_case 'Mysql.get_client_version' do
104
- test 'returns client version as Integer' do
105
- assert{ Mysql.client_version == 50000 }
106
- end
107
- end
108
-
109
- sub_test_case 'Mysql#real_connect' do
110
- test 'connect to mysqld' do
111
- @m = Mysql.init
112
- assert{ @m.real_connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET) == @m }
113
- end
114
- teardown do
115
- @m.close if @m
137
+ test 'overrides arguments of new method' do
138
+ @m = Mysql.new('example.com', 12345)
139
+ @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
116
140
  end
117
- end
118
141
 
119
- sub_test_case 'Mysql#connect' do
120
- test 'connect to mysqld' do
121
- @m = Mysql.init
122
- assert{ @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET) == @m }
123
- end
124
142
  teardown do
125
143
  @m.close if @m
126
144
  end
127
145
  end
128
146
 
129
- sub_test_case 'Mysql#options' do
147
+ sub_test_case 'options' do
130
148
  setup do
131
- @m = Mysql.init
149
+ @m = Mysql.new
132
150
  end
133
151
  teardown do
134
152
  @m.close
135
153
  end
136
- test 'INIT_COMMAND: execute query when connecting' do
137
- assert{ @m.options(Mysql::INIT_COMMAND, "SET AUTOCOMMIT=0") == @m }
154
+ test 'init_command: execute query when connecting' do
155
+ @m.init_command = "SET AUTOCOMMIT=0"
138
156
  assert{ @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET) == @m }
139
157
  assert{ @m.query('select @@AUTOCOMMIT').fetch_row == ["0"] }
140
158
  end
141
- test 'OPT_CONNECT_TIMEOUT: set timeout for connecting' do
142
- assert{ @m.options(Mysql::OPT_CONNECT_TIMEOUT, 0.1) == @m }
159
+ test 'connect_timeout: set timeout for connecting' do
160
+ @m.connect_timeout = 0.1
143
161
  stub(Socket).tcp{ raise Errno::ETIMEDOUT }
144
162
  stub(Socket).unix{ raise Errno::ETIMEDOUT }
145
163
  assert_raise Mysql::ClientError, 'connection timeout' do
@@ -149,41 +167,41 @@ class TestMysql < Test::Unit::TestCase
149
167
  @m.connect
150
168
  end
151
169
  end
152
- test 'OPT_LOCAL_INFILE: client can execute LOAD DATA LOCAL INFILE query' do
170
+ test 'local_infile: client can execute LOAD DATA LOCAL INFILE query' do
153
171
  require 'tempfile'
154
172
  tmpf = Tempfile.new 'mysql_spec'
155
173
  tmpf.puts "123\tabc\n"
156
174
  tmpf.close
157
- assert{ @m.options(Mysql::OPT_LOCAL_INFILE, true) == @m }
175
+ @m.local_infile = true
158
176
  @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
159
177
  @m.query('create temporary table t (i int, c char(10))')
160
178
  @m.query("load data local infile '#{tmpf.path}' into table t")
161
179
  assert{ @m.query('select * from t').fetch_row == ['123','abc'] }
162
180
  end
163
- test 'OPT_LOAD_DATA_LOCAL_DIR: client can execute LOAD DATA LOCAL INFILE query with specified directory' do
181
+ test 'load_data_local_dir: client can execute LOAD DATA LOCAL INFILE query with specified directory' do
164
182
  require 'tempfile'
165
183
  tmpf = Tempfile.new 'mysql_spec'
166
184
  tmpf.puts "123\tabc\n"
167
185
  tmpf.close
168
- assert{ @m.options(Mysql::OPT_LOAD_DATA_LOCAL_DIR, File.dirname(tmpf.path)) == @m }
186
+ @m.load_data_local_dir = File.dirname(tmpf.path)
169
187
  @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
170
188
  @m.query('create temporary table t (i int, c char(10))')
171
189
  @m.query("load data local infile '#{tmpf.path}' into table t")
172
190
  assert{ @m.query('select * from t').fetch_row == ['123','abc'] }
173
191
  end
174
- test 'OPT_LOAD_DATA_LOCAL_DIR: client cannot execute LOAD DATA LOCAL INFILE query without specified directory' do
192
+ test 'load_data_local_dir: client cannot execute LOAD DATA LOCAL INFILE query without specified directory' do
175
193
  require 'tempfile'
176
194
  tmpf = Tempfile.new 'mysql_spec'
177
195
  tmpf.puts "123\tabc\n"
178
196
  tmpf.close
179
- assert{ @m.options(Mysql::OPT_LOAD_DATA_LOCAL_DIR, '/hoge') == @m }
197
+ @m.load_data_local_dir = '/hoge'
180
198
  @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
181
199
  @m.query('create temporary table t (i int, c char(10))')
182
200
  assert_raise Mysql::ClientError::LoadDataLocalInfileRejected, 'LOAD DATA LOCAL INFILE file request rejected due to restrictions on access.' do
183
201
  @m.query("load data local infile '#{tmpf.path}' into table t")
184
202
  end
185
203
  end
186
- test 'without OPT_LOCAL_INFILE and OPT_LOAD_DATA_LOCAL_DIR: client cannot execute LOAD DATA LOCAL INFILE query' do
204
+ test 'without local_infile and load_data_local_dir: client cannot execute LOAD DATA LOCAL INFILE query' do
187
205
  require 'tempfile'
188
206
  tmpf = Tempfile.new 'mysql_spec'
189
207
  tmpf.puts "123\tabc\n"
@@ -200,18 +218,18 @@ class TestMysql < Test::Unit::TestCase
200
218
  end
201
219
  end
202
220
  end
203
- test 'OPT_READ_TIMEOUT: set timeout for reading packet' do
204
- assert{ @m.options(Mysql::OPT_READ_TIMEOUT, 1) == @m }
221
+ test 'read_timeout: set timeout for reading packet' do
222
+ @m.read_timeout = 1
205
223
  @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
206
224
  @m.query("select 123").entries
207
225
  end
208
- test 'OPT_WRITE_TIMEOUT: set timeout for writing packet' do
209
- assert{ @m.options(Mysql::OPT_WRITE_TIMEOUT, 1) == @m }
226
+ test 'write_timeout: set timeout for writing packet' do
227
+ @m.write_timeout = 1
210
228
  @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
211
229
  @m.query("select 123").entries
212
230
  end
213
- test 'SET_CHARSET_NAME: set charset for connection' do
214
- assert{ @m.options(Mysql::SET_CHARSET_NAME, 'utf8mb3') == @m }
231
+ test 'charset: set charset for connection' do
232
+ @m.charset = 'utf8mb3'
215
233
  @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
216
234
  assert do
217
235
  @m.query('select @@character_set_connection').fetch_row == ['utf8mb3'] ||
@@ -222,7 +240,7 @@ class TestMysql < Test::Unit::TestCase
222
240
 
223
241
  sub_test_case 'Mysql' do
224
242
  setup do
225
- @m = Mysql.new(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
243
+ @m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
226
244
  end
227
245
 
228
246
  teardown do
@@ -230,22 +248,9 @@ class TestMysql < Test::Unit::TestCase
230
248
  end
231
249
 
232
250
  sub_test_case '#escape_string' do
233
- if defined? ::Encoding
234
- test 'escape special character for charset' do
235
- @m.charset = 'cp932'
236
- assert{ @m.escape_string("abc'def\"ghi\0jkl%mno_表".encode('cp932')) == "abc\\'def\\\"ghi\\0jkl%mno_表".encode('cp932') }
237
- end
238
- else
239
- test 'raise error if charset is multibyte' do
240
- @m.charset = 'cp932'
241
- assert_raise Mysql::ClientError, 'Mysql#escape_string is called for unsafe multibyte charset' do
242
- @m.escape_string("abc'def\"ghi\0jkl%mno_\x95\\")
243
- end
244
- end
245
- test 'not warn if charset is singlebyte' do
246
- @m.charset = 'latin1'
247
- assert{ @m.escape_string("abc'def\"ghi\0jkl%mno_\x95\\") == "abc\\'def\\\"ghi\\0jkl%mno_\x95\\\\" }
248
- end
251
+ test 'escape special character for charset' do
252
+ @m.charset = 'cp932'
253
+ assert{ @m.escape_string("abc'def\"ghi\0jkl%mno_表".encode('cp932')) == "abc\\'def\\\"ghi\\0jkl%mno_表".encode('cp932') }
249
254
  end
250
255
  end
251
256
 
@@ -255,18 +260,6 @@ class TestMysql < Test::Unit::TestCase
255
260
  end
256
261
  end
257
262
 
258
- sub_test_case '#client_info' do
259
- test 'returns client version as string' do
260
- assert{ @m.client_info == '5.0.0' }
261
- end
262
- end
263
-
264
- sub_test_case '#get_client_info' do
265
- test 'returns client version as string' do
266
- assert{ @m.get_client_info == '5.0.0' }
267
- end
268
- end
269
-
270
263
  sub_test_case '#affected_rows' do
271
264
  test 'returns number of affected rows' do
272
265
  @m.query 'create temporary table t (id int)'
@@ -277,8 +270,8 @@ class TestMysql < Test::Unit::TestCase
277
270
 
278
271
  sub_test_case '#character_set_name' do
279
272
  test 'returns charset name' do
280
- m = Mysql.init
281
- m.options Mysql::SET_CHARSET_NAME, 'cp932'
273
+ m = Mysql.new
274
+ m.charset = 'cp932'
282
275
  m.connect MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET
283
276
  assert{ m.character_set_name == 'cp932' }
284
277
  end
@@ -326,28 +319,6 @@ class TestMysql < Test::Unit::TestCase
326
319
  end
327
320
  end
328
321
 
329
- sub_test_case '#client_version' do
330
- test 'returns client version as Integer' do
331
- assert{ @m.client_version.kind_of? Integer }
332
- end
333
- end
334
-
335
- sub_test_case '#get_client_version' do
336
- test 'returns client version as Integer' do
337
- assert{ @m.get_client_version.kind_of? Integer }
338
- end
339
- end
340
-
341
- sub_test_case '#get_host_info' do
342
- test 'returns connection type as String' do
343
- if MYSQL_SERVER == nil or MYSQL_SERVER == 'localhost'
344
- assert{ @m.get_host_info == 'Localhost via UNIX socket' }
345
- else
346
- assert{ @m.get_host_info == "#{MYSQL_SERVER} via TCP/IP" }
347
- end
348
- end
349
- end
350
-
351
322
  sub_test_case '#host_info' do
352
323
  test 'returns connection type as String' do
353
324
  if MYSQL_SERVER == nil or MYSQL_SERVER == 'localhost'
@@ -358,24 +329,6 @@ class TestMysql < Test::Unit::TestCase
358
329
  end
359
330
  end
360
331
 
361
- sub_test_case '#get_proto_info' do
362
- test 'returns version of connection as Integer' do
363
- assert{ @m.get_proto_info == 10 }
364
- end
365
- end
366
-
367
- sub_test_case '#proto_info' do
368
- test 'returns version of connection as Integer' do
369
- assert{ @m.proto_info == 10 }
370
- end
371
- end
372
-
373
- sub_test_case '#get_server_info' do
374
- test 'returns server version as String' do
375
- assert{ @m.get_server_info =~ /\A\d+\.\d+\.\d+/ }
376
- end
377
- end
378
-
379
332
  sub_test_case '#server_info' do
380
333
  test 'returns server version as String' do
381
334
  assert{ @m.server_info =~ /\A\d+\.\d+\.\d+/ }
@@ -403,7 +356,7 @@ class TestMysql < Test::Unit::TestCase
403
356
 
404
357
  sub_test_case '#kill' do
405
358
  setup do
406
- @m2 = Mysql.new(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
359
+ @m2 = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
407
360
  end
408
361
  teardown do
409
362
  @m2.close rescue nil
@@ -413,61 +366,6 @@ class TestMysql < Test::Unit::TestCase
413
366
  end
414
367
  end
415
368
 
416
- sub_test_case '#list_dbs' do
417
- test 'returns database list' do
418
- ret = @m.list_dbs
419
- assert{ ret.kind_of? Array }
420
- assert{ ret.include? MYSQL_DATABASE }
421
- end
422
- test 'with pattern returns databases that matches pattern' do
423
- assert{ @m.list_dbs('info%').include? 'information_schema' }
424
- end
425
- end
426
-
427
- sub_test_case '#list_fields' do
428
- setup do
429
- @m.query 'create temporary table t (i int, c char(10), d date)'
430
- end
431
- test 'returns result set that contains information of fields' do
432
- ret = @m.list_fields('t')
433
- assert{ ret.kind_of? Mysql::Result }
434
- assert{ ret.num_rows == 0 }
435
- assert{ ret.fetch_fields.map{|f|f.name} == ['i','c','d'] }
436
- end
437
- test 'with pattern returns result set that contains information of fields that matches pattern' do
438
- ret = @m.list_fields('t', 'i')
439
- assert{ ret.kind_of? Mysql::Result }
440
- assert{ ret.num_rows == 0 }
441
- ret.fetch_fields.map{|f|f.name} == ['i']
442
- end
443
- end
444
-
445
- sub_test_case '#list_processes' do
446
- test 'returns result set that contains information of all connections' do
447
- ret = @m.list_processes
448
- assert{ ret.kind_of? Mysql::Result }
449
- assert{ ret.find{|r|r[0].to_i == @m.thread_id}[4] == "Processlist" }
450
- end
451
- end
452
-
453
- sub_test_case '#list_tables' do
454
- setup do
455
- @m.query 'create table test_mysql_list_tables (id int)'
456
- end
457
- teardown do
458
- @m.query 'drop table if exists test_mysql_list_tables'
459
- end
460
- test 'returns table list' do
461
- ret = @m.list_tables
462
- assert{ ret.kind_of? Array }
463
- assert{ ret.include? 'test_mysql_list_tables' }
464
- end
465
- test 'with pattern returns lists that matches pattern' do
466
- ret = @m.list_tables '%mysql\_list\_t%'
467
- assert{ ret.include? 'test_mysql_list_tables' }
468
- end
469
- end
470
-
471
369
  sub_test_case '#ping' do
472
370
  test 'returns self' do
473
371
  assert{ @m.ping == @m }
@@ -481,17 +379,8 @@ class TestMysql < Test::Unit::TestCase
481
379
  test 'returns nil if query returns no results' do
482
380
  assert{ @m.query('set @hoge:=123') == nil }
483
381
  end
484
- test 'returns self if query_with_result is false' do
485
- @m.query_with_result = false
486
- assert{ @m.query('select 123') == @m }
487
- @m.store_result
488
- assert{ @m.query('set @hoge:=123') == @m }
489
- end
490
- end
491
-
492
- sub_test_case '#real_query' do
493
- test 'is same as #query' do
494
- assert{ @m.real_query('select 123').kind_of? Mysql::Result }
382
+ test 'returns self if block is specified' do
383
+ assert{ @m.query('select 123'){} == @m }
495
384
  end
496
385
  end
497
386
 
@@ -523,60 +412,12 @@ class TestMysql < Test::Unit::TestCase
523
412
  end
524
413
  end
525
414
 
526
- sub_test_case '#store_result' do
527
- test 'returns Mysql::Result' do
528
- @m.query_with_result = false
529
- @m.query 'select 1,2,3'
530
- ret = @m.store_result
531
- assert{ ret.kind_of? Mysql::Result }
532
- assert{ ret.fetch_row == ['1','2','3'] }
533
- end
534
- test 'raises error when no query' do
535
- assert_raise Mysql::ClientError, 'invalid usage' do
536
- @m.store_result
537
- end
538
- end
539
- test 'raises error when query does not return results' do
540
- @m.query 'set @hoge:=123'
541
- assert_raise Mysql::ClientError, 'invalid usage' do
542
- @m.store_result
543
- end
544
- end
545
- end
546
-
547
415
  sub_test_case '#thread_id' do
548
416
  test 'returns thread id as Integer' do
549
417
  assert{ @m.thread_id.kind_of? Integer }
550
418
  end
551
419
  end
552
420
 
553
- sub_test_case '#use_result' do
554
- test 'returns Mysql::Result' do
555
- @m.query_with_result = false
556
- @m.query 'select 1,2,3'
557
- ret = @m.use_result
558
- assert{ ret.kind_of? Mysql::Result }
559
- assert{ ret.fetch_row == ['1','2','3'] }
560
- end
561
- test 'raises error when no query' do
562
- assert_raise Mysql::ClientError, 'invalid usage' do
563
- @m.use_result
564
- end
565
- end
566
- test 'raises error when query does not return results' do
567
- @m.query 'set @hoge:=123'
568
- assert_raise Mysql::ClientError, 'invalid usage' do
569
- @m.use_result
570
- end
571
- end
572
- end
573
-
574
- sub_test_case '#get_server_version' do
575
- test 'returns server version as Integer' do
576
- assert{ @m.get_server_version.kind_of? Integer }
577
- end
578
- end
579
-
580
421
  sub_test_case '#server_version' do
581
422
  test 'returns server version as Integer' do
582
423
  assert{ @m.server_version.kind_of? Integer }
@@ -641,27 +482,6 @@ class TestMysql < Test::Unit::TestCase
641
482
  end
642
483
  end
643
484
 
644
- sub_test_case '#query_with_result' do
645
- test 'default value is true' do
646
- assert{ @m.query_with_result == true }
647
- end
648
- test 'can set value' do
649
- assert{ (@m.query_with_result=true) == true }
650
- assert{ @m.query_with_result == true }
651
- assert{ (@m.query_with_result=false) == false }
652
- assert{ @m.query_with_result == false }
653
- end
654
- end
655
-
656
- sub_test_case '#query_with_result is false' do
657
- test 'Mysql#query returns self and Mysql#store_result returns result set' do
658
- @m.query_with_result = false
659
- assert{ @m.query('select 1,2,3') == @m }
660
- res = @m.store_result
661
- assert{ res.fetch_row == ['1','2','3'] }
662
- end
663
- end
664
-
665
485
  sub_test_case '#query with block' do
666
486
  test 'returns self' do
667
487
  assert{ @m.query('select 1'){} == @m }
@@ -694,7 +514,7 @@ class TestMysql < Test::Unit::TestCase
694
514
 
695
515
  sub_test_case 'multiple statement query:' do
696
516
  setup do
697
- @m = Mysql.new(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
517
+ @m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
698
518
  @m.set_server_option(Mysql::OPTION_MULTI_STATEMENTS_ON)
699
519
  @res = @m.query 'select 1,2; select 3,4,5'
700
520
  end
@@ -732,7 +552,7 @@ class TestMysql < Test::Unit::TestCase
732
552
 
733
553
  sub_test_case 'Mysql::Result' do
734
554
  setup do
735
- @m = Mysql.new(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
555
+ @m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
736
556
  @m.charset = 'latin1'
737
557
  @m.query 'create temporary table t (id int default 0, str char(10), primary key (id))'
738
558
  @m.query "insert into t values (1,'abc'),(2,'defg'),(3,'hi'),(4,null)"
@@ -888,7 +708,7 @@ class TestMysql < Test::Unit::TestCase
888
708
 
889
709
  sub_test_case 'Mysql::Field' do
890
710
  setup do
891
- @m = Mysql.new(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
711
+ @m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
892
712
  @m.charset = 'latin1'
893
713
  @m.query 'create temporary table t (id int default 0, str char(10), primary key (id))'
894
714
  @m.query "insert into t values (1,'abc'),(2,'defg'),(3,'hi'),(4,null)"
@@ -911,10 +731,6 @@ class TestMysql < Test::Unit::TestCase
911
731
  assert{ @res.fetch_field.def == nil }
912
732
  end
913
733
 
914
- test '#def for field information is default value' do
915
- assert{ @m.list_fields('t').fetch_field.def == '0' }
916
- end
917
-
918
734
  test '#type is type of field as Integer' do
919
735
  assert{ @res.fetch_field.type == Mysql::Field::TYPE_LONG }
920
736
  assert{ @res.fetch_field.type == Mysql::Field::TYPE_STRING }
@@ -939,8 +755,8 @@ class TestMysql < Test::Unit::TestCase
939
755
  assert{ @m.query('select 1.23').fetch_field.decimals == 2 }
940
756
  end
941
757
 
942
- test '#hash return field as hash' do
943
- assert{ @res.fetch_field.hash == {
758
+ test '#to_hash return field as hash' do
759
+ assert{ @res.fetch_field.to_hash == {
944
760
  'name' => 'id',
945
761
  'table' => 't',
946
762
  'def' => nil,
@@ -951,7 +767,7 @@ class TestMysql < Test::Unit::TestCase
951
767
  'decimals' => 0,
952
768
  }
953
769
  }
954
- assert{ @res.fetch_field.hash == {
770
+ assert{ @res.fetch_field.to_hash == {
955
771
  'name' => 'str',
956
772
  'table' => 't',
957
773
  'def' => nil,
@@ -987,15 +803,15 @@ class TestMysql < Test::Unit::TestCase
987
803
 
988
804
  sub_test_case 'create Mysql::Stmt object:' do
989
805
  setup do
990
- @m = Mysql.new(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
806
+ @m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
991
807
  end
992
808
 
993
809
  teardown do
994
810
  @m.close if @m
995
811
  end
996
812
 
997
- test 'Mysql#stmt_init returns Mysql::Stmt object' do
998
- assert{ @m.stmt_init.kind_of? Mysql::Stmt }
813
+ test 'Mysql#stmt returns Mysql::Stmt object' do
814
+ assert{ @m.stmt.kind_of? Mysql::Stmt }
999
815
  end
1000
816
 
1001
817
  test 'Mysq;#prepare returns Mysql::Stmt object' do
@@ -1005,9 +821,9 @@ class TestMysql < Test::Unit::TestCase
1005
821
 
1006
822
  sub_test_case 'Mysql::Stmt' do
1007
823
  setup do
1008
- @m = Mysql.new(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
824
+ @m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
1009
825
  @m.query("set sql_mode=''")
1010
- @s = @m.stmt_init
826
+ @s = @m.stmt
1011
827
  end
1012
828
 
1013
829
  teardown do
@@ -1027,62 +843,6 @@ class TestMysql < Test::Unit::TestCase
1027
843
  assert{ @s.affected_rows == 3 }
1028
844
  end
1029
845
 
1030
- sub_test_case '#bind_result' do
1031
- setup do
1032
- @m.query 'create temporary table t (i int, c char(10), d double, t datetime)'
1033
- @m.query 'insert into t values (123,"9abcdefg",1.2345,20091208100446)'
1034
- @s.prepare 'select * from t'
1035
- end
1036
-
1037
- test '(nil) make result format to be standard value' do
1038
- @s.bind_result nil, nil, nil, nil
1039
- @s.execute
1040
- assert{ @s.fetch == [123, '9abcdefg', 1.2345, Mysql::Time.new(2009,12,8,10,4,46)] }
1041
- end
1042
-
1043
- test '(Numeric) make result format to be Integer value' do
1044
- @s.bind_result Numeric, Numeric, Numeric, Numeric
1045
- @s.execute
1046
- assert{ @s.fetch == [123, 9, 1, 20091208100446] }
1047
- end
1048
-
1049
- test '(Integer) make result format to be Integer value' do
1050
- @s.bind_result Integer, Integer, Integer, Integer
1051
- @s.execute
1052
- assert{ @s.fetch == [123, 9, 1, 20091208100446] }
1053
- end
1054
-
1055
- test '(String) make result format to be String value' do
1056
- @s.bind_result String, String, String, String
1057
- @s.execute
1058
- assert{ @s.fetch == ["123", "9abcdefg", "1.2345", "2009-12-08 10:04:46"] }
1059
- end
1060
-
1061
- test '(Float) make result format to be Float value' do
1062
- @s.bind_result Float, Float, Float, Float
1063
- @s.execute
1064
- assert{ @s.fetch == [123.0, 9.0, 1.2345 , 20091208100446.0] }
1065
- end
1066
-
1067
- test '(Mysql::Time) make result format to be Mysql::Time value' do
1068
- @s.bind_result Mysql::Time, Mysql::Time, Mysql::Time, Mysql::Time
1069
- @s.execute
1070
- assert{ @s.fetch == [Mysql::Time.new(2000,1,23), Mysql::Time.new, Mysql::Time.new, Mysql::Time.new(2009,12,8,10,4,46)] }
1071
- end
1072
-
1073
- test '(invalid) raises error' do
1074
- assert_raise TypeError do
1075
- @s.bind_result(Time, nil, nil, nil)
1076
- end
1077
- end
1078
-
1079
- test 'with mismatch argument count raise error' do
1080
- assert_raise Mysql::ClientError, 'bind_result: result value count(4) != number of argument(1)' do
1081
- @s.bind_result(nil)
1082
- end
1083
- end
1084
- end
1085
-
1086
846
  test '#close returns nil' do
1087
847
  assert{ @s.close == nil }
1088
848
  end
@@ -1107,8 +867,8 @@ class TestMysql < Test::Unit::TestCase
1107
867
  @s.prepare 'select * from t'
1108
868
  @s.execute
1109
869
  expect = [
1110
- [1, 'abc', Mysql::Time.new(1970,12,24,23,59,05)],
1111
- [2, 'def', Mysql::Time.new(2112,9,3,12,34,56)],
870
+ [1, 'abc', Time.new(1970,12,24,23,59,05)],
871
+ [2, 'def', Time.new(2112,9,3,12,34,56)],
1112
872
  [3, '123', nil],
1113
873
  ]
1114
874
  @s.each do |a|
@@ -1254,20 +1014,16 @@ class TestMysql < Test::Unit::TestCase
1254
1014
  @m.query 'insert into t values (0),(-1),(127),(-128),(255),(-255),(256)'
1255
1015
  @s.prepare 'select i from t'
1256
1016
  @s.execute
1257
- if defined? Encoding
1258
- assert{ @s.entries == [
1259
- ["\x00".force_encoding('ASCII-8BIT')],
1260
- ["\xff".force_encoding('ASCII-8BIT')],
1261
- ["\x7f".force_encoding('ASCII-8BIT')],
1262
- ["\xff".force_encoding('ASCII-8BIT')],
1263
- ["\xff".force_encoding('ASCII-8BIT')],
1264
- ["\xff".force_encoding('ASCII-8BIT')],
1265
- ["\xff".force_encoding('ASCII-8BIT')],
1266
- ]
1267
- }
1268
- else
1269
- assert{ @s.entries == [["\x00"], ["\xff"], ["\x7f"], ["\xff"], ["\xff"], ["\xff"], ["\xff"]] }
1270
- end
1017
+ assert{ @s.entries == [
1018
+ ["\x00".force_encoding('ASCII-8BIT')],
1019
+ ["\xff".force_encoding('ASCII-8BIT')],
1020
+ ["\x7f".force_encoding('ASCII-8BIT')],
1021
+ ["\xff".force_encoding('ASCII-8BIT')],
1022
+ ["\xff".force_encoding('ASCII-8BIT')],
1023
+ ["\xff".force_encoding('ASCII-8BIT')],
1024
+ ["\xff".force_encoding('ASCII-8BIT')],
1025
+ ]
1026
+ }
1271
1027
  end
1272
1028
 
1273
1029
  test '#fetch bit column (64bit)' do
@@ -1275,25 +1031,14 @@ class TestMysql < Test::Unit::TestCase
1275
1031
  @m.query 'insert into t values (0),(-1),(4294967296),(18446744073709551615),(18446744073709551616)'
1276
1032
  @s.prepare 'select i from t'
1277
1033
  @s.execute
1278
- if defined? Encoding
1279
- assert{ @s.entries == [
1280
- ["\x00\x00\x00\x00\x00\x00\x00\x00".force_encoding('ASCII-8BIT')],
1281
- ["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT')],
1282
- ["\x00\x00\x00\x01\x00\x00\x00\x00".force_encoding('ASCII-8BIT')],
1283
- ["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT')],
1284
- ["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT')],
1285
- ]
1286
- }
1287
- else
1288
- assert{ @s.entries == [
1289
- ["\x00\x00\x00\x00\x00\x00\x00\x00"],
1290
- ["\xff\xff\xff\xff\xff\xff\xff\xff"],
1291
- ["\x00\x00\x00\x01\x00\x00\x00\x00"],
1292
- ["\xff\xff\xff\xff\xff\xff\xff\xff"],
1293
- ["\xff\xff\xff\xff\xff\xff\xff\xff"],
1294
- ]
1295
- }
1296
- end
1034
+ assert{ @s.entries == [
1035
+ ["\x00\x00\x00\x00\x00\x00\x00\x00".force_encoding('ASCII-8BIT')],
1036
+ ["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT')],
1037
+ ["\x00\x00\x00\x01\x00\x00\x00\x00".force_encoding('ASCII-8BIT')],
1038
+ ["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT')],
1039
+ ["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT')],
1040
+ ]
1041
+ }
1297
1042
  end
1298
1043
 
1299
1044
  test '#fetch tinyint column' do
@@ -1446,14 +1191,11 @@ class TestMysql < Test::Unit::TestCase
1446
1191
  @s.prepare 'select i from t'
1447
1192
  @s.execute
1448
1193
  cols = @s.fetch
1449
- assert{ cols == [Mysql::Time.new] }
1450
- assert{ cols.first.to_s == '0000-00-00' }
1194
+ assert{ cols == [nil] }
1451
1195
  cols = @s.fetch
1452
- assert{ cols == [Mysql::Time.new(1000,1,1)] }
1453
- assert{ cols.first.to_s == '1000-01-01' }
1196
+ assert{ cols == [Time.new(1000,1,1)] }
1454
1197
  cols = @s.fetch
1455
- assert{ cols == [Mysql::Time.new(9999,12,31)] }
1456
- assert{ cols.first.to_s == '9999-12-31' }
1198
+ assert{ cols == [Time.new(9999,12,31)] }
1457
1199
  end
1458
1200
 
1459
1201
  test '#fetch datetime column' do
@@ -1461,9 +1203,9 @@ class TestMysql < Test::Unit::TestCase
1461
1203
  @m.query "insert into t values ('0000-00-00 00:00:00'),('1000-01-01 00:00:00'),('9999-12-31 23:59:59')"
1462
1204
  @s.prepare 'select i from t'
1463
1205
  @s.execute
1464
- assert{ @s.fetch == [Mysql::Time.new] }
1465
- assert{ @s.fetch == [Mysql::Time.new(1000,1,1)] }
1466
- assert{ @s.fetch == [Mysql::Time.new(9999,12,31,23,59,59)] }
1206
+ assert{ @s.fetch == [nil] }
1207
+ assert{ @s.fetch == [Time.new(1000,1,1)] }
1208
+ assert{ @s.fetch == [Time.new(9999,12,31,23,59,59)] }
1467
1209
  end
1468
1210
 
1469
1211
  test '#fetch timestamp column' do
@@ -1471,8 +1213,8 @@ class TestMysql < Test::Unit::TestCase
1471
1213
  @m.query("insert into t values ('1970-01-02 00:00:00'),('2037-12-30 23:59:59')")
1472
1214
  @s.prepare 'select i from t'
1473
1215
  @s.execute
1474
- assert{ @s.fetch == [Mysql::Time.new(1970,1,2)] }
1475
- assert{ @s.fetch == [Mysql::Time.new(2037,12,30,23,59,59)] }
1216
+ assert{ @s.fetch == [Time.new(1970,1,2)] }
1217
+ assert{ @s.fetch == [Time.new(2037,12,30,23,59,59)] }
1476
1218
  end
1477
1219
 
1478
1220
  test '#fetch time column' do
@@ -1480,9 +1222,9 @@ class TestMysql < Test::Unit::TestCase
1480
1222
  @m.query "insert into t values ('-838:59:59'),(0),('838:59:59')"
1481
1223
  @s.prepare 'select i from t'
1482
1224
  @s.execute
1483
- assert{ @s.fetch == [Mysql::Time.new(0,0,0,838,59,59,true)] }
1484
- assert{ @s.fetch == [Mysql::Time.new(0,0,0,0,0,0,false)] }
1485
- assert{ @s.fetch == [Mysql::Time.new(0,0,0,838,59,59,false)] }
1225
+ assert{ @s.fetch == [-(838*3600+59*60+59)] }
1226
+ assert{ @s.fetch == [0] }
1227
+ assert{ @s.fetch == [838*3600+59*60+59] }
1486
1228
  end
1487
1229
 
1488
1230
  test '#fetch year column' do
@@ -1707,79 +1449,6 @@ class TestMysql < Test::Unit::TestCase
1707
1449
  end
1708
1450
  end
1709
1451
 
1710
- sub_test_case 'Mysql::Time' do
1711
- setup do
1712
- @t = Mysql::Time.new
1713
- end
1714
-
1715
- test '.new with no arguments returns 0' do
1716
- assert{ @t.year == 0 }
1717
- assert{ @t.month == 0 }
1718
- assert{ @t.day == 0 }
1719
- assert{ @t.hour == 0 }
1720
- assert{ @t.minute == 0 }
1721
- assert{ @t.second == 0 }
1722
- assert{ @t.neg == false }
1723
- assert{ @t.second_part == 0 }
1724
- end
1725
-
1726
- test '#inspect' do
1727
- assert{ Mysql::Time.new(2009,12,8,23,35,21).inspect == '#<Mysql::Time:2009-12-08 23:35:21>' }
1728
- end
1729
-
1730
- test '#to_s' do
1731
- assert{ Mysql::Time.new(2009,12,8,23,35,21).to_s == '2009-12-08 23:35:21' }
1732
- end
1733
-
1734
- test '#to_i' do
1735
- assert{ Mysql::Time.new(2009,12,8,23,35,21).to_i == 20091208233521 }
1736
- end
1737
-
1738
- test '#year' do
1739
- assert{ (@t.year = 2009) == 2009 }
1740
- assert{ @t.year == 2009 }
1741
- end
1742
-
1743
- test '#month' do
1744
- assert{ (@t.month = 12) == 12 }
1745
- assert{ @t.month == 12 }
1746
- end
1747
-
1748
- test '#day' do
1749
- assert{ (@t.day = 8) == 8 }
1750
- assert{ @t.day == 8 }
1751
- end
1752
-
1753
- test '#hour' do
1754
- assert{ (@t.hour = 23) == 23 }
1755
- assert{ @t.hour == 23 }
1756
- end
1757
-
1758
- test '#minute' do
1759
- assert{ (@t.minute = 35) == 35 }
1760
- assert{ @t.minute == 35 }
1761
- end
1762
-
1763
- test '#second' do
1764
- assert{ (@t.second = 21) == 21 }
1765
- assert{ @t.second == 21 }
1766
- end
1767
-
1768
- test '#neg' do
1769
- assert{ @t.neg == false }
1770
- end
1771
-
1772
- test '#second_part' do
1773
- assert{ @t.second_part == 0 }
1774
- end
1775
-
1776
- test '#==' do
1777
- t1 = Mysql::Time.new 2009,12,8,23,35,21
1778
- t2 = Mysql::Time.new 2009,12,8,23,35,21
1779
- assert{ t1 == t2 }
1780
- end
1781
- end
1782
-
1783
1452
  sub_test_case 'Mysql::Error' do
1784
1453
  setup do
1785
1454
  m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
@@ -1802,96 +1471,94 @@ class TestMysql < Test::Unit::TestCase
1802
1471
  end
1803
1472
  end
1804
1473
 
1805
- if defined? Encoding
1806
- sub_test_case 'Connection charset is UTF-8:' do
1807
- setup do
1808
- @m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
1809
- @m.charset = "utf8"
1810
- @m.query "create temporary table t (utf8 char(10) charset utf8, cp932 char(10) charset cp932, eucjp char(10) charset eucjpms, bin varbinary(10))"
1811
- @utf8 = "いろは"
1812
- @cp932 = @utf8.encode "CP932"
1813
- @eucjp = @utf8.encode "EUC-JP-MS"
1814
- @bin = "\x00\x01\x7F\x80\xFE\xFF".force_encoding("ASCII-8BIT")
1815
- @default_internal = Encoding.default_internal
1816
- end
1474
+ sub_test_case 'Connection charset is UTF-8:' do
1475
+ setup do
1476
+ @m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
1477
+ @m.charset = "utf8"
1478
+ @m.query "create temporary table t (utf8 char(10) charset utf8, cp932 char(10) charset cp932, eucjp char(10) charset eucjpms, bin varbinary(10))"
1479
+ @utf8 = "いろは"
1480
+ @cp932 = @utf8.encode "CP932"
1481
+ @eucjp = @utf8.encode "EUC-JP-MS"
1482
+ @bin = "\x00\x01\x7F\x80\xFE\xFF".force_encoding("ASCII-8BIT")
1483
+ @default_internal = Encoding.default_internal
1484
+ end
1817
1485
 
1818
- teardown do
1486
+ teardown do
1487
+ v = $VERBOSE
1488
+ $VERBOSE = false
1489
+ Encoding.default_internal = @default_internal
1490
+ $VERBOSE = v
1491
+ end
1492
+
1493
+ sub_test_case 'default_internal is CP932' do
1494
+ setup do
1495
+ @m.prepare("insert into t (utf8,cp932,eucjp,bin) values (?,?,?,?)").execute @utf8, @cp932, @eucjp, @bin
1819
1496
  v = $VERBOSE
1820
1497
  $VERBOSE = false
1821
- Encoding.default_internal = @default_internal
1498
+ Encoding.default_internal = 'CP932'
1822
1499
  $VERBOSE = v
1823
1500
  end
1824
-
1825
- sub_test_case 'default_internal is CP932' do
1826
- setup do
1827
- @m.prepare("insert into t (utf8,cp932,eucjp,bin) values (?,?,?,?)").execute @utf8, @cp932, @eucjp, @bin
1828
- v = $VERBOSE
1829
- $VERBOSE = false
1830
- Encoding.default_internal = 'CP932'
1831
- $VERBOSE = v
1832
- end
1833
- test 'is converted to CP932' do
1834
- assert @m.query('select "あいう"').fetch == ["\x82\xA0\x82\xA2\x82\xA4".force_encoding("CP932")]
1835
- end
1836
- test 'data is stored as is' do
1837
- assert @m.query('select hex(utf8),hex(cp932),hex(eucjp),hex(bin) from t').fetch == ['E38184E3828DE381AF', '82A282EB82CD', 'A4A4A4EDA4CF', '00017F80FEFF']
1838
- end
1839
- test 'By simple query, charset of retrieved data is connection charset' do
1840
- assert @m.query('select utf8,cp932,eucjp,bin from t').fetch == [@cp932, @cp932, @cp932, @bin]
1841
- end
1842
- test 'By prepared statement, charset of retrieved data is connection charset except for binary' do
1843
- assert @m.prepare('select utf8,cp932,eucjp,bin from t').execute.fetch == [@cp932, @cp932, @cp932, @bin]
1844
- end
1501
+ test 'is converted to CP932' do
1502
+ assert @m.query('select "あいう"').fetch == ["\x82\xA0\x82\xA2\x82\xA4".force_encoding("CP932")]
1503
+ end
1504
+ test 'data is stored as is' do
1505
+ assert @m.query('select hex(utf8),hex(cp932),hex(eucjp),hex(bin) from t').fetch == ['E38184E3828DE381AF', '82A282EB82CD', 'A4A4A4EDA4CF', '00017F80FEFF']
1506
+ end
1507
+ test 'By simple query, charset of retrieved data is connection charset' do
1508
+ assert @m.query('select utf8,cp932,eucjp,bin from t').fetch == [@cp932, @cp932, @cp932, @bin]
1509
+ end
1510
+ test 'By prepared statement, charset of retrieved data is connection charset except for binary' do
1511
+ assert @m.prepare('select utf8,cp932,eucjp,bin from t').execute.fetch == [@cp932, @cp932, @cp932, @bin]
1845
1512
  end
1513
+ end
1846
1514
 
1847
- sub_test_case 'query with CP932 encoding' do
1848
- test 'is converted to UTF-8' do
1849
- assert @m.query('select HEX("あいう")'.encode("CP932")).fetch == ["E38182E38184E38186"]
1850
- end
1515
+ sub_test_case 'query with CP932 encoding' do
1516
+ test 'is converted to UTF-8' do
1517
+ assert @m.query('select HEX("あいう")'.encode("CP932")).fetch == ["E38182E38184E38186"]
1851
1518
  end
1519
+ end
1852
1520
 
1853
- sub_test_case 'prepared statement with CP932 encoding' do
1854
- test 'is converted to UTF-8' do
1855
- assert @m.prepare('select HEX("あいう")'.encode("CP932")).execute.fetch == ["E38182E38184E38186"]
1856
- end
1521
+ sub_test_case 'prepared statement with CP932 encoding' do
1522
+ test 'is converted to UTF-8' do
1523
+ assert @m.prepare('select HEX("あいう")'.encode("CP932")).execute.fetch == ["E38182E38184E38186"]
1857
1524
  end
1525
+ end
1858
1526
 
1859
- sub_test_case 'The encoding of data are correspond to charset of column:' do
1860
- setup do
1861
- @m.prepare("insert into t (utf8,cp932,eucjp,bin) values (?,?,?,?)").execute @utf8, @cp932, @eucjp, @bin
1862
- end
1863
- test 'data is stored as is' do
1864
- assert{ @m.query('select hex(utf8),hex(cp932),hex(eucjp),hex(bin) from t').fetch == ['E38184E3828DE381AF', '82A282EB82CD', 'A4A4A4EDA4CF', '00017F80FEFF'] }
1865
- end
1866
- test 'By simple query, charset of retrieved data is connection charset' do
1867
- assert{ @m.query('select utf8,cp932,eucjp,bin from t').fetch == [@utf8, @utf8, @utf8, @bin] }
1868
- end
1869
- test 'By prepared statement, charset of retrieved data is connection charset except for binary' do
1870
- assert{ @m.prepare('select utf8,cp932,eucjp,bin from t').execute.fetch == [@utf8, @utf8, @utf8, @bin] }
1871
- end
1527
+ sub_test_case 'The encoding of data are correspond to charset of column:' do
1528
+ setup do
1529
+ @m.prepare("insert into t (utf8,cp932,eucjp,bin) values (?,?,?,?)").execute @utf8, @cp932, @eucjp, @bin
1530
+ end
1531
+ test 'data is stored as is' do
1532
+ assert{ @m.query('select hex(utf8),hex(cp932),hex(eucjp),hex(bin) from t').fetch == ['E38184E3828DE381AF', '82A282EB82CD', 'A4A4A4EDA4CF', '00017F80FEFF'] }
1533
+ end
1534
+ test 'By simple query, charset of retrieved data is connection charset' do
1535
+ assert{ @m.query('select utf8,cp932,eucjp,bin from t').fetch == [@utf8, @utf8, @utf8, @bin] }
1872
1536
  end
1537
+ test 'By prepared statement, charset of retrieved data is connection charset except for binary' do
1538
+ assert{ @m.prepare('select utf8,cp932,eucjp,bin from t').execute.fetch == [@utf8, @utf8, @utf8, @bin] }
1539
+ end
1540
+ end
1873
1541
 
1874
- sub_test_case 'The encoding of data are different from charset of column:' do
1875
- setup do
1876
- @m.prepare("insert into t (utf8,cp932,eucjp,bin) values (?,?,?,?)").execute @utf8, @utf8, @utf8, @utf8
1877
- end
1878
- test 'stored data is converted' do
1879
- assert{ @m.query("select hex(utf8),hex(cp932),hex(eucjp),hex(bin) from t").fetch == ["E38184E3828DE381AF", "82A282EB82CD", "A4A4A4EDA4CF", "E38184E3828DE381AF"] }
1880
- end
1881
- test 'By simple query, charset of retrieved data is connection charset' do
1882
- assert{ @m.query("select utf8,cp932,eucjp,bin from t").fetch == [@utf8, @utf8, @utf8, @utf8.dup.force_encoding('ASCII-8BIT')] }
1883
- end
1884
- test 'By prepared statement, charset of retrieved data is connection charset except for binary' do
1885
- assert{ @m.prepare("select utf8,cp932,eucjp,bin from t").execute.fetch == [@utf8, @utf8, @utf8, @utf8.dup.force_encoding("ASCII-8BIT")] }
1886
- end
1542
+ sub_test_case 'The encoding of data are different from charset of column:' do
1543
+ setup do
1544
+ @m.prepare("insert into t (utf8,cp932,eucjp,bin) values (?,?,?,?)").execute @utf8, @utf8, @utf8, @utf8
1545
+ end
1546
+ test 'stored data is converted' do
1547
+ assert{ @m.query("select hex(utf8),hex(cp932),hex(eucjp),hex(bin) from t").fetch == ["E38184E3828DE381AF", "82A282EB82CD", "A4A4A4EDA4CF", "E38184E3828DE381AF"] }
1887
1548
  end
1549
+ test 'By simple query, charset of retrieved data is connection charset' do
1550
+ assert{ @m.query("select utf8,cp932,eucjp,bin from t").fetch == [@utf8, @utf8, @utf8, @utf8.dup.force_encoding('ASCII-8BIT')] }
1551
+ end
1552
+ test 'By prepared statement, charset of retrieved data is connection charset except for binary' do
1553
+ assert{ @m.prepare("select utf8,cp932,eucjp,bin from t").execute.fetch == [@utf8, @utf8, @utf8, @utf8.dup.force_encoding("ASCII-8BIT")] }
1554
+ end
1555
+ end
1888
1556
 
1889
- sub_test_case 'The data include invalid byte code:' do
1890
- test 'raises Encoding::InvalidByteSequenceError' do
1891
- cp932 = "\x01\xFF\x80".force_encoding("CP932")
1892
- assert_raise Encoding::InvalidByteSequenceError do
1893
- @m.prepare("insert into t (cp932) values (?)").execute cp932
1894
- end
1557
+ sub_test_case 'The data include invalid byte code:' do
1558
+ test 'raises Encoding::InvalidByteSequenceError' do
1559
+ cp932 = "\x01\xFF\x80".force_encoding("CP932")
1560
+ assert_raise Encoding::InvalidByteSequenceError do
1561
+ @m.prepare("insert into t (cp932) values (?)").execute cp932
1895
1562
  end
1896
1563
  end
1897
1564
  end