Skip to content
Browse files

Extended testing and support for more data types

  • Loading branch information...
1 parent a8e808d commit 6547d67f1e7c3b25de418914cfab6fc868fef1a0 @krobertson committed
Showing with 49 additions and 8 deletions.
  1. +8 −7 lib/do_dbslayer/reader.rb
  2. +1 −0 spec/fixtures/types_test.txt
  3. +40 −1 spec/reader_spec.rb
View
15 lib/do_dbslayer/reader.rb
@@ -35,16 +35,16 @@ def convert(row)
BigDecimal.new(row[p])
when 'MYSQL_TYPE_BIT'
row[p] == '1'
- when 'MYSQL_TYPE_TIMESTAMP'
- row[p]
when 'MYSQL_TYPE_DATE'
- row[p]
+ Date.strptime(row[p], '%Y-%m-%d')
when 'MYSQL_TYPE_TIME'
- row[p]
+ Time.parse(row[p])
+ when 'MYSQL_TYPE_TIMESTAMP'
+ Time.parse(row[p])
when 'MYSQL_TYPE_DATETIME'
- row[p]
+ DateTime.strptime(row[p], '%Y-%m-%d %H:%M:%S')
when 'MYSQL_TYPE_YEAR'
- row[p]
+ row[p].to_i
when 'MYSQL_TYPE_BLOB'
row[p]
when 'MYSQL_TYPE_TINY_BLOB'
@@ -53,13 +53,14 @@ def convert(row)
row[p]
when 'MYSQL_TYPE_LONG_BLOB'
row[p]
+ when 'MYSQL_TYPE_TINY'
+ row[p] == 1 # Bool
when 'MYSQL_TYPE_NULL'
nil
else row[p]
end
# Following not needed since already the right type
- #when 'MYSQL_TYPE_TINY' : row[p].to_i
#when 'MYSQL_TYPE_SHORT' : row[p].to_i
#when 'MYSQL_TYPE_LONG' : row[p].to_i
#when 'MYSQL_TYPE_INT24' : row[p].to_i
View
1 spec/fixtures/types_test.txt
@@ -0,0 +1 @@
+{"RESULT" : {"HEADER" : ["id" , "adate" , "atimestamp" , "adatetime" , "atime" , "ayear" , "abit"] , "ROWS" : [[1 , "2008-05-19" , "2008-05-19 09:46:56" , "2008-05-19 09:48:07" , "09:48:07" , "2008" , 1]] , "TYPES" : ["MYSQL_TYPE_LONG" , "MYSQL_TYPE_DATE" , "MYSQL_TYPE_TIMESTAMP" , "MYSQL_TYPE_DATETIME" , "MYSQL_TYPE_TIME" , "MYSQL_TYPE_YEAR" , "MYSQL_TYPE_TINY"]} , "SERVER" : "localhost"}
View
41 spec/reader_spec.rb
@@ -3,7 +3,11 @@
describe DataObjects::Dbslayer::Reader do
before :all do
@reader_query = File.new(File.join(File.dirname(__FILE__), 'fixtures', 'reader_result.txt')).read
- @reader = DataObjects::Dbslayer::Reader.new( JSON.parse(@reader_query)['RESULT'] )
+ @types_query = File.new(File.join(File.dirname(__FILE__), 'fixtures', 'types_test.txt')).read
+ @reader = DataObjects::Dbslayer::Reader.new( JSON.parse(@reader_query)['RESULT'] )
+
+ @types_reader = DataObjects::Dbslayer::Reader.new( JSON.parse(@types_query)['RESULT'] )
+ @types_reader.next!
end
it 'should return the fields' do
@@ -22,4 +26,39 @@
end
rows.should == 10
end
+
+ it 'should be able to read a number' do
+ @types_reader.values[0].class.should == Fixnum
+ @types_reader.values[0].should == 1
+ end
+
+ it 'should be able to read Date types' do
+ @types_reader.values[1].class.should == Date
+ @types_reader.values[1].should == Date.new(2008, 5, 19)
+ end
+
+ it 'should be able to read a Time(stamp) type' do
+ @types_reader.values[2].class.should == Time
+ @types_reader.values[2].should == Time.mktime(2008, 5, 19, 9, 46, 56)
+ end
+
+ it 'should be able to read a DateTime type' do
+ @types_reader.values[3].class.should == DateTime
+ @types_reader.values[3].should == DateTime.new(2008, 5, 19, 9, 48, 7)
+ end
+
+ it 'should be able to read a basic time type' do
+ @types_reader.values[4].class.should == Time
+ @types_reader.values[4].should == Time.parse('09:48:07')
+ end
+
+ it 'should be able to read a year type' do
+ @types_reader.values[5].class.should == Fixnum
+ @types_reader.values[5].should == 2008
+ end
+
+ it 'should be able to read a boolean type' do
+ @types_reader.values[6].class.should == TrueClass
+ @types_reader.values[6].should == true
+ end
end

0 comments on commit 6547d67

Please sign in to comment.
Something went wrong with that request. Please try again.