ruby-mysql 2.9.14 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
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 == 20913 }
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,66 +123,43 @@ 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' }
94
- end
95
- end
96
-
97
- sub_test_case 'Mysql.client_version' do
98
- test 'returns client version as Integer' do
99
- assert{ Mysql.client_version == 50000 }
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 }
100
130
  end
101
- end
102
131
 
103
- sub_test_case 'Mysql.get_client_version' do
104
- test 'returns client version as Integer' do
105
- 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 }
106
135
  end
107
- end
108
136
 
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 }
143
- stub(UNIXSocket).new{ sleep 1}
144
- stub(TCPSocket).new{ sleep 1}
159
+ test 'connect_timeout: set timeout for connecting' do
160
+ @m.connect_timeout = 0.1
161
+ stub(Socket).tcp{ raise Errno::ETIMEDOUT }
162
+ stub(Socket).unix{ raise Errno::ETIMEDOUT }
145
163
  assert_raise Mysql::ClientError, 'connection timeout' do
146
164
  @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
147
165
  end
@@ -149,33 +167,80 @@ 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
171
+ require 'tempfile'
172
+ tmpf = Tempfile.new 'mysql_spec'
173
+ tmpf.puts "123\tabc\n"
174
+ tmpf.close
175
+ @m.local_infile = true
176
+ @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
177
+ @m.query('create temporary table t (i int, c char(10))')
178
+ @m.query("load data local infile '#{tmpf.path}' into table t")
179
+ assert{ @m.query('select * from t').fetch_row == ['123','abc'] }
180
+ end
181
+ test 'load_data_local_dir: client can execute LOAD DATA LOCAL INFILE query with specified directory' do
153
182
  require 'tempfile'
154
183
  tmpf = Tempfile.new 'mysql_spec'
155
184
  tmpf.puts "123\tabc\n"
156
185
  tmpf.close
157
- assert{ @m.options(Mysql::OPT_LOCAL_INFILE, true) == @m }
186
+ @m.load_data_local_dir = File.dirname(tmpf.path)
158
187
  @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
159
188
  @m.query('create temporary table t (i int, c char(10))')
160
189
  @m.query("load data local infile '#{tmpf.path}' into table t")
161
190
  assert{ @m.query('select * from t').fetch_row == ['123','abc'] }
162
191
  end
163
- test 'OPT_READ_TIMEOUT: set timeout for reading packet' do
164
- assert{ @m.options(Mysql::OPT_READ_TIMEOUT, 10) == @m }
192
+ test 'load_data_local_dir: client cannot execute LOAD DATA LOCAL INFILE query without specified directory' do
193
+ require 'tempfile'
194
+ tmpf = Tempfile.new 'mysql_spec'
195
+ tmpf.puts "123\tabc\n"
196
+ tmpf.close
197
+ @m.load_data_local_dir = '/hoge'
198
+ @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
199
+ @m.query('create temporary table t (i int, c char(10))')
200
+ assert_raise Mysql::ClientError::LoadDataLocalInfileRejected, 'LOAD DATA LOCAL INFILE file request rejected due to restrictions on access.' do
201
+ @m.query("load data local infile '#{tmpf.path}' into table t")
202
+ end
203
+ end
204
+ test 'without local_infile and load_data_local_dir: client cannot execute LOAD DATA LOCAL INFILE query' do
205
+ require 'tempfile'
206
+ tmpf = Tempfile.new 'mysql_spec'
207
+ tmpf.puts "123\tabc\n"
208
+ tmpf.close
209
+ @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
210
+ @m.query('create temporary table t (i int, c char(10))')
211
+ if @m.server_version >= 80000
212
+ assert_raise Mysql::ServerError, 'Loading local data is disabled; this must be enabled on both the client and server sides' do
213
+ @m.query("load data local infile '#{tmpf.path}' into table t")
214
+ end
215
+ else
216
+ assert_raise Mysql::ServerError::NotAllowedCommand, 'The used command is not allowed with this MySQL version' do
217
+ @m.query("load data local infile '#{tmpf.path}' into table t")
218
+ end
219
+ end
220
+ end
221
+ test 'read_timeout: set timeout for reading packet' do
222
+ @m.read_timeout = 1
223
+ @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
224
+ @m.query("select 123").entries
165
225
  end
166
- test 'OPT_WRITE_TIMEOUT: set timeout for writing packet' do
167
- assert{ @m.options(Mysql::OPT_WRITE_TIMEOUT, 10) == @m }
226
+ test 'write_timeout: set timeout for writing packet' do
227
+ @m.write_timeout = 1
228
+ @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
229
+ @m.query("select 123").entries
168
230
  end
