|
338 | 338 | if defined? Encoding
|
339 | 339 | context "string encoding for ENUM values" do
|
340 | 340 | it "should default to the connection's encoding if Encoding.default_internal is nil" do
|
341 |
| - Encoding.default_internal = nil |
342 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
343 |
| - result['enum_test'].encoding.should eql(Encoding.find('utf-8')) |
344 |
| - |
345 |
| - client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii')) |
346 |
| - result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
347 |
| - result['enum_test'].encoding.should eql(Encoding.find('us-ascii')) |
348 |
| - client2.close |
| 341 | + with_internal_encoding nil do |
| 342 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 343 | + result['enum_test'].encoding.should eql(Encoding.find('utf-8')) |
| 344 | + |
| 345 | + client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii')) |
| 346 | + result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 347 | + result['enum_test'].encoding.should eql(Encoding.find('us-ascii')) |
| 348 | + client2.close |
| 349 | + end |
349 | 350 | end
|
350 | 351 |
|
351 | 352 | it "should use Encoding.default_internal" do
|
352 |
| - Encoding.default_internal = Encoding.find('utf-8') |
353 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
354 |
| - result['enum_test'].encoding.should eql(Encoding.default_internal) |
355 |
| - Encoding.default_internal = Encoding.find('us-ascii') |
356 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
357 |
| - result['enum_test'].encoding.should eql(Encoding.default_internal) |
| 353 | + with_internal_encoding 'utf-8' do |
| 354 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 355 | + result['enum_test'].encoding.should eql(Encoding.default_internal) |
| 356 | + end |
| 357 | + |
| 358 | + with_internal_encoding 'us-ascii' do |
| 359 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 360 | + result['enum_test'].encoding.should eql(Encoding.default_internal) |
| 361 | + end |
358 | 362 | end
|
359 | 363 | end
|
360 | 364 | end
|
|
367 | 371 | if defined? Encoding
|
368 | 372 | context "string encoding for SET values" do
|
369 | 373 | it "should default to the connection's encoding if Encoding.default_internal is nil" do
|
370 |
| - Encoding.default_internal = nil |
371 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
372 |
| - result['set_test'].encoding.should eql(Encoding.find('utf-8')) |
373 |
| - |
374 |
| - client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii')) |
375 |
| - result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
376 |
| - result['set_test'].encoding.should eql(Encoding.find('us-ascii')) |
377 |
| - client2.close |
| 374 | + with_internal_encoding nil do |
| 375 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 376 | + result['set_test'].encoding.should eql(Encoding.find('utf-8')) |
| 377 | + |
| 378 | + client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii')) |
| 379 | + result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 380 | + result['set_test'].encoding.should eql(Encoding.find('us-ascii')) |
| 381 | + client2.close |
| 382 | + end |
378 | 383 | end
|
379 | 384 |
|
380 | 385 | it "should use Encoding.default_internal" do
|
381 |
| - Encoding.default_internal = Encoding.find('utf-8') |
382 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
383 |
| - result['set_test'].encoding.should eql(Encoding.default_internal) |
384 |
| - Encoding.default_internal = Encoding.find('us-ascii') |
385 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
386 |
| - result['set_test'].encoding.should eql(Encoding.default_internal) |
| 386 | + with_internal_encoding 'utf-8' do |
| 387 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 388 | + result['set_test'].encoding.should eql(Encoding.default_internal) |
| 389 | + end |
| 390 | + |
| 391 | + with_internal_encoding 'us-ascii' do |
| 392 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 393 | + result['set_test'].encoding.should eql(Encoding.default_internal) |
| 394 | + end |
387 | 395 | end
|
388 | 396 | end
|
389 | 397 | end
|
|
396 | 404 | if defined? Encoding
|
397 | 405 | context "string encoding for BINARY values" do
|
398 | 406 | it "should default to binary if Encoding.default_internal is nil" do
|
399 |
| - Encoding.default_internal = nil |
400 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
401 |
| - result['binary_test'].encoding.should eql(Encoding.find('binary')) |
| 407 | + with_internal_encoding nil do |
| 408 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 409 | + result['binary_test'].encoding.should eql(Encoding.find('binary')) |
| 410 | + end |
402 | 411 | end
|
403 | 412 |
|
404 | 413 | it "should not use Encoding.default_internal" do
|
405 |
| - Encoding.default_internal = Encoding.find('utf-8') |
406 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
407 |
| - result['binary_test'].encoding.should eql(Encoding.find('binary')) |
408 |
| - Encoding.default_internal = Encoding.find('us-ascii') |
409 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
410 |
| - result['binary_test'].encoding.should eql(Encoding.find('binary')) |
| 414 | + with_internal_encoding 'utf-8' do |
| 415 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 416 | + result['binary_test'].encoding.should eql(Encoding.find('binary')) |
| 417 | + end |
| 418 | + |
| 419 | + with_internal_encoding 'us-ascii' do |
| 420 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 421 | + result['binary_test'].encoding.should eql(Encoding.find('binary')) |
| 422 | + end |
411 | 423 | end
|
412 | 424 | end
|
413 | 425 | end
|
|
434 | 446 | context "string encoding for #{type} values" do
|
435 | 447 | if ['VARBINARY', 'TINYBLOB', 'BLOB', 'MEDIUMBLOB', 'LONGBLOB'].include?(type)
|
436 | 448 | it "should default to binary if Encoding.default_internal is nil" do
|
437 |
| - Encoding.default_internal = nil |
438 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
439 |
| - result['binary_test'].encoding.should eql(Encoding.find('binary')) |
| 449 | + with_internal_encoding nil do |
| 450 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 451 | + result['binary_test'].encoding.should eql(Encoding.find('binary')) |
| 452 | + end |
440 | 453 | end
|
441 | 454 |
|
442 | 455 | it "should not use Encoding.default_internal" do
|
443 |
| - Encoding.default_internal = Encoding.find('utf-8') |
444 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
445 |
| - result['binary_test'].encoding.should eql(Encoding.find('binary')) |
446 |
| - Encoding.default_internal = Encoding.find('us-ascii') |
447 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
448 |
| - result['binary_test'].encoding.should eql(Encoding.find('binary')) |
| 456 | + with_internal_encoding 'utf-8' do |
| 457 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 458 | + result['binary_test'].encoding.should eql(Encoding.find('binary')) |
| 459 | + end |
| 460 | + |
| 461 | + with_internal_encoding 'us-ascii' do |
| 462 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 463 | + result['binary_test'].encoding.should eql(Encoding.find('binary')) |
| 464 | + end |
449 | 465 | end
|
450 | 466 | else
|
451 | 467 | it "should default to utf-8 if Encoding.default_internal is nil" do
|
452 |
| - Encoding.default_internal = nil |
453 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
454 |
| - result[field].encoding.should eql(Encoding.find('utf-8')) |
455 |
| - |
456 |
| - client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii')) |
457 |
| - result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
458 |
| - result[field].encoding.should eql(Encoding.find('us-ascii')) |
459 |
| - client2.close |
| 468 | + with_internal_encoding nil do |
| 469 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 470 | + result[field].encoding.should eql(Encoding.find('utf-8')) |
| 471 | + |
| 472 | + client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii')) |
| 473 | + result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 474 | + result[field].encoding.should eql(Encoding.find('us-ascii')) |
| 475 | + client2.close |
| 476 | + end |
460 | 477 | end
|
461 | 478 |
|
462 | 479 | it "should use Encoding.default_internal" do
|
463 |
| - Encoding.default_internal = Encoding.find('utf-8') |
464 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
465 |
| - result[field].encoding.should eql(Encoding.default_internal) |
466 |
| - Encoding.default_internal = Encoding.find('us-ascii') |
467 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
468 |
| - result[field].encoding.should eql(Encoding.default_internal) |
| 480 | + with_internal_encoding 'utf-8' do |
| 481 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 482 | + result[field].encoding.should eql(Encoding.default_internal) |
| 483 | + end |
| 484 | + |
| 485 | + with_internal_encoding 'us-ascii' do |
| 486 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 487 | + result[field].encoding.should eql(Encoding.default_internal) |
| 488 | + end |
469 | 489 | end
|
470 | 490 | end
|
471 | 491 | end
|
|
0 commit comments