From dfca514562450cef0a913918ad3cbd7f8acc16d2 Mon Sep 17 00:00:00 2001 From: Brian Lopez Date: Thu, 29 Apr 2010 10:01:25 -0700 Subject: [PATCH] make specs a little finer grained, fix a few issues found along the way --- ext/mysql2_ext.c | 4 +- spec/mysql2/result_spec.rb | 134 ++++++++++++++++++++++++------------- 2 files changed, 90 insertions(+), 48 deletions(-) diff --git a/ext/mysql2_ext.c b/ext/mysql2_ext.c index 7fe5659..1dd2635 100644 --- a/ext/mysql2_ext.c +++ b/ext/mysql2_ext.c @@ -352,8 +352,10 @@ static VALUE rb_mysql_result_fetch_row(int argc, VALUE * argv, VALUE self) { case MYSQL_TYPE_NULL: // NULL-type field val = Qnil; break; - case MYSQL_TYPE_TINY: // TINYINT field case MYSQL_TYPE_BIT: // BIT field (MySQL 5.0.3 and up) + val = rb_str_new(row[i], fieldLengths[i]); + break; + case MYSQL_TYPE_TINY: // TINYINT field case MYSQL_TYPE_SHORT: // SMALLINT field case MYSQL_TYPE_LONG: // INTEGER field case MYSQL_TYPE_INT24: // MEDIUMINT field diff --git a/spec/mysql2/result_spec.rb b/spec/mysql2/result_spec.rb index fa63608..3e04e93 100644 --- a/spec/mysql2/result_spec.rb +++ b/spec/mysql2/result_spec.rb @@ -52,8 +52,9 @@ describe Mysql2::Result do @client.query "USE test" @client.query %[ CREATE TABLE IF NOT EXISTS mysql2_test ( + id MEDIUMINT NOT NULL AUTO_INCREMENT, null_test VARCHAR(10), - bit_test BIT, + bit_test BIT(64), tiny_int_test TINYINT, small_int_test SMALLINT, medium_int_test MEDIUMINT, @@ -80,7 +81,8 @@ describe Mysql2::Result do long_blob_test LONGBLOB, long_text_test LONGTEXT, enum_test ENUM('val1', 'val2'), - set_test SET('val1', 'val2') + set_test SET('val1', 'val2'), + PRIMARY KEY (id) ) ] @client.query %[ @@ -93,14 +95,18 @@ describe Mysql2::Result do ) VALUES ( - NULL, 1, 1, 10, 10, 10, 10, + NULL, b'101', 1, 10, 10, 10, 10, 10.3, 10.3, 10.3, '2010-4-4', '2010-4-4 11:44:00', '2010-4-4 11:44:00', '11:44:00', 2009, "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", "test", 'val1', 'val1,val2' ) ] - @test_result = @client.query("SELECT * FROM mysql2_test LIMIT 1").first + @test_result = @client.query("SELECT * FROM mysql2_test ORDER BY id DESC LIMIT 1").first + end + + after(:all) do + @client.query("DELETE FROM mysql2_test WHERE id=#{@test_result['id']}") end it "should return nil for a NULL value" do @@ -108,59 +114,94 @@ describe Mysql2::Result do @test_result['null_test'].should eql(nil) end - { - 'bit_test' => 'BIT', - 'tiny_int_test' => 'TINYINT', - 'small_int_test' => 'SMALLINT', - 'medium_int_test' => 'MEDIUMINT', - 'int_test' => 'INT', - 'big_int_test' => 'BIGINT', - 'year_test' => 'YEAR' - }.each do |field, type| - it "should return a Fixnum for #{type}" do - [Fixnum, Bignum].should include(@test_result[field].class) - end + it "should return Fixnum for a BIT value" do + @test_result['bit_test'].class.should eql(String) + @test_result['bit_test'].should eql("\000\000\000\000\000\000\000\005") end - { - 'decimal_test' => 'DECIMAL' - }.each do |field, type| - it "should return a Fixnum for #{type}" do - @test_result[field].class.should eql(BigDecimal) - end + it "should return Fixnum for a TINYINT value" do + [Fixnum, Bignum].should include(@test_result['tiny_int_test'].class) + @test_result['tiny_int_test'].should eql(1) end - { - 'float_test' => 'FLOAT', - 'double_test' => 'DOUBLE' - }.each do |field, type| - it "should return a Float for #{type}" do - @test_result[field].class.should eql(Float) - end + it "should return Fixnum for a SMALLINT value" do + [Fixnum, Bignum].should include(@test_result['small_int_test'].class) + @test_result['small_int_test'].should eql(10) end - { - 'date_time_test' => 'DATETIME', - 'timestamp_test' => 'TIMESTAMP', - 'time_test' => 'TIME' - }.each do |field, type| - it "should return a Time for #{type}" do - @test_result[field].class.should eql(Time) - end + it "should return Fixnum for a MEDIUMINT value" do + [Fixnum, Bignum].should include(@test_result['medium_int_test'].class) + @test_result['medium_int_test'].should eql(10) end - { - 'date_test' => 'DATE' - }.each do |field, type| - it "should return a Date for #{type}" do - @test_result[field].class.should eql(Date) - end + it "should return Fixnum for an INT value" do + [Fixnum, Bignum].should include(@test_result['int_test'].class) + @test_result['int_test'].should eql(10) + end + + it "should return Fixnum for a BIGINT value" do + [Fixnum, Bignum].should include(@test_result['big_int_test'].class) + @test_result['big_int_test'].should eql(10) + end + + it "should return Fixnum for a YEAR value" do + [Fixnum, Bignum].should include(@test_result['year_test'].class) + @test_result['year_test'].should eql(2009) + end + + it "should return BigDecimal for a DECIMAL value" do + @test_result['decimal_test'].class.should eql(BigDecimal) + @test_result['decimal_test'].should eql(10.3) + end + + it "should return Float for a FLOAT value" do + @test_result['float_test'].class.should eql(Float) + @test_result['float_test'].should eql(10.3) + end + + it "should return Float for a DOUBLE value" do + @test_result['double_test'].class.should eql(Float) + @test_result['double_test'].should eql(10.3) + end + + it "should return Time for a DATETIME value" do + @test_result['date_time_test'].class.should eql(Time) + @test_result['date_time_test'].strftime("%F %T").should eql('2010-04-04 11:44:00') + end + + it "should return Time for a TIMESTAMP value" do + @test_result['timestamp_test'].class.should eql(Time) + @test_result['timestamp_test'].strftime("%F %T").should eql('2010-04-04 11:44:00') + end + + it "should return Time for a TIME value" do + @test_result['time_test'].class.should eql(Time) + @test_result['time_test'].strftime("%F %T").should eql('2000-01-01 11:44:00') + end + + it "should return Date for a DATE value" do + @test_result['date_test'].class.should eql(Date) + @test_result['date_test'].strftime("%F").should eql('2010-04-04') + end + + it "should return String for an ENUM value" do + @test_result['enum_test'].class.should eql(String) + @test_result['enum_test'].should eql('val1') + end + + it "should return String for a SET value" do + @test_result['set_test'].class.should eql(String) + @test_result['set_test'].should eql('val1,val2') + end + + it "should return String for a BINARY value" do + @test_result['binary_test'].class.should eql(String) + @test_result['binary_test'].should eql("test#{"\000"*6}") end { 'char_test' => 'CHAR', 'varchar_test' => 'VARCHAR', - 'binary_test' => 'BINARY', 'varbinary_test' => 'VARBINARY', 'tiny_blob_test' => 'TINYBLOB', 'tiny_text_test' => 'TINYTEXT', @@ -169,12 +210,11 @@ describe Mysql2::Result do 'medium_blob_test' => 'MEDIUMBLOB', 'medium_text_test' => 'MEDIUMTEXT', 'long_blob_test' => 'LONGBLOB', - 'long_text_test' => 'LONGTEXT', - 'enum_test' => 'ENUM', - 'set_test' => 'SET' + 'long_text_test' => 'LONGTEXT' }.each do |field, type| it "should return a String for #{type}" do @test_result[field].class.should eql(String) + @test_result[field].should eql("test") end end end