169
- test 'SET_CHARSET_NAME: set charset for connection' do
170
- assert{ @m.options(Mysql::SET_CHARSET_NAME, 'utf8') == @m }
231
+ test 'charset: set charset for connection' do
232
+ @m.charset = 'utf8mb3'
171
233
  @m.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
172
- assert{ @m.query('select @@character_set_connection').fetch_row == ['utf8'] }
234
+ assert do
235
+ @m.query('select @@character_set_connection').fetch_row == ['utf8mb3'] ||
236
+ @m.query('select @@character_set_connection').fetch_row == ['utf8']
237
+ end
173
238
  end
174
239
  end
175
240
 
176
241
  sub_test_case 'Mysql' do
177
242
  setup do
178
- @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)
179
244
  end
180
245
 
181
246
  teardown do
@@ -183,22 +248,9 @@ class TestMysql < Test::Unit::TestCase
183
248
  end
184
249
 
185
250
  sub_test_case '#escape_string' do
186
- if defined? ::Encoding
187
- test 'escape special character for charset' do
188
- @m.charset = 'cp932'
189
- assert{ @m.escape_string("abc'def\"ghi\0jkl%mno_表".encode('cp932')) == "abc\\'def\\\"ghi\\0jkl%mno_表".encode('cp932') }
190
- end
191
- else
192
- test 'raise error if charset is multibyte' do
193
- @m.charset = 'cp932'
194
- assert_raise Mysql::ClientError, 'Mysql#escape_string is called for unsafe multibyte charset' do
195
- @m.escape_string("abc'def\"ghi\0jkl%mno_\x95\\")
196
- end
197
- end
198
- test 'not warn if charset is singlebyte' do
199
- @m.charset = 'latin1'
200
- assert{ @m.escape_string("abc'def\"ghi\0jkl%mno_\x95\\") == "abc\\'def\\\"ghi\\0jkl%mno_\x95\\\\" }
201
- 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') }
202
254
  end
203
255
  end
204
256
 
@@ -208,18 +260,6 @@ class TestMysql < Test::Unit::TestCase
208
260
  end
209
261
  end
210
262
 
211
- sub_test_case '#client_info' do
212
- test 'returns client version as string' do
213
- assert{ @m.client_info == '5.0.0' }
214
- end
215
- end
216
-
217
- sub_test_case '#get_client_info' do
218
- test 'returns client version as string' do
219
- assert{ @m.get_client_info == '5.0.0' }
220
- end
221
- end
222
-
223
263
  sub_test_case '#affected_rows' do
224
264
  test 'returns number of affected rows' do
225
265
  @m.query 'create temporary table t (id int)'
@@ -230,8 +270,8 @@ class TestMysql < Test::Unit::TestCase
230
270
 
231
271
  sub_test_case '#character_set_name' do
232
272
  test 'returns charset name' do
233
- m = Mysql.init
234
- m.options Mysql::SET_CHARSET_NAME, 'cp932'
273
+ m = Mysql.new
274
+ m.charset = 'cp932'
235
275
  m.connect MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET
236
276
  assert{ m.character_set_name == 'cp932' }
237
277
  end
@@ -279,28 +319,6 @@ class TestMysql < Test::Unit::TestCase
279
319
  end
280
320
  end
281
321
 
282
- sub_test_case '#client_version' do
283
- test 'returns client version as Integer' do
284
- assert{ @m.client_version.kind_of? Integer }
285
- end
286
- end
287
-
288
- sub_test_case '#get_client_version' do
289
- test 'returns client version as Integer' do
290
- assert{ @m.get_client_version.kind_of? Integer }
291
- end
292
- end
293
-
294
- sub_test_case '#get_host_info' do
295
- test 'returns connection type as String' do
296
- if MYSQL_SERVER == nil or MYSQL_SERVER == 'localhost'
297
- assert{ @m.get_host_info == 'Localhost via UNIX socket' }
298
- else
299
- assert{ @m.get_host_info == "#{MYSQL_SERVER} via TCP/IP" }
300
- end
301
- end
302
- end
303
-
304
322
  sub_test_case '#host_info' do
305
323
  test 'returns connection type as String' do
306
324
  if MYSQL_SERVER == nil or MYSQL_SERVER == 'localhost'
@@ -311,24 +329,6 @@ class TestMysql < Test::Unit::TestCase
311
329
  end
312
330
  end
313
331
 
314
- sub_test_case '#get_proto_info' do
315
- test 'returns version of connection as Integer' do
316
- assert{ @m.get_proto_info == 10 }
317
- end
318
- end
319
-
320
- sub_test_case '#proto_info' do
321
- test 'returns version of connection as Integer' do
322
- assert{ @m.proto_info == 10 }
323
- end
324
- end
325
-
326
- sub_test_case '#get_server_info' do
327
- test 'returns server version as String' do
328
- assert{ @m.get_server_info =~ /\A\d+\.\d+\.\d+/ }
329
- end
330
- end
331
-
332
332
  sub_test_case '#server_info' do
333
333
  test 'returns server version as String' do
334
334
  assert{ @m.server_info =~ /\A\d+\.\d+\.\d+/ }
@@ -356,7 +356,7 @@ class TestMysql < Test::Unit::TestCase
356
356
 
357
357
  sub_test_case '#kill' do
358
358
  setup do
359
- @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)
360
360
  end
361
361
  teardown do
362
362
  @m2.close rescue nil
@@ -366,61 +366,6 @@ class TestMysql < Test::Unit::TestCase
366
366
  end
367
367
  end
368
368
 
369
- sub_test_case '#list_dbs' do
370
- test 'returns database list' do
371
- ret = @m.list_dbs
372
- assert{ ret.kind_of? Array }
373
- assert{ ret.include? MYSQL_DATABASE }
374
- end
375
- test 'with pattern returns databases that matches pattern' do
376
- assert{ @m.list_dbs('info%').include? 'information_schema' }
377
- end
378
- end
379
-
380
- sub_test_case '#list_fields' do
381
- setup do
382
- @m.query 'create temporary table t (i int, c char(10), d date)'
383
- end
384
- test 'returns result set that contains information of fields' do
385
- ret = @m.list_fields('t')
386
- assert{ ret.kind_of? Mysql::Result }
387
- assert{ ret.num_rows == 0 }
388
- assert{ ret.fetch_fields.map{|f|f.name} == ['i','c','d'] }
389
- end
390
- test 'with pattern returns result set that contains information of fields that matches pattern' do
391
- ret = @m.list_fields('t', 'i')
392
- assert{ ret.kind_of? Mysql::Result }
393
- assert{ ret.num_rows == 0 }
394
- ret.fetch_fields.map{|f|f.name} == ['i']
395
- end
396
- end
397
-
398
- sub_test_case '#list_processes' do
399
- test 'returns result set that contains information of all connections' do
400
- ret = @m.list_processes
401
- assert{ ret.kind_of? Mysql::Result }
402
- assert{ ret.find{|r|r[0].to_i == @m.thread_id}[4] == "Processlist" }
403
- end
404
- end
405
-
406
- sub_test_case '#list_tables' do
407
- setup do
408
- @m.query 'create table test_mysql_list_tables (id int)'
409
- end
410
- teardown do
411
- @m.query 'drop table if exists test_mysql_list_tables'
412
- end
413
- test 'returns table list' do
414
- ret = @m.list_tables
415
- assert{ ret.kind_of? Array }
416
- assert{ ret.include? 'test_mysql_list_tables' }
417
- end
418
- test 'with pattern returns lists that matches pattern' do
419
- ret = @m.list_tables '%mysql\_list\_t%'
420
- assert{ ret.include? 'test_mysql_list_tables' }
421
- end
422
- end
423
-
424
369
  sub_test_case '#ping' do
425
370
  test 'returns self' do
426
371
  assert{ @m.ping == @m }
@@ -434,17 +379,8 @@ class TestMysql < Test::Unit::TestCase
434
379
  test 'returns nil if query returns no results' do
435
380
  assert{ @m.query('set @hoge:=123') == nil }
436
381
  end
437
- test 'returns self if query_with_result is false' do
438
- @m.query_with_result = false
439
- assert{ @m.query('select 123') == @m }
440
- @m.store_result
441
- assert{ @m.query('set @hoge:=123') == @m }
442
- end
443
- end
444
-
445
- sub_test_case '#real_query' do
446
- test 'is same as #query' do
447
- 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 }
448
384
  end
449
385
  end
450
386
 
@@ -476,60 +412,12 @@ class TestMysql < Test::Unit::TestCase
476
412
  end
477
413
  end
478
414
 
479
- sub_test_case '#store_result' do
480
- test 'returns Mysql::Result' do
481
- @m.query_with_result = false
482
- @m.query 'select 1,2,3'
483
- ret = @m.store_result
484
- assert{ ret.kind_of? Mysql::Result }
485
- assert{ ret.fetch_row == ['1','2','3'] }
486
- end
487
- test 'raises error when no query' do
488
- assert_raise Mysql::ClientError, 'invalid usage' do
489
- @m.store_result
490
- end
491
- end
492
- test 'raises error when query does not return results' do
493
- @m.query 'set @hoge:=123'
494
- assert_raise Mysql::ClientError, 'invalid usage' do
495
- @m.store_result
496
- end
497
- end
498
- end
499
-
500
415
  sub_test_case '#thread_id' do
501
416
  test 'returns thread id as Integer' do
502
417
  assert{ @m.thread_id.kind_of? Integer }
503
418
  end
504
419
  end
505
420
 
