Skip to content

Instantly share code, notes, and snippets.

@ocher
Created January 10, 2013 08:47
Show Gist options
  • Save ocher/4500550 to your computer and use it in GitHub Desktop.
Save ocher/4500550 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'active_record'
require 'test/unit'
DBName = 'test_custom_select'
DBUser = 'root'
DBPass = ''
unless defined?(JRUBY_VERSION)
# require 'openssl'
# ActiveRecord::Base.establish_connection(:adapter => 'mysql2', :database => DBName, :user => DBUser, :password => DBPass)
ActiveRecord::Base.establish_connection(:adapter => 'postgresql', :database => DBName, :user => 'postgres', :password => DBPass)
# ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => DBName)
else
ActiveRecord::Base.establish_connection(:adapter => 'jdbcmysql', :database => DBName, :user => DBUser, :password => DBPass)
end
ActiveRecord::Schema.drop_table('users') rescue nil
ActiveRecord::Schema.define do
create_table 'users' do |t|
t.datetime 'created_at'
t.decimal 'money', :precision => 8, :scale => 2
t.float 'float_val'
end
end
class User < ActiveRecord::Base
end
user = User.create! :money => '25.10', :float_val => 1.25
class TestCustomSelect < Test::Unit::TestCase
def test_custom_selects
assert_equal(1, User.count)
puts "Float class: ", User.select('float_val AS custom_float_val').first.custom_float_val.class
puts "Decimal class: ", User.select('money AS custom_money').first.custom_money.class
puts "Datetime class: ", User.select('created_at AS custom_created_at').first.custom_created_at.class
assert_equal(Time, User.first.created_at.class) # ok
assert_equal(BigDecimal, User.first.money.class) # ok
assert_equal(Float, User.first.float_val.class) # ok
# tests below fail on JRuby. mysql2 works fine.
# assert_equal(Float, User.select('float_val AS custom_float_val').first.custom_float_val.class)
# assert_equal(BigDecimal, User.select('money AS custom_money').first.custom_money.class)
# assert_equal(Time, User.select('created_at AS custom_created_at').first.custom_created_at.class)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment