|
244 | 244 | expect(@test_result['null_test']).to eql(nil)
|
245 | 245 | end
|
246 | 246 |
|
247 |
| - it "should return Fixnum for a BIT value" do |
| 247 | + it "should return String for a BIT(64) value" do |
248 | 248 | expect(@test_result['bit_test'].class).to eql(String)
|
249 | 249 | expect(@test_result['bit_test']).to eql("\000\000\000\000\000\000\000\005")
|
250 | 250 | end
|
251 | 251 |
|
| 252 | + it "should return String for a BIT(1) value" do |
| 253 | + expect(@test_result['single_bit_test'].class).to eql(String) |
| 254 | + expect(@test_result['single_bit_test']).to eql("\001") |
| 255 | + end |
| 256 | + |
252 | 257 | it "should return Fixnum for a TINYINT value" do
|
253 | 258 | expect([Fixnum, Bignum]).to include(@test_result['tiny_int_test'].class)
|
254 | 259 | expect(@test_result['tiny_int_test']).to eql(1)
|
|
272 | 277 | @client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2},#{id3})"
|
273 | 278 | end
|
274 | 279 |
|
| 280 | + it "should return TrueClass or FalseClass for a BIT(1) value if :cast_booleans is enabled" do |
| 281 | + @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (1)' |
| 282 | + id1 = @client.last_id |
| 283 | + @client.query 'INSERT INTO mysql2_test (single_bit_test) VALUES (0)' |
| 284 | + id2 = @client.last_id |
| 285 | + |
| 286 | + result1 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id1}", :cast_booleans => true |
| 287 | + result2 = @client.query "SELECT single_bit_test FROM mysql2_test WHERE id = #{id2}", :cast_booleans => true |
| 288 | + expect(result1.first['single_bit_test']).to be true |
| 289 | + expect(result2.first['single_bit_test']).to be false |
| 290 | + |
| 291 | + @client.query "DELETE from mysql2_test WHERE id IN(#{id1},#{id2})" |
| 292 | + end |
| 293 | + |
275 | 294 | it "should return Fixnum for a SMALLINT value" do
|
276 | 295 | expect([Fixnum, Bignum]).to include(@test_result['small_int_test'].class)
|
277 | 296 | expect(@test_result['small_int_test']).to eql(10)
|
|
309 | 328 |
|
310 | 329 | it "should return Float for a DOUBLE value" do
|
311 | 330 | expect(@test_result['double_test'].class).to eql(Float)
|
312 |
| - expect(@test_result['double_test']).to be_within(1e-5).of(10.3) |
| 331 | + expect(@test_result['double_test']).to eql(10.3) |
313 | 332 | end
|
314 | 333 |
|
315 | 334 | it "should return Time for a DATETIME value when within the supported range" do
|
|
318 | 337 | end
|
319 | 338 |
|
320 | 339 | if 1.size == 4 # 32bit
|
321 |
| - if RUBY_VERSION =~ /1.8/ |
| 340 | + unless RUBY_VERSION =~ /1.8/ |
322 | 341 | klass = Time
|
323 | 342 | else
|
324 | 343 | klass = DateTime
|
|
384 | 403 | expect(@test_result['enum_test']).to eql('val1')
|
385 | 404 | end
|
386 | 405 |
|
| 406 | + it "should raise an error given an invalid DATETIME" do |
| 407 | + expect { @client.query("SELECT CAST('1972-00-27 00:00:00' AS DATETIME) as bad_datetime").each }.to \ |
| 408 | + raise_error(Mysql2::Error, "Invalid date in field 'bad_datetime': 1972-00-27 00:00:00") |
| 409 | + end |
| 410 | + |
387 | 411 | if defined? Encoding
|
388 | 412 | context "string encoding for ENUM values" do
|
389 | 413 | it "should default to the connection's encoding if Encoding.default_internal is nil" do
|
390 |
| - Encoding.default_internal = nil |
391 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
392 |
| - expect(result['enum_test'].encoding).to eql(Encoding.find('utf-8')) |
393 |
| - |
394 |
| - client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "ascii")) |
395 |
| - client2.query "USE test" |
396 |
| - result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
397 |
| - expect(result['enum_test'].encoding).to eql(Encoding.find('us-ascii')) |
| 414 | + with_internal_encoding nil do |
| 415 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 416 | + expect(result['enum_test'].encoding).to eql(Encoding.find('utf-8')) |
| 417 | + |
| 418 | + client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii')) |
| 419 | + result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 420 | + expect(result['enum_test'].encoding).to eql(Encoding.find('us-ascii')) |
| 421 | + client2.close |
| 422 | + end |
398 | 423 | end
|
399 | 424 |
|
400 | 425 | it "should use Encoding.default_internal" do
|
401 |
| - Encoding.default_internal = Encoding.find('utf-8') |
402 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
403 |
| - expect(result['enum_test'].encoding).to eql(Encoding.default_internal) |
404 |
| - Encoding.default_internal = Encoding.find('us-ascii') |
405 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
406 |
| - expect(result['enum_test'].encoding).to eql(Encoding.default_internal) |
| 426 | + with_internal_encoding 'utf-8' do |
| 427 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 428 | + expect(result['enum_test'].encoding).to eql(Encoding.default_internal) |
| 429 | + end |
| 430 | + |
| 431 | + with_internal_encoding 'us-ascii' do |
| 432 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 433 | + expect(result['enum_test'].encoding).to eql(Encoding.default_internal) |
| 434 | + end |
407 | 435 | end
|
408 | 436 | end
|
409 | 437 | end
|
|
416 | 444 | if defined? Encoding
|
417 | 445 | context "string encoding for SET values" do
|
418 | 446 | it "should default to the connection's encoding if Encoding.default_internal is nil" do
|
419 |
| - Encoding.default_internal = nil |
420 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
421 |
| - expect(result['set_test'].encoding).to eql(Encoding.find('utf-8')) |
422 |
| - |
423 |
| - client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "ascii")) |
424 |
| - client2.query "USE test" |
425 |
| - result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
426 |
| - expect(result['set_test'].encoding).to eql(Encoding.find('us-ascii')) |
| 447 | + with_internal_encoding nil do |
| 448 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 449 | + expect(result['set_test'].encoding).to eql(Encoding.find('utf-8')) |
| 450 | + |
| 451 | + client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii')) |
| 452 | + result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 453 | + expect(result['set_test'].encoding).to eql(Encoding.find('us-ascii')) |
| 454 | + client2.close |
| 455 | + end |
427 | 456 | end
|
428 | 457 |
|
429 | 458 | it "should use Encoding.default_internal" do
|
430 |
| - Encoding.default_internal = Encoding.find('utf-8') |
431 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
432 |
| - expect(result['set_test'].encoding).to eql(Encoding.default_internal) |
433 |
| - Encoding.default_internal = Encoding.find('us-ascii') |
434 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
435 |
| - expect(result['set_test'].encoding).to eql(Encoding.default_internal) |
| 459 | + with_internal_encoding 'utf-8' do |
| 460 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 461 | + expect(result['set_test'].encoding).to eql(Encoding.default_internal) |
| 462 | + end |
| 463 | + |
| 464 | + with_internal_encoding 'us-ascii' do |
| 465 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 466 | + expect(result['set_test'].encoding).to eql(Encoding.default_internal) |
| 467 | + end |
436 | 468 | end
|
437 | 469 | end
|
438 | 470 | end
|
|
445 | 477 | if defined? Encoding
|
446 | 478 | context "string encoding for BINARY values" do
|
447 | 479 | it "should default to binary if Encoding.default_internal is nil" do
|
448 |
| - Encoding.default_internal = nil |
449 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
450 |
| - expect(result['binary_test'].encoding).to eql(Encoding.find('binary')) |
| 480 | + with_internal_encoding nil do |
| 481 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 482 | + expect(result['binary_test'].encoding).to eql(Encoding.find('binary')) |
| 483 | + end |
451 | 484 | end
|
452 | 485 |
|
453 | 486 | it "should not use Encoding.default_internal" do
|
454 |
| - Encoding.default_internal = Encoding.find('utf-8') |
455 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
456 |
| - expect(result['binary_test'].encoding).to eql(Encoding.find('binary')) |
457 |
| - Encoding.default_internal = Encoding.find('us-ascii') |
458 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
459 |
| - expect(result['binary_test'].encoding).to eql(Encoding.find('binary')) |
| 487 | + with_internal_encoding 'utf-8' do |
| 488 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 489 | + expect(result['binary_test'].encoding).to eql(Encoding.find('binary')) |
| 490 | + end |
| 491 | + |
| 492 | + with_internal_encoding 'us-ascii' do |
| 493 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 494 | + expect(result['binary_test'].encoding).to eql(Encoding.find('binary')) |
| 495 | + end |
460 | 496 | end
|
461 | 497 | end
|
462 | 498 | end
|
|
483 | 519 | context "string encoding for #{type} values" do
|
484 | 520 | if ['VARBINARY', 'TINYBLOB', 'BLOB', 'MEDIUMBLOB', 'LONGBLOB'].include?(type)
|
485 | 521 | it "should default to binary if Encoding.default_internal is nil" do
|
486 |
| - Encoding.default_internal = nil |
487 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
488 |
| - expect(result['binary_test'].encoding).to eql(Encoding.find('binary')) |
| 522 | + with_internal_encoding nil do |
| 523 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 524 | + expect(result['binary_test'].encoding).to eql(Encoding.find('binary')) |
| 525 | + end |
489 | 526 | end
|
490 | 527 |
|
491 | 528 | it "should not use Encoding.default_internal" do
|
492 |
| - Encoding.default_internal = Encoding.find('utf-8') |
493 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
494 |
| - expect(result['binary_test'].encoding).to eql(Encoding.find('binary')) |
495 |
| - Encoding.default_internal = Encoding.find('us-ascii') |
496 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
497 |
| - expect(result['binary_test'].encoding).to eql(Encoding.find('binary')) |
| 529 | + with_internal_encoding 'utf-8' do |
| 530 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 531 | + expect(result['binary_test'].encoding).to eql(Encoding.find('binary')) |
| 532 | + end |
| 533 | + |
| 534 | + with_internal_encoding 'us-ascii' do |
| 535 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 536 | + expect(result['binary_test'].encoding).to eql(Encoding.find('binary')) |
| 537 | + end |
498 | 538 | end
|
499 | 539 | else
|
500 | 540 | it "should default to utf-8 if Encoding.default_internal is nil" do
|
501 |
| - Encoding.default_internal = nil |
502 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
503 |
| - expect(result[field].encoding).to eql(Encoding.find('utf-8')) |
504 |
| - |
505 |
| - client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => "ascii")) |
506 |
| - client2.query "USE test" |
507 |
| - result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
508 |
| - expect(result[field].encoding).to eql(Encoding.find('us-ascii')) |
| 541 | + with_internal_encoding nil do |
| 542 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 543 | + expect(result[field].encoding).to eql(Encoding.find('utf-8')) |
| 544 | + |
| 545 | + client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii')) |
| 546 | + result = client2.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 547 | + expect(result[field].encoding).to eql(Encoding.find('us-ascii')) |
| 548 | + client2.close |
| 549 | + end |
509 | 550 | end
|
510 | 551 |
|
511 | 552 | it "should use Encoding.default_internal" do
|
512 |
| - Encoding.default_internal = Encoding.find('utf-8') |
513 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
514 |
| - expect(result[field].encoding).to eql(Encoding.default_internal) |
515 |
| - Encoding.default_internal = Encoding.find('us-ascii') |
516 |
| - result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
517 |
| - expect(result[field].encoding).to eql(Encoding.default_internal) |
| 553 | + with_internal_encoding 'utf-8' do |
| 554 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 555 | + expect(result[field].encoding).to eql(Encoding.default_internal) |
| 556 | + end |
| 557 | + |
| 558 | + with_internal_encoding 'us-ascii' do |
| 559 | + result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first |
| 560 | + expect(result[field].encoding).to eql(Encoding.default_internal) |
| 561 | + end |
518 | 562 | end
|
519 | 563 | end
|
520 | 564 | end
|
521 | 565 | end
|
522 | 566 | end
|
523 | 567 | end
|
524 |
| - |
525 | 568 | end
|
0 commit comments