506
- sub_test_case '#use_result' do
507
- test 'returns Mysql::Result' do
508
- @m.query_with_result = false
509
- @m.query 'select 1,2,3'
510
- ret = @m.use_result
511
- assert{ ret.kind_of? Mysql::Result }
512
- assert{ ret.fetch_row == ['1','2','3'] }
513
- end
514
- test 'raises error when no query' do
515
- assert_raise Mysql::ClientError, 'invalid usage' do
516
- @m.use_result
517
- end
518
- end
519
- test 'raises error when query does not return results' do
520
- @m.query 'set @hoge:=123'
521
- assert_raise Mysql::ClientError, 'invalid usage' do
522
- @m.use_result
523
- end
524
- end
525
- end
526
-
527
- sub_test_case '#get_server_version' do
528
- test 'returns server version as Integer' do
529
- assert{ @m.get_server_version.kind_of? Integer }
530
- end
531
- end
532
-
533
421
  sub_test_case '#server_version' do
534
422
  test 'returns server version as Integer' do
535
423
  assert{ @m.server_version.kind_of? Integer }
@@ -594,27 +482,6 @@ class TestMysql < Test::Unit::TestCase
594
482
  end
595
483
  end
596
484
 
597
- sub_test_case '#query_with_result' do
598
- test 'default value is true' do
599
- assert{ @m.query_with_result == true }
600
- end
601
- test 'can set value' do
602
- assert{ (@m.query_with_result=true) == true }
603
- assert{ @m.query_with_result == true }
604
- assert{ (@m.query_with_result=false) == false }
605
- assert{ @m.query_with_result == false }
606
- end
607
- end
608
-
609
- sub_test_case '#query_with_result is false' do
610
- test 'Mysql#query returns self and Mysql#store_result returns result set' do
611
- @m.query_with_result = false
612
- assert{ @m.query('select 1,2,3') == @m }
613
- res = @m.store_result
614
- assert{ res.fetch_row == ['1','2','3'] }
615
- end
616
- end
617
-
618
485
  sub_test_case '#query with block' do
619
486
  test 'returns self' do
620
487
  assert{ @m.query('select 1'){} == @m }
@@ -647,7 +514,7 @@ class TestMysql < Test::Unit::TestCase
647
514
 
648
515
  sub_test_case 'multiple statement query:' do
649
516
  setup do
650
- @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)
651
518
  @m.set_server_option(Mysql::OPTION_MULTI_STATEMENTS_ON)
652
519
  @res = @m.query 'select 1,2; select 3,4,5'
653
520
  end
@@ -685,7 +552,7 @@ class TestMysql < Test::Unit::TestCase
685
552
 
686
553
  sub_test_case 'Mysql::Result' do
687
554
  setup do
688
- @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)
689
556
  @m.charset = 'latin1'
690
557
  @m.query 'create temporary table t (id int default 0, str char(10), primary key (id))'
691
558
  @m.query "insert into t values (1,'abc'),(2,'defg'),(3,'hi'),(4,null)"
@@ -841,7 +708,7 @@ class TestMysql < Test::Unit::TestCase
841
708
 
842
709
  sub_test_case 'Mysql::Field' do
843
710
  setup do
844
- @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)
845
712
  @m.charset = 'latin1'
846
713
  @m.query 'create temporary table t (id int default 0, str char(10), primary key (id))'
847
714
  @m.query "insert into t values (1,'abc'),(2,'defg'),(3,'hi'),(4,null)"
@@ -864,10 +731,6 @@ class TestMysql < Test::Unit::TestCase
864
731
  assert{ @res.fetch_field.def == nil }
865
732
  end
866
733
 
867
- test '#def for field information is default value' do
868
- assert{ @m.list_fields('t').fetch_field.def == '0' }
869
- end
870
-
871
734
  test '#type is type of field as Integer' do
872
735
  assert{ @res.fetch_field.type == Mysql::Field::TYPE_LONG }
873
736
  assert{ @res.fetch_field.type == Mysql::Field::TYPE_STRING }
@@ -892,8 +755,8 @@ class TestMysql < Test::Unit::TestCase
892
755
  assert{ @m.query('select 1.23').fetch_field.decimals == 2 }
893
756
  end
894
757
 
