There's a test for activerecord called test_numeric_fields in base_test.rb. here's the test
def test_numeric_fields m = NumericData.new( :bank_balance => 1586.43, :big_bank_balance => BigDecimal("1000234000567.95"), :world_population => 6000000000, :my_house_population => 3 ) assert m.save
m1 = NumericData.find(m.id) assert_not_nil m1
# As with migration_test.rb, we should make world_population >= 2**62 # to cover 64-bit platforms and test it is a Bignum, but the main thing # is that it's an Integer. assert_kind_of Integer, m1.world_population assert_equal 6000000000, m1.world_population
assert_kind_of Fixnum, m1.my_house_population assert_equal 3, m1.my_house_population
assert_kind_of BigDecimal, m1.bank_balance assert_equal BigDecimal("1586.43"), m1.bank_balance
assert_kind_of BigDecimal, m1.big_bank_balance assert_equal BigDecimal("1000234000567.95"), m1.big_bank_balance end
The numeric_data table is defined like this
CREATE TABLE `numeric_data` ( `id` INTEGER NOT NULL auto_increment PRIMARY KEY, `bank_balance` decimal(10,2), `big_bank_balance` decimal(15,2), `world_population` decimal(10), `my_house_population` decimal(2), `decimal_number_with_default` decimal(3,2) DEFAULT 2.78 ) TYPE=InnoDB;
My question is with assert_kind_of Integer, m1.world_population. Why would world_population be an integer. It seems like this test makes an assumption about how a particular database stores the actual data type internally. To me when defining a field as decimal(10) or lets say numeric(10) both of these really should be of kind BigDecimal since that's what the database field is stored as, it's just that the BigDecimal object should have no numers after the decimal point. Am I just way off here? Making this assumption for a database like Firebird where two different databases might be a different dialect. For example decimal(15,2) in dialect 1 firebird is really a double precision underneath, in dialect 3 it's really being stored as a bigint. Regardless of how the database is storing this underneat the Firebird driver (correctly in my opinion) translate this to a BigDecimal column. The test above then fails because it's looking for wrong types. Anyone have any thoughts on this?