895
- test '#hash return field as hash' do
896
- assert{ @res.fetch_field.hash == {
758
+ test '#to_hash return field as hash' do
759
+ assert{ @res.fetch_field.to_hash == {
897
760
  'name' => 'id',
898
761
  'table' => 't',
899
762
  'def' => nil,
@@ -904,7 +767,7 @@ class TestMysql < Test::Unit::TestCase
904
767
  'decimals' => 0,
905
768
  }
906
769
  }
907
- assert{ @res.fetch_field.hash == {
770
+ assert{ @res.fetch_field.to_hash == {
908
771
  'name' => 'str',
909
772
  'table' => 't',
910
773
  'def' => nil,
@@ -940,15 +803,15 @@ class TestMysql < Test::Unit::TestCase
940
803
 
941
804
  sub_test_case 'create Mysql::Stmt object:' do
942
805
  setup do
943
- @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)
944
807
  end
945
808
 
946
809
  teardown do
947
810
  @m.close if @m
948
811
  end
949
812
 
950
- test 'Mysql#stmt_init returns Mysql::Stmt object' do
951
- 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 }
952
815
  end
953
816
 
954
817
  test 'Mysq;#prepare returns Mysql::Stmt object' do
@@ -958,9 +821,9 @@ class TestMysql < Test::Unit::TestCase
958
821
 
959
822
  sub_test_case 'Mysql::Stmt' do
960
823
  setup do
961
- @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)
962
825
  @m.query("set sql_mode=''")
963
- @s = @m.stmt_init
826
+ @s = @m.stmt
964
827
  end
965
828
 
966
829
  teardown do
@@ -980,68 +843,6 @@ class TestMysql < Test::Unit::TestCase
980
843
  assert{ @s.affected_rows == 3 }
981
844
  end
982
845
 
983
- sub_test_case '#bind_result' do
984
- setup do
985
- @m.query 'create temporary table t (i int, c char(10), d double, t datetime)'
986
- @m.query 'insert into t values (123,"9abcdefg",1.2345,20091208100446)'
987
- @s.prepare 'select * from t'
988
- end
989
-
990
- test '(nil) make result format to be standard value' do
991
- @s.bind_result nil, nil, nil, nil
992
- @s.execute
993
- assert{ @s.fetch == [123, '9abcdefg', 1.2345, Mysql::Time.new(2009,12,8,10,4,46)] }
994
- end
995
-
996
- test '(Numeric) make result format to be Integer value' do
997
- @s.bind_result Numeric, Numeric, Numeric, Numeric
998
- @s.execute
999
- assert{ @s.fetch == [123, 9, 1, 20091208100446] }
1000
- end
1001
-
1002
- test '(Integer) make result format to be Integer value' do
1003
- @s.bind_result Integer, Integer, Integer, Integer
1004
- @s.execute
1005
- assert{ @s.fetch == [123, 9, 1, 20091208100446] }
1006
- end
1007
-
1008
- test '(Fixnum) make result format to be Integer value' do
1009
- @s.bind_result Fixnum, Fixnum, Fixnum, Fixnum
1010
- @s.execute
1011
- assert{ @s.fetch == [123, 9, 1, 20091208100446] }
1012
- end
1013
-
1014
- test '(String) make result format to be String value' do
1015
- @s.bind_result String, String, String, String
1016
- @s.execute
1017
- assert{ @s.fetch == ["123", "9abcdefg", "1.2345", "2009-12-08 10:04:46"] }
1018
- end
1019
-
1020
- test '(Float) make result format to be Float value' do
1021
- @s.bind_result Float, Float, Float, Float
1022
- @s.execute
1023
- assert{ @s.fetch == [123.0, 9.0, 1.2345 , 20091208100446.0] }
1024
- end
1025
-
1026
- test '(Mysql::Time) make result format to be Mysql::Time value' do
1027
- @s.bind_result Mysql::Time, Mysql::Time, Mysql::Time, Mysql::Time
1028
- @s.execute
1029
- assert{ @s.fetch == [Mysql::Time.new(2000,1,23), Mysql::Time.new, Mysql::Time.new, Mysql::Time.new(2009,12,8,10,4,46)] }
1030
- end
1031
-
1032
- test '(invalid) raises error' do
1033
- assert_raise TypeError do
1034
- @s.bind_result(Time, nil, nil, nil)
1035
- end
1036
- end
1037
-
1038
- test 'with mismatch argument count raise error' do
1039
- assert_raise Mysql::ClientError, 'bind_result: result value count(4) != number of argument(1)' do
1040
- @s.bind_result(nil)
1041
- end
1042
- end
1043
- end
1044
-
1045
846
  test '#close returns nil' do
1046
847
  assert{ @s.close == nil }
1047
848
  end
@@ -1066,8 +867,8 @@ class TestMysql < Test::Unit::TestCase
1066
867
  @s.prepare 'select * from t'
1067
868
  @s.execute
1068
869
  expect = [
1069
- [1, 'abc', Mysql::Time.new(1970,12,24,23,59,05)],
1070
- [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)],
1071
872
  [3, '123', nil],
1072
873
  ]
1073
874
  @s.each do |a|
@@ -1213,20 +1014,16 @@ class TestMysql < Test::Unit::TestCase
1213
1014
  @m.query 'insert into t values (0),(-1),(127),(-128),(255),(-255),(256)'
1214
1015
  @s.prepare 'select i from t'
1215
1016
  @s.execute
1216
- if defined? Encoding
1217
- assert{ @s.entries == [
1218
- ["\x00".force_encoding('ASCII-8BIT')],
1219
- ["\xff".force_encoding('ASCII-8BIT')],
1220
- ["\x7f".force_encoding('ASCII-8BIT')],
1221
- ["\xff".force_encoding('ASCII-8BIT')],
1222
- ["\xff".force_encoding('ASCII-8BIT')],
1223
- ["\xff".force_encoding('ASCII-8BIT')],
1224
- ["\xff".force_encoding('ASCII-8BIT')],
1225
- ]
1226
- }
1227
- else
1228
- assert{ @s.entries == [["\x00"], ["\xff"], ["\x7f"], ["\xff"], ["\xff"], ["\xff"], ["\xff"]] }
1229
- 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
+ }
1230
1027
  end
1231
1028
 
1232
1029
  test '#fetch bit column (64bit)' do
@@ -1234,25 +1031,14 @@ class TestMysql < Test::Unit::TestCase
1234
1031
  @m.query 'insert into t values (0),(-1),(4294967296),(18446744073709551615),(18446744073709551616)'
1235
1032
  @s.prepare 'select i from t'
1236
1033
  @s.execute
1237
- if defined? Encoding
1238
- assert{ @s.entries == [
1239
- ["\x00\x00\x00\x00\x00\x00\x00\x00".force_encoding('ASCII-8BIT')],
1240
- ["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT')],
1241
- ["\x00\x00\x00\x01\x00\x00\x00\x00".force_encoding('ASCII-8BIT')],
1242
- ["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT')],
1243
- ["\xff\xff\xff\xff\xff\xff\xff\xff".force_encoding('ASCII-8BIT')],
1244
- ]
1245
- }
1246
- else
1247
- assert{ @s.entries == [
1248
- ["\x00\x00\x00\x00\x00\x00\x00\x00"],
1249
- ["\xff\xff\xff\xff\xff\xff\xff\xff"],
1250
- ["\x00\x00\x00\x01\x00\x00\x00\x00"],
1251
- ["\xff\xff\xff\xff\xff\xff\xff\xff"],
1252
- ["\xff\xff\xff\xff\xff\xff\xff\xff"],
1253
- ]
1254
- }
1255
- 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
+ }
1256
1042
  end
1257
1043
 
1258
1044
  test '#fetch tinyint column' do
@@ -1405,14 +1191,11 @@ class TestMysql < Test::Unit::TestCase
1405
1191
  @s.prepare 'select i from t'
1406
1192
  @s.execute
1407
1193
  cols = @s.fetch
1408
- assert{ cols == [Mysql::Time.new] }
1409
- assert{ cols.first.to_s == '0000-00-00' }
1194
+ assert{ cols == [nil] }
1410
1195
  cols = @s.fetch
1411
- assert{ cols == [Mysql::Time.new(1000,1,1)] }
1412
- assert{ cols.first.to_s == '1000-01-01' }
1196
+ assert{ cols == [Time.new(1000,1,1)] }
1413
1197
  cols = @s.fetch
1414
- assert{ cols == [Mysql::Time.new(9999,12,31)] }
1415
- assert{ cols.first.to_s == '9999-12-31' }
1198
+ assert{ cols == [Time.new(9999,12,31)] }
1416
1199
  end
1417
1200
 
1418
1201
  test '#fetch datetime column' do
@@ -1420,9 +1203,9 @@ class TestMysql < Test::Unit::TestCase
1420
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')"
1421
1204
  @s.prepare 'select i from t'
1422
1205
  @s.execute
1423
- assert{ @s.fetch == [Mysql::Time.new] }
1424
- assert{ @s.fetch == [Mysql::Time.new(1000,1,1)] }
1425
- 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)] }
1426
1209
  end
1427
1210
 
1428
1211
  test '#fetch timestamp column' do
@@ -1430,8 +1213,8 @@ class TestMysql < Test::Unit::TestCase
1430
1213
  @m.query("insert into t values ('1970-01-02 00:00:00'),('2037-12-30 23:59:59')")
1431
1214
  @s.prepare 'select i from t'
1432
1215
  @s.execute
1433
- assert{ @s.fetch == [Mysql::Time.new(1970,1,2)] }
1434
- 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)] }
1435
1218
  end
1436
1219
 
1437
1220
  test '#fetch time column' do
@@ -1439,9 +1222,9 @@ class TestMysql < Test::Unit::TestCase
1439
1222
  @m.query "insert into t values ('-838:59:59'),(0),('838:59:59')"
1440
1223
  @s.prepare 'select i from t'
1441
1224
  @s.execute
1442
- assert{ @s.fetch == [Mysql::Time.new(0,0,0,838,59,59,true)] }
1443
- assert{ @s.fetch == [Mysql::Time.new(0,0,0,0,0,0,false)] }
1444
- 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] }
1445
1228
  end
1446
1229
 
1447
1230
  test '#fetch year column' do
@@ -1666,79 +1449,6 @@ class TestMysql < Test::Unit::TestCase
1666
1449
  end
1667
1450
  end
1668
1451
 
1669
- sub_test_case 'Mysql::Time' do
1670
- setup do
1671
- @t = Mysql::Time.new
1672
- end
1673
-
1674
- test '.new with no arguments returns 0' do
1675
- assert{ @t.year == 0 }
1676
- assert{ @t.month == 0 }
1677
- assert{ @t.day == 0 }
1678
- assert{ @t.hour == 0 }
1679
- assert{ @t.minute == 0 }
1680
- assert{ @t.second == 0 }
1681
- assert{ @t.neg == false }
1682
- assert{ @t.second_part == 0 }
1683
- end
1684
-
1685
- test '#inspect' do
1686
- assert{ Mysql::Time.new(2009,12,8,23,35,21).inspect == '#<Mysql::Time:2009-12-08 23:35:21>' }
1687
- end
1688
-
1689
- test '#to_s' do
1690
- assert{ Mysql::Time.new(2009,12,8,23,35,21).to_s == '2009-12-08 23:35:21' }
1691
- end
1692
-
1693
- test '#to_i' do
1694
- assert{ Mysql::Time.new(2009,12,8,23,35,21).to_i == 20091208233521 }
1695
- end
1696
-
1697
- test '#year' do
1698
- assert{ (@t.year = 2009) == 2009 }
1699
- assert{ @t.year == 2009 }
1700
- end
1701
-
1702
- test '#month' do
1703
- assert{ (@t.month = 12) == 12 }
1704
- assert{ @t.month == 12 }
1705
- end
1706
-
1707
- test '#day' do
1708
- assert{ (@t.day = 8) == 8 }
1709
- assert{ @t.day == 8 }
1710
- end
1711
-
1712
- test '#hour' do
1713
- assert{ (@t.hour = 23) == 23 }
1714
- assert{ @t.hour == 23 }
1715
- end
1716
-
1717
- test '#minute' do
1718
- assert{ (@t.minute = 35) == 35 }
1719
- assert{ @t.minute == 35 }
1720
- end
1721
-
1722
- test '#second' do
1723
- assert{ (@t.second = 21) == 21 }
1724
- assert{ @t.second == 21 }
1725
- end
1726
-
1727
- test '#neg' do
1728
- assert{ @t.neg == false }
1729
- end
1730
-
1731
- test '#second_part' do
1732
- assert{ @t.second_part == 0 }
1733
- end
1734
-
1735
- test '#==' do
1736
- t1 = Mysql::Time.new 2009,12,8,23,35,21
1737
- t2 = Mysql::Time.new 2009,12,8,23,35,21
1738
- assert{ t1 == t2 }
1739
- end
1740
- end
1741
-
1742
1452
  sub_test_case 'Mysql::Error' do
1743
1453
  setup do
1744
1454
  m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
@@ -1761,90 +1471,94 @@ class TestMysql < Test::Unit::TestCase
1761
1471
  end
1762
1472
  end
1763
1473
 
1764
- if defined? Encoding
1765
- sub_test_case 'Connection charset is UTF-8:' do
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
1485
+
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
1766
1494
  setup do
1767
- @m = Mysql.connect(MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT, MYSQL_SOCKET)
1768
- @m.charset = "utf8"
1769
- @m.query "create temporary table t (utf8 char(10) charset utf8, cp932 char(10) charset cp932, eucjp char(10) charset eucjpms, bin varbinary(10))"
1770
- @utf8 = "いろは"
1771
- @cp932 = @utf8.encode "CP932"
1772
- @eucjp = @utf8.encode "EUC-JP-MS"
1773
- @bin = "\x00\x01\x7F\x80\xFE\xFF".force_encoding("ASCII-8BIT")
1774
- @default_internal = Encoding.default_internal
1495
+ @m.prepare("insert into t (utf8,cp932,eucjp,bin) values (?,?,?,?)").execute @utf8, @cp932, @eucjp, @bin
1496
+ v = $VERBOSE
1497
+ $VERBOSE = false
1498
+ Encoding.default_internal = 'CP932'
1499
+ $VERBOSE = v
1775
1500
  end
1776
-
1777
- teardown do
1778
- Encoding.default_internal = @default_internal
1501
+ test 'is converted to CP932' do
1502
+ assert @m.query('select "あいう"').fetch == ["\x82\xA0\x82\xA2\x82\xA4".force_encoding("CP932")]
1779
1503
  end
1780
-
1781
- sub_test_case 'default_internal is CP932' do
1782
- setup do
1783
- @m.prepare("insert into t (utf8,cp932,eucjp,bin) values (?,?,?,?)").execute @utf8, @cp932, @eucjp, @bin
1784
- Encoding.default_internal = 'CP932'
1785
- end
1786
- test 'is converted to CP932' do
1787
- assert @m.query('select "あいう"').fetch == ["\x82\xA0\x82\xA2\x82\xA4".force_encoding("CP932")]
1788
- end
1789
- test 'data is stored as is' do
1790
- assert @m.query('select hex(utf8),hex(cp932),hex(eucjp),hex(bin) from t').fetch == ['E38184E3828DE381AF', '82A282EB82CD', 'A4A4A4EDA4CF', '00017F80FEFF']
1791
- end
1792
- test 'By simple query, charset of retrieved data is connection charset' do
1793
- assert @m.query('select utf8,cp932,eucjp,bin from t').fetch == [@cp932, @cp932, @cp932, @bin]
1794
- end
1795
- test 'By prepared statement, charset of retrieved data is connection charset except for binary' do
1796
- assert @m.prepare('select utf8,cp932,eucjp,bin from t').execute.fetch == [@cp932, @cp932, @cp932, @bin]
1797
- 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]
1798
1512
  end
1513
+ end
1799
1514
 
1800
- sub_test_case 'query with CP932 encoding' do
1801
- test 'is converted to UTF-8' do
1802
- assert @m.query('select HEX("あいう")'.encode("CP932")).fetch == ["E38182E38184E38186"]
1803
- 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"]
1804
1518
  end
1519
+ end
1805
1520
 
1806
- sub_test_case 'prepared statement with CP932 encoding' do
1807
- test 'is converted to UTF-8' do
1808
- assert @m.prepare('select HEX("あいう")'.encode("CP932")).execute.fetch == ["E38182E38184E38186"]
1809
- 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"]
1810
1524
  end
1525
+ end
1811
1526
 
1812
- sub_test_case 'The encoding of data are correspond to charset of column:' do
1813
- setup do
1814
- @m.prepare("insert into t (utf8,cp932,eucjp,bin) values (?,?,?,?)").execute @utf8, @cp932, @eucjp, @bin
1815
- end
1816
- test 'data is stored as is' do
1817
- assert{ @m.query('select hex(utf8),hex(cp932),hex(eucjp),hex(bin) from t').fetch == ['E38184E3828DE381AF', '82A282EB82CD', 'A4A4A4EDA4CF', '00017F80FEFF'] }
1818
- end
1819
- test 'By simple query, charset of retrieved data is connection charset' do
1820
- assert{ @m.query('select utf8,cp932,eucjp,bin from t').fetch == [@utf8, @utf8, @utf8, @bin] }
1821
- end
1822
- test 'By prepared statement, charset of retrieved data is connection charset except for binary' do
1823
- assert{ @m.prepare('select utf8,cp932,eucjp,bin from t').execute.fetch == [@utf8, @utf8, @utf8, @bin] }
1824
- 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'] }
1825
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] }
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
1826
1541
 
1827
- sub_test_case 'The encoding of data are different from charset of column:' do
1828
- setup do
1829
- @m.prepare("insert into t (utf8,cp932,eucjp,bin) values (?,?,?,?)").execute @utf8, @utf8, @utf8, @utf8
1830
- end
1831
- test 'stored data is converted' do
1832
- assert{ @m.query("select hex(utf8),hex(cp932),hex(eucjp),hex(bin) from t").fetch == ["E38184E3828DE381AF", "82A282EB82CD", "A4A4A4EDA4CF", "E38184E3828DE381AF"] }
1833
- end
1834
- test 'By simple query, charset of retrieved data is connection charset' do
1835
- assert{ @m.query("select utf8,cp932,eucjp,bin from t").fetch == [@utf8, @utf8, @utf8, @utf8.dup.force_encoding('ASCII-8BIT')] }
1836
- end
1837
- test 'By prepared statement, charset of retrieved data is connection charset except for binary' do
1838
- assert{ @m.prepare("select utf8,cp932,eucjp,bin from t").execute.fetch == [@utf8, @utf8, @utf8, @utf8.dup.force_encoding("ASCII-8BIT")] }
1839
- 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
1840
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"] }
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
1841
1556
 
1842
- sub_test_case 'The data include invalid byte code:' do
1843
- test 'raises Encoding::InvalidByteSequenceError' do
1844
- cp932 = "\x01\xFF\x80".force_encoding("CP932")
1845
- assert_raise Encoding::InvalidByteSequenceError do
1846
- @m.prepare("insert into t (cp932) values (?)").execute cp932
1847
- 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
1848
1562
  end
1849
1563
  end
1850
1564
  end